General Actions to handlers Refacto
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
|
||||
export const eventHandlers = {
|
||||
'arena:agents:*': {
|
||||
change(msg, chan) {
|
||||
const agentId = msg.sender
|
||||
if(!agentId || typeof(agentId) !== 'string') {
|
||||
console.warn(`[${this.redisId}] Agent event without sender`)
|
||||
return
|
||||
}
|
||||
const newVector = msg.payload?.newVector
|
||||
if(!newVector || typeof(newVector.x) !== 'number' || typeof(newVector.y) !== 'number' || typeof(newVector.z) !== 'number') {
|
||||
console.warn(`[${this.redisId}] Invalid newVector from ${agentId}`)
|
||||
return
|
||||
}
|
||||
const newPosition = msg.payload?.newPosition ?? null
|
||||
this.gpsSrv?.onVectorChange(agentId, newVector, newPosition)
|
||||
},
|
||||
remove(msg, chan) {
|
||||
const agentId = msg.sender
|
||||
if(!agentId || typeof(agentId) !== 'string') {
|
||||
console.warn(`[${this.redisId}] Agent event without sender`)
|
||||
return
|
||||
}
|
||||
this.gpsSrv?.onAgentRemove(agentId)
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
|
||||
export const construct = (redisCnx) => {
|
||||
const tickMs = redisCnx.gpsSrv?.getGpsSettings().collisionTickMs ?? 100
|
||||
// Interval always runs; tickArena no-ops until LIVE (see gpsServer.tickArena)
|
||||
setInterval(() => {
|
||||
redisCnx.gpsSrv?.tickArena()
|
||||
}, tickMs)
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
|
||||
handleLifecycleEvent(msg) {
|
||||
const srv = this.gpsSrv
|
||||
if(!srv) return
|
||||
|
||||
if(msg.eventType === 'onYourMarks') {
|
||||
srv.onYourMarks(msg.payload ?? {}).catch(err => {
|
||||
console.error(`[${this.redisId}] onYourMarks failed:`, err)
|
||||
srv.publishReadyToStart({ success: false, err: err.message ?? 'onYourMarks failed' })
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if(msg.eventType === 'bigBang') {
|
||||
srv.onBigBang(msg.payload ?? {})
|
||||
return
|
||||
}
|
||||
},
|
||||
|
||||
handleAgentEvent(msg) {
|
||||
const agentId = msg.sender
|
||||
if(!agentId || typeof(agentId) !== 'string') {
|
||||
console.warn(`[${this.redisId}] Agent event without sender`)
|
||||
return
|
||||
}
|
||||
|
||||
if(msg.eventType === 'change') {
|
||||
const newVector = msg.payload?.newVector
|
||||
if(!newVector || typeof(newVector.x) !== 'number' || typeof(newVector.y) !== 'number' || typeof(newVector.z) !== 'number') {
|
||||
console.warn(`[${this.redisId}] Invalid newVector from ${agentId}`)
|
||||
return
|
||||
}
|
||||
const newPosition = msg.payload?.newPosition ?? null
|
||||
this.gpsSrv.onVectorChange(agentId, newVector, newPosition)
|
||||
return
|
||||
}
|
||||
|
||||
if(msg.eventType === 'remove') {
|
||||
this.gpsSrv.onAgentRemove(agentId)
|
||||
return
|
||||
}
|
||||
},
|
||||
|
||||
dispatchArenaMessage(msg, chan) {
|
||||
const gps = this.config.gps
|
||||
if(!gps || !this.gpsSrv) return(false)
|
||||
|
||||
if(this.matchesChan(chan, gps.lifecycle?.arenaChannel ?? 'arena:lifecycle')) {
|
||||
this.handleLifecycleEvent(msg)
|
||||
return(true)
|
||||
}
|
||||
|
||||
if(this.matchesChan(chan, gps.agentVectorChangeChannel)) {
|
||||
this.handleAgentEvent(msg)
|
||||
return(true)
|
||||
}
|
||||
return(false)
|
||||
},
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
export function dispatchMessage(redisCnx, msg, chan) {
|
||||
if(!redisCnx.config.gps || typeof(redisCnx.dispatchArenaMessage) !== 'function') return
|
||||
redisCnx.dispatchArenaMessage(msg, chan)
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
import { methods as arenaMethods, construct as arenaConstruct } from './arenaHandlers.js'
|
||||
import { dispatchMessage } from './dispatch.js'
|
||||
import { assembleHandlers, createDispatchMessage } from '../../../bus/assembleMesh.js'
|
||||
import * as lifecycle from './lifecycle.js'
|
||||
import * as agentMotion from './agentMotion.js'
|
||||
|
||||
export const afterLoginMethods = [
|
||||
arenaConstruct,
|
||||
]
|
||||
const { actionHandlers, eventHandlers, afterLogin } = assembleHandlers([lifecycle, agentMotion])
|
||||
|
||||
export const meshActions = {
|
||||
...arenaMethods,
|
||||
}
|
||||
export { dispatchMessage }
|
||||
export { actionHandlers, afterLogin }
|
||||
|
||||
export const dispatchMessage = createDispatchMessage({
|
||||
eventHandlers,
|
||||
actionRules(redisCnx) {
|
||||
const gps = redisCnx.config.gps ?? {}
|
||||
const arenaChannel = gps.bus?.arena?.actionsChannel
|
||||
return({
|
||||
channels: arenaChannel ? [arenaChannel] : [],
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
export function construct(redisCnx) {
|
||||
const tickMs = redisCnx.gpsSrv?.getGpsSettings().collisionTickMs ?? 100
|
||||
setInterval(() => {
|
||||
redisCnx.gpsSrv?.tickArena()
|
||||
}, tickMs)
|
||||
}
|
||||
|
||||
export const eventHandlers = {
|
||||
'arena:lifecycle': {
|
||||
onYourMarks(msg, chan) {
|
||||
const srv = this.gpsSrv
|
||||
if(!srv) return
|
||||
srv.onYourMarks(msg.payload ?? {}).catch(err => {
|
||||
console.error(`[${this.redisId}] onYourMarks failed:`, err)
|
||||
srv.publishReadyToStart({ success: false, err: err.message ?? 'onYourMarks failed' })
|
||||
})
|
||||
},
|
||||
bigBang(msg, chan) {
|
||||
this.gpsSrv?.onBigBang(msg.payload ?? {})
|
||||
},
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user