Skip to content

Commit 73c1d7b

Browse files
committed
feat: add a special achievement
1 parent 97aa22a commit 73c1d7b

File tree

2 files changed

+83
-46
lines changed

2 files changed

+83
-46
lines changed
Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,85 @@
1-
import { useEffect, useState } from 'react';
2-
import { useAchievements } from '../context/AchievementContext';
1+
import {useEffect, useState} from 'react';
2+
import {useAchievements} from '../context/AchievementContext';
33

44
const AchievementListeners = () => {
5-
const { unlockAchievement } = useAchievements();
5+
const {unlockAchievement} = useAchievements();
66
const [konamiIndex, setKonamiIndex] = useState(0);
77

8-
// Night Owl Check
9-
useEffect(() => {
10-
const checkNightOwl = () => {
11-
const now = new Date();
12-
const hour = now.getHours();
13-
// Between 3 AM (03:00) and 5 AM (05:00)
14-
if (hour >= 3 && hour < 5) {
15-
unlockAchievement('night_owl');
16-
}
17-
};
18-
checkNightOwl();
19-
}, [unlockAchievement]);
20-
// Konami Code Listener
21-
useEffect(() => { // Konami Code Sequence: Up, Up, Down, Down, Left, Right, Left, Right, B, A
22-
const konamiCode = [
23-
'ArrowUp',
24-
'ArrowUp',
25-
'ArrowDown',
26-
'ArrowDown',
27-
'ArrowLeft',
28-
'ArrowRight',
29-
'ArrowLeft',
30-
'ArrowRight',
31-
'b',
32-
'a',
33-
];
34-
const handleKeyDown = (e) => {
35-
// Check if the key matches the current step in the sequence
36-
if (e.key === konamiCode[konamiIndex]) {
37-
const nextIndex = konamiIndex + 1;
38-
// If the sequence is complete
39-
if (nextIndex === konamiCode.length) {
40-
unlockAchievement('konami_code');
41-
setKonamiIndex(0); // Reset
42-
} else {
43-
setKonamiIndex(nextIndex); // Advance
44-
}
8+
const [cheaterIndex, setCheaterIndex] = useState(0);
9+
// Night Owl Check
10+
11+
useEffect(() => {
12+
const checkNightOwl = () => {
13+
const now = new Date();
14+
const hour = now.getHours();
15+
// Between 3 AM (03:00) and 5 AM (05:00)
16+
if (hour >= 3 && hour < 5) {
17+
unlockAchievement('night_owl');
18+
}
19+
};
20+
21+
checkNightOwl();
22+
}, [unlockAchievement]);
23+
// Konami Code Listener
24+
useEffect(() => {
25+
// Konami Code Sequence: Up, Up, Down, Down, Left, Right, Left, Right, B, A
26+
const konamiCode = [
27+
'ArrowUp',
28+
'ArrowUp',
29+
'ArrowDown',
30+
'ArrowDown',
31+
'ArrowLeft',
32+
'ArrowRight',
33+
'ArrowLeft',
34+
'ArrowRight',
35+
'b',
36+
'a',
37+
];
38+
39+
const handleKeyDown = (e) => {
40+
// Check if the key matches the current step in the sequence
41+
if (e.key === konamiCode[konamiIndex]) {
42+
const nextIndex = konamiIndex + 1;
43+
// If the sequence is complete
44+
if (nextIndex === konamiCode.length) {
45+
unlockAchievement('konami_code');
46+
setKonamiIndex(0); // Reset
4547
} else {
46-
setKonamiIndex(0); // Mistake, reset
48+
setKonamiIndex(nextIndex); // Advance
4749
}
48-
};
49-
window.addEventListener('keydown', handleKeyDown);
50-
return () => window.removeEventListener('keydown', handleKeyDown);
51-
}, [konamiIndex, unlockAchievement]);
50+
} else {
51+
setKonamiIndex(0); // Mistake, reset
52+
}
53+
};
54+
55+
window.addEventListener('keydown', handleKeyDown);
56+
57+
return () => window.removeEventListener('keydown', handleKeyDown);
58+
}, [konamiIndex, unlockAchievement]);
59+
60+
// Cheater Code Listener
61+
62+
useEffect(() => {
63+
const cheaterCode = ['c', 'h', 'e', 'a', 't', 'e', 'r'];
64+
const handleKeyDown = (e) => {
65+
// Check if the key matches the current step in the sequence (case insensitive)
66+
if (e.key.toLowerCase() === cheaterCode[cheaterIndex]) {
67+
const nextIndex = cheaterIndex + 1;
68+
// If the sequence is complete
69+
if (nextIndex === cheaterCode.length) {
70+
unlockAchievement('cheater');
71+
setCheaterIndex(0); // Reset
72+
} else {
73+
setCheaterIndex(nextIndex); // Advance
74+
}
75+
} else {
76+
setCheaterIndex(0); // Mistake, reset
77+
}
78+
};
79+
window.addEventListener('keydown', handleKeyDown);
80+
return () => window.removeEventListener('keydown', handleKeyDown);
81+
}, [cheaterIndex, unlockAchievement]);
82+
5283
return null; // This component renders nothing
5384
};
54-
5585
export default AchievementListeners;

src/config/achievements.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,4 +736,11 @@ export const ACHIEVEMENTS = [
736736
icon: <SkullIcon size={32} weight="duotone" />,
737737
category: 'Secret',
738738
},
739+
{
740+
id: 'cheater',
741+
title: 'Rule Breaker',
742+
description: 'Admitting it is the first step.',
743+
icon: <SkullIcon size={32} weight="duotone" />,
744+
category: 'Secret',
745+
},
739746
];

0 commit comments

Comments
 (0)