This commit is contained in:
Maciej Caderek
2022-01-16 05:20:19 +01:00
parent e1767fd48c
commit 7052c74171
7 changed files with 168 additions and 40 deletions

View File

@@ -1,4 +1,5 @@
import { Chess, ChessInstance, Move } from "chess.js";
import { cleanPGN } from "./PGNHelpers";
class Game {
private game: ChessInstance;
@@ -12,33 +13,8 @@ class Game {
this.moves = [];
}
private cleanPGN(pgn: string) {
const game = new Chess();
game.load_pgn(pgn);
game.delete_comments();
const [_, moves] = game.pgn().split("\n\n");
const header = Object.entries(game.header())
.filter(([key]) =>
[
"event",
"site",
"white",
"black",
"date",
"result",
"opening",
].includes(key.toLowerCase())
)
.map(([key, val]) => `[${key} "${val}"]`)
.sort()
.join("\n");
return [header, moves].join("\n\n");
}
loadPGN(pgn: string) {
this.game.load_pgn(this.cleanPGN(pgn));
this.game.load_pgn(cleanPGN(pgn));
this.game.delete_comments();
this.moves = this.game.history({ verbose: true });
this.currentPly = 0;