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"use client";import { useRef, useState } from "react";import { questions, rankFor } from "@/data/trivia";import { play } from "@/lib/sound";import { useConsoleInput, useStep } from "../input";import { HowTo, useHowTo } from "../HowTo"; type Phase = "ask" | "feedback" | "end"; const T = { en: { score: "Score", streak: "Streak", pick: "↑↓ choose · Enter / click: confirm", next: "Enter: next", right: "Right!", wrong: "Not quite.", again: "Enter: try again", rank: "Rank", of: "of" }, es: { score: "Puntos", streak: "Racha", pick: "↑↓ elegir · Enter / clic: confirmar", next: "Enter: siguiente", right: "¡Correcto!", wrong: "Incorrecto.", again: "Enter: otra vez", rank: "Rango", of: "de" },}; export function BookDemo({ locale }: { locale: "en" | "es" }) { const t = T[locale]; const step = useStep(); const how = useHowTo("book"); const [n, setN] = useState(0); const [sel, setSel] = useState(0); const [phase, setPhase] = useState<Phase>("ask"); const [score, setScore] = useState(0); const [streak, setStreak] = useState(0); const [best, setBest] = useState(0); const [ok, setOk] = useState(false); const live = useRef({ n, sel, phase, streak, score }); live.current = { n, sel, phase, streak, score }; const answer = (choice: number) => { const c = live.current; if (c.phase !== "ask") return; step("answer"); const right = choice === questions[c.n].answer; setSel(choice); setOk(right); setPhase("feedback"); if (right) { play("confirm"); setScore(c.score + 1); setStreak(c.streak + 1); setBest(Math.max(best, c.streak + 1)); } else { play("back"); setStreak(0); } }; useConsoleInput((a) => { if (how.openRef.current) { if (a === "a" || a === "b") how.hide(); return; } const c = live.current; if (c.phase === "ask") { if (a === "up" || a === "down") { play("blip"); setSel((s) => (s + (a === "down" ? 1 : 2)) % 3); } else if (a === "a") { answer(c.sel); } } else if (c.phase === "feedback" && a === "a") { if (c.n + 1 >= questions.length) { step("next"); play("epic"); setPhase("end"); } else { step("render"); play("flip"); setN(c.n + 1); setSel(0); setPhase("ask"); } } else if (c.phase === "end" && a === "a") { play("boot"); setN(0); setSel(0); setScore(0); setStreak(0); setBest(0); setPhase("ask"); } }); if (phase === "end") { return ( <div className="dm dm-book"> <p className="bk-title">{t.rank}</p> <p className="bk-rank">{rankFor(score, questions.length, locale)}</p> <p className="dm-note" aria-live="polite">{score} {t.of} {questions.length} · {t.streak} {best}</p> <p className="dm-hint">{t.again}</p> </div> ); } const q = questions[n]; return ( <div className="dm dm-book"> <div className="bk-bar"> <span>{n + 1}/{questions.length}</span> <span>{t.score} {score}</span> <span className={streak > 1 ? "hot" : undefined}>{t.streak} {streak}</span> </div> <pre className="bk-code"><code>{q.code}</code></pre> <p className="bk-q">{q.q[locale]}</p> <ul className="bk-opts"> {q.options.map((o, i) => { const state = phase === "feedback" ? (i === q.answer ? "right" : i === sel ? "wrong" : "") : ""; return <li key={i} className={`${i === sel ? "sel" : ""} ${state}`.trim()}><button type="button" onClick={(e) => { e.currentTarget.blur(); answer(i); }} disabled={phase !== "ask"}><span aria-hidden="true">{i === sel ? "▸" : " "}</span> {o[locale]}</button></li>; })} </ul> <p className="dm-note bk-fb" aria-live="polite">{phase === "feedback" ? <><b>{ok ? t.right : t.wrong}</b> {q.why[locale]}</> : ""}</p> <p className="dm-hint">{phase === "ask" ? t.pick : t.next}</p> <HowTo id="book" locale={locale} how={how} /> </div> );}
Recreación simplificada hecha para este sitio, no es el código del proyecto.