import * as React from "react"; import { FunctionComponent } from "react"; import { Context } from "../../context/context"; import { BoardViewModel, PitViewModel } from "@mancala/core"; import PitView from "./PitView"; import StoreView from "./StoreView"; import { Game } from "@mancala/core"; import { Pit } from "mancala.js"; const BoardView: FunctionComponent<{ game: Game; context: Context; boardId: string; boardViewModel: BoardViewModel; revert: boolean; onPitSelect: (index: number, pit: Pit) => void; // Width in px, e.g. for previews; by default the board is as wide as the viewport. width?: number; }> = ({ game, context, boardId, boardViewModel, revert, onPitSelect, width }) => { const mancalaGame = game?.mancalaGame; const theme = context.themeManager.theme; // The board is 92 units wide (8 columns of 11 plus padding); all board parts are sized in units. const unit = width !== undefined ? width / 92 : (typeof window !== "undefined" ? window.innerWidth / 100 : 10); const pitWidth = unit * 11; const sizeVariables = { "--board-unit": width !== undefined ? `${unit}px` : "1vw", "--board-spacing": width !== undefined ? `${unit * 0.3}px` : "5px", } as React.CSSProperties; const createPitView = (key: any, pit: Pit, pitViewModel: PitViewModel) => ( onPitSelect(pit.index, pit)} fontSize={pitWidth / 6} /> ); const createPitViewList = (pits: Pit[]) => pits.map((pit, index) => createPitView(index, pit, boardViewModel.pits[pit.index])); const player1Pits = createPitViewList(mancalaGame?.board.player1Pits); const player2Pits = createPitViewList(mancalaGame?.board.player2Pits); const player1BankIndex = mancalaGame?.board.player1BankIndex(); const player2BankIndex = mancalaGame?.board.player2BankIndex(); const player1BankViewModel = boardViewModel.pits[player1BankIndex]; const player2BankViewModel = boardViewModel.pits[player2BankIndex]; return (
{revert ? player1Pits?.reverse() : player2Pits?.reverse()} {revert ? player2Pits : player1Pits}
); }; export default BoardView;