2022-05-08 17:13:21 +03:00
|
|
|
import { Bank, MancalaGame, Pit } from "mancala.js";
|
|
|
|
|
import * as React from "react";
|
|
|
|
|
import { FunctionComponent, useState } from "react";
|
2022-05-15 02:01:51 +03:00
|
|
|
import { Context } from "../context";
|
|
|
|
|
import BoardViewModel from "../viewmodel/BoardViewModel";
|
|
|
|
|
import PitViewModel from "../viewmodel/PitViewModel";
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2022-05-08 17:13:21 +03:00
|
|
|
const BallView: FunctionComponent<{ color: string }> = ({ color }) => {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
2021-06-30 16:11:22 +03:00
|
|
|
background: color,
|
2021-06-29 03:29:46 +03:00
|
|
|
margin: "1px",
|
2021-06-30 16:11:22 +03:00
|
|
|
width: "1vw",
|
|
|
|
|
height: "1vw",
|
2022-05-08 17:13:21 +03:00
|
|
|
borderRadius: "10vw",
|
2022-05-23 22:13:52 +03:00
|
|
|
transition: "background-color 0.5s",
|
2022-05-08 17:13:21 +03:00
|
|
|
}}
|
|
|
|
|
></div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-06-29 03:29:46 +03:00
|
|
|
|
|
|
|
|
function range(size: number) {
|
2022-05-08 17:13:21 +03:00
|
|
|
var ans = [];
|
|
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
|
ans.push(i);
|
|
|
|
|
}
|
|
|
|
|
return ans;
|
2021-06-29 03:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
2022-05-08 17:13:21 +03:00
|
|
|
const HoleView: FunctionComponent<{
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel: PitViewModel;
|
2022-05-08 17:13:21 +03:00
|
|
|
onClick: () => void;
|
2022-05-15 02:01:51 +03:00
|
|
|
}> = ({ pitViewModel, onClick }) => {
|
|
|
|
|
const balls = [...range(pitViewModel.stoneCount)].map((i) => (
|
|
|
|
|
<BallView color={pitViewModel.stoneColor} />
|
2022-05-08 17:13:21 +03:00
|
|
|
));
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2022-05-08 17:13:21 +03:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
style={{
|
2022-05-15 02:01:51 +03:00
|
|
|
background: pitViewModel.pitColor,
|
2022-05-08 17:13:21 +03:00
|
|
|
margin: "5px",
|
|
|
|
|
padding: "5px",
|
|
|
|
|
borderRadius: "10vw",
|
2022-05-23 22:13:52 +03:00
|
|
|
transition: "background-color 0.5s",
|
2022-05-08 17:13:21 +03:00
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
alignContent: "center",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
justifyItems: "center",
|
|
|
|
|
flexWrap: "wrap",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{balls}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2022-05-08 17:13:21 +03:00
|
|
|
const StoreView: FunctionComponent<{
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel: PitViewModel;
|
2022-05-08 17:13:21 +03:00
|
|
|
gridColumn: string;
|
|
|
|
|
gridRow: string;
|
2022-05-15 02:01:51 +03:00
|
|
|
}> = ({ pitViewModel, gridColumn, gridRow }) => {
|
|
|
|
|
const balls = [...range(pitViewModel.stoneCount)].map((i) => (
|
|
|
|
|
<BallView color={pitViewModel.stoneColor} />
|
2022-05-08 17:13:21 +03:00
|
|
|
));
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
gridColumn: gridColumn,
|
|
|
|
|
gridRow: gridRow,
|
2022-05-15 02:01:51 +03:00
|
|
|
background: pitViewModel.pitColor,
|
2022-05-08 17:13:21 +03:00
|
|
|
margin: "5px",
|
|
|
|
|
borderRadius: "10vw",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignContent: "center",
|
|
|
|
|
flexWrap: "wrap",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{balls}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2022-05-08 17:13:21 +03:00
|
|
|
const BoardView: FunctionComponent<{
|
|
|
|
|
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;
|
|
|
|
|
onHoleSelect: (index: number, hole: Pit) => void;
|
2022-05-15 02:01:51 +03:00
|
|
|
}> = ({ game, context, boardId, boardViewModel, userKey, onHoleSelect }) => {
|
|
|
|
|
const createPitView = (pitViewModel: PitViewModel, onClick: () => void) => {
|
|
|
|
|
return <HoleView pitViewModel={pitViewModel} onClick={onClick} />;
|
|
|
|
|
};
|
2022-05-12 23:41:24 +03:00
|
|
|
const player1Pits = game?.board.player1Pits.map((pit) => {
|
2022-05-15 02:01:51 +03:00
|
|
|
const pitViewModel = boardViewModel.pits[pit.index];
|
|
|
|
|
return createPitView(pitViewModel, () => {
|
|
|
|
|
if (game.turnPlayerId === game.player1Id)
|
|
|
|
|
onHoleSelect(game.board.player1Pits.indexOf(pit), pit);
|
|
|
|
|
});
|
2022-05-12 23:41:24 +03:00
|
|
|
});
|
|
|
|
|
const player2Pits = game!!.board.player2Pits.map((pit) => {
|
2022-05-15 02:01:51 +03:00
|
|
|
const pitViewModel = boardViewModel.pits[pit.index];
|
|
|
|
|
return createPitView(pitViewModel, () => {
|
|
|
|
|
if (game.turnPlayerId === game.player2Id)
|
|
|
|
|
onHoleSelect(game.board.player2Pits.indexOf(pit), pit);
|
|
|
|
|
});
|
2022-05-12 23:41:24 +03:00
|
|
|
});
|
2022-05-08 17:13:21 +03:00
|
|
|
const isUserTurn = game.checkIsPlayerTurn(userKey);
|
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()];
|
|
|
|
|
const player1Bank = (
|
|
|
|
|
<StoreView
|
|
|
|
|
pitViewModel={player1BankViewModel}
|
|
|
|
|
gridColumn="1 / 2"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
const player2Bank = (
|
|
|
|
|
<StoreView
|
|
|
|
|
pitViewModel={player2BankViewModel}
|
|
|
|
|
gridColumn="8 / 9"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
);
|
2022-05-08 17:13:21 +03:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
margin: "10px",
|
|
|
|
|
padding: "20px",
|
|
|
|
|
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-05-08 17:13:21 +03:00
|
|
|
background: isUserTurn
|
|
|
|
|
? theme.boardColor
|
|
|
|
|
: theme.boardColorWhenPlayerTurn,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{userKey === game.player2Id ? (
|
|
|
|
|
<>
|
|
|
|
|
<StoreView
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel={player1BankViewModel}
|
2022-05-08 17:13:21 +03:00
|
|
|
gridColumn="1 / 2"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
<StoreView
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel={player2BankViewModel}
|
2022-05-08 17:13:21 +03:00
|
|
|
gridColumn="8 / 9"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
{player1Pits.reverse()}
|
|
|
|
|
{player2Pits}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<StoreView
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel={player1BankViewModel}
|
|
|
|
|
gridColumn="8 / 9"
|
2022-05-08 17:13:21 +03:00
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
<StoreView
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel={player2BankViewModel}
|
|
|
|
|
gridColumn="1 / 2"
|
2022-05-08 17:13:21 +03:00
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
{player2Pits.reverse()}
|
|
|
|
|
{player1Pits}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2022-05-08 17:13:21 +03:00
|
|
|
export default BoardView;
|