File size: 3,864 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * 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";
}