Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add New Game button to menu #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/components/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,26 @@ export function Game(props: Props) {
};
}, [replay.cursor, game]);

async function onRestartGame() {
async function onRestartGame(opts?: { abandoned: boolean }) {
const nextGame = recreateGame(liveGame());

const updatedGame = { ...liveGame(), nextGameId: nextGame.id };
if (opts?.abandoned) {
updatedGame.status = IGameStatus.OVER;
updatedGame.endedAt = Date.now();
logEvent("Game", "Game abandoned");
}

onStopReplay();
await updateGame(updatedGame);

await updateGame(updatedGame);
await updateGame(nextGame);
onGameChange(nextGame);

logEvent("Game", "Game recreated");

router.push(`/${nextGame.id}`);
}

function liveGame() {
return game.originalGame || game;
}
Expand All @@ -425,7 +432,9 @@ export function Game(props: Props) {
<GameBoard onMenuClick={onMenuClick} onRollbackClick={onRollbackClick} />
</div>
<div className="flex flex-column bg-black-50 bb b--yellow ph6.5-m">
{selectedArea.type === ActionAreaType.MENU && <MenuArea onCloseArea={onCloseArea} />}
{selectedArea.type === ActionAreaType.MENU && (
<MenuArea onCloseArea={onCloseArea} onRestartGame={onRestartGame} />
)}

{game.status === IGameStatus.LOBBY && (
<Lobby host={host} onAddBot={onAddBot} onJoinGame={onJoinGame} onStartGame={onStartGame} />
Expand Down
9 changes: 8 additions & 1 deletion src/components/menuArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { UserPreferences, useUserPreferences } from "~/hooks/userPreferences";

interface Props {
onCloseArea: () => void;
onRestartGame: ({ abandoned }: { abandoned: boolean }) => Promise<void>;
}

export default function MenuArea(props: Props) {
const { onCloseArea } = props;
const { onCloseArea, onRestartGame } = props;

const [showRules, setShowRules] = useState(false);
const { reset } = useContext(TutorialContext);
Expand All @@ -36,6 +37,11 @@ export default function MenuArea(props: Props) {
onCloseArea();
}

async function onRestartClick() {
onRestartGame({ abandoned: true });
onCloseArea();
}

if (showUserPreferences) {
return (
<UserPreferencesDialog
Expand Down Expand Up @@ -69,6 +75,7 @@ export default function MenuArea(props: Props) {
text={t("rules")}
onClick={() => setShowRules(true)}
/>
<Button className="mb3 w-100" size={ButtonSize.MEDIUM} text={t("newGame")} onClick={onRestartClick} />
</div>
)}

Expand Down