pgn-manager
Version:
Libraray built on top of chess.js and pgn-parser to load and process PGN files in typescript.
137 lines (136 loc) • 5.12 kB
TypeScript
import { ShortMove } from "chess.js";
import * as pgnParser from "pgn-parser";
import type { ParsedPGN, Move, Rav, Header, Result } from "pgn-parser";
export declare const FEN_START_POSITION = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
export declare const FEN_EMPTY_POSITION = "8/8/8/8/8/8/8/8";
declare class PGNManager {
/** The raw PGN string input */
private rawPGN;
/** The parsed PGN game object */
private game;
/** Array of moves in traversal order */
private sortedMoves;
/** Map of moves to their FEN position strings */
private moveFen;
/** Map of FEN position string to their move object */
private fenMove;
/** Map of moves to their parent variations (or null for mainline) */
private moveParent;
/** Map of variations to their parent moves */
private ravParent;
/** Map of moves to the color of the player who made the move */
private moveColor;
/**
* Creates a new PGNManager instance
* @param pgn - The PGN string to parse and manage
*/
constructor(pgn: string);
/**
* Initializes the game traversal starting from the initial position
* @param game - The parsed PGN game object
*/
private dfOnGame;
/**
* Performs depth-first traversal of the game moves and variations
* @param move - The current move being processed
* @param parent - The parent RAV (variation) containing the move
* @param chessGame - The chess instance for the current position
*/
private dfsOnGame;
/**
* Gets the raw PGN string
* @returns The original PGN string
*/
get pgn(): string;
/**
* Gets the parsed PGN object
* @returns The parsed PGN game object
*/
get parsedPGN(): ParsedPGN;
/**
* Gets the game headers
* @returns Array of game headers
*/
get headers(): Array<Header>;
/**
* Gets a move by its number in the sequence
* @param moveNumber - The 1-based index of the move
* @returns The move object at the specified position
*/
getMove: (moveNumber: number) => pgnParser.Move;
/**
* Gets the number of a move in the sequence
* @param move - The move object
* @returns The 1-based index of the move
*/
getMoveNumber: (move: Move) => number;
/**
* Gets the next move in the sequence
* @param moveOrId - The current move object or move number
* @returns The next move in the sequence
* @throws Error if there are no moves in the game
*/
nextMove: (moveOrId: Move | number | undefined) => Move;
/**
* Checks if there is a next move available
* @param moveOrId - The current move object or move number
* @returns True if there is a next move, false otherwise
*/
hasNextMove: (moveOrId: Move | number) => boolean;
/**
* Gets the previous move in the sequence
* @param moveOrId - The current move object or move number
* @returns The previous move or undefined if at the start
* @throws Error if there are no moves or if the move parameter is invalid
*/
previousMove: (moveOrId: Move | number) => Move | undefined;
/**
* Gets the first move in the game
* @returns The first move
* @throws Error if there are no moves in the game
*/
getFirstMove: () => pgnParser.Move;
/**
* Gets the last move in the game
* @returns The last move
* @throws Error if there are no moves in the game
*/
getLastMove: () => pgnParser.Move;
/**
* Gets the FEN string for a specific move
* @param moveOrId - The move object or move number
* @returns The FEN string representing the position after the move
* @throws Error if the move parameter is invalid
*/
getMoveFen: (moveOrId: Move | number) => string;
/**
* Gets the parent RAV (variation) for a move
* @param moveOrId - The move object or move number
* @returns The parent RAV or null if the move is in the main line
* @throws Error if the move parameter is invalid
*/
getParentRav: (moveOrId: Move | number) => Rav | null;
/**
* Gets the color of the player who made the move
* @param moveOrId - The move object or move ID number
* @returns "w" for white or "b" for black
* @throws Error if the move parameter is invalid
*/
getMoveColor: (moveOrId: Move | number) => "w" | "b";
/***
* Pushes a new move into the game
* @param moveId - The ID of the move to push
* @param newMove - The move object to add
* @param result - The result of the game after this move (default is "*")
* @returns The newly created move object
* @throws Error if the move parameter is invalid
*/
pushMove: (moveId: number, newMove: ShortMove, result?: Result) => Move;
/**
* Delete a move and all subsequent moves in its variation from the game
* @param moveId - The ID of the move to delete from
* @throws Error if the move parameter is invalid
*/
deleteMove: (moveId: number) => void;
}
export default PGNManager;