RotNot / src /lib /chess /pgn.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
3.86 kB
/**
* Chess PGN and UCI utilities shared between server and client.
*
* These are pure functions that operate on chess data strings — no React,
* no browser APIs. Extracted from the PuzzleBoard component so they can be
* unit-tested in the Node/Vitest environment and reused by future routes or
* CLI tooling.
*/
import { Chess } from "chess.js";
import type { Square } from "chess.js";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface UciMove {
from: Square;
to: Square;
promotion?: string;
}
// ---------------------------------------------------------------------------
// UCI helpers
// ---------------------------------------------------------------------------
/**
* Parse a UCI move string (e.g. "e2e4" or "e7e8q") into typed from/to/promotion.
*
* UCI format: <from-square><to-square>[promotion-piece]
* - from/to are always 2 characters (file + rank)
* - promotion piece is a single lowercase letter (q/r/b/n), present only for
* pawn promotions — length 5 total
*
* No validation is performed; callers are responsible for passing valid UCI strings.
*/
export function parseUci(uci: string): UciMove {
return {
from: uci.slice(0, 2) as Square,
to: uci.slice(2, 4) as Square,
promotion: uci.length === 5 ? uci[4] : undefined,
};
}
/**
* Validate that a string looks like a plausible UCI move.
* Returns true if the string is 4 or 5 characters with a valid file+rank pattern.
*
* Does NOT validate that the move is legal on any particular board.
*/
export function isValidUci(uci: string): boolean {
if (uci.length !== 4 && uci.length !== 5) return false;
const squareRe = /^[a-h][1-8]$/;
if (!squareRe.test(uci.slice(0, 2))) return false;
if (!squareRe.test(uci.slice(2, 4))) return false;
if (uci.length === 5 && !/^[qrbn]$/.test(uci[4])) return false;
return true;
}
// ---------------------------------------------------------------------------
// PGN helpers
// ---------------------------------------------------------------------------
/**
* Replay a full game PGN and return the resulting FEN string (the position
* after all moves in the PGN have been played).
*
* Lichess puzzle PGNs embed the full source game; the puzzle starts from the
* final position. This function advances a Chess instance through the entire
* move list so we can hand the start-FEN to the board component.
*
* Returns null if the PGN is empty, malformed, or cannot be loaded.
*/
export function fenFromPgn(pgn: string): string | null {
if (!pgn || !pgn.trim()) return null;
try {
const chess = new Chess();
chess.loadPgn(pgn);
return chess.fen();
} catch {
return null;
}
}
/**
* Determine whose turn it is from a FEN string.
*
* Returns "w" or "b" (the active colour field, FEN field 2).
* Returns null if the FEN is invalid.
*/
export function sideToMove(fen: string): "w" | "b" | null {
if (!fen || !fen.trim()) return null;
try {
const chess = new Chess(fen);
return chess.turn();
} catch {
return null;
}
}
/**
* Determine the human-readable player colour for the side that must *respond*
* to the first puzzle move.
*
* In a Lichess puzzle the first move in `solution` is always an "opponent"
* forcing move. The human player controls the other side.
*
* Accepts a FEN of the position BEFORE the opponent's first move.
* Returns "white" if the opponent moves first as white (player is black), etc.
*
* Returns null on invalid FEN.
*/
export function puzzlePlayerColor(fen: string): "white" | "black" | null {
const turn = sideToMove(fen);
if (turn === null) return null;
// Opponent moves first, so the player is the other colour.
return turn === "w" ? "black" : "white";
}