a lot of refactos

This commit is contained in:
STEINNI
2026-06-21 21:08:46 +00:00
parent 3066a54a4c
commit 4c9e989bda
16 changed files with 472 additions and 60 deletions
+60 -2
View File
@@ -28,6 +28,8 @@ export class gpsServer {
this.state = SimState.IDLE
this.simulationId = null
this.bigBangEpoch = null
this.resumeEpoch = null
this.pausedAt = null
this.ignoredChangeCount = 0
}
@@ -62,17 +64,29 @@ export class gpsServer {
return(this.state === SimState.LIVE)
}
isPaused() {
return(this.state === SimState.PAUSED)
}
canQuerySim() {
return(this.isLive() || this.isPaused())
}
isPrepare() {
return(this.state === SimState.PREPARE)
}
simNow() {
if(this.bigBangEpoch === null) return(null)
if(this.resumeEpoch !== null) {
return(this.pausedAt + (performance.now() - this.resumeEpoch) / 1000)
}
return((performance.now() - this.bigBangEpoch) / 1000)
}
now() {
if(this.isLive()) return(this.simNow())
if(this.isPaused()) return(this.pausedAt)
if(this.isPrepare()) return(0)
return(null)
}
@@ -124,7 +138,7 @@ export class gpsServer {
}
getAgentPosition(agentId, at = null) {
if(!this.isLive()) return(null)
if(!this.canQuerySim()) return(null)
const agent = this.agents.get(agentId)
if(!agent) return(null)
const t = at ?? this.now()
@@ -154,7 +168,7 @@ export class gpsServer {
}
getAgentsInPrism(prism, at = null) {
if(!this.isLive()) return([])
if(!this.canQuerySim()) return([])
const t = at ?? this.now()
if(t === null) return([])
const agents = []
@@ -172,6 +186,8 @@ export class gpsServer {
this.registry = new CollisionRegistry()
this.simulationId = null
this.bigBangEpoch = null
this.resumeEpoch = null
this.pausedAt = null
this.ignoredChangeCount = 0
this.state = SimState.IDLE
await this.agentStore?.clearAll()
@@ -269,6 +285,8 @@ export class gpsServer {
}
this.bigBangEpoch = performance.now()
this.resumeEpoch = null
this.pausedAt = null
this.state = SimState.LIVE
for(const agent of this.agents.values()) {
@@ -278,6 +296,46 @@ export class gpsServer {
if(this.debug) console.log(`[GPS] LIVE: bigBangEpoch set, simulationId=${this.simulationId}`)
}
onSimulationPaused(payload = {}) {
if(this.state === SimState.IDLE || this.state === SimState.PAUSED) return
if(payload.simulationId && this.simulationId && payload.simulationId !== this.simulationId) {
console.error('[GPS] simulationPaused rejected: simulationId mismatch')
return
}
const t = (typeof(payload.t) === 'number' && !Number.isNaN(payload.t))
? payload.t
: this.now()
this.pausedAt = t
this.state = SimState.PAUSED
if(this.debug) console.log(`[GPS] PAUSED at t=${t}, simulationId=${this.simulationId}`)
}
async onSimulationStopped(payload = {}) {
if(this.state === SimState.IDLE) return
if(payload.simulationId && this.simulationId && payload.simulationId !== this.simulationId) {
console.error('[GPS] simulationStopped rejected: simulationId mismatch')
return
}
if(this.debug) console.log(`[GPS] STOPPED (reset), simulationId=${this.simulationId}`)
await this.resetSimulation()
}
onSimulationResumed(payload = {}) {
if(this.state !== SimState.PAUSED) {
console.error(`[GPS] simulationResumed rejected: expected PAUSED, got ${this.state}`)
return
}
if(payload.simulationId && this.simulationId && payload.simulationId !== this.simulationId) {
console.error('[GPS] simulationResumed rejected: simulationId mismatch')
return
}
this.resumeEpoch = performance.now()
this.state = SimState.LIVE
if(this.debug) console.log(`[GPS] RESUMED at t=${this.pausedAt}, simulationId=${this.simulationId}`)
}
upsertAgent(agentId, newVector, newPosition = null) {
const now = this.now()
let agent = this.agents.get(agentId)