From cd9bad76467cb2d0c4e26033a0de28b122dd5849 Mon Sep 17 00:00:00 2001 From: djmil Date: Mon, 20 Nov 2023 17:29:28 +0100 Subject: [PATCH] default board is empty --- webapp/src/components/Checkers.jsx | 12 +++-- webapp/src/container/games/GameBoard.jsx | 54 +++++++++++-------- .../src/container/games/view/GameSelector.jsx | 3 +- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/webapp/src/components/Checkers.jsx b/webapp/src/components/Checkers.jsx index 25c73d4..13c3120 100644 --- a/webapp/src/components/Checkers.jsx +++ b/webapp/src/components/Checkers.jsx @@ -71,10 +71,12 @@ export function Player({ color, name }) { /* * Board */ -export function Board({ game, onStoneClick, onStoneMove }) { +export function Board({ board, flip, onStoneClick, onStoneMove }) { const [[moveId, moveX, moveY], setMove] = useState([0, 0, 0]); - const board = (game && game.board && typeof game.board === 'object') ? game.board : defaultBoard; + if (!board) + board = []; + const isInteractive = (typeof onStoneClick === 'function' || typeof onStoneMove === 'function') ? ' interactive' : ''; const WhiteTile = ({ id }) => { @@ -83,7 +85,7 @@ export function Board({ game, onStoneClick, onStoneMove }) { return (
onStoneClick(game.uuid, id) + () => onStoneClick(id) } onMouseDown={!onStoneMove || !stone ? null : @@ -91,7 +93,7 @@ export function Board({ game, onStoneClick, onStoneMove }) { } onMouseUp={!onStoneMove || !moveId ? null : - () => { onStoneMove(game.uuid, [moveId, id]); setMove([0, 0, 0]) } + () => { onStoneMove([moveId, id]); setMove([0, 0, 0]) } } > @@ -106,7 +108,7 @@ export function Board({ game, onStoneClick, onStoneMove }) { const movingStone = board[moveId]; return ( -
e.buttons ? setMove([moveId, e.clientX, e.clientY]) : setMove([0, 0, 0]) } diff --git a/webapp/src/container/games/GameBoard.jsx b/webapp/src/container/games/GameBoard.jsx index 2d741d0..71f4202 100644 --- a/webapp/src/container/games/GameBoard.jsx +++ b/webapp/src/container/games/GameBoard.jsx @@ -6,34 +6,46 @@ import { Color, Player, Board } from '../../components/Checkers'; import './GameBoard.css'; export default function GameBoard({ username, onStoneClick, onStoneMove }) { - const games = useContext(GamesContext); - const { pathname } = useLocation(); - - const game = (() => { - if (matchPath('/games/new', pathname)) - return games.newGame; - - if (matchPath('/games/proposal', pathname)) - return games.findGame({ uuid: games.proposal.selectedUUID }); - - if (matchPath('/games/active', pathname)) - return games.findGame({ uuid: games.active.selectedUUID }); - - if (matchPath('/games/archive', pathname)) - return games.findGame({ uuid: games.archive.selectedUUID }); - - return {}; - })(); // <<-- Execute + const game = useSelectedGame(); const opponentColor = Color.opposite(game?.myColor); - const [opponentName, myName] = game?.opponentName ? [game.opponentName, username] : ['', '']; + const [opponentName, myName] = (game?.opponentName) ? [game.opponentName, username] : ['', '']; + const flipBoard = (game?.myColor === Color.black) ? true : null; + + const optionalOnStoneClick = (typeof onStoneClick !== 'function') ? null : + (cellId) => onStoneClick(game?.uuid, cellId); + + const optionalOnStoneMove = (typeof onStoneMove !== 'function') ? null : + (move) => onStoneMove(game?.uuid, move); return (
- + - { games.isPushingGameMove ? Moving... : null} + {game?.isPushingGameMove ? Moving... : null /* TODO: isPushing shall be stored per game. curernty it is global indicator */}
) +} + +export function useSelectedGame() { + const games = useContext(GamesContext); + const { pathname } = useLocation(); + + if (matchPath('/games/new', pathname)) + return games.newGame; + + if (matchPath('/games/proposal', pathname)) + return games.findGame({ uuid: games.proposal.selectedUUID }); + + if (matchPath('/games/active', pathname)) + return games.findGame({ uuid: games.active.selectedUUID }); + + if (matchPath('/games/archive', pathname)) + return games.findGame({ uuid: games.archive.selectedUUID }); + + return undefined; } \ No newline at end of file diff --git a/webapp/src/container/games/view/GameSelector.jsx b/webapp/src/container/games/view/GameSelector.jsx index cc23657..86e1e52 100644 --- a/webapp/src/container/games/view/GameSelector.jsx +++ b/webapp/src/container/games/view/GameSelector.jsx @@ -113,12 +113,13 @@ function Selectable({ game, selected, onClick }) { const myColor = game.myColor; const opponentColor = Color.opposite(myColor); const opponentName = game.opponentName; + const flipBoard = (game?.myColor === Color.black) ? true : null; return (
onClick(game.uuid)} > - +
{game.message}