import type { Color } from '@blind-chess/shared'; import type { Game } from './state.js'; export function endGame(game: Game, reason: Game['endReason'], winner: Color | null): void { game.status = 'finished'; game.endReason = reason; game.winner = winner; game.finishedAt = Date.now(); } export function finalizeIfEnded(game: Game, announcements: ReadonlyArray<{ text: string }>): void { // Detect terminal moderator announcements. const lastTexts = new Set(announcements.map((a) => a.text)); if (lastTexts.has('white_checkmate')) endGame(game, 'checkmate', 'w'); else if (lastTexts.has('black_checkmate')) endGame(game, 'checkmate', 'b'); else if (lastTexts.has('stalemate')) endGame(game, 'stalemate', null); else if (lastTexts.has('draw_insufficient')) endGame(game, 'insufficient', null); else if (lastTexts.has('draw_threefold')) endGame(game, 'threefold', null); else if (lastTexts.has('draw_fifty')) endGame(game, 'fifty_move', null); }