refacto : load game
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { ChessComRawGameData } from "@/types/chessCom";
|
||||
import { ChessComGame } from "@/types/chessCom";
|
||||
import { getPaddedNumber } from "./helpers";
|
||||
import { LoadedGame } from "@/types/game";
|
||||
import { Chess } from "chess.js";
|
||||
|
||||
export const getChessComUserRecentGames = async (
|
||||
username: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<ChessComRawGameData[]> => {
|
||||
): Promise<LoadedGame[]> => {
|
||||
const date = new Date();
|
||||
const year = date.getUTCFullYear();
|
||||
const month = date.getUTCMonth() + 1;
|
||||
@@ -24,7 +26,7 @@ export const getChessComUserRecentGames = async (
|
||||
throw new Error("Error fetching games from Chess.com");
|
||||
}
|
||||
|
||||
const games: ChessComRawGameData[] = data?.games ?? [];
|
||||
const games: ChessComGame[] = data?.games ?? [];
|
||||
|
||||
if (games.length < 50) {
|
||||
const previousMonth = month === 1 ? 12 : month - 1;
|
||||
@@ -42,7 +44,8 @@ export const getChessComUserRecentGames = async (
|
||||
|
||||
const gamesToReturn = games
|
||||
.sort((a, b) => b.end_time - a.end_time)
|
||||
.slice(0, 50);
|
||||
.slice(0, 50)
|
||||
.map(formatChessComGame);
|
||||
|
||||
return gamesToReturn;
|
||||
};
|
||||
@@ -58,3 +61,57 @@ export const getChessComUserAvatar = async (
|
||||
|
||||
return typeof avatarUrl === "string" ? avatarUrl : null;
|
||||
};
|
||||
|
||||
const formatChessComGame = (data: ChessComGame): LoadedGame => {
|
||||
const game = new Chess();
|
||||
game.loadPgn(data.pgn);
|
||||
|
||||
return {
|
||||
id: data.uuid || data.url?.split("/").pop() || data.id,
|
||||
pgn: data.pgn || "",
|
||||
white: {
|
||||
name: data.white?.username || "White",
|
||||
rating: data.white?.rating || 0,
|
||||
title: data.white?.title,
|
||||
},
|
||||
black: {
|
||||
name: data.black?.username || "Black",
|
||||
rating: data.black?.rating || 0,
|
||||
title: data.black?.title,
|
||||
},
|
||||
result: game.getHeaders().Result,
|
||||
timeControl: getGameTimeControl(data),
|
||||
date: data.end_time
|
||||
? new Date(data.end_time * 1000).toLocaleDateString()
|
||||
: new Date().toLocaleDateString(),
|
||||
movesNb: game.history().length,
|
||||
url: data.url,
|
||||
};
|
||||
};
|
||||
|
||||
const getGameTimeControl = (game: ChessComGame): string | undefined => {
|
||||
const rawTimeControl = game.time_control;
|
||||
if (!rawTimeControl) return undefined;
|
||||
|
||||
const [firstPart, secondPart] = rawTimeControl.split("+");
|
||||
if (!firstPart) return undefined;
|
||||
|
||||
const timeControl = Number(firstPart);
|
||||
const increment = secondPart ? `+${secondPart}` : "";
|
||||
if (timeControl < 60) return `${timeControl}s${increment}`;
|
||||
|
||||
if (timeControl < 3600) {
|
||||
const minutes = Math.floor(timeControl / 60);
|
||||
const seconds = timeControl % 60;
|
||||
|
||||
return seconds
|
||||
? `${minutes}m${getPaddedNumber(seconds)}s${increment}`
|
||||
: `${minutes}m${increment}`;
|
||||
}
|
||||
|
||||
const hours = Math.floor(timeControl / 3600);
|
||||
const minutes = Math.floor((timeControl % 3600) / 60);
|
||||
return minutes
|
||||
? `${hours}h${getPaddedNumber(minutes)}m${increment}`
|
||||
: `${hours}h${increment}`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user