mancala/src/components/BoardView.tsx

182 lines
4.6 KiB
TypeScript
Raw Normal View History

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";
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",
}}
></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<{
pitViewModel: PitViewModel;
2022-05-08 17:13:21 +03:00
onClick: () => void;
}> = ({ 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={{
background: pitViewModel.pitColor,
2022-05-08 17:13:21 +03:00
margin: "5px",
padding: "5px",
borderRadius: "10vw",
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<{
pitViewModel: PitViewModel;
2022-05-08 17:13:21 +03:00
gridColumn: string;
gridRow: string;
}> = ({ 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,
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;
context: Context;
boardId: string;
boardViewModel: BoardViewModel;
2022-05-08 17:13:21 +03:00
userKey: string;
onHoleSelect: (index: number, hole: Pit) => void;
}> = ({ 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) => {
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) => {
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);
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",
background: isUserTurn
? theme.boardColor
: theme.boardColorWhenPlayerTurn,
}}
>
{userKey === game.player2Id ? (
<>
<StoreView
pitViewModel={player1BankViewModel}
2022-05-08 17:13:21 +03:00
gridColumn="1 / 2"
gridRow="1 / 3"
/>
<StoreView
pitViewModel={player2BankViewModel}
2022-05-08 17:13:21 +03:00
gridColumn="8 / 9"
gridRow="1 / 3"
/>
{player1Pits.reverse()}
{player2Pits}
</>
) : (
<>
<StoreView
pitViewModel={player1BankViewModel}
gridColumn="8 / 9"
2022-05-08 17:13:21 +03:00
gridRow="1 / 3"
/>
<StoreView
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;