diff --git a/server/src/game/GameLoop.ts b/server/src/game/GameLoop.ts index d45801e..e44ab5f 100644 --- a/server/src/game/GameLoop.ts +++ b/server/src/game/GameLoop.ts @@ -16,12 +16,14 @@ import { createNarrationService, type NarrationService } from '../llm/narrationS import { narrationEmitter } from '../systems/narrationEmitter.js'; import type { EntityId } from '@dflike/shared'; import { createThoughtSystem, type ThoughtSystem } from '../systems/thoughtSystem.js'; +import { createEventMemoryService, type EventMemoryService } from '../llm/eventMemoryService.js'; export class GameLoop { readonly world: World; readonly map: GameMap; readonly llmService: LlmService; readonly narrationService: NarrationService; + readonly eventMemoryService: EventMemoryService; readonly thoughtSystem: ThoughtSystem; public followedEntityIds: Set = new Set(); private tick = 0; @@ -34,6 +36,7 @@ export class GameLoop { this.map = new GameMap(); this.llmService = createLlmService(); this.narrationService = createNarrationService(this.llmService); + this.eventMemoryService = createEventMemoryService(); this.thoughtSystem = createThoughtSystem(this.llmService, this.narrationService); this.setupMap(); this.spawnInitialNPCs(8); diff --git a/server/src/network/SocketServer.ts b/server/src/network/SocketServer.ts index c1e8ab4..0d022f6 100644 --- a/server/src/network/SocketServer.ts +++ b/server/src/network/SocketServer.ts @@ -45,6 +45,16 @@ export class SocketServer { this.gameLoop.thoughtSystem.onThought = (data) => { this.io.emit('npc-thought', data); }; + + this.gameLoop.eventMemoryService.onEventRecorded = (entityId, event) => { + // Only send to clients following this NPC + for (const [socketId, followedId] of this.followedEntities) { + if (followedId === entityId) { + const sock = this.io.sockets.sockets.get(socketId); + sock?.emit('memory-event', { entityId, event }); + } + } + }; } private setupConnections(): void { @@ -118,6 +128,14 @@ export class SocketServer { socket.on('follow-npc', (data: { entityId: EntityId | null }) => { this.followedEntities.set(socket.id, data.entityId); this.rebuildFollowedEntityIds(); + + // Send memory history when following a new NPC + if (data.entityId !== null) { + const events = this.gameLoop.eventMemoryService.getEvents(data.entityId); + if (events.length > 0) { + socket.emit('memory-history', { entityId: data.entityId, events }); + } + } }); // Handle disconnect