default board is empty

This commit is contained in:
djmil 2023-11-20 17:29:28 +01:00
parent 579f52ed04
commit cd9bad7646
3 changed files with 42 additions and 27 deletions

View File

@ -71,10 +71,12 @@ export function Player({ color, name }) {
/* /*
* Board * Board
*/ */
export function Board({ game, onStoneClick, onStoneMove }) { export function Board({ board, flip, onStoneClick, onStoneMove }) {
const [[moveId, moveX, moveY], setMove] = useState([0, 0, 0]); 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 isInteractive = (typeof onStoneClick === 'function' || typeof onStoneMove === 'function') ? ' interactive' : '';
const WhiteTile = ({ id }) => { const WhiteTile = ({ id }) => {
@ -83,7 +85,7 @@ export function Board({ game, onStoneClick, onStoneMove }) {
return ( return (
<div className={'Tile white' + isInteractive + (id === moveId ? ' selected' : '')} <div className={'Tile white' + isInteractive + (id === moveId ? ' selected' : '')}
onClick={!onStoneClick ? null : onClick={!onStoneClick ? null :
() => onStoneClick(game.uuid, id) () => onStoneClick(id)
} }
onMouseDown={!onStoneMove || !stone ? null : onMouseDown={!onStoneMove || !stone ? null :
@ -91,7 +93,7 @@ export function Board({ game, onStoneClick, onStoneMove }) {
} }
onMouseUp={!onStoneMove || !moveId ? null : onMouseUp={!onStoneMove || !moveId ? null :
() => { onStoneMove(game.uuid, [moveId, id]); setMove([0, 0, 0]) } () => { onStoneMove([moveId, id]); setMove([0, 0, 0]) }
} }
> >
<Stone color={stone?.color} type={stone?.type} /> <Stone color={stone?.color} type={stone?.type} />
@ -106,7 +108,7 @@ export function Board({ game, onStoneClick, onStoneMove }) {
const movingStone = board[moveId]; const movingStone = board[moveId];
return ( return (
<div className={'Board' + (game?.myColor === Color.black ? ' flip' : '')} <div className={'Board' + (flip ? ' flip' : '')}
onMouseMove={!moveId ? null : onMouseMove={!moveId ? null :
(e) => e.buttons ? setMove([moveId, e.clientX, e.clientY]) : setMove([0, 0, 0]) (e) => e.buttons ? setMove([moveId, e.clientX, e.clientY]) : setMove([0, 0, 0])
} }

View File

@ -6,34 +6,46 @@ import { Color, Player, Board } from '../../components/Checkers';
import './GameBoard.css'; import './GameBoard.css';
export default function GameBoard({ username, onStoneClick, onStoneMove }) { export default function GameBoard({ username, onStoneClick, onStoneMove }) {
const games = useContext(GamesContext); const game = useSelectedGame();
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 opponentColor = Color.opposite(game?.myColor); 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 ( return (
<div className='GameBoard'> <div className='GameBoard'>
<Player color={opponentColor || Color.black} name={opponentName} /> <Player color={opponentColor || Color.black} name={opponentName} />
<Board game={game} onStoneClick={onStoneClick} onStoneMove={onStoneMove} /> <Board board={game?.board} flip={flipBoard}
onStoneClick={optionalOnStoneClick}
onStoneMove={optionalOnStoneMove}
/>
<Player color={game?.myColor || Color.white} name={myName} /> <Player color={game?.myColor || Color.white} name={myName} />
{ games.isPushingGameMove ? <span>Moving...</span> : null} {game?.isPushingGameMove ? <span>Moving...</span> : null /* TODO: isPushing shall be stored per game. curernty it is global indicator */}
</div> </div>
) )
} }
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;
}

View File

@ -113,12 +113,13 @@ function Selectable({ game, selected, onClick }) {
const myColor = game.myColor; const myColor = game.myColor;
const opponentColor = Color.opposite(myColor); const opponentColor = Color.opposite(myColor);
const opponentName = game.opponentName; const opponentName = game.opponentName;
const flipBoard = (game?.myColor === Color.black) ? true : null;
return ( return (
<div className={'Selectable' + (selected ? ' selected' : '')} <div className={'Selectable' + (selected ? ' selected' : '')}
onClick={() => onClick(game.uuid)} onClick={() => onClick(game.uuid)}
> >
<Board game={game} /> <Board board={game.board} flip={flipBoard} />
<div className='Message'> <div className='Message'>
<Player color={opponentColor} name={opponentName} /> <Player color={opponentColor} name={opponentName} />
<q>{game.message}</q> <q>{game.message}</q>