-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAchievementListeners.js
More file actions
91 lines (81 loc) · 2.67 KB
/
AchievementListeners.js
File metadata and controls
91 lines (81 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { useEffect, useState } from 'react';
import { useAchievements } from '../context/AchievementContext';
const AchievementListeners = () => {
const { unlockAchievement } = useAchievements();
const [konamiIndex, setKonamiIndex] = useState(0);
const [cheaterIndex, setCheaterIndex] = useState(0);
// Night Owl Check
useEffect(() => {
const checkNightOwl = () => {
const now = new Date();
const hour = now.getHours();
// Between 3 AM (03:00) and 5 AM (05:00)
if (hour >= 3 && hour < 5) {
unlockAchievement('night_owl');
}
};
checkNightOwl();
}, [unlockAchievement]);
// Time Traveller Check
useEffect(() => {
if (new Date().getFullYear() < 2000) {
unlockAchievement('time_traveller_system');
}
}, [unlockAchievement]);
// Konami Code Listener
useEffect(() => {
// Konami Code Sequence: Up, Up, Down, Down, Left, Right, Left, Right, B, A
const konamiCode = [
'ArrowUp',
'ArrowUp',
'ArrowDown',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'ArrowLeft',
'ArrowRight',
'b',
'a',
];
const handleKeyDown = (e) => {
// Check if the key matches the current step in the sequence
if (e.key === konamiCode[konamiIndex]) {
const nextIndex = konamiIndex + 1;
// If the sequence is complete
if (nextIndex === konamiCode.length) {
unlockAchievement('konami_code');
setKonamiIndex(0); // Reset
} else {
setKonamiIndex(nextIndex); // Advance
}
} else {
setKonamiIndex(0); // Mistake, reset
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [konamiIndex, unlockAchievement]);
// Cheater Code Listener
useEffect(() => {
const cheaterCode = ['c', 'h', 'e', 'a', 't', 'e', 'r'];
const handleKeyDown = (e) => {
// Check if the key matches the current step in the sequence (case insensitive)
if (e.key.toLowerCase() === cheaterCode[cheaterIndex]) {
const nextIndex = cheaterIndex + 1;
// If the sequence is complete
if (nextIndex === cheaterCode.length) {
unlockAchievement('cheater');
setCheaterIndex(0); // Reset
} else {
setCheaterIndex(nextIndex); // Advance
}
} else {
setCheaterIndex(0); // Mistake, reset
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [cheaterIndex, unlockAchievement]);
return null; // This component renders nothing
};
export default AchievementListeners;