|
@@ -1,4 +1,4 @@
|
|
|
-import React, { useState } from "react";
|
|
|
+import React from "react";
|
|
|
import {
|
|
|
PRE_GAME,
|
|
|
PRE_ROUND,
|
|
@@ -6,19 +6,15 @@ import {
|
|
|
POST_ROUND,
|
|
|
POST_GAME,
|
|
|
ERROR,
|
|
|
-} from "./GameState";
|
|
|
-import HeaderAndFooter from "./HeaderAndFooter";
|
|
|
+} from "../../domain/GameState";
|
|
|
import PreGame from '../screens/PreGame';
|
|
|
import PreRound from '../screens/PreRound';
|
|
|
import GamePanel from "../screens/GamePanel";
|
|
|
import RoundSummary from '../screens/RoundSummary';
|
|
|
import PlayerScores from "../screens/PlayerScores";
|
|
|
-import { gameIdStore } from "../../domain/store";
|
|
|
-
|
|
|
-const initialState = {
|
|
|
- gameState: PRE_GAME,
|
|
|
- joined: false,
|
|
|
-}
|
|
|
+import { gameIdStore, gameStateStore } from "../../domain/store";
|
|
|
+import useObservable from "../../hooks/useObservable";
|
|
|
+import { useEffect } from "react";
|
|
|
|
|
|
const extractAndRemoveSearchParam = param => {
|
|
|
const u = new URL(window.location.href);
|
|
@@ -28,60 +24,37 @@ const extractAndRemoveSearchParam = param => {
|
|
|
return extracted;
|
|
|
}
|
|
|
|
|
|
+const componentMap = {
|
|
|
+ [PRE_GAME]: PreGame,
|
|
|
+ [PRE_ROUND]: PreRound,
|
|
|
+ [IN_ROUND]: GamePanel,
|
|
|
+ [POST_ROUND]: RoundSummary,
|
|
|
+ [POST_GAME]: PlayerScores,
|
|
|
+ [ERROR]: () => <p>Application encountered unrecoverable error, please refresh the page.</p>,
|
|
|
+}
|
|
|
+
|
|
|
const Game = () => {
|
|
|
- const [ state, setState ] = useState(initialState);
|
|
|
- const setGameState = gameState => setState({ ...state, gameState });
|
|
|
- const onGameJoined = () => setState({ gameState: PRE_ROUND, joined: true });
|
|
|
-
|
|
|
- const joinCode = extractAndRemoveSearchParam("join");
|
|
|
- if (joinCode) {
|
|
|
- gameIdStore.set(joinCode);
|
|
|
- setGameState(PRE_ROUND);
|
|
|
- }
|
|
|
+ const gameState = useObservable(gameStateStore);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const joinCode = extractAndRemoveSearchParam("join");
|
|
|
+ if (joinCode) {
|
|
|
+ gameIdStore.set(joinCode);
|
|
|
+ gameStateStore.set(PRE_ROUND);
|
|
|
+ }
|
|
|
+ }, [])
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const summaryCode = extractAndRemoveSearchParam("summary");
|
|
|
+ if (summaryCode) {
|
|
|
+ gameIdStore.set(summaryCode);
|
|
|
+ gameStateStore.set(POST_GAME);
|
|
|
+ }
|
|
|
+ }, [])
|
|
|
|
|
|
- const summaryCode = extractAndRemoveSearchParam("summary");
|
|
|
- if (summaryCode) {
|
|
|
- gameIdStore.set(summaryCode);
|
|
|
- setGameState(POST_GAME);
|
|
|
- }
|
|
|
+ const Screen = componentMap[gameState];
|
|
|
|
|
|
- switch (state.gameState) {
|
|
|
- case PRE_GAME:
|
|
|
- return (
|
|
|
- <HeaderAndFooter>
|
|
|
- <PreGame onGameJoined={onGameJoined} />
|
|
|
- </HeaderAndFooter>
|
|
|
- );
|
|
|
- case PRE_ROUND:
|
|
|
- return (
|
|
|
- <HeaderAndFooter>
|
|
|
- <PreRound
|
|
|
- joined={state.joined}
|
|
|
- onGameJoined={onGameJoined}
|
|
|
- onStart={() => setGameState(IN_ROUND)}
|
|
|
- />
|
|
|
- </HeaderAndFooter>
|
|
|
- );
|
|
|
- case IN_ROUND:
|
|
|
- return <GamePanel
|
|
|
- onRoundEnd={() => setGameState(POST_ROUND)}
|
|
|
- onGameEnd={() => setGameState(POST_GAME)}
|
|
|
- />
|
|
|
- case POST_ROUND:
|
|
|
- return <RoundSummary onNext={() => setGameState(IN_ROUND)} />
|
|
|
- case POST_GAME:
|
|
|
- return (
|
|
|
- <HeaderAndFooter>
|
|
|
- <PlayerScores onReturnToStart={() => setState(initialState)} />
|
|
|
- </HeaderAndFooter>
|
|
|
- );
|
|
|
- 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>
|
|
|
- }
|
|
|
+ return <Screen />
|
|
|
}
|
|
|
|
|
|
export default Game;
|