mancala/apps/backend/src/game/GameClockScheduler.ts

26 lines
873 B
TypeScript
Raw Normal View History

import { GameManager } from "./GameManager";
export const CLOCK_CHECK_INTERVAL_MS = 1_000;
// The scheduled service that ends games whose running clock has reached zero, also when nobody is connected
// (docs/specs/game-clock, R2.2). A move that arrives late is refused by GameManager itself, so how often this runs
// only decides how soon the end is announced, never who wins.
export class GameClockScheduler {
private timer?: NodeJS.Timeout;
constructor(
private readonly gameManager: Pick<GameManager, "endGamesOutOfTime">,
private readonly intervalMs = CLOCK_CHECK_INTERVAL_MS,
) { }
start() {
if (this.timer) return;
this.timer = setInterval(() => this.gameManager.endGamesOutOfTime(), this.intervalMs);
}
stop() {
if (this.timer) clearInterval(this.timer);
this.timer = undefined;
}
}