-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d19f86
commit d5f6fc3
Showing
11 changed files
with
147 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,77 @@ | ||
import Status from "./GameStatus"; | ||
|
||
export const GameToolbar = () => { | ||
return ( | ||
<div className="bg-slate-100 w-full h-10 shadow-lg shadow-black"> | ||
<Status /> | ||
<div className="w-full shadow-md"> | ||
<div className="text-center text-3xl py-4 bg-surface w-full"> | ||
<GameStatusText /> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { Context, useGame } from "../game_logic/GameContext"; | ||
import { getExpectedState } from "../game_logic/state_machine"; | ||
import Loading from "./Loading"; | ||
|
||
const getTimeInfo = (context: Context): string => { | ||
const { state, timeToChange } = getExpectedState(context); | ||
|
||
let prefix = ""; | ||
switch (state) { | ||
case "waiting": | ||
prefix = "Game starts in %s"; | ||
break; | ||
case "in_progress": | ||
prefix = "Swap in %s"; | ||
break; | ||
case "saving_work": | ||
prefix = "Saving your work %s"; | ||
break; | ||
case "finished": | ||
return "Game ended"; | ||
default: | ||
throw new Error("Don't know how to handle status: " + state); | ||
} | ||
|
||
const minutes = Math.floor(timeToChange! / 60000); | ||
const seconds = ((timeToChange! % 60000) / 1000).toFixed(0).padStart(2, "0"); | ||
return prefix.replace("%s", `${minutes}:${seconds}`); | ||
}; | ||
|
||
function GameStatusText() { | ||
const { state } = useGame(); | ||
|
||
const [timeInfo, setTimeInfo] = useState(getTimeInfo(state.context)); | ||
|
||
useEffect(() => { | ||
if (!["waiting", "in_progress", "saving_work"].includes(state.value)) { | ||
setTimeInfo(getTimeInfo(state.context)); | ||
return; | ||
} | ||
|
||
const timer = setInterval(() => { | ||
setTimeInfo(getTimeInfo(state.context)); | ||
}, 500); | ||
return () => clearInterval(timer); | ||
}, [state.value, state.context.meta]); | ||
|
||
switch (state.value) { | ||
case "game_not_found": | ||
return <p>Game not found!</p>; | ||
case "loading_game": | ||
case "loading_work": | ||
return <Loading />; | ||
case "loaded": | ||
case "waiting": | ||
case "in_progress": | ||
case "finished": | ||
case "saving_work": | ||
return ( | ||
<p> | ||
<strong>{timeInfo}</strong> | ||
</p> | ||
); | ||
default: | ||
throw new Error("Don't know how to handle status: " + state.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.