-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
392 lines (336 loc) · 12.7 KB
/
App.tsx
File metadata and controls
392 lines (336 loc) · 12.7 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useState, useEffect, useCallback, useRef } from "react";
import Board from "./components/Board";
import { generatePuzzle } from "./services/geminiService";
import { calculateClues, checkWinCondition, createEmptyPlayerGrid, getOverflowIndices } from "./utils/gameLogic";
import { PuzzleState, CellState, Grid } from "./types";
import { getPath } from "./utils/path";
import FooterLeftContent from './components/FooterLeftContent';
import useAudio from "./hooks/useAudio";
import InfoDialog from './components/builds/infoDialog.tsx'
const PUZZLE_WORDS = [
"Key", "Sword", "Glasses", "Headphones", "Scissors", "Bird_In_Flight", "Fish_Skeleton", "Tree_Dead",
"Flower_Stem", "Star_Outline", "Crown", "Anchor", "Lightning_Bolt", "Cactus", "Rocket", "Gamepad",
"Phone_Landline", "Microphone", "Wrench", "Chair", "Spider", "Umbrella", "Ladder", "Bicycle",
"Table", "Trophy", "Ribbon", "Fork", "Arrow"
];
const INITIAL_GRID: Grid = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 1, 0, 1, 0],
[1, 0, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
];
const INITIAL_HINTS = [
{ r: 2, c: 1 }, { r: 2, c: 2 }, { r: 2, c: 3 }, {r: 2, c: 5}, {r:2, c: 7},
];
const GAMEPAD_GRID_FALLBACK: Grid = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
];
const CALENDAR_GRID_FALLBACK: Grid = [
[0, 1, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 1],
[1, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
];
const PUZZLE_PATTERNS = [
{ grid: INITIAL_GRID, title: "I/O" },
{ grid: GAMEPAD_GRID_FALLBACK, title: "Gamepad" },
{ grid: CALENDAR_GRID_FALLBACK, title: "Calendar" },
];
const createStartingGrid = (size: number, hints?: { r: number, c: number }[]): Grid => {
const grid = Array(size).fill(null).map(() => Array(size).fill(CellState.EMPTY));
if (hints) {
hints.forEach(({ r, c }) => {
if (r < size && c < size) {
grid[r][c] = CellState.CROSSED;
}
});
}
return grid;
};
const App: React.FC = () => {
const [loading, setLoading] = useState(false);
const [currentPuzzleIndex, setCurrentPuzzleIndex] = useState(0);
const [level, setLevel] = useState(1);
const [showClearBanner, setShowClearBanner] = useState(false);
const [showPuzzleComplete, setShowPuzzleComplete] = useState(false);
const { playForeground } = useAudio();
const [isResetting, setIsResetting] = useState(false);
const [hasOpenedCustomModal, setHasOpenedCustomModal] = useState(false);
const [showCompletionModal, setShowCompletionModal] = useState(false);
const [isWinScreenVisible, setIsWinScreenVisible] = useState(false);
// Prefetched data storage
const prefetchedPuzzles = useRef<Record<number, { grid: Grid; image: string | null; source: string }>>({});
const [gameState, setGameState] = useState<PuzzleState>({
solution: INITIAL_GRID,
playerGrid: createStartingGrid(8, INITIAL_HINTS),
clues: calculateClues(INITIAL_GRID),
isComplete: false,
size: 8,
title: "I/O",
});
useEffect(() => {
setLevel(1);
setCurrentPuzzleIndex(0);
setHasOpenedCustomModal(false);
const firstPattern = PUZZLE_PATTERNS[0];
setGameState({
solution: firstPattern.grid,
playerGrid: createStartingGrid(8, INITIAL_HINTS),
clues: calculateClues(firstPattern.grid),
isComplete: false,
size: 8,
title: firstPattern.title,
});
setIsWinScreenVisible(false);
setShowPuzzleComplete(false);
}, []);
useEffect(() => {
if (!gameState.isComplete) return;
if (level >= 3) {
// start win?
}
const winSequenceTimer = setTimeout(() => {
setShowClearBanner(true);
playForeground(getPath("/media/audio/sfx/global/win.mp3"));
setTimeout(() => {
setShowPuzzleComplete(true);
}, 3300);
setTimeout(() => {
setShowClearBanner(false);
}, 4300);
if (level >= 3) {
setTimeout(() => {
setIsWinScreenVisible(true);
}, 6000);
}
}, 1000);
return () => clearTimeout(winSequenceTimer);
}, [gameState.isComplete, level]);
const getUniqueRandomWords = (count: number) => {
const shuffled = [...PUZZLE_WORDS].sort(() => 0.5 - Math.random());
return shuffled.slice(0, count);
};
// Initial Load and Immediate Background Prefetching
useEffect(() => {
const prefetchSequentially = async () => {
const indicesToFetch = [1, 2];
// 2. Pick 2 unique words at the start of the prefetch process
const randomThemes = getUniqueRandomWords(indicesToFetch.length);
for (let i = 0; i < indicesToFetch.length; i++) {
const index = indicesToFetch[i];
const randomTheme = randomThemes[i]; // Unique word for this level
const pattern = PUZZLE_PATTERNS[index];
try {
const grid = await generatePuzzle(randomTheme, 8);
prefetchedPuzzles.current[index] = {
grid: grid,
source: `✨ GEMINI API (${randomTheme})`,
// @ts-ignore - adding dynamic title to the ref
themeTitle: randomTheme
};
} catch (e) {
console.error(`Gemini prefetch failed for ${randomTheme}, using fallback.`);
prefetchedPuzzles.current[index] = {
grid: pattern.grid,
source: "📁 FALLBACK (API FAILED)",
// @ts-ignore
themeTitle: pattern.title
};
}
}
};
prefetchSequentially();
}, []);
const handleCellClick = useCallback(
(r: number, c: number) => {
if (gameState.isComplete) return;
playForeground(getPath("/media/audio/sfx/nonogram/fillspace.mp3"));
setGameState((prev) => {
const newGrid = prev.playerGrid.map((row) => [...row]);
const currentState = newGrid[r][c];
if (currentState === CellState.CROSSED) return prev;
const nextState = currentState === CellState.FILLED ? CellState.EMPTY : CellState.FILLED;
newGrid[r][c] = nextState;
const isWin = checkWinCondition(newGrid, prev.solution);
return { ...prev, playerGrid: newGrid, isComplete: isWin };
});
},
[gameState.isComplete, playForeground]
);
const handleCellRightClick = useCallback(
(r: number, c: number) => {
if (gameState.isComplete) return;
playForeground(getPath("/media/audio/sfx/nonogram/markx.mp3"));
setGameState((prev) => {
const newGrid = prev.playerGrid.map((row) => [...row]);
const currentState = newGrid[r][c];
if (currentState === CellState.FILLED) return prev;
const nextState = currentState === CellState.CROSSED ? CellState.EMPTY : CellState.CROSSED;
newGrid[r][c] = nextState;
const isWin = checkWinCondition(newGrid, prev.solution);
return { ...prev, playerGrid: newGrid, isComplete: isWin };
});
},
[gameState.isComplete, playForeground]
);
const handleGenerate = async () => {
if (level === PUZZLE_PATTERNS.length) {
setShowPuzzleComplete(false);
setShowClearBanner(false);
setShowCompletionModal(true);
return;
}
setLoading(true);
const nextLevel = level + 1;
const nextIndex = (currentPuzzleIndex + 1) % PUZZLE_PATTERNS.length;
const patternTemplate = PUZZLE_PATTERNS[nextIndex];
const prefetchedData = prefetchedPuzzles.current[nextIndex];
const finalGrid = prefetchedData ? prefetchedData.grid : patternTemplate.grid;
const finalTitle = (prefetchedData as any)?.themeTitle || patternTemplate.title;
setLevel(nextLevel);
setCurrentPuzzleIndex(nextIndex);
setShowPuzzleComplete(false);
setGameState({
solution: finalGrid,
playerGrid: createEmptyPlayerGrid(8),
clues: calculateClues(finalGrid),
isComplete: false,
size: 8,
title: finalTitle,
});
setLoading(false);
};
const { rowOverflows, colOverflows } = React.useMemo(() =>
getOverflowIndices(gameState.playerGrid, gameState.clues),
[gameState.playerGrid, gameState.clues]
);
// Footer
const overrideFooterStyle = `
footer {
position: static;
padding-block: 24px;
width: 100%;
transition: opacity 0.5s ease;
${showClearBanner ? `opacity: 0;` : ``}
@media (min-width: 1280px) {
padding-top: 48px;
padding-bottom: 0;
}
}
main > div {
display: flex;
flex-direction: column;
align-items: center;
gap: 24px;
justify-content: center;
}
`
const handleReset = () => {
setIsResetting(true);
playForeground(getPath("/media/audio/sfx/global/buttonclick.mp3"));
setTimeout(() => {
// Check if we are resetting from the Win Screen (Full Game Reset)
if (isWinScreenVisible) {
setLevel(1);
setCurrentPuzzleIndex(0);
setHasOpenedCustomModal(false);
const firstPattern = PUZZLE_PATTERNS[0];
setGameState({
solution: firstPattern.grid,
playerGrid: createStartingGrid(8, INITIAL_HINTS),
clues: calculateClues(firstPattern.grid),
isComplete: false,
size: 8,
title: firstPattern.title,
});
setIsWinScreenVisible(false); // Hide the win screen
}
// Otherwise, just clear the board for the current level (Soft Reset)
else {
setGameState(prev => ({
...prev,
// If level 1, restore initial hints. Otherwise, create empty grid.
playerGrid: level === 1
? createStartingGrid(8, INITIAL_HINTS)
: createEmptyPlayerGrid(prev.size),
isComplete: false,
}));
}
setShowPuzzleComplete(false);
setIsResetting(false);
}, 500);
};
return (
<div className="flex-1 w-full h-full bg-black flex flex-col text-white justify-center">
<div className="flex justify-center w-full h-auto mt-[0]">
<Board
playerGrid={gameState.playerGrid}
solution={gameState.solution}
clues={gameState.clues}
onCellClick={handleCellClick}
onCellRightClick={handleCellRightClick}
isGameOver={gameState.isComplete}
title={gameState.title}
rowOverflows={rowOverflows}
colOverflows={colOverflows}
showPuzzleComplete={showPuzzleComplete}
showClearBanner={showClearBanner}
isResetting={isResetting}
/>
</div>
<style>
{overrideFooterStyle}
</style>
{isWinScreenVisible && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/1 backdrop-blur-2xl animate-in fade-in duration-500">
<div className="bg-black px-[55px] lg:px-[40px] py-[50px] gap-[40px] rounded-3xl shadow-2xl flex flex-col items-center mx-[55px] rainbow-border w-full max-w-[430px] text-center">
<div>
<h2 className="text-[48px] font-medium text-white tracking-[-1.2px] leading-[1.1]">You won!</h2>
<h3 className="font-medium text-[#9BA0A6] text-[18px] leading-[1.6] tracking-[-0.36px]">You decoded all 3 puzzles</h3>
</div>
<div className="flex flex-col gap-[16px] items-center">
<button onClick={() => { handleReset(); setShowCompletionModal(false); }} className="bg-[#202020] px-[20px] py-[12px] text-white rounded-full font-medium self-center px-[28px]">Play again</button>
</div>
</div>
</div>
)}
<div className={`flex flex-row items-center justify-center gap-[15.5px] mt-[48px] transition-opacity duration-500 ${showClearBanner ? 'opacity-0' : ''}`}>
<FooterLeftContent
levelId={level}
totalLevels={3}
infoText={showPuzzleComplete ? "Success!" : ""}
onReset={handleReset}
/>
{showPuzzleComplete && level <3 && (
<button
className="inline-flex text-black bg-white flex rounded-full justify-center items-center h-[48px] px-[28px] leading-0 enabled-pointer button-white"
onClick={handleGenerate}
>
Next level
</button>
)}
</div>
<InfoDialog title="Complete the nonogram" goal="Fill cells to reveal a hidden image. Numbers show the size and order of solid blocks (e.g., 3 1 = a block of 3, then 1). Blocks must have at least one gap between them. Use X to mark guaranteed empty spaces." onClose={() => {}} />
</div>
);
};
export default App;