2022-04-30 00:01:38 +02:00
|
|
|
import { Paper, Typography, Box } from "@mui/material";
|
2021-09-05 01:09:30 +02:00
|
|
|
import React, { useState } from "react";
|
2023-06-12 06:34:20 +02:00
|
|
|
import { AugmentationName } from "@enums";
|
2022-10-10 00:42:14 +02:00
|
|
|
import { Player } from "@player";
|
2022-04-25 01:51:30 +02:00
|
|
|
import { Settings } from "../../Settings/Settings";
|
|
|
|
import { KEY } from "../../utils/helpers/keyCodes";
|
|
|
|
import { downArrowSymbol, getArrow, leftArrowSymbol, rightArrowSymbol, upArrowSymbol } from "../utils";
|
|
|
|
import { interpolate } from "./Difficulty";
|
|
|
|
import { GameTimer } from "./GameTimer";
|
|
|
|
import { IMinigameProps } from "./IMinigameProps";
|
|
|
|
import { KeyHandler } from "./KeyHandler";
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
interface Difficulty {
|
2021-09-05 01:09:30 +02:00
|
|
|
[key: string]: number;
|
|
|
|
timer: number;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
symbols: number;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
2022-04-30 00:01:38 +02:00
|
|
|
interface GridItem {
|
|
|
|
content: string;
|
|
|
|
color: string;
|
|
|
|
selected?: boolean;
|
|
|
|
}
|
|
|
|
|
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: 12500, width: 3, height: 3, symbols: 6 },
|
|
|
|
Normal: { timer: 15000, width: 4, height: 4, symbols: 7 },
|
|
|
|
Hard: { timer: 12500, width: 5, height: 5, symbols: 8 },
|
|
|
|
Impossible: { timer: 10000, width: 6, height: 6, symbols: 9 },
|
|
|
|
};
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
export function Cyberpunk2077Game(props: IMinigameProps): React.ReactElement {
|
2021-09-05 01:09:30 +02:00
|
|
|
const difficulty: Difficulty = { timer: 0, width: 0, height: 0, symbols: 0 };
|
|
|
|
interpolate(difficulties, props.difficulty, difficulty);
|
|
|
|
const timer = difficulty.timer;
|
|
|
|
const [grid] = useState(generatePuzzle(difficulty));
|
2022-03-23 15:31:56 +01:00
|
|
|
const [answers] = useState(generateAnswers(grid, difficulty));
|
|
|
|
const [currentAnswerIndex, setCurrentAnswerIndex] = useState(0);
|
2021-09-05 01:09:30 +02:00
|
|
|
const [pos, setPos] = useState([0, 0]);
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2023-06-12 06:34:20 +02:00
|
|
|
const hasAugment = Player.hasAugmentation(AugmentationName.FloodOfPoseidon, 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();
|
|
|
|
const move = [0, 0];
|
|
|
|
const arrow = getArrow(event);
|
|
|
|
switch (arrow) {
|
2022-03-23 15:31:56 +01:00
|
|
|
case upArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[1]--;
|
|
|
|
break;
|
2022-03-23 15:31:56 +01:00
|
|
|
case leftArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[0]--;
|
|
|
|
break;
|
2022-03-23 15:31:56 +01:00
|
|
|
case downArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[1]++;
|
|
|
|
break;
|
2022-03-23 15:31:56 +01:00
|
|
|
case rightArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[0]++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const next = [pos[0] + move[0], pos[1] + move[1]];
|
|
|
|
next[0] = (next[0] + grid[0].length) % grid[0].length;
|
|
|
|
next[1] = (next[1] + grid.length) % grid.length;
|
|
|
|
setPos(next);
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2022-03-23 15:31:56 +01:00
|
|
|
if (event.key === KEY.SPACE) {
|
2021-09-05 01:09:30 +02:00
|
|
|
const selected = grid[pos[1]][pos[0]];
|
2022-03-23 15:31:56 +01:00
|
|
|
const expected = answers[currentAnswerIndex];
|
2021-09-05 01:09:30 +02:00
|
|
|
if (selected !== expected) {
|
|
|
|
props.onFailure();
|
|
|
|
return;
|
|
|
|
}
|
2022-03-23 15:31:56 +01:00
|
|
|
setCurrentAnswerIndex(currentAnswerIndex + 1);
|
|
|
|
if (answers.length === currentAnswerIndex + 1) props.onSuccess();
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2022-04-30 00:01:38 +02:00
|
|
|
const flatGrid: GridItem[] = [];
|
|
|
|
grid.map((line, y) =>
|
|
|
|
line.map((cell, x) => {
|
|
|
|
const isCorrectAnswer = cell === answers[currentAnswerIndex];
|
|
|
|
const optionColor = hasAugment && !isCorrectAnswer ? Settings.theme.disabled : Settings.theme.primary;
|
|
|
|
|
|
|
|
if (x === pos[0] && y === pos[1]) {
|
|
|
|
flatGrid.push({ color: optionColor, content: cell, selected: true });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
flatGrid.push({ color: optionColor, content: cell });
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const fontSize = "2em";
|
|
|
|
return (
|
2022-04-24 21:57:18 +02:00
|
|
|
<>
|
2021-09-05 01:09:30 +02:00
|
|
|
<GameTimer millis={timer} onExpire={props.onFailure} />
|
2022-04-30 00:01:38 +02:00
|
|
|
<Paper sx={{ display: "grid", justifyItems: "center", pb: 1 }}>
|
2021-10-01 19:08:37 +02:00
|
|
|
<Typography variant="h4">Match the symbols!</Typography>
|
2022-03-23 15:31:56 +01:00
|
|
|
<Typography variant="h5" color={Settings.theme.primary}>
|
2021-09-05 01:09:30 +02:00
|
|
|
Targets:{" "}
|
2022-03-23 15:31:56 +01:00
|
|
|
{answers.map((a, i) => {
|
|
|
|
if (i == currentAnswerIndex)
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
2022-04-30 00:01:38 +02:00
|
|
|
<span key={`${i}`} style={{ fontSize: "1em", color: Settings.theme.infolight }}>
|
2021-09-05 01:09:30 +02:00
|
|
|
{a}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
return (
|
2022-03-23 15:31:56 +01:00
|
|
|
<span key={`${i}`} style={{ fontSize: "1em", color: Settings.theme.primary }}>
|
2021-09-05 01:09:30 +02:00
|
|
|
{a}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
})}
|
2021-10-01 19:08:37 +02:00
|
|
|
</Typography>
|
2021-09-05 01:09:30 +02:00
|
|
|
<br />
|
2022-04-30 00:01:38 +02:00
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "grid",
|
|
|
|
gridTemplateColumns: `repeat(${Math.round(difficulty.width)}, 1fr)`,
|
|
|
|
gap: 1,
|
|
|
|
}}
|
|
|
|
>
|
2023-01-08 08:36:55 +01:00
|
|
|
{flatGrid.map((item, idx) => (
|
2022-04-30 00:01:38 +02:00
|
|
|
<Typography
|
2023-01-08 08:36:55 +01:00
|
|
|
key={idx}
|
2022-04-30 00:01:38 +02:00
|
|
|
sx={{
|
|
|
|
fontSize: fontSize,
|
|
|
|
color: item.color,
|
|
|
|
border: item.selected ? `2px solid ${Settings.theme.infolight}` : "unset",
|
|
|
|
lineHeight: "unset",
|
|
|
|
p: item.selected ? "2px" : "4px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{item.content}
|
2021-10-02 04:27:32 +02:00
|
|
|
</Typography>
|
2022-04-30 00:01:38 +02:00
|
|
|
))}
|
|
|
|
</Box>
|
2021-09-05 01:09:30 +02:00
|
|
|
<KeyHandler onKeyDown={press} onFailure={props.onFailure} />
|
2022-04-24 21:57:18 +02:00
|
|
|
</Paper>
|
|
|
|
</>
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 15:31:56 +01:00
|
|
|
function generateAnswers(grid: string[][], difficulty: Difficulty): string[] {
|
|
|
|
const answers = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
for (let i = 0; i < Math.round(difficulty.symbols); i++) {
|
2022-03-23 15:31:56 +01:00
|
|
|
answers.push(grid[Math.floor(Math.random() * grid.length)][Math.floor(Math.random() * grid[0].length)]);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2022-03-23 15:31:56 +01:00
|
|
|
return answers;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function randChar(): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
return "ABCDEF0123456789"[Math.floor(Math.random() * 16)];
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function generatePuzzle(difficulty: Difficulty): string[][] {
|
2021-09-05 01:09:30 +02:00
|
|
|
const puzzle = [];
|
|
|
|
for (let i = 0; i < Math.round(difficulty.height); i++) {
|
|
|
|
const line = [];
|
|
|
|
for (let j = 0; j < Math.round(difficulty.width); j++) {
|
|
|
|
line.push(randChar() + randChar());
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
puzzle.push(line);
|
|
|
|
}
|
|
|
|
return puzzle;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|