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";
|
2022-07-13 15:18:13 +03:00
|
|
|
import { getColorByBrightness } from "../util/ColorUtil";
|
2022-05-15 02:01:51 +03:00
|
|
|
import BoardViewModel from "../viewmodel/BoardViewModel";
|
|
|
|
|
import PitViewModel from "../viewmodel/PitViewModel";
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2022-07-13 22:42:45 +03:00
|
|
|
const StoneView: FunctionComponent<{ color: string }> = ({ color }) => {
|
2022-05-08 17:13:21 +03:00
|
|
|
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 }) => {
|
2022-07-13 22:42:45 +03:00
|
|
|
const stones = [...range(pitViewModel.stoneCount)].map((i, index) => (
|
|
|
|
|
<StoneView key={index} 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",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-07-13 22:42:45 +03:00
|
|
|
{stones}
|
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
|
|
|
const StoreView: FunctionComponent<{
|
2022-05-24 22:25:33 +03:00
|
|
|
context: Context;
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel: PitViewModel;
|
2022-05-08 17:13:21 +03:00
|
|
|
gridColumn: string;
|
|
|
|
|
gridRow: string;
|
2022-05-24 22:25:33 +03:00
|
|
|
}> = ({ context, pitViewModel, gridColumn, gridRow }) => {
|
2022-07-13 22:42:45 +03:00
|
|
|
const stones = [...range(pitViewModel.stoneCount)].map((i, index) => (
|
|
|
|
|
<StoneView key={index} color={pitViewModel.stoneColor} />
|
2022-05-08 17:13:21 +03:00
|
|
|
));
|
2022-07-13 15:18:13 +03:00
|
|
|
const textColor = getColorByBrightness(
|
2022-06-04 17:16:32 +03:00
|
|
|
pitViewModel.pitColor,
|
2022-07-13 15:21:30 +03:00
|
|
|
context.themeManager.theme.textColor,
|
|
|
|
|
context.themeManager.theme.textLightColor
|
2022-06-04 17:16:32 +03:00
|
|
|
);
|
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",
|
2022-05-24 22:25:33 +03:00
|
|
|
position: "relative",
|
2022-05-08 17:13:21 +03:00
|
|
|
}}
|
|
|
|
|
>
|
2022-07-13 22:42:45 +03:00
|
|
|
{stones}
|
2022-05-24 22:25:33 +03:00
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
bottom: "2vw",
|
|
|
|
|
fontFamily: "monospace",
|
|
|
|
|
fontWeight: "bold",
|
|
|
|
|
fontSize: "2vw",
|
2022-06-04 17:16:32 +03:00
|
|
|
color: textColor,
|
2022-05-24 22:25:33 +03:00
|
|
|
}}
|
|
|
|
|
>
|
2022-07-13 22:42:45 +03:00
|
|
|
{stones.length}
|
2022-05-24 22:25:33 +03:00
|
|
|
</span>
|
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
|
|
|
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 }) => {
|
2022-07-13 22:39:24 +03:00
|
|
|
const createPitView = (key: any, pitViewModel: PitViewModel, onClick: () => void) => {
|
|
|
|
|
return <HoleView 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-05-15 02:01:51 +03:00
|
|
|
if (game.turnPlayerId === game.player1Id)
|
|
|
|
|
onHoleSelect(game.board.player1Pits.indexOf(pit), pit);
|
|
|
|
|
});
|
2022-05-12 23:41:24 +03:00
|
|
|
});
|
2022-07-13 22:39:24 +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-05-15 02:01:51 +03:00
|
|
|
if (game.turnPlayerId === game.player2Id)
|
|
|
|
|
onHoleSelect(game.board.player2Pits.indexOf(pit), pit);
|
|
|
|
|
});
|
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()];
|
|
|
|
|
const player1Bank = (
|
|
|
|
|
<StoreView
|
2022-05-24 22:25:33 +03:00
|
|
|
context={context}
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel={player1BankViewModel}
|
|
|
|
|
gridColumn="1 / 2"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
const player2Bank = (
|
|
|
|
|
<StoreView
|
2022-05-24 22:25:33 +03:00
|
|
|
context={context}
|
2022-05-15 02:01:51 +03:00
|
|
|
pitViewModel={player2BankViewModel}
|
|
|
|
|
gridColumn="8 / 9"
|
|
|
|
|
gridRow="1 / 3"
|
|
|
|
|
/>
|
|
|
|
|
);
|
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
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{userKey === game.player2Id ? (
|
|
|
|
|
<>
|
|
|
|
|
<StoreView
|
2022-05-24 22:25:33 +03:00
|
|
|
context={context}
|
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-24 22:25:33 +03:00
|
|
|
context={context}
|
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-24 22:25:33 +03:00
|
|
|
context={context}
|
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-24 22:25:33 +03:00
|
|
|
context={context}
|
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;
|