mancala/apps/frontend/src/components/board/BoardView.tsx

84 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-05-08 17:13:21 +03:00
import * as React from "react";
2022-07-14 13:23:15 +03:00
import { FunctionComponent } from "react";
2022-07-14 13:38:00 +03:00
import { Context } from "../../context/context";
import { BoardViewModel, PitViewModel } from "@mancala/core";
2022-07-14 08:53:26 +03:00
import PitView from "./PitView";
import StoreView from "./StoreView";
2024-06-17 15:37:17 +03:00
import { Game } from "@mancala/core";
2022-08-01 22:24:48 +03:00
import { Pit } from "mancala.js";
2021-06-29 03:29:46 +03:00
2022-05-08 17:13:21 +03:00
const BoardView: FunctionComponent<{
2022-08-01 22:24:48 +03:00
game: Game;
context: Context;
boardId: string;
boardViewModel: BoardViewModel;
2022-09-02 00:10:41 +03:00
revert: boolean;
2022-07-13 22:45:06 +03:00
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 }) => {
2022-08-01 22:24:48 +03:00
const mancalaGame = game?.mancalaGame;
const theme = context.themeManager.theme;
2022-09-02 00:10:41 +03:00
// 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) => (
<PitView
key={key}
pitViewModel={pitViewModel}
context={context}
onClick={() => onPitSelect(pit.index, pit)}
fontSize={pitWidth / 6}
/>
);
const createPitViewList = (pits: Pit[]) =>
pits.map((pit, index) => createPitView(index, pit, boardViewModel.pits[pit.index]));
2022-09-02 00:10:41 +03:00
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];
2022-05-08 17:13:21 +03:00
return (
<div className="board" style={{ background: theme.boardColor, ...sizeVariables }}>
2022-07-14 13:23:15 +03:00
<StoreView
context={context}
2022-09-02 00:10:41 +03:00
pitViewModel={revert ? player2BankViewModel : player1BankViewModel}
2022-07-14 13:23:15 +03:00
gridColumn="8 / 9"
gridRow="1 / 3"
fontSize={pitWidth / 6}
2022-07-14 13:23:15 +03:00
/>
<StoreView
context={context}
2022-09-02 00:10:41 +03:00
pitViewModel={revert ? player1BankViewModel : player2BankViewModel}
2022-07-14 13:23:15 +03:00
gridColumn="1 / 2"
gridRow="1 / 3"
fontSize={pitWidth / 6}
2022-07-14 13:23:15 +03:00
/>
2022-09-02 00:10:41 +03:00
{revert ? player1Pits?.reverse() : player2Pits?.reverse()}
{revert ? player2Pits : player1Pits}
2022-07-14 23:13:41 +03:00
<style jsx>{`
.board {
padding: calc(2 * var(--board-unit));
2022-07-14 23:13:41 +03:00
display: grid;
grid-template-columns: repeat(8, calc(11 * var(--board-unit)));
grid-template-rows: repeat(2, calc(11 * var(--board-unit)));
border-radius: calc(3 * var(--board-unit));
2022-07-14 23:13:41 +03:00
transition: background-color 0.5s;
}
`}</style>
2022-05-08 17:13:21 +03:00
</div>
);
};
2021-06-29 03:29:46 +03:00
2022-05-08 17:13:21 +03:00
export default BoardView;