✨ Different leaderboard on difficulty & size parameters
This commit is contained in:
@@ -30,20 +30,18 @@ const Home = () => {
|
|||||||
|
|
||||||
const [, setGameState] = useRecoilState(gameStateAtom);
|
const [, setGameState] = useRecoilState(gameStateAtom);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const initIDB = async () => {
|
|
||||||
if ((await store.getItem('leaderboard')) === null) {
|
|
||||||
await store.setItem('leaderboard', []);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
initIDB();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
if (difficultyRef.current !== null && sizeRef.current !== null) {
|
if (difficultyRef.current !== null && sizeRef.current !== null) {
|
||||||
const difficulty = difficultyRef.current.value;
|
const difficulty = difficultyRef.current.value;
|
||||||
const size = sizeRef.current.value;
|
const size = sizeRef.current.value;
|
||||||
|
|
||||||
|
const initIDB = async () => {
|
||||||
|
if ((await store.getItem(`${difficulty}:${size}`)) === null) {
|
||||||
|
await store.setItem(`${difficulty}:${size}`, []);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
initIDB();
|
||||||
|
|
||||||
setGameState((prev) => ({
|
setGameState((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
difficulty: difficulty,
|
difficulty: difficulty,
|
||||||
|
|||||||
@@ -34,12 +34,12 @@ const store = localforage.createInstance({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const LeaderBoard = () => {
|
const LeaderBoard = () => {
|
||||||
const [, setGameState] = useRecoilState(gameStateAtom);
|
const [gameState, setGameState] = useRecoilState(gameStateAtom);
|
||||||
const [data, setData] = useState<LeaderBoardItem[]>([]);
|
const [data, setData] = useState<LeaderBoardItem[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const getData = async () => {
|
const getData = async () => {
|
||||||
const storeData = await store.getItem<LeaderBoardItem[]>('leaderboard');
|
const storeData = await store.getItem<LeaderBoardItem[]>(`${gameState.difficulty}:${gameState.gridSize}`);
|
||||||
|
|
||||||
setData(storeData || []);
|
setData(storeData || []);
|
||||||
};
|
};
|
||||||
@@ -95,7 +95,7 @@ const LeaderBoard = () => {
|
|||||||
className='controlButton clear'
|
className='controlButton clear'
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setGameState((prev) => ({ ...prev, status: 'settings', endType: '', placedFlags: 0, bombs: 0, gameTime: '' }));
|
setGameState((prev) => ({ ...prev, status: 'settings', endType: '', placedFlags: 0, bombs: 0, gameTime: '' }));
|
||||||
store.setItem('leaderboard', []);
|
store.setItem(`${gameState.difficulty}:${gameState.gridSize}`, []);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Trash />
|
<Trash />
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ import { useRecoilState } from 'recoil';
|
|||||||
import { gameStateAtom } from '../../../contexts/gameState';
|
import { gameStateAtom } from '../../../contexts/gameState';
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// ------------------------------------------------------ Utils --------------------------------------------------------
|
// -------------------------------------------------- Utils & Types ----------------------------------------------------
|
||||||
|
import { Difficulty } from '../../../types/game';
|
||||||
import { saveToLocalStorage } from '../../../utils/generics';
|
import { saveToLocalStorage } from '../../../utils/generics';
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ const Timer = () => {
|
|||||||
clearInterval(intervalId);
|
clearInterval(intervalId);
|
||||||
|
|
||||||
if (gameState.endType === 'win') {
|
if (gameState.endType === 'win') {
|
||||||
saveToLocalStorage(displayedTime);
|
saveToLocalStorage(displayedTime, gameState.difficulty as Difficulty, gameState.gridSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [intervalId, gameState.endType, setGameState, displayedTime]);
|
}, [intervalId, gameState.endType, setGameState, displayedTime]);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import localforage from 'localforage';
|
|||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// ------------------------------------------------------ Types --------------------------------------------------------
|
// ------------------------------------------------------ Types --------------------------------------------------------
|
||||||
import { LeaderBoardItem } from '../types/game';
|
import { Difficulty, LeaderBoardItem } from '../types/game';
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
export const getRandomArbitrary = (min: number, max: number): number => {
|
export const getRandomArbitrary = (min: number, max: number): number => {
|
||||||
@@ -22,8 +22,8 @@ const store = localforage.createInstance({
|
|||||||
description: 'Store 10 best game time',
|
description: 'Store 10 best game time',
|
||||||
});
|
});
|
||||||
|
|
||||||
export const saveToLocalStorage = async (time: string) => {
|
export const saveToLocalStorage = async (time: string, difficulty: Difficulty, gridSize: number) => {
|
||||||
let leaderboard = await store.getItem<LeaderBoardItem[]>('leaderboard');
|
let leaderboard = await store.getItem<LeaderBoardItem[]>(`${difficulty}:${gridSize}`);
|
||||||
|
|
||||||
const currentDate = new Date().toLocaleString().split(' ')[0];
|
const currentDate = new Date().toLocaleString().split(' ')[0];
|
||||||
|
|
||||||
@@ -42,5 +42,5 @@ export const saveToLocalStorage = async (time: string) => {
|
|||||||
leaderboard = leaderboard.slice(0, 20);
|
leaderboard = leaderboard.slice(0, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
await store.setItem('leaderboard', leaderboard);
|
await store.setItem(`${difficulty}:${gridSize}`, leaderboard);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user