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"use client";import { useRef, useState } from "react";import "@/styles/demos/films.css";import { play } from "@/lib/sound";import { ORDER_GOAL, correctOrder, filmById, move, newRound, reveal, score, type Round } from "@/lib/games/films";import { useConsoleInput, useStep, type Action } from "../input";import { HowTo, useHowTo } from "../HowTo"; const T = { en: { goal: "Put 5 films in release order", oldest: "Oldest", newest: "Newest", arrange: "Arranging", inPlace: "in place", hint: "Arrows: choose · Enter / click: pick up or drop a card · last stop: Reveal", hintHeld: "Arrows: move the card · Enter / click: drop it", check: "Reveal the years", again: "Enter · Try again", earlier: "Move earlier", later: "Move later", year: "Year hidden", credit: "Inspired by my Studio Ghibli API university project; film facts only (titles, directors, release years).", verdicts: ["Back to the archive.", "A start.", "Getting warmer.", "Good memory.", "Nearly there.", "Perfect order."], held: "Holding", ok: "right place", no: "wrong place", }, es: { goal: "Pon 5 películas en orden de estreno", oldest: "La más antigua", newest: "La más nueva", arrange: "Ordenando", inPlace: "en su lugar", hint: "Flechas: elegir · Enter / clic: tomar o soltar una carta · última parada: Revelar", hintHeld: "Flechas: mover la carta · Enter / clic: soltarla", check: "Revelar los años", again: "Enter · Otra vez", earlier: "Mover antes", later: "Mover después", year: "Año oculto", credit: "Inspirado en mi proyecto universitario con la API de Studio Ghibli; solo datos de las películas (títulos, directores, años de estreno).", verdicts: ["De vuelta al archivo.", "Un comienzo.", "Vas calentando.", "Buena memoria.", "Casi.", "Orden perfecto."], held: "En la mano", ok: "lugar correcto", no: "lugar equivocado", },} as const; const seed = () => Math.floor(Math.random() * 1_000_000); export function FilmsDemo({ locale }: { locale: "en" | "es" }) { const t = T[locale]; const step = useStep(); const how = useHowTo("films"); const [round, setRound] = useState<Round>(() => newRound(seed())); const [sel, setSel] = useState(0); const [held, setHeld] = useState(false); const [drag, setDrag] = useState<number | null>(null); const live = useRef({ round, sel, held }); live.current = { round, sel, held }; const shift = (i: number, dir: -1 | 1) => { const c = live.current; const next = move(c.round, i, dir); if (next === c.round) return; step("reorder"); setRound(next); setSel(i + dir); play("blip"); }; const dropOn = (from: number, to: number) => { let r = live.current.round; for (let i = from; i !== to; i += to > from ? 1 : -1) r = move(r, i, to > from ? 1 : -1); if (r !== live.current.round) { step("reorder"); setRound(r); play("blip"); } setSel(to); setDrag(null); }; const check = () => { const c = live.current; if (c.round.revealed) return; const next = reveal(c.round); step("reveal"); setRound(next); setHeld(false); play(score(next) === ORDER_GOAL ? "epic" : "confirm"); }; const again = () => { setRound(newRound(seed())); setSel(0); setHeld(false); step("replay"); play("back"); }; useConsoleInput((a: Action) => { if (how.openRef.current) { if (a === "a" || a === "b") how.hide(); return; } const c = live.current; if (c.round.revealed) { if (a === "a") again(); return; } if (a === "b") { check(); return; } if (a === "a") { if (c.sel === ORDER_GOAL) check(); else { setHeld(!c.held); play("blip"); } return; } const dir = a === "left" || a === "up" ? -1 : a === "right" || a === "down" ? 1 : 0; if (!dir) return; if (c.held && c.sel < ORDER_GOAL) shift(c.sel, dir); else setSel(Math.max(0, Math.min(ORDER_GOAL, c.sel + dir))); }); const revealed = round.revealed; const right = correctOrder(round); const s = score(round); return ( <div className="dm dm-films" data-revealed={revealed}> <header className="fm-head"> <b>Film Order</b> <span>{t.goal}</span> <em aria-live="polite">{revealed ? `${s}/${ORDER_GOAL} ${t.inPlace}` : t.arrange}</em> </header> <div className="fm-axis" aria-hidden="true"><span>{t.oldest}</span><i /><span>{t.newest}</span></div> <ol className="fm-row"> {round.order.map((id, i) => { const f = filmById(id); const good = revealed && right[i] === id; return ( <li key={id} className={`fm-card${i === sel ? " sel" : ""}${held && i === sel ? " held" : ""}${revealed ? (good ? " ok" : " no") : ""}${drag === i ? " drag" : ""}`} style={{ "--tilt": `${((i * 37) % 5) - 2}deg` } as React.CSSProperties} draggable={!revealed} onDragStart={(e) => { setDrag(i); setSel(i); e.dataTransfer.effectAllowed = "move"; e.dataTransfer.setData("text/plain", String(i)); }} onDragOver={(e) => { if (drag !== null) e.preventDefault(); }} onDrop={(e) => { e.preventDefault(); if (drag !== null) dropOn(drag, i); }} onDragEnd={() => setDrag(null)} onClick={() => setSel(i)} > <span className="fm-no" aria-hidden="true">{i + 1}</span> <div className="fm-text"> <strong>{f.title}</strong> <span>{f.director}</span> </div> <span className="fm-year" aria-label={revealed ? String(f.year) : t.year}>{revealed ? f.year : "????"}</span> {revealed && <span className="fm-mark" role="img" aria-label={good ? t.ok : t.no}>{good ? "✓" : "✗"}</span>} {!revealed && ( <span className="fm-move"> <button type="button" aria-label={`${t.earlier}: ${f.title}`} disabled={i === 0} onClick={(e) => { e.stopPropagation(); shift(i, -1); }}>‹</button> <button type="button" aria-label={`${t.later}: ${f.title}`} disabled={i === ORDER_GOAL - 1} onClick={(e) => { e.stopPropagation(); shift(i, 1); }}>›</button> </span> )} </li> ); })} </ol> <div className="fm-bar"> {revealed ? ( <> <span className="fm-verdict" role="status">{t.verdicts[s]}</span> <button type="button" className="fm-btn go" onClick={again}>{t.again}</button> </> ) : ( <button type="button" className={`fm-btn go${sel === ORDER_GOAL ? " sel" : ""}`} onClick={check}>{t.check}</button> )} </div> <p className="fm-credit">{t.credit}</p> <p className="dm-hint fm-hint">{revealed ? "" : held ? t.hintHeld : t.hint}</p> <HowTo id="films" locale={locale} how={how} /> </div> );}
Recreación simplificada hecha para este sitio, no es el código del proyecto.