This commit is contained in:
Maciej Caderek
2022-02-14 00:00:41 +01:00
parent 6274236ac7
commit e0b79a7071
26 changed files with 608 additions and 358 deletions

View File

@@ -1,6 +1,7 @@
import { PieceType, PieceColor, BoardData, Position } from "../types";
import { Chess, ChessInstance } from "chess.js";
import { cleanPGN } from "./PGNHelpers";
import { formatDate, formatName } from "../utils/formatters";
const MATERIAL_VALUE: Map<PieceType, number> = new Map([
["q", 9],
@@ -10,6 +11,13 @@ const MATERIAL_VALUE: Map<PieceType, number> = new Map([
["p", 1],
]);
const prepareHeaderEntry = (
entry: string | undefined,
ifEmpty: null | string = null
) => {
return !entry || entry === "?" ? ifEmpty : entry;
};
class Game {
private positions: Position[] = [];
private game: ChessInstance = new Chess();
@@ -149,7 +157,30 @@ class Game {
}
get header() {
return this.game.header();
const header = this.game.header();
const white = prepareHeaderEntry(header.White, "Anonymous") as string;
const black = prepareHeaderEntry(header.Black, "Anonymous") as string;
const date = prepareHeaderEntry(header.Date);
return {
White: white,
Black: black,
WhitePretty: formatName(white),
BlackPretty: formatName(black),
WhiteElo: prepareHeaderEntry(header.WhiteElo),
BlackElo: prepareHeaderEntry(header.BlackElo),
Date: date,
DatePretty: date === null ? null : formatDate(date),
Event: prepareHeaderEntry(header.Event),
Round: prepareHeaderEntry(header.Round),
Site: prepareHeaderEntry(header.Site),
Result: prepareHeaderEntry(header.Result),
};
}
get pgn() {
return this.game.pgn();
}
getPosition(ply: number) {