-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
232 lines (212 loc) · 8.21 KB
/
App.tsx
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { InformationCircleIcon } from "@heroicons/react/outline";
import { useState, useEffect, useRef } from "react";
import { Alert } from "./components/alerts/Alert";
import { Grid } from "./components/grid/Grid";
import { TimeLeft } from './components/grid/TimeLeft';
import { Keyboard } from "./components/keyboard/Keyboard";
import { AboutModal } from "./components/modals/AboutModal";
import { InfoModal } from "./components/modals/InfoModal";
import { WinModal } from "./components/modals/WinModal";
import { clearGameState, getGameState, storeGameState } from './lib/gameState';
import { isWordInWordList, isWinningWord, getDay } from "./lib/words";
function App() {
const [backgroundColor, setBackgroundColor] = useState("");
const existingGameState = getGameState();
const [guesses, setGuesses] = useState<string[]>(existingGameState?.guesses || []);
const [currentGuess, setCurrentGuess] = useState(existingGameState?.currentGuess || "");
const [guessTimeStartedAt, setGuessTimeStartedAt] = useState(null as number|null);
const [isGameWon, setIsGameWon] = useState(existingGameState?.isGameWon || false);
const [isWinModalOpen, setIsWinModalOpen] = useState(false);
const [isInfoModalOpen, setIsInfoModalOpen] = useState(false);
const [isAboutModalOpen, setIsAboutModalOpen] = useState(false);
const [isWordNotFoundAlertOpen, setIsWordNotFoundAlertOpen] = useState(false);
const [isGameLost, setIsGameLost] = useState(existingGameState?.isGameLost || false);
const [gameStartedAt, setGameStartedAt] = useState(existingGameState?.gameStartedAt || null);
const maxGuessCount = 12;
const guessTimeLimitMs = 8000;
const timeLimitMs = maxGuessCount * guessTimeLimitMs;
const acceptedWordLength = 5;
useEffect(() => {
if (isGameWon || isGameLost) {
setIsWinModalOpen(true);
}
}, [isGameWon, isGameLost]);
const guessesRef = useRef([] as string[]);
const currentGuessRef = useRef('');
const isGameWonRef = useRef(false);
const guessTimeStartedAtRef = useRef<number|null>(null);
useEffect(() => {
guessesRef.current = guesses;
}, [guesses]);
useEffect(() => {
currentGuessRef.current = currentGuess;
}, [currentGuess]);
useEffect(() => {
isGameWonRef.current = isGameWon;
}, [isGameWon]);
useEffect(() => {
guessTimeStartedAtRef.current = guessTimeStartedAt;
}, [guessTimeStartedAt]);
// Add event listeners
const onChar = (value: string) => {
if (currentGuess.length < acceptedWordLength) {
setCurrentGuess(`${currentGuess}${value}`);
}
};
useEffect(() => {
console.log('store game state')
if (gameStartedAt === null) {
return;
}
storeGameState({dayIndex: getDay(), gameStartedAt, isGameWon, isGameLost, guesses, currentGuess})
}, [gameStartedAt, isGameLost, isGameWon, guesses, currentGuess]);
// useEffect(() => {
// if (gameStartedAt === null) {
// return;
// }
// const intervalId = setInterval(() => {
// const updatedAt = Date.now();
// const elapsedTimeMs = updatedAt - gameStartedAt;
// const opaqueTimeLimitMs = timeLimitMs-(timeLimitMs/2);
// // console.log(1, gameStartedAt, updatedAt, elapsedTimeMs)
// const dangerColorOpacity = elapsedTimeMs/opaqueTimeLimitMs;
// setBackgroundColor(`rgba(255, ${255-255*dangerColorOpacity}, ${255-255*dangerColorOpacity}, ${1*dangerColorOpacity})`);
// }, 10);
// return () => clearInterval(intervalId)
// }, [gameStartedAt, timeLimitMs])
const onDelete = () => {
setCurrentGuess(currentGuess.slice(0, -1));
};
let turnSkipTimeout: NodeJS.Timeout;
function submitWord(isTurnSkipped: boolean, currentGuess: string, guesses: string[], isGameWon: boolean) {
if (guesses.length >= maxGuessCount
|| isGameWon
|| currentGuess.length !== acceptedWordLength) {
return
}
if (gameStartedAt === null) {
setGameStartedAt(Date.now());
console.log('game started at', gameStartedAt)
}
if (!isTurnSkipped && !isWordInWordList(currentGuess)) {
setIsWordNotFoundAlertOpen(true);
return setTimeout(() => {
setIsWordNotFoundAlertOpen(false);
}, 2000);
}
if (turnSkipTimeout) {
clearTimeout(turnSkipTimeout)
}
const winningWord = isWinningWord(currentGuess);
setGuesses([...guesses, currentGuess]);
if (!isTurnSkipped || !currentGuess.includes(" ")) {
setCurrentGuess("");
}
if (winningWord) {
return setIsGameWon(true);
}
if (guessesRef.current.length === maxGuessCount) {
return setIsGameLost(true);
}
setGuessTimeStartedAt(Date.now());
turnSkipTimeout = setTimeout(() => {
if (guesses.length + 1 !== guessesRef.current.length) {
return;
}
submitWord(
true,
currentGuessRef.current.padEnd(acceptedWordLength, ' '),
guessesRef.current,
isGameWonRef.current
)
}, guessTimeLimitMs);
};
const isTimerActive = () => guessTimeStartedAt !== null && !isGameWon && !isGameLost;
const onEnter = () => submitWord(false, currentGuessRef.current, guessesRef.current, isGameWonRef.current);
useEffect(() => {
const downHandler = ({ key, altKey, ctrlKey, metaKey }: KeyboardEvent) => {
key = key.toLocaleUpperCase()
if (key === "ENTER") {
return onEnter();
}
if (key === "DELETE" || key === "BACKSPACE") {
return onDelete();
}
if (altKey || ctrlKey || metaKey || !key.match(/^[A-Z]$/)) {
return;
}
onChar(key);
}
window.addEventListener("keydown", downHandler);
return () => {
window.removeEventListener("keydown", downHandler);
};
});
return (
<div className="max-w-7xl mx-auto sm:px-6 lg:px-8" style={{backgroundColor}}>
<Alert message="Word not found" isOpen={isWordNotFoundAlertOpen} />
<div className="flex w-80 mx-auto items-center mb-8">
<h1 className="text-xl grow font-bold">Speedle</h1>
<InformationCircleIcon
className="h-6 w-6 cursor-pointer"
onClick={() => setIsInfoModalOpen(true)}
/>
</div>
<Grid
guesses={guesses}
currentGuess={currentGuess}
maxGuessCount={maxGuessCount}
gameStartedAt={gameStartedAt}
timeLimitMs={timeLimitMs}
/>
{isTimerActive() && <TimeLeft startedAt={guessTimeStartedAt!} timeLimitMs={guessTimeLimitMs} />}
<Keyboard
onChar={onChar}
onDelete={onDelete}
onEnter={onEnter}
guesses={guesses}
/>
<WinModal
isGameWon={isGameWon}
isOpen={isWinModalOpen}
handleClose={() => setIsWinModalOpen(false)}
guesses={guesses}
maxGuessCount={maxGuessCount}
/>
<InfoModal
isOpen={isInfoModalOpen}
handleClose={() => setIsInfoModalOpen(false)}
guessTimeLimitMs={guessTimeLimitMs}
maxGuessCount={maxGuessCount}
/>
<AboutModal
isOpen={isAboutModalOpen}
handleClose={() => setIsAboutModalOpen(false)}
/>
{!isTimerActive() && <div className="flex">
<button
type="button"
className="mx-auto mt-8 flex items-center px-2.5 py-1.5 border border-transparent text-xs font-medium rounded text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
onClick={() => setIsAboutModalOpen(true)}
>
About this game
</button>
{(isGameWon || isGameLost) && <button
type="button"
className="mx-auto mt-8 flex items-center px-2.5 py-1.5 border border-transparent text-xs font-medium rounded text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
onClick={() => setIsWinModalOpen(true)}
>
Show Result
</button>}
<button
type="button"
className="mx-auto mt-8 flex items-center px-2.5 py-1.5 border border-transparent text-xs font-medium rounded text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
onClick={() => clearGameState()}
>
Reset Game
</button>
</div>}
</div>
);
}
export default App;