From d1dc1db234b4fd9486f9438e149a120c65ad43f2 Mon Sep 17 00:00:00 2001 From: Kenneth Allen Date: Sun, 17 Oct 2021 02:35:47 +1100 Subject: [PATCH] Build out pregame interface --- .../wonders-client/src/components/App/App.tsx | 7 +- .../wonders-client/src/components/Civ/Civ.tsx | 6 +- .../src/components/Game/Game.tsx | 81 +++++++++++++++---- .../components/UserSelector/UserSelector.tsx | 36 +++++---- 4 files changed, 94 insertions(+), 36 deletions(-) diff --git a/packages/wonders-client/src/components/App/App.tsx b/packages/wonders-client/src/components/App/App.tsx index 8c82593..3a75fc8 100644 --- a/packages/wonders-client/src/components/App/App.tsx +++ b/packages/wonders-client/src/components/App/App.tsx @@ -25,12 +25,17 @@ export default function App() { } } + function resetGame() { + dispatch(resetAction()) + setUser(undefined) + } + return p.name) ?? []} locked={state !== undefined && state.stage !== 'starting'} /> {state ? <> - + : Loading... } diff --git a/packages/wonders-client/src/components/Civ/Civ.tsx b/packages/wonders-client/src/components/Civ/Civ.tsx index 632c4a4..4768487 100644 --- a/packages/wonders-client/src/components/Civ/Civ.tsx +++ b/packages/wonders-client/src/components/Civ/Civ.tsx @@ -21,8 +21,10 @@ const wonderBgExts = ['jpg', 'webp'] export default function Civ({ player, style }: { player: Player, style: 'player' | 'neighbor' | 'compact' }) { const pStats = useMemo(() => playerStats(player), [player]) - const outerStyle = { - backgroundImage: player.wonder && wonderBgExts.map(ext => `url(/assets/wonders/${player.wonder}.${ext})`).join(', '), + const outerStyle: React.CSSProperties = { + backgroundImage: player.wonder && wonderBgExts.map(ext => `url("/assets/wonders/${player.wonder}.${ext}")`).join(', '), + backgroundSize: player.wonder && 'cover', + backgroundPosition: player.wonder && 'center', } return
diff --git a/packages/wonders-client/src/components/Game/Game.tsx b/packages/wonders-client/src/components/Game/Game.tsx index 730b874..d5cf765 100644 --- a/packages/wonders-client/src/components/Game/Game.tsx +++ b/packages/wonders-client/src/components/Game/Game.tsx @@ -1,28 +1,29 @@ /* eslint-disable react/style-prop-object */ import { Dispatch } from 'ketchup-react' -import { Action, Player, State, structures } from 'wonders-common' +import { Action, chooseWonderAction, maxPlayers, minPlayers, Player, startGameAction, State, structures, wonders } from 'wonders-common' import Civ from '../Civ/Civ' import Card from '../Card/Card' import Row from 'react-bootstrap/Row' import Col from 'react-bootstrap/Col' +import { ErrorMessage, Field, Form, Formik } from 'formik' function getDistant(arr: T[], idx: number) { - switch (idx) { - case 0: return arr.slice(2, -1) - case arr.length - 1: return arr.slice(1, -2) - default: return [...arr.slice(idx + 2), ...arr.slice(0, idx - 1)] - } + switch (idx) { + case 0: return arr.slice(2, -1) + case arr.length - 1: return arr.slice(1, -2) + default: return [...arr.slice(idx + 2), ...arr.slice(0, idx - 1)] + } } function DistantCivs({ players }: { players: Player[] }) { return {players.map(p => - + )} } -export default function Game({ state, playerName }: { state: State, playerName?: string, dispatch: Dispatch }) { +export default function Game({ state, playerName, dispatch }: { state: State, playerName?: string, dispatch: Dispatch }) { const playerIdx: number | undefined = state.players.findIndex(p => p.name === playerName) const player: Player | undefined = state.players[playerIdx] const started = state.stage !== 'play' ? undefined : { @@ -31,28 +32,74 @@ export default function Game({ state, playerName }: { state: State, playerName?: distant: getDistant(state.players, playerIdx), } - if (started && player) { + function isWonderInUse(wonder: string) { + return state.players.filter(p => p.name !== playerName).map(p => p.wonder).includes(wonder) + } + + if (!player) { + return + } else if (started) { return <> - + - +
{state?.age === undefined ? '' : `Age ${state.age + 1} ${state.age % 2 === 0 ? '🔃' : '🔄'}`}
- +
- + - {player.hand.map(s => structures.get(s)!).map(s => + {player.hand?.map(s => structures.get(s)!)?.map(s => - + )} } else { - return - // TODO Pick wonder + return <> + + + + + { + const errors: Partial = {} + if (values.wonder.length === 0) { + errors.wonder = 'Wonder not selected.' + } + if (isWonderInUse(values.wonder)) { + errors.wonder = 'Wonder already in use.' + } + return errors + }} + onSubmit={({ wonder }) => dispatch(chooseWonderAction(player.name, wonder))} + >{() =>
+ + {' '} + + {['', ...wonders.keys()].map(w => + + )} + + {' '} + + } +
+
+ + + + {/* TODO Pick wonder */} + } } diff --git a/packages/wonders-client/src/components/UserSelector/UserSelector.tsx b/packages/wonders-client/src/components/UserSelector/UserSelector.tsx index ba76254..e87d579 100644 --- a/packages/wonders-client/src/components/UserSelector/UserSelector.tsx +++ b/packages/wonders-client/src/components/UserSelector/UserSelector.tsx @@ -2,6 +2,7 @@ import { ErrorMessage, Field, Form, Formik } from 'formik'; import { Dispatch, useState } from 'react'; import sample from 'lodash/sample' import Row from 'react-bootstrap/Row'; +import Col from 'react-bootstrap/Col'; const namePlaceholders = [ 'Alexander the Great', @@ -21,23 +22,26 @@ export default function UserSelector({ users, user, selectUser, removeUser, lock const [namePlaceholder] = useState(() => sample(namePlaceholders)) return {user - ? <>Playing as {user} + ? Playing as {user} : { - const errors: Partial = {} - if (values.newUser.length === 0) { - errors.newUser = 'Name cannot be blank.' - } - return errors - }} - onSubmit={(values) => { - selectUser(values.newUser) - }} - >{() =>
- - - } + initialValues={{ newUser: '' }} + validate={values => { + const errors: Partial = {} + if (values.newUser.length === 0) { + errors.newUser = 'Name cannot be blank.' + } + return errors + }} + onSubmit={(values) => { + selectUser(values.newUser) + }} + >{() =>
+ + {' '} + + {' '} + + }
}