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 ? "*" : ""}`}
  • ))}
-