39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
const RESERVED_HASH_FIELDS = new Set(['position', 'vector', 'speed', 'segment'])
|
|
|
|
export class ArenaGroom {
|
|
|
|
constructor(arenaCnx, arenaStorage, debug = false) {
|
|
this.cnx = arenaCnx
|
|
this.arenaStorage = arenaStorage
|
|
this.debug = debug
|
|
}
|
|
|
|
agentHashKey(agentId) {
|
|
return(this.arenaStorage.agentHashKey.replace(/\[UID\]/g, agentId))
|
|
}
|
|
|
|
async clearArena() {
|
|
const ids = await this.cnx.redisSmembers(this.arenaStorage.agentsIndexKey)
|
|
for(const id of ids) {
|
|
await this.cnx.redisDel(this.agentHashKey(id))
|
|
}
|
|
await this.cnx.redisDel(this.arenaStorage.agentsIndexKey)
|
|
if(this.debug) console.log(`[Maestro] Cleared arena store (${ids.length} agent(s))`)
|
|
}
|
|
|
|
async seedAgents(agents) {
|
|
for(const agent of agents) {
|
|
const key = this.agentHashKey(agent.id)
|
|
await this.cnx.redisHset(key, 'position', agent.position)
|
|
await this.cnx.redisHset(key, 'vector', agent.vector)
|
|
for(const [field, value] of Object.entries(agent.store ?? {})) {
|
|
if(RESERVED_HASH_FIELDS.has(field)) continue
|
|
await this.cnx.redisHset(key, field, value)
|
|
}
|
|
await this.cnx.redisSadd(this.arenaStorage.agentsIndexKey, agent.id)
|
|
}
|
|
if(this.debug) console.log(`[Maestro] Groomed ${agents.length} agent(s) into arena store`)
|
|
}
|
|
|
|
}
|