2022-05-08 17:13:21 +03:00
|
|
|
import * as React from "react";
|
2022-07-14 13:23:15 +03:00
|
|
|
import { MancalaGame, Pit } from "mancala.js";
|
|
|
|
|
import { FunctionComponent } from "react";
|
2022-07-14 13:38:00 +03:00
|
|
|
import { Context } from "../../context/context";
|
2022-07-14 08:53:26 +03:00
|
|
|
import BoardViewModel from "../../viewmodel/BoardViewModel";
|
|
|
|
|
import PitViewModel from "../../viewmodel/PitViewModel";
|
|
|
|
|
import PitView from "./PitView";
|
|
|
|
|
import StoreView from "./StoreView";
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2022-05-08 17:13:21 +03:00
|
|
|
const BoardView: FunctionComponent<{
|
2022-07-14 13:23:15 +03:00
|
|
|
game: MancalaGame;
|
2022-05-15 02:01:51 +03:00
|
|
|
context: Context;
|
|
|
|
|
boardId: string;
|
|
|
|
|
boardViewModel: BoardViewModel;
|
2022-05-08 17:13:21 +03:00
|
|
|
userKey: string;
|
2022-07-13 22:45:06 +03:00
|
|
|
onPitSelect: (index: number, pit: Pit) => void;
|
|
|
|
|
}> = ({ game, context, boardId, boardViewModel, userKey, onPitSelect: onPitSelect }) => {
|
2022-07-13 22:39:24 +03:00
|
|
|
const createPitView = (key: any, pitViewModel: PitViewModel, onClick: () => void) => {
|
2022-07-13 22:45:06 +03:00
|
|
|
return <PitView key={key} pitViewModel={pitViewModel} onClick={onClick} />;
|
2022-05-15 02:01:51 +03:00
|
|
|
};
|
2022-07-13 22:39:24 +03:00
|
|
|
const player1Pits = game?.board.player1Pits.map((pit, index) => {
|
2022-05-15 02:01:51 +03:00
|
|
|
const pitViewModel = boardViewModel.pits[pit.index];
|
2022-07-13 22:39:24 +03:00
|
|
|
return createPitView(index, pitViewModel, () => {
|
2022-07-14 13:23:15 +03:00
|
|
|
if (game.turnPlayerId === game.player1Id && userKey === game.player1Id)
|
2022-07-13 22:45:06 +03:00
|
|
|
onPitSelect(game.board.player1Pits.indexOf(pit), pit);
|
2022-05-15 02:01:51 +03:00
|
|
|
});
|
2022-05-12 23:41:24 +03:00
|
|
|
});
|
2022-07-13 23:44:06 +03:00
|
|
|
const player2Pits = game?.board.player2Pits.map((pit, index) => {
|
2022-05-15 02:01:51 +03:00
|
|
|
const pitViewModel = boardViewModel.pits[pit.index];
|
2022-07-13 22:39:24 +03:00
|
|
|
return createPitView(index, pitViewModel, () => {
|
2022-07-13 23:44:06 +03:00
|
|
|
if (game.turnPlayerId === game.player2Id && userKey === game.player2Id)
|
2022-07-13 22:45:06 +03:00
|
|
|
onPitSelect(game.board.player2Pits.indexOf(pit), pit);
|
2022-05-15 02:01:51 +03:00
|
|
|
});
|
2022-05-12 23:41:24 +03:00
|
|
|
});
|
2022-05-15 02:01:51 +03:00
|
|
|
const theme = context.themeManager.theme;
|
|
|
|
|
const player1BankViewModel =
|
|
|
|
|
boardViewModel.pits[game.board.player1BankIndex()];
|
|
|
|
|
const player2BankViewModel =
|
|
|
|
|
boardViewModel.pits[game.board.player2BankIndex()];
|
2022-07-14 13:23:15 +03:00
|
|
|
const isPlayer2 = userKey === game?.player2Id;
|
2022-05-08 17:13:21 +03:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
2022-06-04 14:49:34 +03:00
|
|
|
margin: "1vw",
|
|
|
|
|
padding: "2vw",
|
2022-05-08 17:13:21 +03:00
|
|
|
display: "grid",
|
|
|
|
|
gridTemplateColumns: "repeat(8, 11vw)",
|
|
|
|
|
gridTemplateRows: "repeat(2, 11vw)",
|
|
|
|
|
borderRadius: "3vw",
|
2022-05-23 22:13:52 +03:00
|
|
|
transition: "background-color 0.5s",
|
2022-06-04 14:49:34 +03:00
|
|
|
background: theme.boardColor,
|
2022-05-08 17:13:21 +03:00
|
|
|
}}
|
|
|
|
|
>
|
2022-07-14 13:23:15 +03:00
|
|
|
<StoreView
|
|
|
|
|
context={context}
|
|
|
|
|
pitViewModel={isPlayer2 ? player2BankViewModel : player1BankViewModel}
|
|
|
|
|
gridColumn="8 / 9"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
<StoreView
|
|
|
|
|
context={context}
|
|
|
|
|
pitViewModel={isPlayer2 ? player1BankViewModel : player2BankViewModel}
|
|
|
|
|
gridColumn="1 / 2"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
{isPlayer2 ? player1Pits?.reverse() : player2Pits?.reverse()}
|
|
|
|
|
{isPlayer2 ? player2Pits : player1Pits}
|
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;
|