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

142 lines
3.3 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 { interpolate } from "./Difficulty";
2021-10-01 19:08:37 +02:00
import Typography from "@mui/material/Typography";
interface Difficulty {
2021-09-05 01:09:30 +02:00
[key: string]: number;
timer: number;
size: 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: 12000, size: 6 },
Normal: { timer: 9000, size: 8 },
Hard: { timer: 5000, size: 9 },
Impossible: { timer: 2500, size: 12 },
};
export function BribeGame(props: IMinigameProps): React.ReactElement {
2021-09-05 01:09:30 +02:00
const difficulty: Difficulty = { timer: 0, size: 0 };
interpolate(difficulties, props.difficulty, difficulty);
const timer = difficulty.timer;
const [choices] = useState(makeChoices(difficulty));
const [index, setIndex] = useState(0);
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();
const k = event.key;
if (k === " ") {
2021-09-05 01:09:30 +02:00
if (positive.includes(choices[index])) props.onSuccess();
else props.onFailure();
return;
}
2021-09-05 01:09:30 +02:00
let newIndex = index;
if (["ArrowUp", "w", "ArrowRight", "d"].includes(k)) newIndex++;
if (["ArrowDown", "s", "ArrowLeft", "a"].includes(k)) newIndex--;
2021-09-05 01:09:30 +02:00
while (newIndex < 0) newIndex += choices.length;
while (newIndex > choices.length - 1) newIndex -= choices.length;
setIndex(newIndex);
}
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">Say something nice about the guard.</Typography>
2021-09-05 01:09:30 +02:00
<KeyHandler onKeyDown={press} onFailure={props.onFailure} />
</Grid>
<Grid item xs={6}>
2021-10-01 19:08:37 +02:00
<Typography variant="h5" color="primary">
</Typography>
<Typography variant="h5" color="primary">
{choices[index]}
</Typography>
<Typography variant="h5" color="primary">
</Typography>
2021-09-05 01:09:30 +02:00
</Grid>
</Grid>
);
}
function shuffleArray(array: string[]): void {
2021-09-05 01:09:30 +02:00
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function makeChoices(difficulty: Difficulty): string[] {
2021-09-05 01:09:30 +02:00
const choices = [];
choices.push(positive[Math.floor(Math.random() * positive.length)]);
for (let i = 0; i < difficulty.size; i++) {
const option = negative[Math.floor(Math.random() * negative.length)];
if (choices.includes(option)) {
i--;
continue;
}
2021-09-05 01:09:30 +02:00
choices.push(option);
}
shuffleArray(choices);
return choices;
}
2021-09-05 01:09:30 +02:00
const positive = [
"affectionate",
"agreeable",
"bright",
"charming",
"creative",
"determined",
"energetic",
"friendly",
"funny",
"generous",
"polite",
"likable",
"diplomatic",
"helpful",
"giving",
"kind",
"hardworking",
"patient",
"dynamic",
"loyal",
];
2021-09-05 01:09:30 +02:00
const negative = [
"aggressive",
"aloof",
"arrogant",
"big-headed",
"boastful",
"boring",
"bossy",
"careless",
"clingy",
"couch potato",
"cruel",
"cynical",
"grumpy",
"hot air",
"know it all",
"obnoxious",
"pain in the neck",
"picky",
"tactless",
"thoughtless",
];