2021-09-05 01:09:30 +02:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import Grid from "@material-ui/core/Grid";
|
2021-06-13 17:05:40 +02:00
|
|
|
import { IMinigameProps } from "./IMinigameProps";
|
|
|
|
import { KeyHandler } from "./KeyHandler";
|
|
|
|
import { GameTimer } from "./GameTimer";
|
|
|
|
import { random } from "../utils";
|
|
|
|
import { interpolate } from "./Difficulty";
|
|
|
|
import { BlinkingCursor } from "./BlinkingCursor";
|
|
|
|
|
|
|
|
interface Difficulty {
|
2021-09-05 01:09:30 +02:00
|
|
|
[key: string]: number;
|
|
|
|
timer: number;
|
|
|
|
min: number;
|
|
|
|
max: number;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const difficulties: {
|
2021-09-05 01:09:30 +02:00
|
|
|
Trivial: Difficulty;
|
|
|
|
Normal: Difficulty;
|
|
|
|
Hard: Difficulty;
|
|
|
|
Impossible: Difficulty;
|
2021-06-13 17:05:40 +02:00
|
|
|
} = {
|
2021-09-05 01:09:30 +02:00
|
|
|
Trivial: { timer: 8000, min: 2, max: 3 },
|
|
|
|
Normal: { timer: 6000, min: 4, max: 5 },
|
|
|
|
Hard: { timer: 4000, min: 4, max: 6 },
|
|
|
|
Impossible: { timer: 2500, min: 7, max: 7 },
|
|
|
|
};
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
function generateLeftSide(difficulty: Difficulty): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
let str = "";
|
|
|
|
const length = random(difficulty.min, difficulty.max);
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
str += ["[", "<", "(", "{"][Math.floor(Math.random() * 4)];
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return str;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getChar(event: React.KeyboardEvent<HTMLElement>): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (event.keyCode == 48 && event.shiftKey) return ")";
|
|
|
|
if (event.keyCode == 221 && !event.shiftKey) return "]";
|
|
|
|
if (event.keyCode == 221 && event.shiftKey) return "}";
|
|
|
|
if (event.keyCode == 190 && event.shiftKey) return ">";
|
|
|
|
return "";
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function match(left: string, right: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
|
|
|
(left === "[" && right === "]") ||
|
|
|
|
(left === "<" && right === ">") ||
|
|
|
|
(left === "(" && right === ")") ||
|
|
|
|
(left === "{" && right === "}")
|
|
|
|
);
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function BracketGame(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 [right, setRight] = useState("");
|
|
|
|
const [left] = useState(generateLeftSide(difficulty));
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function press(event: React.KeyboardEvent<HTMLElement>): void {
|
|
|
|
event.preventDefault();
|
|
|
|
const char = getChar(event);
|
|
|
|
if (!char) return;
|
|
|
|
if (!match(left[left.length - right.length - 1], char)) {
|
|
|
|
props.onFailure();
|
|
|
|
return;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (left.length === right.length + 1) {
|
|
|
|
props.onSuccess();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setRight(right + char);
|
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
|
|
|
<Grid container spacing={3}>
|
|
|
|
<GameTimer millis={timer} onExpire={props.onFailure} />
|
|
|
|
<Grid item xs={12}>
|
|
|
|
<h1 className={"noselect"}>Close the brackets</h1>
|
|
|
|
<p style={{ fontSize: "5em" }}>
|
|
|
|
{`${left}${right}`}
|
|
|
|
<BlinkingCursor />
|
|
|
|
</p>
|
|
|
|
<KeyHandler onKeyDown={press} onFailure={props.onFailure} />
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
}
|