Files
nissefolk/src/NetworkAdapter.ts
tekki mariani 3b021127a4 replace polling timers with sorted event queues + action log
Crops, tree seedlings, and tile recovery no longer iterate all entries
every frame. Each event stores an absolute gameTime timestamp (growsAt).
A sorted priority queue is drained each tick — only due items are touched.

WorldState now tracks gameTime (ms); stateManager.advanceTime(delta)
increments it each frame. Save version bumped 5→6 with migration.

Action log ring buffer (15 entries) added to LocalAdapter; shown in
the F3 debug panel under "Last Actions".

Closes #36
Closes #37
2026-03-24 08:08:05 +00:00

50 lines
1.7 KiB
TypeScript

import type { GameAction } from './types'
import { stateManager } from './StateManager'
/** All state mutations go through an adapter.
* Swap LocalAdapter for WebSocketAdapter to enable multiplayer. */
export interface NetworkAdapter {
send(action: GameAction): void
onAction?: (action: GameAction) => void
}
const ACTION_LOG_SIZE = 15
/** Singleplayer: apply actions immediately and synchronously */
export class LocalAdapter implements NetworkAdapter {
onAction?: (action: GameAction) => void
/** Ring-buffer of the last ACTION_LOG_SIZE dispatched action summaries. */
private _actionLog: string[] = []
send(action: GameAction): void {
stateManager.apply(action)
this._recordAction(action)
this.onAction?.(action)
}
/** Returns a copy of the recent action log (oldest first). */
getActionLog(): readonly string[] { return this._actionLog }
/**
* Appends a short summary of the action to the ring-buffer.
* @param action - The dispatched game action
*/
private _recordAction(action: GameAction): void {
let entry = action.type
if ('tileX' in action && 'tileY' in action)
entry += ` (${(action as any).tileX},${(action as any).tileY})`
else if ('villagerId' in action)
entry += ` v=…${(action as any).villagerId.slice(-4)}`
else if ('resourceId' in action)
entry += ` r=…${(action as any).resourceId.slice(-4)}`
else if ('cropId' in action)
entry += ` c=…${(action as any).cropId.slice(-4)}`
else if ('seedlingId' in action)
entry += ` s=…${(action as any).seedlingId.slice(-4)}`
if (this._actionLog.length >= ACTION_LOG_SIZE) this._actionLog.shift()
this._actionLog.push(entry)
}
}