The worker runtime hosts engine execution and translates core game objects into UI-friendly transfer data.
Primary files:
src/js/backend.tssrc/js/Engine/Game.tssrc/js/Engine/DataTransferClient.tssrc/js/Engine/DataQueue.tssrc/js/Engine/Transport.ts
Game listens for incoming control messages:
start: bind event notifications and callstart().setOption: set single engine option.getOptions: return selected option values.setOptions: apply batch options and acknowledge.
On start():
- Hooks
engine:startto create players. - Registers one human
DataTransferClient(player index 0). - Registers AI clients (
SimpleAIClient) for all other players. - Calls
engine.start()and then dynamic-imports generated plugins.
DataTransferClient extends core client behavior for web-renderer communication.
Receives frontend events over transport:
action: all player actions (unit movement, end turn, city actions, etc.).cheat: ad-hoc debug operations (RevealMap,GrantAdvance,GrantGold,ModifyUnit).
Sends data to frontend:
gameData: initial full snapshot.gameDataPatch: incremental updates fromDataQueue.gameNotification: user-facing gameplay notifications.chooseFromList: player decisions required by game flow.
handleAction(...) maps transport payloads to concrete player actions, with special handling for:
ActiveUnitaction + target resolution.InactiveUnitupdates.ChangeProduction,CityBuild,ChooseResearch,Revolution,AdjustTradeRates,LaunchSpaceship.- Synthetic/utility action:
ReassignWorkers.
Diplomacy uses iterative chooseFromList prompts while negotiation is active.
AI clients are timeout-protected for negotiation responses.
- Backend filters outgoing objects (
toPlainObject) to avoid leaking hidden/unavailable entities. - Unknown-object wrappers (
UnknownCity,UnknownUnit,UnknownPlayer) represent fog-of-war/visibility boundaries. - Function-valued patch payloads are resolved at send time in
DataQueue.transferData().
- Heavy class-based mutation makes selective diffs difficult.
- Patch queue currently unchunked (
DataQueueTODO). restartandquitare declared in the typed transport map but have no receiver on the other side (restartsent by the backend has no frontend handler;quitsent by the frontend has no backend handler).Gamecurrently hardcodes only player index 0 as human.
- Keep worker boundary and message transport abstraction.
- Introduce explicit protocol schema versioning for transport payloads.
- Normalize action payloads behind a declarative dispatcher map.
- Replace manual patch path strings with operation objects or JSON Patch-like structure.