Something went wrong. Try again.
The atchess bot. Monitors the firehose, indexes games, plays via Stockfish.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import { Chess } from 'chess.js';import type { Logger } from './logger.js';import { extractFenFromPgn } from './types.js';import type { GameRecord } from './types.js';
export interface VerificationResult { verified: boolean; moveCount: number; computedResult: { result: string; reason: string } | null; error?: string;}
/** * Derive the game result from the board state. * Adapted from checkmate.blue src/lib/game-logic.ts */function gameResult(chess: Chess): { result: '1-0' | '0-1' | '1/2-1/2'; reason: string } | null { if (!chess.isGameOver()) return null;
if (chess.isCheckmate()) { return { result: chess.turn() === 'w' ? '0-1' : '1-0', reason: 'checkmate', }; } if (chess.isStalemate()) return { result: '1/2-1/2', reason: 'stalemate' }; if (chess.isInsufficientMaterial()) return { result: '1/2-1/2', reason: 'insufficient' }; if (chess.isThreefoldRepetition()) return { result: '1/2-1/2', reason: 'repetition' }; if (chess.isDraw()) return { result: '1/2-1/2', reason: 'fifty_moves' };
return null;}
export class Verifier { private log: Logger;
constructor(logger: Logger) { this.log = logger.child({ component: 'verifier' }); }
verify(record: GameRecord, ownerDid: string): VerificationResult { // Load PGN into chess.js (handles [FEN "..."] headers for variant games) const startingFen = extractFenFromPgn(record.pgn || ''); let chess: Chess; try { chess = startingFen ? new Chess(startingFen) : new Chess(); } catch (err) { return { verified: false, moveCount: 0, computedResult: null, error: `Invalid starting FEN: ${err}` }; }
if (record.pgn) { try { chess.loadPgn(record.pgn); } catch (err) { return { verified: false, moveCount: 0, computedResult: null, error: `Invalid PGN: ${err}` }; } }
const moveCount = chess.history().length; const computed = gameResult(chess);
// Resignation: can't verify from PGN. Accept if the owner claims they lost. if (record.resultReason === 'resignation') { const ownerIsWhite = record.white === ownerDid; const ownerLost = (ownerIsWhite && record.result === '0-1') || (!ownerIsWhite && record.result === '1-0');
if (ownerLost) { return { verified: true, moveCount, computedResult: null }; } this.log.warn({ ownerDid, result: record.result }, 'resignation claim: owner did not claim they lost'); return { verified: false, moveCount, computedResult: null, error: 'Resignation claimed but owner is not the loser' }; }
// Draw by agreement: accept if both sides claim it if (record.resultReason === 'agreement') { if (record.result === '1/2-1/2') { return { verified: true, moveCount, computedResult: null }; } return { verified: false, moveCount, computedResult: null, error: 'Agreement claim but result is not a draw' }; }
// Abandonment: accept if record owner claims they lost if (record.resultReason === 'abandonment') { return { verified: true, moveCount, computedResult: null }; }
// Board-derived results: PGN must produce the claimed result if (!computed) { this.log.warn({ result: record.result, reason: record.resultReason }, 'game claims completed but PGN does not show game over'); return { verified: false, moveCount, computedResult: null, error: 'PGN does not show game over' }; }
if (computed.result === record.result && computed.reason === record.resultReason) { return { verified: true, moveCount, computedResult: computed }; }
// Result matches but reason might differ slightly (e.g., both say 1/2-1/2) if (computed.result === record.result) { this.log.warn( { claimed: record.resultReason, computed: computed.reason }, 'result matches but reason differs', ); return { verified: true, moveCount, computedResult: computed }; }
this.log.warn( { claimedResult: record.result, computedResult: computed.result }, 'result mismatch', ); return { verified: false, moveCount, computedResult: computed, error: `Result mismatch: claimed ${record.result}, computed ${computed.result}` }; }}