bitburner-src/src/Infiltration/ui/CheatCodeGame.tsx

80 lines
2.4 KiB
TypeScript
Raw Normal View History

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";
import { IMinigameProps } from "./IMinigameProps";
import { KeyHandler } from "./KeyHandler";
import { GameTimer } from "./GameTimer";
import {
random,
getArrow,
getInverseArrow,
leftArrowSymbol,
rightArrowSymbol,
upArrowSymbol,
downArrowSymbol,
} from "../utils";
import { interpolate } from "./Difficulty";
2021-10-01 19:08:37 +02:00
import Typography from "@mui/material/Typography";
import { AugmentationNames } from "../../Augmentation/data/AugmentationNames";
import { Player } from "../../Player";
interface Difficulty {
2021-09-05 01:09:30 +02:00
[key: string]: number;
timer: number;
min: number;
max: number;
}
const difficulties: {
2021-09-05 01:09:30 +02:00
Trivial: Difficulty;
Normal: Difficulty;
Hard: Difficulty;
Impossible: Difficulty;
} = {
2021-09-05 01:09:30 +02:00
Trivial: { timer: 13000, min: 6, max: 8 },
Normal: { timer: 7000, min: 7, max: 8 },
Hard: { timer: 5000, min: 8, max: 9 },
2021-09-05 02:09:22 +02:00
Impossible: { timer: 3000, min: 9, max: 10 },
2021-09-05 01:09:30 +02:00
};
export function CheatCodeGame(props: IMinigameProps): React.ReactElement {
2021-09-05 01:09:30 +02:00
const difficulty: Difficulty = { timer: 0, min: 0, max: 0 };
interpolate(difficulties, props.difficulty, difficulty);
const timer = difficulty.timer;
const [code] = useState(generateCode(difficulty));
const [index, setIndex] = useState(0);
const hasAugment = Player.hasAugmentation(AugmentationNames.LameSharkRepository, true);
2021-09-25 04:15:19 +02:00
function press(this: Document, event: KeyboardEvent): void {
2021-09-05 01:09:30 +02:00
event.preventDefault();
if (code[index] !== getArrow(event) || (hasAugment && getInverseArrow(event))) {
2021-09-05 01:09:30 +02:00
props.onFailure();
return;
}
2021-09-05 01:09:30 +02:00
setIndex(index + 1);
if (index + 1 >= code.length) props.onSuccess();
}
2021-09-05 01:09:30 +02:00
return (
<Grid container spacing={3}>
<GameTimer millis={timer} onExpire={props.onFailure} />
<Grid item xs={12}>
2021-10-01 19:08:37 +02:00
<Typography variant="h4">Enter the Code!</Typography>
<Typography variant="h4">{code[index]}</Typography>
2021-09-05 01:09:30 +02:00
<KeyHandler onKeyDown={press} onFailure={props.onFailure} />
</Grid>
</Grid>
);
}
function generateCode(difficulty: Difficulty): string {
const arrows = [leftArrowSymbol, rightArrowSymbol, upArrowSymbol, downArrowSymbol];
2021-09-05 01:09:30 +02:00
let code = "";
for (let i = 0; i < random(difficulty.min, difficulty.max); i++) {
let arrow = arrows[Math.floor(4 * Math.random())];
2021-09-09 05:47:34 +02:00
while (arrow === code[code.length - 1]) arrow = arrows[Math.floor(4 * Math.random())];
2021-09-05 01:09:30 +02:00
code += arrow;
}
2021-09-05 01:09:30 +02:00
return code;
}