diff --git a/src/components/common/index.tsx b/src/components/common/index.tsx index 9289c90..ff00b71 100644 --- a/src/components/common/index.tsx +++ b/src/components/common/index.tsx @@ -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, diff --git a/src/components/play/playSidebar.tsx b/src/components/play/playSidebar.tsx index 8702f35..d0abc88 100644 --- a/src/components/play/playSidebar.tsx +++ b/src/components/play/playSidebar.tsx @@ -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; } @@ -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)); } diff --git a/src/slices/gameSlice.tsx b/src/slices/gameSlice.tsx index 1cc3dfe..91dd031 100644 --- a/src/slices/gameSlice.tsx +++ b/src/slices/gameSlice.tsx @@ -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({ @@ -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 } @@ -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; @@ -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 @@ -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(); diff --git a/src/types.tsx b/src/types.tsx index 1cb3b64..02bb6cd 100644 --- a/src/types.tsx +++ b/src/types.tsx @@ -32,7 +32,8 @@ interface Game { moves: string, start: string, lastMove: string, - greedy: boolean + greedy: boolean, + fromOpponent: boolean } interface User { diff --git a/src/utils/findPieces.tsx b/src/utils/findPieces.tsx index 7de4a63..e0c9611 100644 --- a/src/utils/findPieces.tsx +++ b/src/utils/findPieces.tsx @@ -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++) { @@ -220,6 +220,7 @@ export const findPieces = (modelRef: any, videoRef: any, canvasRef: any, boardRef.current.playSan(move); possibleMoves.clear(); greedyMoveToTime = {}; + state = zeros(64, 12); } } @@ -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); } } diff --git a/src/utils/lichess.tsx b/src/utils/lichess.tsx index 001584d..3e7ec5d 100644 --- a/src/utils/lichess.tsx +++ b/src/utils/lichess.tsx @@ -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; diff --git a/src/utils/math.tsx b/src/utils/math.tsx index cc2d166..7dca658 100644 --- a/src/utils/math.tsx +++ b/src/utils/math.tsx @@ -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)); } \ No newline at end of file