diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2021-06-25 12:56:04 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2021-06-25 12:56:04 +0200 |
| commit | 6bd8d0345e3ac653c3fad4f1c7a6352e8a4a166e (patch) | |
| tree | f142d3f43add40c2dd56bc496d6b4ab497f31a3e /client/src/Mancala/Pit.tsx | |
| parent | 38d59e2876b9f4d7c589d58295ef8acdf336a45b (diff) | |
| parent | 102b25f18d9b269c58d15677f10cd71c15003c4b (diff) | |
Merge branch 'mvcFeature' into mainline
Diffstat (limited to 'client/src/Mancala/Pit.tsx')
| -rw-r--r-- | client/src/Mancala/Pit.tsx | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/client/src/Mancala/Pit.tsx b/client/src/Mancala/Pit.tsx new file mode 100644 index 0000000..300f3f4 --- /dev/null +++ b/client/src/Mancala/Pit.tsx @@ -0,0 +1,65 @@ +import React, { useState } from "react"; +import type { GameState } from "../gameState"; +import "./Pit.css"; + +type Flatten<T> = T extends any[] ? T[number] : T; + +type Player = Flatten<GameState["players"]>; + +type Pit = Flatten<Player["pits"]>; + +type PitProps = { + player: Player; + index: number; + pit: Pit; + onClick?: (index: number, player: Player, pit: Pit) => (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => Promise<void> + displayStones: (pit: Pit, spread: "small" | "big") => JSX.Element[] +} + +type SmallBowl = Pit & {kind: "small"}; +type Kalaha = Pit & {kind: "big"}; + + + +export function Pit({player, index, pit, onClick, displayStones}: PitProps) { + const smallBowl = pit as SmallBowl; + smallBowl.kind = "small"; + if (onClick) { + return ( + <div className="Pit"> + {pit.nrOfStones > 10 ? "" + pit.nrOfStones : ""} + <div className="Recession" onClick={onClick(index, player, pit)}> + <div className="stones" id={"pit" + pit.index}> + {displayStones(pit, smallBowl.kind)} + </div> + </div> + </div> + ); + } else { + return ( + <div className="Pit"> + <div className="Recession"> + <div className="stones"> + {pit.nrOfStones > 10 ? pit.nrOfStones : ""} + {displayStones(pit, smallBowl.kind)} + </div> + </div> + </div> + ); + } +} + +export function Kalaha({player, pit, displayStones}: PitProps) { + const kalaha = pit as Kalaha; + kalaha.kind = "big"; + return ( + <div className="Kalaha"> + {pit.nrOfStones > 10 ? pit.nrOfStones : ""} + <div className="Recession"> + <div className="stones" id={"pit" + pit.index}> + {displayStones(pit, kalaha.kind)} + </div> + </div> + </div> + ); +} |
