2021-09-18 01:43:08 +02:00
|
|
|
import { use } from "../../ui/Context";
|
2021-09-05 01:09:30 +02:00
|
|
|
import React, { useState } from "react";
|
2021-09-17 01:23:03 +02:00
|
|
|
import Grid from "@mui/material/Grid";
|
2021-06-13 17:05:40 +02:00
|
|
|
import { Countdown } from "./Countdown";
|
|
|
|
import { BracketGame } from "./BracketGame";
|
|
|
|
import { SlashGame } from "./SlashGame";
|
|
|
|
import { BackwardGame } from "./BackwardGame";
|
|
|
|
import { BribeGame } from "./BribeGame";
|
|
|
|
import { CheatCodeGame } from "./CheatCodeGame";
|
|
|
|
import { Cyberpunk2077Game } from "./Cyberpunk2077Game";
|
|
|
|
import { MinesweeperGame } from "./MinesweeperGame";
|
|
|
|
import { WireCuttingGame } from "./WireCuttingGame";
|
|
|
|
import { Victory } from "./Victory";
|
2021-10-01 19:08:37 +02:00
|
|
|
import Typography from "@mui/material/Typography";
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
interface IProps {
|
2021-09-05 01:09:30 +02:00
|
|
|
StartingDifficulty: number;
|
|
|
|
Difficulty: number;
|
|
|
|
MaxLevel: number;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Stage {
|
2021-09-05 01:09:30 +02:00
|
|
|
Countdown = 0,
|
|
|
|
Minigame,
|
|
|
|
Result,
|
|
|
|
Sell,
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const minigames = [
|
2021-09-05 01:09:30 +02:00
|
|
|
SlashGame,
|
|
|
|
BracketGame,
|
|
|
|
BackwardGame,
|
|
|
|
BribeGame,
|
|
|
|
CheatCodeGame,
|
|
|
|
Cyberpunk2077Game,
|
|
|
|
MinesweeperGame,
|
|
|
|
WireCuttingGame,
|
|
|
|
];
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
export function Game(props: IProps): React.ReactElement {
|
2021-09-18 01:43:08 +02:00
|
|
|
const player = use.Player();
|
|
|
|
const router = use.Router();
|
2021-09-05 01:09:30 +02:00
|
|
|
const [level, setLevel] = useState(1);
|
|
|
|
const [stage, setStage] = useState(Stage.Countdown);
|
|
|
|
const [results, setResults] = useState("");
|
|
|
|
const [gameIds, setGameIds] = useState({
|
|
|
|
lastGames: [-1, -1],
|
|
|
|
id: Math.floor(Math.random() * minigames.length),
|
|
|
|
});
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function nextGameId(): number {
|
|
|
|
let id = gameIds.lastGames[0];
|
|
|
|
const ids = [gameIds.lastGames[0], gameIds.lastGames[1], gameIds.id];
|
|
|
|
while (ids.includes(id)) {
|
|
|
|
id = Math.floor(Math.random() * minigames.length);
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return id;
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function setupNextGame(): void {
|
|
|
|
setGameIds({
|
|
|
|
lastGames: [gameIds.lastGames[1], gameIds.id],
|
|
|
|
id: nextGameId(),
|
|
|
|
});
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function success(): void {
|
|
|
|
pushResult(true);
|
|
|
|
if (level === props.MaxLevel) {
|
|
|
|
setStage(Stage.Sell);
|
|
|
|
} else {
|
|
|
|
setStage(Stage.Countdown);
|
|
|
|
setLevel(level + 1);
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
setupNextGame();
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function pushResult(win: boolean): void {
|
|
|
|
setResults((old) => {
|
|
|
|
let next = old;
|
|
|
|
next += win ? "✓" : "✗";
|
|
|
|
if (next.length > 15) next = next.slice(1);
|
|
|
|
return next;
|
|
|
|
});
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function failure(options?: { automated: boolean }): void {
|
|
|
|
setStage(Stage.Countdown);
|
|
|
|
pushResult(false);
|
|
|
|
// Kill the player immediately if they use automation, so
|
|
|
|
// it's clear they're not meant to
|
2021-09-18 01:43:08 +02:00
|
|
|
const damage = options?.automated ? player.hp : props.StartingDifficulty * 3;
|
|
|
|
if (player.takeDamage(damage)) {
|
|
|
|
router.toCity();
|
|
|
|
return;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
setupNextGame();
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
let stageComponent: React.ReactNode;
|
|
|
|
switch (stage) {
|
2021-06-13 17:05:40 +02:00
|
|
|
case Stage.Countdown:
|
2021-09-05 01:09:30 +02:00
|
|
|
stageComponent = <Countdown onFinish={() => setStage(Stage.Minigame)} />;
|
|
|
|
break;
|
2021-06-13 17:05:40 +02:00
|
|
|
case Stage.Minigame: {
|
2021-09-05 01:09:30 +02:00
|
|
|
const MiniGame = minigames[gameIds.id];
|
2021-09-09 05:47:34 +02:00
|
|
|
stageComponent = <MiniGame onSuccess={success} onFailure={failure} difficulty={props.Difficulty + level / 50} />;
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
case Stage.Sell:
|
2021-09-05 01:09:30 +02:00
|
|
|
stageComponent = (
|
|
|
|
<Victory
|
|
|
|
StartingDifficulty={props.StartingDifficulty}
|
|
|
|
Difficulty={props.Difficulty}
|
|
|
|
MaxLevel={props.MaxLevel}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function Progress(): React.ReactElement {
|
|
|
|
return (
|
2021-11-02 21:20:32 +01:00
|
|
|
<Typography variant="h4">
|
2021-09-09 05:47:34 +02:00
|
|
|
<span style={{ color: "gray" }}>{results.slice(0, results.length - 1)}</span>
|
2021-09-05 01:09:30 +02:00
|
|
|
{results[results.length - 1]}
|
2021-11-02 21:20:32 +01:00
|
|
|
</Typography>
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Grid container spacing={3}>
|
|
|
|
<Grid item xs={3}>
|
2021-10-01 19:08:37 +02:00
|
|
|
<Typography>
|
2021-09-05 01:09:30 +02:00
|
|
|
Level: {level} / {props.MaxLevel}
|
2021-10-01 19:08:37 +02:00
|
|
|
</Typography>
|
2021-09-05 01:09:30 +02:00
|
|
|
<Progress />
|
|
|
|
</Grid>
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
<Grid item xs={12}>
|
|
|
|
{stageComponent}
|
2021-06-13 17:05:40 +02:00
|
|
|
</Grid>
|
2021-09-05 01:09:30 +02:00
|
|
|
</Grid>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|