refacto : global refacto

This commit is contained in:
GuillaumeSD
2024-02-22 23:18:02 +01:00
parent 2a74b62bae
commit 2af0959e82
24 changed files with 230 additions and 190 deletions

View File

@@ -1,20 +1,22 @@
import { Game } from "@/types/game";
import { Chess } from "chess.js";
export const pgnToFens = (pgn: string): string[] => {
const game = new Chess();
game.loadPgn(pgn);
export const getFens = (game: Chess): string[] => {
return game.history({ verbose: true }).map((move) => move.before);
};
export const getGameFromPgn = (pgn: string): Omit<Game, "id"> => {
export const getGameFromPgn = (pgn: string): Chess => {
const game = new Chess();
game.loadPgn(pgn);
return game;
};
export const formatGameToDatabase = (game: Chess): Omit<Game, "id"> => {
const headers: Record<string, string | undefined> = game.header();
return {
pgn,
pgn: game.pgn(),
event: headers.Event,
site: headers.Site,
date: headers.Date,