Skip to main content

Game engine

Server-side game logic under server/src/game/, driven by Socket.IO handlers and persisted via services + src/data/.

Core classes​

ClassFileRole
ForagingGameForagingGame.jsForaging round state: patches, tokens, timing, erasures
ArithmeticTestArithmeticTest.jsStress-task problem generation and scoring
BotManagerBotManager.jsBot policies for multiplayer / simulations (POLICIES)
GameRegistryGameRegistry.jsIn-memory maps of active single- and multiplayer games; erasure-mode helpers

Surrounding services​

PieceRole
services/RoomService.jsCreate/join experiment, friends, and solo rooms; player lifecycle
services/foragingSaveService.jssaveSinglePlayerForaging, saveMultiplayerForaging
data/saveSinglePlayerForagingData.js (and siblings)Lower-level Prisma writes for SP / MP / practice / arithmetic
utils/timerManager.jsRegister/clear timers so round timers do not leak across rooms
utils/safeArithmeticEval.jsEvaluate arithmetic expressions with a digit/+ - * / whitelist — never eval
utils/regionPartition.jsSpatial / region helpers for foraging patches (when used by game code)

Lifecycle (simplified)​

  1. Client joins a room (experiment secret code, friends, or solo).
  2. Socket handlers mutate the in-memory game via GameRegistry.
  3. Round / session end triggers save services → Prisma models (SinglePlayerForaging, MultiplayerForaging, MultiplayerForagingRound, PracticeSession, ArithmeticTest, …).

Simulations​

Admin simulations reuse bot policies (BotManager) through simulationService + simulationSocket, writing SimulationRun / SimulationRound / SimulationBotResult. See the Admin docs (Phase 2) for the UI path.

Design rules​

  • Game timers go through timerManager
  • Math answers go through safeArithmeticEval
  • Socket handlers use wrapHandler / wrapSyncHandler
  • Prefer services for DB writes over ad-hoc Prisma in socket files