From 35750af200b9f58870878f5dd6e3fff386784bde Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Fri, 2 Sep 2022 00:10:41 +0300 Subject: [PATCH 01/10] refactor BoardView --- src/MancalaApp.tsx | 1 + src/components/board/BoardView.tsx | 46 ++++++++++++------------------ 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/MancalaApp.tsx b/src/MancalaApp.tsx index 2329499..79fdc6a 100644 --- a/src/MancalaApp.tsx +++ b/src/MancalaApp.tsx @@ -77,6 +77,7 @@ const MancalaApp: FunctionComponent = () => { context.themeManager.theme.textColor, context.themeManager.theme.textLightColor ); + if(!userKey) return <>; return ( <> diff --git a/src/components/board/BoardView.tsx b/src/components/board/BoardView.tsx index 3da5925..29cf0dd 100644 --- a/src/components/board/BoardView.tsx +++ b/src/components/board/BoardView.tsx @@ -13,49 +13,39 @@ const BoardView: FunctionComponent<{ context: Context; boardId: string; boardViewModel: BoardViewModel; - userKey: string; + revert: boolean; onPitSelect: (index: number, pit: Pit) => void; -}> = ({ game, context, boardId, boardViewModel, userKey, onPitSelect: onPitSelect }) => { +}> = ({ game, context, boardId, boardViewModel, revert, onPitSelect: onPitSelect }) => { const mancalaGame = game?.mancalaGame; - const createPitView = (key: any, pitViewModel: PitViewModel, onClick: () => void) => { - return ; - }; - const player1Pits = mancalaGame?.board.player1Pits.map((pit, index) => { - const pitViewModel = boardViewModel.pits[pit.index]; - return createPitView(index, pitViewModel, () => { - if (mancalaGame?.turnPlayerId === mancalaGame?.player1Id && userKey === mancalaGame?.player1Id) - onPitSelect(mancalaGame?.board.player1Pits.indexOf(pit), pit); - }); - }); - const player2Pits = mancalaGame?.board.player2Pits.map((pit, index) => { - const pitViewModel = boardViewModel.pits[pit.index]; - return createPitView(index, pitViewModel, () => { - if (mancalaGame?.turnPlayerId === mancalaGame?.player2Id && userKey === mancalaGame?.player2Id) - onPitSelect(mancalaGame?.board.player2Pits.indexOf(pit), pit); - }); - }); const theme = context.themeManager.theme; - const player1BankViewModel = - boardViewModel.pits[mancalaGame?.board.player1BankIndex()]; - const player2BankViewModel = - boardViewModel.pits[mancalaGame?.board.player2BankIndex()]; - const isPlayer2 = userKey === mancalaGame?.player2Id; + + const createPitView = (key: any, pit: Pit, pitViewModel: PitViewModel) => { + return onPitSelect(pit.index, pit)} />; + }; + const createPitViewList = (pits: Pit[]) => pits.map((pit, index) => createPitView(index, pit, boardViewModel.pits[pit.index])); + + const player1Pits = createPitViewList(mancalaGame?.board.player1Pits); + const player2Pits = createPitViewList(mancalaGame?.board.player2Pits); + const player1BankIndex = mancalaGame?.board.player1BankIndex(); + const player2BankIndex = mancalaGame?.board.player2BankIndex(); + const player1BankViewModel = boardViewModel.pits[player1BankIndex]; + const player2BankViewModel = boardViewModel.pits[player2BankIndex]; return (
- {isPlayer2 ? player1Pits?.reverse() : player2Pits?.reverse()} - {isPlayer2 ? player2Pits : player1Pits} + {revert ? player1Pits?.reverse() : player2Pits?.reverse()} + {revert ? player2Pits : player1Pits}