General Actions to handlers Refacto

This commit is contained in:
STEINNI
2026-06-20 18:50:26 +00:00
parent 7435d96135
commit 44a84c64ec
56 changed files with 832 additions and 973 deletions
+27
View File
@@ -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)
},
},
}
-70
View File
@@ -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)
},
}
-5
View File
@@ -1,5 +0,0 @@
export function dispatchMessage(redisCnx, msg, chan) {
if(!redisCnx.config.gps || typeof(redisCnx.dispatchArenaMessage) !== 'function') return
redisCnx.dispatchArenaMessage(msg, chan)
}
+16 -9
View File
@@ -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] : [],
})
},
})
+23
View File
@@ -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 ?? {})
},
},
}
-28
View File
@@ -1,28 +0,0 @@
export function dispatchMessage(redisCnx, msg, chan) {
const gps = redisCnx.config.gps
if(!gps?.gpsActionsChannel) return
const actionsChan = redisCnx.fullChan(gps.gpsActionsChannel)
if(chan != actionsChan) return
const action = msg.action
if(!action || typeof(action) !== 'string') {
console.warn(`[${redisCnx.redisId}] Ignoring message without action on ${chan}`)
return
}
const handler = redisCnx['action_'+action]
if(typeof(handler) != 'function') {
if(redisCnx.debug) console.warn(`[${redisCnx.redisId}] Unknown action ${action} on ${chan}`)
return
}
const payload = ('payload' in msg) ? msg.payload : null
const reqid = ('reqid' in msg) ? msg.reqid.substr(0, 50) : null
const sender = msg.sender || null
const roles = Array.isArray(msg.roles) ? msg.roles : ['*']
if(redisCnx.debug) console.log(`[${redisCnx.redisId}] Dispatching action ${action} from ${sender}`)
handler.call(redisCnx, action, payload, reqid, sender, roles)
}
+15 -9
View File
@@ -1,10 +1,16 @@
import { methods as utilities, construct as utilitiesConstruct } from './utilities.js'
import { dispatchMessage } from './dispatch.js'
import { assembleHandlers, createDispatchMessage } from '../../../bus/assembleMesh.js'
import * as utilities from './utilities.js'
export const afterLoginMethods = [
utilitiesConstruct,
]
export const meshActions = {
...utilities,
}
export { dispatchMessage }
const { actionHandlers, eventHandlers, afterLogin } = assembleHandlers([utilities])
export { actionHandlers, afterLogin }
export const dispatchMessage = createDispatchMessage({
eventHandlers,
actionRules(redisCnx) {
const gps = redisCnx.config.gps ?? {}
return({
channels: [gps.gpsActionsChannel].filter(Boolean),
})
},
})
+14 -90
View File
@@ -1,109 +1,33 @@
import { publishActionReply } from '../../actionsHelper.js'
import { replyToAction } from '../../../bus/publishActionReply.js'
export const construct = (redisCnx) => {
// console.log('Hello after login from utilities...')
// redisCnx.v42=0
// setInterval(redisCnx.move4243.bind(redisCnx), 200)
}
export const actions = {
export const methods = {
/* Event-Rx:
{
"action": "TIME"
"reqid": "6az5e4r6a"
}
Event-Tx:
{
"action": "TIME",
"success": true,
"payload" : {
gpsTime: "2022-09-01T14:42:22.603Z",
redisTime: "2022-09-01T14:42:22.603Z"
},
"reqid": "6az5e4r6a"
}
*/
async action_TIME(action, payload, reqid, sender, roles){
publishActionReply(this, {
async action_TIME(action, payload, reqid, sender, roles) {
replyToAction(this, {
action,
reqid,
sender,
replyChannel: this.config.gps.gpsActionsReply,
reply: {
success: true,
payload: {
gpsTime: new Date().toISOString(),
redisTime: await this.redisClient.time(),
},
success: true,
payload: {
gpsTime: new Date().toISOString(),
redisTime: await this.redisClient.time(),
},
})
},
/* Event-Rx:
{
"action": "RELOADCONFIG"
"reqid": "6az5e4r6a"
}
Event-Tx:
{
"action": "RELOADCONFIG",
"success": true,
"reqid": "6az5e4r6a"
}
*/
async action_RELOADCONFIG(action, payload, reqid, sender, roles){
const replyOpts = {
action,
reqid,
sender,
replyChannel: this.config.gps.gpsActionsReply,
}
if(!this.accessRights.canDo(roles, action)) {
publishActionReply(this, { ...replyOpts, reply: {
success: false,
err: 'Unauthorized action !',
} })
return
}
async action_RELOADCONFIG(action, payload, reqid, sender, roles) {
this.reloadAccessRights()
publishActionReply(this, { ...replyOpts, reply: {
success: true,
} })
replyToAction(this, { action, reqid, sender, success: true })
},
/* Event-Rx:
{
"action": "GETCONFIG"
"reqid": "6az5e4r6a"
}
Event-Tx:
{
"action": "GETCONFIG",
"success": true,
"reqid": "6az5e4r6a",
payload: { ...the access rights, and roles... }
}
*/
async action_GETCONFIG(action, payload, reqid, sender, roles){
const replyOpts = {
async action_GETCONFIG(action, payload, reqid, sender, roles) {
replyToAction(this, {
action,
reqid,
sender,
replyChannel: this.config.gps.gpsActionsReply,
}
if(!this.accessRights.canDo(roles, action)) {
publishActionReply(this, { ...replyOpts, reply: {
success: false,
err: 'Unauthorized action !',
} })
return
}
publishActionReply(this, { ...replyOpts, reply: {
success: true,
payload: this.getAccessRights(),
} })
})
},
}