Skip to content

Commit

Permalink
refactor(webapp): use map game context to props in user panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ben196888 committed Jul 21, 2024
1 parent dab7515 commit 29d06ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
11 changes: 1 addition & 10 deletions packages/webapp/src/components/BoardGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import Table from '@/components/Table/Table';
import DevActions from '@/components/DevActions/DevActions';
import ActionBar from './ActionBoard/ActionBar';
import GameHeader from './GameHeader/GameHeader';
import { playerNameMap } from './playerNameMap';
import { PlayersSelector } from '@/game/store/slice/players';
import { ScoreBoardSelector } from '@/game/store/slice/scoreBoard';
import UserPanel from './UserPanel/UserPanel';
import { Box } from '@mui/material';
import { GameContext } from './GameContextHelpers';
Expand All @@ -18,13 +15,7 @@ const Board: React.FC<GameContext> = (gameContext) => {
return (
<Box sx={{ display: 'flex' }}>
{!!playerID &&
<UserPanel
userName={playerNameMap[playerID]}
actionTokens={PlayersSelector.getNumActionTokens(G.players, playerID)}
workerTokens={PlayersSelector.getNumWorkerTokens(G.players, playerID)}
score={ScoreBoardSelector.getPlayerPoints(G.table.scoreBoard, playerID)}
projectCards={PlayersSelector.getProjectCards(G.players, playerID)}
/>
<UserPanel gameContext={gameContext}/>
}
<Box sx={{ flex: 1, padding: '16px', marginLeft: { xs: 0 } }}>
<GameHeader players={G.players} scoreBoard={G.table.scoreBoard} />
Expand Down
25 changes: 24 additions & 1 deletion packages/webapp/src/components/UserPanel/UserPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { styled } from '@mui/material/styles';
import { ProjectCard } from '@/game';
import { getSelectedHandProjectCards, toggleHandProjectCardSelection } from '@/lib/reducers/handProjectCardSlice';
import { Chip } from '@mui/material';
import { GameContext, connectGameContext } from '../GameContextHelpers';
import { playerNameMap } from '../playerNameMap';
import { PlayersSelector } from '@/game/store/slice/players';
import { ScoreBoardSelector } from '@/game/store/slice/scoreBoard';

type UserPanelProps = {
userName: string;
Expand Down Expand Up @@ -93,4 +97,23 @@ const UserPanel: React.FC<UserPanelProps> = ({ userName, workerTokens, actionTok
);
};

export default UserPanel;
const mapGameContextToProps = ({ G, playerID }: GameContext): UserPanelProps => {
// only player will see user panel
playerID = playerID!;

const userName = playerNameMap[playerID];
const actionTokens = PlayersSelector.getNumActionTokens(G.players, playerID);
const workerTokens=PlayersSelector.getNumWorkerTokens(G.players, playerID);
const score=ScoreBoardSelector.getPlayerPoints(G.table.scoreBoard, playerID);
const projectCards= PlayersSelector.getProjectCards(G.players, playerID);

return {
userName,
workerTokens,
actionTokens,
score,
projectCards,
};
}

export default connectGameContext(mapGameContextToProps)(UserPanel);

0 comments on commit 29d06ec

Please sign in to comment.