From 758e7ce10fff8c51519f85c522569683e80b89e8 Mon Sep 17 00:00:00 2001 From: djmil Date: Thu, 16 Nov 2023 22:03:11 +0100 Subject: [PATCH] GameBoard: show slected board stones --- webapp/src/components/Checkers.jsx | 121 ++++++++++++++------- webapp/src/container/games/ActionPanel.jsx | 13 +-- webapp/src/container/games/GameBoard.jsx | 34 +++--- webapp/src/reducer/games.js | 1 + 4 files changed, 103 insertions(+), 66 deletions(-) diff --git a/webapp/src/components/Checkers.jsx b/webapp/src/components/Checkers.jsx index 6a94137..b940335 100644 --- a/webapp/src/components/Checkers.jsx +++ b/webapp/src/components/Checkers.jsx @@ -18,30 +18,36 @@ export const Color = { /* * Stone */ -export function Stone({ color }) { +export function Stone({ color, type }) { switch (color) { case Color.white: - return WhiteStone(); + return ; case Color.black: - return BlackStone(); + return ; case '': case undefined: case null: - return; // no color :) + return; // no stone :) default: console.warn("Unknown color: ", color) } } -export function WhiteStone() { - return +export function WhiteStone({ type }) { + if (type === 'KING') + return + else + return } -export function BlackStone() { - return +export function BlackStone({ type }) { + if (type === 'KING') + return + else + return } /* @@ -59,67 +65,98 @@ export function Player({ color, name }) { /* * Board */ -export function Board() { +export function Board({ game }) { + + const board = (game && game.board && typeof game.board === 'object') ? game.board : defaultBoard(); return
- - - - + + + +
- - - - + + + +
- - - - + + + +
- - - - + + + +
- - - - + + + +
- - - - + + + +
- - - - + + + +
- - - - + + + +
} +function defaultBoard() { + return { + 1: { color: "BLACK", type: "MAN" }, + 2: { color: "BLACK", type: "MAN" }, + 3: { color: "BLACK", type: "MAN" }, + 4: { color: "BLACK", type: "MAN" }, + 5: { color: "BLACK", type: "MAN" }, + 6: { color: "BLACK", type: "MAN" }, + 7: { color: "BLACK", type: "MAN" }, + 8: { color: "BLACK", type: "MAN" }, + 9: { color: "BLACK", type: "MAN" }, + 10: { color: "BLACK", type: "MAN" }, + 11: { color: "BLACK", type: "MAN" }, + 12: { color: "BLACK", type: "MAN" }, + 21: { color: "WHITE", type: "MAN" }, + 22: { color: "WHITE", type: "MAN" }, + 23: { color: "WHITE", type: "MAN" }, + 24: { color: "WHITE", type: "MAN" }, + 25: { color: "WHITE", type: "MAN" }, + 26: { color: "WHITE", type: "MAN" }, + 27: { color: "WHITE", type: "MAN" }, + 28: { color: "WHITE", type: "MAN" }, + 29: { color: "WHITE", type: "MAN" }, + 30: { color: "WHITE", type: "MAN" }, + 31: { color: "WHITE", type: "MAN" }, + 32: { color: "WHITE", type: "MAN" }, + } +} + function WhiteTile({ id, stone }) { return (
console.log('click', id)} > - {stone} +
); } diff --git a/webapp/src/container/games/ActionPanel.jsx b/webapp/src/container/games/ActionPanel.jsx index 5c217c4..5ad9865 100644 --- a/webapp/src/container/games/ActionPanel.jsx +++ b/webapp/src/container/games/ActionPanel.jsx @@ -10,23 +10,16 @@ export function Create({ onClick }) { const hasOpponent = games.newGame.opponentName && games.newGame.opponentColor; - const prepareNewGameRequest = () => { + const validateNewGame = () => { if (!hasOpponent) return alert("You have to select an opponent"); - const reqParams = { - opponentName: games.newGame.opponentName, - opponentColor: games.newGame.opponentColor, - board: null, // default board configuration - message: games.newGame.message - } - - onClick(reqParams); + onClick(games.newGame); } return ( diff --git a/webapp/src/container/games/GameBoard.jsx b/webapp/src/container/games/GameBoard.jsx index 047a5fb..65173b5 100644 --- a/webapp/src/container/games/GameBoard.jsx +++ b/webapp/src/container/games/GameBoard.jsx @@ -9,30 +9,36 @@ export default function GameBoard() { const games = useContext(GamesContext); const { pathname } = useLocation(); - const [opponentName, opponentColor, myColor] = (() => { + const game = (() => { if (matchPath('/games/new', pathname)) - return [games.newGame.opponentName, games.newGame.opponentColor, Color.opposite(games.newGame.opponentColor)]; - - let selectedGame; + return gameFrom(games.newGame); if (matchPath('/games/proposal', pathname)) - selectedGame = games.findGame({ uuid: games.proposal.selectedUUID }); + return games.findGame({ uuid: games.proposal.selectedUUID }); else if (matchPath('/games/active', pathname)) - selectedGame = games.findGame({ uuid: games.active.selectedUUID }); + return games.findGame({ uuid: games.active.selectedUUID }); else if (matchPath('/games/archive', pathname)) - selectedGame = games.findGame({ uuid: games.archive.selectedUUID }); + return games.findGame({ uuid: games.archive.selectedUUID }); - if (selectedGame) - return [selectedGame?.opponentName, Color.opposite(selectedGame?.myColor), selectedGame?.myColor]; - else - return ['', Color.white, Color.black]; // defaults + return {}; // defaults })(); // <<-- Execute + const opponentColor = Color.opposite(game?.myColor); +console.log("game", game) return (
- - - + + +
) +} + +function gameFrom(newGame) { + return { + myColor: Color.opposite(newGame.opponentColor), + opponentName: newGame.opponentName, + opponentColor: newGame.opponentColor, + board: newGame.board, + }; } \ No newline at end of file diff --git a/webapp/src/reducer/games.js b/webapp/src/reducer/games.js index 9dc5f41..379d4e8 100644 --- a/webapp/src/reducer/games.js +++ b/webapp/src/reducer/games.js @@ -7,6 +7,7 @@ export const gamesInitialState = { newGame: { opponentName: '', opponentColor: '', + board: null, message: '', },