2022-10-18 18:01:53 +02:00
|
|
|
import { useRef, useState } from 'react';
|
2022-10-13 14:00:34 +02:00
|
|
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|
|
|
|
import Indicator from './indicator';
|
2022-10-18 17:59:02 +02:00
|
|
|
import { fetchNui } from '../../utils/fetchNui';
|
2023-02-13 21:24:33 +01:00
|
|
|
import { Box, createStyles } from '@mantine/core';
|
2023-02-15 13:00:09 +01:00
|
|
|
import type { SkillCheckProps, GameDifficulty } from '../../typings';
|
2022-10-13 14:00:34 +02:00
|
|
|
|
2022-10-20 13:30:25 +02:00
|
|
|
export const circleCircumference = 2 * 50 * Math.PI;
|
|
|
|
|
|
2022-10-13 14:00:34 +02:00
|
|
|
const getRandomAngle = (min: number, max: number) => Math.floor(Math.random() * (max - min)) + min;
|
|
|
|
|
|
|
|
|
|
const difficultyOffsets = {
|
2022-10-20 13:30:25 +02:00
|
|
|
easy: 50,
|
|
|
|
|
medium: 40,
|
|
|
|
|
hard: 25,
|
2022-10-13 14:00:34 +02:00
|
|
|
};
|
|
|
|
|
|
2022-12-23 10:29:22 +01:00
|
|
|
const useStyles = createStyles((theme) => ({
|
|
|
|
|
svg: {
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
top: '50%',
|
|
|
|
|
left: '50%',
|
|
|
|
|
transform: 'translate(-50%, -50%)',
|
|
|
|
|
},
|
|
|
|
|
track: {
|
|
|
|
|
fill: 'transparent',
|
2022-12-30 13:01:41 +01:00
|
|
|
stroke: theme.colors.dark[5],
|
2022-12-23 10:29:22 +01:00
|
|
|
strokeWidth: 8,
|
|
|
|
|
},
|
|
|
|
|
skillArea: {
|
|
|
|
|
fill: 'transparent',
|
|
|
|
|
stroke: theme.fn.primaryColor(),
|
|
|
|
|
strokeWidth: 8,
|
|
|
|
|
},
|
|
|
|
|
indicator: {
|
|
|
|
|
stroke: 'red',
|
|
|
|
|
strokeWidth: 16,
|
|
|
|
|
fill: 'transparent',
|
|
|
|
|
},
|
|
|
|
|
button: {
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
left: '50%',
|
|
|
|
|
top: '50%',
|
|
|
|
|
transform: 'translate(-50%, -50%)',
|
2022-12-30 13:01:41 +01:00
|
|
|
backgroundColor: theme.colors.dark[5],
|
2022-12-23 10:29:22 +01:00
|
|
|
width: 25,
|
|
|
|
|
height: 25,
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
borderRadius: 5,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: 500,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-10-13 14:00:34 +02:00
|
|
|
const SkillCheck: React.FC = () => {
|
2022-12-23 10:29:22 +01:00
|
|
|
const { classes } = useStyles();
|
2022-10-15 21:39:46 +02:00
|
|
|
const [visible, setVisible] = useState(false);
|
2023-02-12 19:26:09 +01:00
|
|
|
const dataRef = useRef<{ difficulty: GameDifficulty | GameDifficulty[]; inputs?: string[] } | null>(null);
|
2022-10-18 17:59:02 +02:00
|
|
|
const dataIndexRef = useRef<number>(0);
|
2022-10-13 14:00:34 +02:00
|
|
|
const [skillCheck, setSkillCheck] = useState<SkillCheckProps>({
|
|
|
|
|
angle: 0,
|
2022-10-20 13:30:25 +02:00
|
|
|
difficultyOffset: 50,
|
2022-10-13 14:00:34 +02:00
|
|
|
difficulty: 'easy',
|
2023-02-12 19:26:09 +01:00
|
|
|
key: 'e',
|
2022-10-13 14:00:34 +02:00
|
|
|
});
|
|
|
|
|
|
2023-02-12 19:26:09 +01:00
|
|
|
useNuiEvent('startSkillCheck', (data: { difficulty: GameDifficulty | GameDifficulty[]; inputs?: string[] }) => {
|
2022-10-18 17:59:02 +02:00
|
|
|
dataRef.current = data;
|
|
|
|
|
dataIndexRef.current = 0;
|
2023-02-12 19:26:09 +01:00
|
|
|
const gameData = Array.isArray(data.difficulty) ? data.difficulty[0] : data.difficulty;
|
2022-10-18 17:59:02 +02:00
|
|
|
const offset = typeof gameData === 'object' ? gameData.areaSize : difficultyOffsets[gameData];
|
2023-02-12 19:26:09 +01:00
|
|
|
const randomKey = data.inputs ? data.inputs[Math.floor(Math.random() * data.inputs.length)] : 'e';
|
2022-10-13 14:00:34 +02:00
|
|
|
setSkillCheck({
|
2022-10-20 13:30:25 +02:00
|
|
|
angle: -90 + getRandomAngle(120, 360 - offset),
|
2022-10-13 23:22:07 +02:00
|
|
|
difficultyOffset: offset,
|
2022-10-18 17:59:02 +02:00
|
|
|
difficulty: gameData,
|
2023-02-12 19:26:09 +01:00
|
|
|
key: randomKey,
|
2022-10-13 14:00:34 +02:00
|
|
|
});
|
2022-10-18 17:59:02 +02:00
|
|
|
|
2022-10-15 21:39:46 +02:00
|
|
|
setVisible(true);
|
2022-10-13 14:00:34 +02:00
|
|
|
});
|
|
|
|
|
|
2022-10-18 17:59:02 +02:00
|
|
|
const handleComplete = (success: boolean) => {
|
2023-02-12 19:26:09 +01:00
|
|
|
if (!dataRef.current) return;
|
|
|
|
|
if (!success || !Array.isArray(dataRef.current.difficulty)) {
|
2022-10-18 17:59:02 +02:00
|
|
|
setVisible(false);
|
|
|
|
|
return fetchNui('skillCheckOver', success);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 19:26:09 +01:00
|
|
|
if (dataIndexRef.current >= dataRef.current.difficulty.length - 1) {
|
2022-10-18 17:59:02 +02:00
|
|
|
setVisible(false);
|
|
|
|
|
return fetchNui('skillCheckOver', success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataIndexRef.current++;
|
2023-02-12 19:26:09 +01:00
|
|
|
const data = dataRef.current.difficulty[dataIndexRef.current];
|
|
|
|
|
const key = dataRef.current.inputs
|
|
|
|
|
? dataRef.current.inputs[Math.floor(Math.random() * dataRef.current.inputs.length)]
|
|
|
|
|
: 'e';
|
2022-10-18 17:59:02 +02:00
|
|
|
const offset = typeof data === 'object' ? data.areaSize : difficultyOffsets[data];
|
|
|
|
|
setSkillCheck({
|
2022-10-20 13:30:25 +02:00
|
|
|
angle: -90 + getRandomAngle(120, 360 - offset),
|
2022-10-18 17:59:02 +02:00
|
|
|
difficultyOffset: offset,
|
|
|
|
|
difficulty: data,
|
2023-02-12 19:26:09 +01:00
|
|
|
key,
|
2022-10-18 17:59:02 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-13 14:00:34 +02:00
|
|
|
return (
|
2022-12-23 10:29:22 +01:00
|
|
|
<>
|
2022-10-15 21:39:46 +02:00
|
|
|
{visible && (
|
2022-10-13 14:00:34 +02:00
|
|
|
<>
|
2022-12-23 10:29:22 +01:00
|
|
|
<svg r={50} width={500} height={500} className={classes.svg}>
|
2022-10-13 14:00:34 +02:00
|
|
|
{/*Circle track*/}
|
2022-12-23 10:29:22 +01:00
|
|
|
<circle r={50} cx={250} cy={250} className={classes.track} strokeDasharray={circleCircumference} />
|
2022-10-13 14:00:34 +02:00
|
|
|
{/*SkillCheck area*/}
|
|
|
|
|
<circle
|
|
|
|
|
r={50}
|
|
|
|
|
cx={250}
|
|
|
|
|
cy={250}
|
2022-10-20 13:30:25 +02:00
|
|
|
strokeDasharray={circleCircumference}
|
|
|
|
|
strokeDashoffset={circleCircumference - (Math.PI * 50 * skillCheck.difficultyOffset) / 180}
|
2022-10-13 14:00:34 +02:00
|
|
|
transform={`rotate(${skillCheck.angle}, 250, 250)`}
|
2022-12-23 10:29:22 +01:00
|
|
|
className={classes.skillArea}
|
2022-10-13 14:00:34 +02:00
|
|
|
/>
|
|
|
|
|
<Indicator
|
|
|
|
|
angle={skillCheck.angle}
|
|
|
|
|
offset={skillCheck.difficultyOffset}
|
2022-10-14 20:42:54 +02:00
|
|
|
multiplier={
|
|
|
|
|
skillCheck.difficulty === 'easy'
|
|
|
|
|
? 1
|
|
|
|
|
: skillCheck.difficulty === 'medium'
|
|
|
|
|
? 1.5
|
|
|
|
|
: skillCheck.difficulty === 'hard'
|
|
|
|
|
? 1.75
|
|
|
|
|
: skillCheck.difficulty.speedMultiplier
|
|
|
|
|
}
|
2022-10-18 17:59:02 +02:00
|
|
|
handleComplete={handleComplete}
|
2022-12-23 10:29:22 +01:00
|
|
|
className={classes.indicator}
|
2022-10-18 17:59:02 +02:00
|
|
|
skillCheck={skillCheck}
|
2022-10-13 14:00:34 +02:00
|
|
|
/>
|
|
|
|
|
</svg>
|
2023-02-12 19:26:09 +01:00
|
|
|
<Box className={classes.button}>{skillCheck.key.toUpperCase()}</Box>
|
2022-10-13 14:00:34 +02:00
|
|
|
</>
|
|
|
|
|
)}
|
2022-12-23 10:29:22 +01:00
|
|
|
</>
|
2022-10-13 14:00:34 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SkillCheck;
|