Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/common/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import StudyButton from "./studyButton";
import FenButton from "./fenButton";
import DeviceButton from "./deviceButton";

export {
export {
SidebarButton, CornersButton, PgnButton, Icon, Corners,
HomeButton, Sidebar, Container, VideoAndSidebar, RecordButton, StopButton, StudyButton,
FenButton, DeviceButton,
Expand Down
7 changes: 4 additions & 3 deletions src/components/play/playSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const PlaySidebar = ({ piecesModelRef, xcornersModelRef, videoRef, canvasRef, si
useEffect(() => {
const colorToMove = gameRef.current.fen.split(" ")[1];
const lastMove = gameRef.current.lastMove;
if ((colorToMove === color) || (lastMove === "") || (gameId === undefined) || (color === undefined)) {
const fromOpponent = gameRef.current.fromOpponent;
if ((colorToMove === color) || (lastMove === "") || (gameId === undefined) || (color === undefined) || fromOpponent) {
return;
}

Expand All @@ -51,8 +52,8 @@ const PlaySidebar = ({ piecesModelRef, xcornersModelRef, videoRef, canvasRef, si
}

const board = makeBoard(gameRef.current);
board.move(lastMove);
const payload = makeUpdatePayload(board);
board.playUci(lastMove);
const payload = makeUpdatePayload(board, false, true);
console.log("payload", payload);
dispatch(gameUpdate(payload));
}
Expand Down
27 changes: 21 additions & 6 deletions src/slices/gameSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { START_FEN } from '../utils/constants';
import { parseFen, makeFen } from 'chessops/fen';
import { Chess } from 'chessops/chess';
import { parsePgn } from 'chessops/pgn';
import { parseSan } from 'chessops/san';
import { makeUci } from 'chessops/util';
import { parseSan, makeSan } from 'chessops/san';
import { makeUci, parseUci } from 'chessops/util';

const initialState: Game = {
"moves": "",
"fen": START_FEN,
"start": START_FEN,
"lastMove": "",
"greedy": false
"greedy": false,
"fromOpponent": false
};

const gameSlice = createSlice({
Expand Down Expand Up @@ -50,7 +51,8 @@ const gameSlice = createSlice({
"moves": action.payload.moves,
"fen": action.payload.fen,
"lastMove": action.payload.lastMove,
"greedy": action.payload.greedy
"greedy": action.payload.greedy,
"fromOpponent": action.payload.fromOpponent
}
return newState
}
Expand Down Expand Up @@ -82,7 +84,7 @@ export const makePgn = (game: Game) => {
return `[FEN "${game.start}"]` + "\n \n" + game.moves;
}

export const makeUpdatePayload = (board: any, greedy: boolean = false) => {
export const makeUpdatePayload = (board: any, greedy: boolean = false, fromOpponent: boolean = false) => {
const history = board.history || [];
const startFen = board.startFen || START_FEN;

Expand All @@ -94,7 +96,8 @@ export const makeUpdatePayload = (board: any, greedy: boolean = false) => {
"moves": moves,
"fen": fen,
"lastMove": lastMove,
"greedy": greedy
"greedy": greedy,
"fromOpponent": fromOpponent
}

return payload
Expand Down Expand Up @@ -130,6 +133,18 @@ export const makeBoard = (game: Game): any => {
return null;
};

board.playUci = (uci: string) => {
const move = parseUci(uci);
if (move) {
const san = makeSan(board, move);
(move as any).san = san;
board.history.push(move);
board.play(move);
return move;
}
return null;
};

board.undo = () => {
if (board.history.length > 0) {
board.history.pop();
Expand Down
3 changes: 2 additions & 1 deletion src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ interface Game {
moves: string,
start: string,
lastMove: string,
greedy: boolean
greedy: boolean,
fromOpponent: boolean
}

interface User {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/findPieces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const getUpdate = (scoresTensor: tf.Tensor2D, squares: number[]) => {

for (let i = 0; i < squares.length; i++) {
const square = squares[i];
if (square == -1) {
if (typeof square !== 'number' || !Number.isInteger(square) || square < 0 || square >= 64) {
continue;
}
for (let j = 0; j < 12; j++) {
Expand Down Expand Up @@ -220,6 +220,7 @@ export const findPieces = (modelRef: any, videoRef: any, canvasRef: any,
boardRef.current.playSan(move);
possibleMoves.clear();
greedyMoveToTime = {};
state = zeros(64, 12);
}
}

Expand All @@ -236,6 +237,7 @@ export const findPieces = (modelRef: any, videoRef: any, canvasRef: any,
if (hasGreedyMove) {
boardRef.current.playSan(move);
greedyMoveToTime = { greedyMove: greedyMoveToTime[move] };
state = zeros(64, 12);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/lichess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const fetchResponse = async (token: string, path: string, options: any = {}) =>
const res: any = await window.fetch(`${lichessHost}${path}`, config);
if (!res.ok) {
const err = `${res.status} ${res.statusText}`;
alert(err);
console.error(err);
throw err;
}
return res;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/math.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export const clamp = (x: number, min: number, max: number) => {
}

export const zeros = (rows: number, columns: number) => {
return Array.from(Array(rows), _ => Array(columns).fill(0));
return Array.from({ length: rows }, () => Array(columns).fill(0));
}