GameBoard: show previous and current moves

This commit is contained in:
djmil 2023-11-24 13:01:48 +01:00
parent 5639b5e8c9
commit a9eae999f6
5 changed files with 28 additions and 8 deletions

View File

@ -73,7 +73,7 @@ export default function useGamesApi() {
const pushGameMove = ({ uuid, move, message }) => { const pushGameMove = ({ uuid, move, message }) => {
doPushing(`/api/game/${uuid}/move`, 'PUT', { move, message }, { doPushing(`/api/game/${uuid}/move`, 'PUT', { move, message }, {
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'move' }), onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && {moveFrom: move[0], moveTo: move[1]} }),
onSuccess: (game) => { onSuccess: (game) => {
dispatchState({ type: 'update', game }); dispatchState({ type: 'update', game });
dispatchGuide({ type: 'UUIDmessage', uuid, message: ''}); dispatchGuide({ type: 'UUIDmessage', uuid, message: ''});

View File

@ -7,7 +7,7 @@
.Board { .Board {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
} }
.Board.flip { .Board.flip {
@ -16,6 +16,7 @@
.Tile { .Tile {
border: 1px solid #e4e4e4; border: 1px solid #e4e4e4;
padding: 0;
float: left; float: left;
text-align: center; text-align: center;
} }
@ -30,4 +31,8 @@
.Tile.selected { .Tile.selected {
color: grey; color: grey;
}
.Tile.prevmove {
background-color:bisque;
} }

View File

@ -71,16 +71,29 @@ export function Player({ color, name }) {
/* /*
* Board * Board
*/ */
export function Board({ board, flip, onStoneClick, onStoneMove }) { export function Board({ board, flip, prevMove, currMove, onStoneClick, onStoneMove }) {
// ugly & lazy
if (!board)
board = [];
if (!prevMove)
prevMove = [];
if (!currMove)
currMove = [];
const [[moveId, moveX, moveY], setMove] = useState([0, 0, 0]); const [[moveId, moveX, moveY], setMove] = useState([0, 0, 0]);
const isInteractive = (board && (typeof onStoneClick === 'function' || typeof onStoneMove === 'function')) ? ' interactive' : ''; const isInteractive = (board && (typeof onStoneClick === 'function' || typeof onStoneMove === 'function')) ? ' interactive' : '';
const WhiteTile = ({ id }) => { const WhiteTile = ({ id }) => {
const stone = board[id]; const stone = (id === currMove[1]) ? board[currMove[0]] : board[id];
return ( return (
<div className={'Tile white' + isInteractive + (id === moveId ? ' selected' : '')} <div className={'Tile white'
+ isInteractive
+ (id === moveId ? ' selected' : '')
+ (id === currMove[0] || id === currMove[1] ? ' selected' : '')
+ (id === prevMove[0] || id === prevMove[1] ? ' prevmove' : '')
}
onClick={!onStoneClick ? null : onClick={!onStoneClick ? null :
() => onStoneClick(id) () => onStoneClick(id)
} }

View File

@ -23,8 +23,10 @@ export default function GameBoard({ username, getGame, onStoneClick, onStoneMove
name={game.opponentName} name={game.opponentName}
/> />
<Board <Board
board={game.board || []} board={game.board}
flip={flipBoard} flip={flipBoard}
prevMove={game.previousMove}
currMove={[isPushing?.moveFrom, isPushing?.moveTo]}
onStoneClick={optionalOnStoneClick} onStoneClick={optionalOnStoneClick}
onStoneMove={optionalOnStoneMove} onStoneMove={optionalOnStoneMove}
/> />
@ -32,7 +34,6 @@ export default function GameBoard({ username, getGame, onStoneClick, onStoneMove
color={game.myColor || Color.white} color={game.myColor || Color.white}
name={myName} name={myName}
/> />
{(isPushing === 'move') ? <span>Moving...</span> : null}
</div> </div>
) )
} }

View File

@ -138,8 +138,9 @@ function Selectable({ game, selected, onClick }) {
onClick={() => onClick(game.uuid)} onClick={() => onClick(game.uuid)}
> >
<Board <Board
board={game.board || []} board={game.board}
flip={flipBoard} flip={flipBoard}
prevMove={game.previousMove}
/> />
<div className='Message'> <div className='Message'>
<Player <Player