Skip to content

Commit

Permalink
fix missing chess-tcn dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Apr 27, 2023
1 parent 3120080 commit e7481a5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@tiptap/pm": "2.0.0-beta.220",
"@tiptap/react": "2.0.0-beta.220",
"@tiptap/starter-kit": "2.0.0-beta.220",
"chess-tcn": "^1.0.3",
"chess.js": "1.0.0-beta.3",
"chessground": "^8.3.7",
"dayjs": "^1.11.7",
Expand Down
8 changes: 1 addition & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/components/tabs/NewTabHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ function ImportModal({
if (!link) return;
let pgn = "";
if (link.includes("chess.com")) {
const gameId = link.split("/").pop()!;
pgn = await getChesscomGame(gameId);
pgn = await getChesscomGame(link);
} else if (link.includes("lichess")) {
const gameId = link.split("/")[3];
pgn = await getLichessGame(gameId);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,10 @@ export function getCompleteGame(pgn: string): CompleteGame {
return game;
}

export function handleMove(chess: Chess, orig: Key, dest: Key): Square | null {
export function handleMove(chess: Chess, orig: Key, dest: Key): Square {
if (orig === "a0" || dest === "a0") {
// NOTE: Idk if this can happen
return null;
throw new Error("Invalid move");
}
// allow castling to the rooks
if (chess.get(orig).type === KING && chess.get(dest).type === ROOK) {
Expand Down
25 changes: 19 additions & 6 deletions src/utils/chesscom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { writeTextFile } from "@tauri-apps/api/fs";
import { fetch } from "@tauri-apps/api/http";
import { appDataDir, resolve } from "@tauri-apps/api/path";
import tcn from "chess-tcn";
import { Chess } from "chess.js";
import { handleMove } from "./chess";
import { decodeTCN } from "./tcn";
const base_url = "https://api.chess.com";

type ChessComPerf = {
Expand Down Expand Up @@ -97,10 +98,20 @@ export async function downloadChessCom(
);
}

export async function getChesscomGame(gameId: string) {
const apiData = await fetch<{ game: { moveList: string, pgnHeaders: any } }>(
`https://www.chess.com/callback/live/game/${gameId}`
);
export async function getChesscomGame(gameURL: string) {
const regex = /.*\/game\/(live|daily)\/(\d+)/;
const match = gameURL.match(regex);

if (!match) {
return "";
}

const gameType = match[1];
const gameId = match[2];

const apiData = await fetch<{
game: { moveList: string; pgnHeaders: any };
}>(`https://www.chess.com/callback/${gameType}/game/${gameId}`);
const apiDataJson = await apiData.data;
const moveList = apiDataJson.game.moveList;
const headers = apiDataJson.game.pgnHeaders;
Expand All @@ -113,7 +124,9 @@ export async function getChesscomGame(gameId: string) {
chess.header(header, headers[header]);
}
moves.forEach((move) => {
const m = tcn.decode(move);
const m = decodeTCN(move);
const newDest = handleMove(chess, m.from, m.to);
m.to = newDest;
chess.move(m);
});
return chess.pgn();
Expand Down
38 changes: 38 additions & 0 deletions src/utils/tcn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Move, PieceSymbol, Square } from "chess.js";

const pieceEncoding =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?{~}(^)[_]@#$,./&-*++=";

export function decodeTCN(moveCode: string): Move {
// @ts-ignore
const move: Move = {};
const codeLength = moveCode.length;
const decodedMoves: Move[] = [];

for (let i = 0; i < codeLength; i += 2) {
const pieceIndex1 = pieceEncoding.indexOf(moveCode[i]);
let pieceIndex2 = pieceEncoding.indexOf(moveCode[i + 1]);

if (pieceIndex2 > 63) {
const promotion = "qnrbkp"[Math.floor((pieceIndex2 - 64) / 3)];
const newIndex =
pieceIndex1 +
(pieceIndex1 < 16 ? -8 : 8) +
((pieceIndex2 - 1) % 3) -
1;

move.promotion = promotion as PieceSymbol;
pieceIndex2 = newIndex;
}

move.from = (pieceEncoding[pieceIndex1 % 8] +
(Math.floor(pieceIndex1 / 8) + 1).toString()) as Square;

move.to = (pieceEncoding[pieceIndex2 % 8] +
(Math.floor(pieceIndex2 / 8) + 1).toString()) as Square;

decodedMoves.push(move);
}

return decodedMoves[0];
}

0 comments on commit e7481a5

Please sign in to comment.