a lot of refactos
This commit is contained in:
@@ -7,5 +7,8 @@ export const eventHandlers = {
|
||||
bigBang(msg, chan) {
|
||||
this.observerSrv?.onBigBang()
|
||||
},
|
||||
simulationStopped(msg, chan) {
|
||||
this.observerSrv?.onSimulationStopped(msg.payload ?? {})
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -107,7 +107,6 @@ export const actions = {
|
||||
success: true,
|
||||
payload: {
|
||||
frequency: result.frequency,
|
||||
agents: result.agents,
|
||||
t: result.t,
|
||||
},
|
||||
})
|
||||
|
||||
+43
-11
@@ -1,7 +1,6 @@
|
||||
import { AccesRights } from '../accesRights.js'
|
||||
import { GpsStorageReader } from './gpsStorageReader.js'
|
||||
import { RequestorRegistry } from './requestorRegistry.js'
|
||||
import { replyToAction } from '../bus/publishActionReply.js'
|
||||
import { SimState } from '../GPS/simulationState.js'
|
||||
|
||||
export class observerServer {
|
||||
@@ -26,6 +25,8 @@ export class observerServer {
|
||||
return({
|
||||
senderId: observer.senderId ?? 'observer',
|
||||
scanIntervalMs: observer.scanIntervalMs ?? 300,
|
||||
frustumEventsChannel: observer.observerFrustumEventsChannel
|
||||
?? 'system:observer:subscribed[UID]:agents',
|
||||
lifecycle: {
|
||||
arenaChannel: observer.lifecycle?.arenaChannel ?? 'arena:lifecycle',
|
||||
godsReadyChannel: observer.lifecycle?.godsReadyChannel ?? 'arena:gods:ready',
|
||||
@@ -53,20 +54,44 @@ export class observerServer {
|
||||
this.gpsStorageReader,
|
||||
() => this.now(),
|
||||
scanIntervalMs,
|
||||
(sender, payload) => this.publishFrustumUpdate(sender, payload),
|
||||
(subscriberId, payload) => this.publishFrustumAgentEvents(
|
||||
subscriberId,
|
||||
payload.agents,
|
||||
payload.t
|
||||
),
|
||||
this.debug
|
||||
)
|
||||
}
|
||||
|
||||
publishFrustumUpdate(sender, payload) {
|
||||
if(!this.systemCnx || !sender) return
|
||||
const observer = this.observerConfig.observer ?? {}
|
||||
replyToAction(this.systemCnx, {
|
||||
action: 'GETAGENTSINFRUSTUM',
|
||||
sender,
|
||||
success: true,
|
||||
payload,
|
||||
})
|
||||
async publishFrustumAgentEvents(subscriberId, agents, t) {
|
||||
if(!this.systemCnx || !subscriberId) return
|
||||
if(!Array.isArray(agents) || !agents.length) return
|
||||
|
||||
const { frustumEventsChannel } = this.getObserverSettings()
|
||||
const chan = frustumEventsChannel.replace(/\[UID\]/g, subscriberId)
|
||||
const senderId = this.getObserverSettings().senderId
|
||||
|
||||
for(const agent of agents) {
|
||||
if(!agent?.id || !agent?.position) continue
|
||||
|
||||
await this.systemCnx.redisPublish(chan, {
|
||||
eventType: 'move',
|
||||
sender: senderId,
|
||||
payload: {
|
||||
aid: agent.id,
|
||||
coords: {
|
||||
x: agent.position.x,
|
||||
y: agent.position.y,
|
||||
z: agent.position.z,
|
||||
},
|
||||
t,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if(this.debug) {
|
||||
console.log(`[Observer] Frustum events: ${agents.length} agent(s) on ${chan} at t=${t}`)
|
||||
}
|
||||
}
|
||||
|
||||
isLive() {
|
||||
@@ -94,6 +119,13 @@ export class observerServer {
|
||||
this.state = SimState.LIVE
|
||||
}
|
||||
|
||||
onSimulationStopped(payload = {}) {
|
||||
this.requestorRegistry?.clear()
|
||||
this.state = SimState.IDLE
|
||||
this.bigBangEpoch = null
|
||||
if(this.debug) console.log(`[Observer] simulationStopped at t=${payload.t}`)
|
||||
}
|
||||
|
||||
wireSystemConnexion(cnx) {
|
||||
cnx.observerSrv = this
|
||||
cnx.accessRights = this.accessRights
|
||||
|
||||
@@ -56,11 +56,11 @@ export class RequestorRegistry {
|
||||
updatedAt: Date.now(),
|
||||
})
|
||||
this.#ensureTick()
|
||||
this.#pushAgentEvents(this.requestors.get(id))
|
||||
|
||||
return({
|
||||
ok: true,
|
||||
frequency: frequencyMs,
|
||||
agents: matching,
|
||||
t,
|
||||
})
|
||||
}
|
||||
@@ -130,9 +130,9 @@ export class RequestorRegistry {
|
||||
return(matching)
|
||||
}
|
||||
|
||||
#pushUpdate(requestor) {
|
||||
#pushAgentEvents(requestor) {
|
||||
if(typeof(this.onPush) !== 'function') return
|
||||
this.onPush(requestor.id, {
|
||||
void this.onPush(requestor.id, {
|
||||
agents: [...requestor.agents],
|
||||
t: requestor.t,
|
||||
})
|
||||
@@ -160,7 +160,7 @@ export class RequestorRegistry {
|
||||
requestor.tickCounter++
|
||||
if(requestor.tickCounter >= requestor.pushEveryNTicks) {
|
||||
requestor.tickCounter = 0
|
||||
this.#pushUpdate(requestor)
|
||||
this.#pushAgentEvents(requestor)
|
||||
}
|
||||
}
|
||||
if(this.debug) console.log(`[Observer] Scanned ${agents.size} agent(s) for ${this.requestors.size} requestor(s)`)
|
||||
|
||||
Reference in New Issue
Block a user