diff --git a/src/Home.tsx b/src/Home.tsx index 7017d9d..2148a47 100644 --- a/src/Home.tsx +++ b/src/Home.tsx @@ -1,6 +1,6 @@ import * as React from "react"; import { FunctionComponent, useEffect, useState } from "react"; -import BoardView from "./components/BoardView"; +import BoardView from "./components/board/BoardView"; import { context } from "./context"; import { RTMTWS } from "./rtmt/rtmt_websocket"; import { diff --git a/src/components/BoardView.tsx b/src/components/board/BoardView.tsx similarity index 53% rename from src/components/BoardView.tsx rename to src/components/board/BoardView.tsx index 61fb7e2..1d482f2 100644 --- a/src/components/BoardView.tsx +++ b/src/components/board/BoardView.tsx @@ -1,110 +1,11 @@ import { Bank, MancalaGame, Pit } from "mancala.js"; import * as React from "react"; import { FunctionComponent, useState } from "react"; -import { Context } from "../context"; -import { getColorByBrightness } from "../util/ColorUtil"; -import BoardViewModel from "../viewmodel/BoardViewModel"; -import PitViewModel from "../viewmodel/PitViewModel"; - -const StoneView: FunctionComponent<{ color: string }> = ({ color }) => { - return ( -
- ); -}; - -function range(size: number) { - var ans = []; - for (let i = 0; i < size; i++) { - ans.push(i); - } - return ans; -} - -const PitView: FunctionComponent<{ - pitViewModel: PitViewModel; - onClick: () => void; -}> = ({ pitViewModel, onClick }) => { - const stones = [...range(pitViewModel.stoneCount)].map((i, index) => ( - - )); - - return ( -
- {stones} -
- ); -}; - -const StoreView: FunctionComponent<{ - context: Context; - pitViewModel: PitViewModel; - gridColumn: string; - gridRow: string; -}> = ({ context, pitViewModel, gridColumn, gridRow }) => { - const stones = [...range(pitViewModel.stoneCount)].map((i, index) => ( - - )); - const textColor = getColorByBrightness( - pitViewModel.pitColor, - context.themeManager.theme.textColor, - context.themeManager.theme.textLightColor - ); - return ( -
- {stones} - - {stones.length} - -
- ); -}; +import { Context } from "../../context"; +import BoardViewModel from "../../viewmodel/BoardViewModel"; +import PitViewModel from "../../viewmodel/PitViewModel"; +import PitView from "./PitView"; +import StoreView from "./StoreView"; const BoardView: FunctionComponent<{ game?: MancalaGame; @@ -165,7 +66,7 @@ const BoardView: FunctionComponent<{ background: theme.boardColor, }} > - {userKey === game.player2Id ? ( + {userKey === game?.player2Id ? ( <> - {player1Pits.reverse()} + {player1Pits?.reverse()} {player2Pits} ) : ( @@ -196,7 +97,7 @@ const BoardView: FunctionComponent<{ gridColumn="1 / 2" gridRow="1 / 3" /> - {player2Pits.reverse()} + {player2Pits?.reverse()} {player1Pits} )} diff --git a/src/components/board/PitView.tsx b/src/components/board/PitView.tsx new file mode 100644 index 0000000..5691042 --- /dev/null +++ b/src/components/board/PitView.tsx @@ -0,0 +1,38 @@ +import * as React from "react"; +import { FunctionComponent } from "react"; +import Util from "../../util/Util"; +import PitViewModel from "../../viewmodel/PitViewModel"; +import StoneView from "./StoneView"; + + +const PitView: FunctionComponent<{ + pitViewModel: PitViewModel; + onClick: () => void; +}> = ({ pitViewModel, onClick }) => { + const stones = [...Util.range(pitViewModel.stoneCount)].map((i, index) => ( + + )); + + return ( +
+ {stones} +
+ ); +}; + +export default PitView; \ No newline at end of file diff --git a/src/components/board/StoneView.tsx b/src/components/board/StoneView.tsx new file mode 100644 index 0000000..2b5e16a --- /dev/null +++ b/src/components/board/StoneView.tsx @@ -0,0 +1,19 @@ +import * as React from "react"; +import { FunctionComponent } from "react"; + +const StoneView: FunctionComponent<{ color: string }> = ({ color }) => { + return ( +
+ ); + }; + + export default StoneView; \ No newline at end of file diff --git a/src/components/board/StoreView.tsx b/src/components/board/StoreView.tsx new file mode 100644 index 0000000..375481f --- /dev/null +++ b/src/components/board/StoreView.tsx @@ -0,0 +1,56 @@ +import * as React from "react"; +import { FunctionComponent } from "react"; +import { Context } from "../../context"; +import { getColorByBrightness } from "../../util/ColorUtil"; +import Util from "../../util/Util"; +import PitViewModel from "../../viewmodel/PitViewModel"; +import StoneView from "./StoneView"; + +const StoreView: FunctionComponent<{ + context: Context; + pitViewModel: PitViewModel; + gridColumn: string; + gridRow: string; + }> = ({ context, pitViewModel, gridColumn, gridRow }) => { + const stones = [...Util.range(pitViewModel.stoneCount)].map((i, index) => ( + + )); + const textColor = getColorByBrightness( + pitViewModel.pitColor, + context.themeManager.theme.textColor, + context.themeManager.theme.textLightColor + ); + return ( +
+ {stones} + + {stones.length} + +
+ ); + }; + + export default StoreView; \ No newline at end of file diff --git a/src/util/Util.ts b/src/util/Util.ts new file mode 100644 index 0000000..905b84d --- /dev/null +++ b/src/util/Util.ts @@ -0,0 +1,10 @@ + +export default class Util { + public static range(size: number) { + var ans = []; + for (let i = 0; i < size; i++) { + ans.push(i); + } + return ans; + } +}