refacto : gameFromUrl

This commit is contained in:
GuillaumeSD
2024-02-23 23:32:29 +01:00
parent f77a425067
commit 89ca7d8f13
9 changed files with 105 additions and 31 deletions

View File

@@ -26,3 +26,26 @@ export const formatGameToDatabase = (game: Chess): Omit<Game, "id"> => {
result: headers.Result,
};
};
export const getGameToSave = (game: Chess, board: Chess): Chess => {
if (game.history().length) return game;
const headers = board.header();
if (!headers.Event) {
board.header("Event", "Freechess Game");
}
if (!headers.Site) {
board.header("Site", "Freechess");
}
if (!headers.Date) {
board.header(
"Date",
new Date().toISOString().split("T")[0].replaceAll("-", ".")
);
}
return board;
};

View File

@@ -130,7 +130,23 @@ export class Stockfish {
"bestmove"
);
return this.parseResults(results);
const parsedResults = this.parseResults(results);
const whiteToPlay = fen.split(" ")[1] === "w";
if (!whiteToPlay) {
const lines = parsedResults.lines.map((line) => ({
...line,
cp: line.cp ? -line.cp : line.cp,
}));
return {
...parsedResults,
lines,
};
}
return parsedResults;
}
private parseResults(results: string[]): MoveEval {
@@ -151,7 +167,16 @@ export class Stockfish {
if (result.startsWith("info")) {
const pv = this.getResultPv(result);
const multiPv = this.getResultProperty(result, "multipv");
if (!pv || !multiPv) continue;
const depth = this.getResultProperty(result, "depth");
if (!pv || !multiPv || !depth) continue;
if (
tempResults[multiPv] &&
parseInt(depth) < tempResults[multiPv].depth
) {
continue;
}
const cp = this.getResultProperty(result, "cp");
const mate = this.getResultProperty(result, "mate");
@@ -159,6 +184,8 @@ export class Stockfish {
pv,
cp: cp ? parseInt(cp) : undefined,
mate: mate ? parseInt(mate) : undefined,
depth: parseInt(depth),
multiPv: parseInt(multiPv),
};
}
}