123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import React, { useState } from "react";
- import {
- PRE_GAME,
- PRE_ROUND,
- IN_ROUND,
- POST_ROUND,
- POST_GAME,
- ERROR,
- } from "./game-state.enum";
- import PreGameContainer from '../pre-game.component';
- import PreRound from '../pre-round.component';
- import GamePanelContainer from "../game-panel.component";
- import RoundSummary from '../round-summary.component';
- import PlayerScoresContainer from "../player-scores.component";
- const initialState = {
- gameState: PRE_GAME,
- gameId: null,
- playerName: null,
- lastRound: null,
- }
- const Game = () => {
- const [ state, rawSetState ] = useState(initialState);
- const setGameState = gameState => rawSetState({ ...state, gameState });
- const setGameStateAnd = (gameState, updates) => rawSetState({ ...state, gameState, ...updates });
- switch (state.gameState) {
- case PRE_GAME:
- return <PreGameContainer
- playerName={state.playerName}
- onGameJoined={({ gameId, playerName }) => setGameStateAnd(PRE_ROUND, { gameId, playerName })}
- />
- case PRE_ROUND:
- return <PreRound
- gameId={state.gameId}
- playerName={state.playerName}
- onStart={() => setGameState(IN_ROUND)}
- />
- case IN_ROUND:
- return <GamePanelContainer
- gameId={state.gameId}
- playerName={state.playerName}
- onRoundEnd={lastRound => setGameStateAnd(POST_ROUND, { lastRound })}
- onGameEnd={() => setGameState(POST_GAME)}
- />
- case POST_ROUND:
- return <RoundSummary
- round={state.lastRound}
- onNext={() => setGameState(IN_ROUND)}
- />
- case POST_GAME:
- return <PlayerScoresContainer
- gameId={state.gameId}
- onReturnToStart={() => setGameState(PRE_GAME)}
- />
- case ERROR:
- // TODO - would be nice to hook this into the sub-components, maybe with a HOC?
- return <p>Application encountered unrecoverable error, please refresh the page.</p>
- default:
- setGameState(ERROR);
- return <p>Application state is inconsistent, please refresh and rejoin your previous game.</p>
- }
- }
- export default Game;
|