From 93ed0c1d8b225e4f86cc2d522da8fb9372923da5 Mon Sep 17 00:00:00 2001 From: Manu Artero Anguita Date: Fri, 25 Nov 2022 14:12:14 +0100 Subject: [PATCH] feat: declare greatest empire - function declareGreatestEmpire(player) on usePlayer() - test usePlayer.declareGreatestEmpire - function: empireSize(board): Record - test empire-size - refactor: move isConquering({ ... }) from useBoard() - check if player is creating greatest empire on build action - TODO: check if player is creating greatest empire on conquer! --- src/contexts/game-context/use-board.ts | 14 +--- .../game-context/use-game-context.tsx | 18 +++-- .../game-context/use-players.test.tsx | 67 ++++++++++++++++--- src/contexts/game-context/use-players.ts | 18 ++++- src/models/empire-size.test.ts | 40 +++++++++++ src/models/empire-size.ts | 18 +++++ src/models/score.ts | 35 ++++++++++ 7 files changed, 184 insertions(+), 26 deletions(-) create mode 100644 src/models/empire-size.test.ts create mode 100644 src/models/empire-size.ts create mode 100644 src/models/score.ts diff --git a/src/contexts/game-context/use-board.ts b/src/contexts/game-context/use-board.ts index 92e934f..c76f2d0 100644 --- a/src/contexts/game-context/use-board.ts +++ b/src/contexts/game-context/use-board.ts @@ -26,14 +26,6 @@ export function useBoard() { }); }; - const isConquering = ({ piece, to }: { to: TileID; piece: Piece }) => { - if (board[to].building && board[to].building?.owner !== piece.owner) { - console.info(`GameContext.isConquering({ ... }): YES`); - return true; - } - return false; - }; - /** direct update on the board; no validity check */ const movePiece = ({ piece, from, to }: MoveAction) => { console.info( @@ -48,7 +40,6 @@ export function useBoard() { ...currentBoard[to], piece, }; - // is it better to use isConquering() ? if (targetTile.building) { targetTile.building = { ...targetTile.building, @@ -79,7 +70,7 @@ export function useBoard() { }); }; - return { board, buildOnTile, movePiece, recruitOnTile, isConquering }; + return { board, buildOnTile, movePiece, recruitOnTile }; } /** @@ -90,6 +81,7 @@ export const emptyBoard: Board = tiles.reduce((acc, key) => { acc[key] = { terrain: "field", piece: undefined, + building: undefined, }; return acc; -}, {} as any); +}, {} as Record); diff --git a/src/contexts/game-context/use-game-context.tsx b/src/contexts/game-context/use-game-context.tsx index 785f154..319cbad 100644 --- a/src/contexts/game-context/use-game-context.tsx +++ b/src/contexts/game-context/use-game-context.tsx @@ -3,6 +3,8 @@ import { logRender } from "utils/console"; import { useBoard, emptyBoard } from "./use-board"; import usePlayers from "./use-players"; import { emptyTimeline, useTimeline } from "./use-timeline"; +import { isConquering, isCreatingGreatesEmpire } from "models/score"; +import { empireSize } from "models/empire-size"; const GameContext = createContext({ phase: "setup", @@ -46,10 +48,10 @@ export function GameContextProvider({ children }: Props): JSX.Element { logRender("GameContextProvider"); const [phase, setPhase] = useState("planification"); // will be setup - const { board, isConquering, buildOnTile, movePiece, recruitOnTile } = - useBoard(); + const { board, buildOnTile, movePiece, recruitOnTile } = useBoard(); const { timeline, nextCard, planification, newTurn } = useTimeline(); - const { players, firstPlayer, scorePoint } = usePlayers(); + const { players, firstPlayer, scorePoint, declareGreatestEmpire } = + usePlayers(); /* derived state */ const activeCard = timeline.current; @@ -98,13 +100,19 @@ export function GameContextProvider({ children }: Props): JSX.Element { players, build(action: BuildAction) { + if ( + isCreatingGreatesEmpire({ ...action, empires: empireSize(board) }) + ) { + declareGreatestEmpire(action.building.owner); + } buildOnTile(action); resolveActionCard(); }, move(action: MoveAction) { - if (isConquering(action)) { - scorePoint(action.piece.owner); + const player = action.piece.owner; + if (isConquering({ player, targetTile: board[action.to] })) { + scorePoint(player); } movePiece(action); resolveActionCard(); diff --git a/src/contexts/game-context/use-players.test.tsx b/src/contexts/game-context/use-players.test.tsx index f663096..a2a616f 100644 --- a/src/contexts/game-context/use-players.test.tsx +++ b/src/contexts/game-context/use-players.test.tsx @@ -2,21 +2,37 @@ import { render, screen, fireEvent } from "@testing-library/react"; import usePlayers from "./use-players"; function TestingComponent() { - const { players, firstPlayer, scorePoint } = usePlayers(); + const { players, firstPlayer, scorePoint, declareGreatestEmpire } = + usePlayers(); return ( <>
    {players.map((player, i) => ( -
  • {`${player.player} - ${player.points} points`}
  • +
  • {`${player.player} - ${ + player.points + } points${player.greatestEmpirePoint ? "*" : ""}`}
  • ))}
-