/** * 핫스트릭 경주로 (docs/games/hot-streak.md §9): PC는 가로(왼쪽 13번 칸 → 오른쪽 결승선·시상대), 휴대폰은 세로(위가 결승선). * The view already has the result of every card; `useReplay` draws the race record a beat behind, card by card: * the card turns over, the mascot hops space by space, then bumps / falls / 실격 / 결승 show up. */ import { useEffect, useRef, useState, type ReactElement } from 'react'; import type { HsRaceView, RaceLine, Racer } from '@bg/games/hot-streak'; import { MASCOTS, MASCOT_IDS, josa, type MascotId } from '@bg/games/hot-streak/cards'; import { noise, sfx, tone } from '../../lib/feedback'; import { fxMs, useMotionHold } from '../../lib/motion'; import { MascotFace } from './art'; export const reducedMotion = () => typeof matchMedia !== 'undefined' && matchMedia('(prefers-reduced-motion: reduce)').matches; // ---------------------------------------------------------------- geometry export interface Geo { vertical: boolean; w: number; h: number; /** Cell box of (lane, space). */ cell(lane: number, pos: number): { x: number; y: number; w: number; h: number }; /** Podium slot box in the finish zone (1~4). */ slot(place: number): { x: number; y: number; w: number; h: number }; tok: number; /** Line between space p+1 and p (fold lines): from / to points. */ between(p: number): [number, number, number, number]; } const H_LM = 30; const H_CW = 62; const H_LH = 58; const H_TM = 24; const H_FW = 118; export const WIDE: Geo = { vertical: false, w: H_LM + 13 * H_CW + H_FW, h: H_TM + 4 * H_LH + 6, cell: (lane, pos) => ({ x: H_LM + (13 - pos) * H_CW, y: H_TM + lane * H_LH, w: H_CW, h: H_LH }), slot: (place) => ({ x: H_LM + 13 * H_CW, y: H_TM + (place - 1) * H_LH, w: H_FW, h: H_LH }), tok: 46, between: (p) => { const x = H_LM + (13 - p) * H_CW; return [x, H_TM, x, H_TM + 4 * H_LH]; }, }; const V_LM = 4; const V_LW = 76; const V_CH = 34; const V_FH = 66; export const TALL: Geo = { vertical: true, w: V_LM * 2 + 4 * V_LW, h: V_FH + 13 * V_CH + 6, cell: (lane, pos) => ({ x: V_LM + lane * V_LW, y: V_FH + (pos - 1) * V_CH, w: V_LW, h: V_CH }), slot: (place) => ({ x: V_LM + (place - 1) * V_LW, y: 0, w: V_LW, h: V_FH - 4 }), tok: 30, between: (p) => { const y = V_FH + p * V_CH; return [V_LM, y, V_LM + 4 * V_LW, y]; }, }; // ---------------------------------------------------------------- replay export interface Shown { racers: Racer[]; trackEnd: number; /** Line on screen (card, text). */ line: RaceLine | null; /** Index of the hop in the current walk (forces a re-render). */ hop: number; } interface Float { k: number; id: MascotId; text: string; cls: string; lane: number; pos: number; } const START: Racer[] = MASCOT_IDS.map((id) => ({ id, lane: MASCOTS[id].lane, pos: 11, facing: 'fwd', down: false, status: 'racing' })); const raceSound = { hop: (i: number) => tone(i % 2 ? 640 : 560, 35, 'triangle', 0.06), bump: () => { noise(110, 0.18, 0, 'lowpass', 600); tone(140, 160, 'square', 0.05); }, down: () => tone(320, 220, 'sawtooth', 0.05, 0, { slideTo: 120 }), dq: () => [520, 390, 260].forEach((f, i) => tone(f, 120, 'square', 0.05, i * 90)), finish: () => [784, 988, 1318].forEach((f, i) => tone(f, 130, 'triangle', 0.1, i * 80)), fold: () => { noise(380, 0.12, 0, 'lowpass', 500); tone(110, 300, 'sine', 0.08); }, }; /** * Plays new race lines one after the other. A race already running when the board opens is shown as it is. When the * screen falls behind (빨리 보기, a slow phone) the next lines play faster. `busy` is true while lines are still queued. */ export function useReplay(r: HsRaceView | null) { const [shown, setShown] = useState(() => ({ racers: r ? r.log.at(-1)?.racers ?? r.racers : START, trackEnd: r?.trackEnd ?? 13, line: r?.log.at(-1) ?? null, hop: 0 })); const [floats, setFloats] = useState([]); const [busy, setBusy] = useState(false); const q = useRef([]); const st = useRef({ race: r?.no ?? 0, last: r?.log.at(-1)?.n ?? 0, playing: false, k: 0, dead: false }); const timers = useRef(new Set>()); const later = (fn: () => void, ms: number) => { const t = setTimeout(() => { timers.current.delete(t); if (!st.current.dead) fn(); }, ms); timers.current.add(t); }; useEffect(() => { const s = st.current; s.dead = false; return () => { s.dead = true; for (const t of timers.current) clearTimeout(t); timers.current.clear(); }; }, []); const pump = () => { const s = st.current; const line = q.current.shift(); if (!line) { s.playing = false; setBusy(false); return; } s.playing = true; setBusy(true); const behind = q.current.length; const speed = behind > 4 ? 0.25 : behind > 1 ? 0.5 : 1; const T = (ms: number) => Math.max(10, Math.round(fxMs(ms) * speed)); const finishLine = () => { setShown({ racers: line.racers, trackEnd: line.trackEnd, line, hop: 0 }); for (const f of line.fx ?? []) { const x = line.racers.find((y) => y.id === f.id)!; const text = { bump: '쾅!', down: '쿵!', dq: '실격!', finish: `${f.place ?? ''}등!`, turn: '↩', up: '벌떡!' }[f.t]; const k = ++s.k; setFloats((fl) => [...fl.slice(-6), { k, id: f.id, text, cls: f.t, lane: x.lane, pos: x.pos }]); later(() => setFloats((fl) => fl.filter((z) => z.k !== k)), fxMs(1300)); } const t = new Set((line.fx ?? []).map((f) => f.t)); if (t.has('dq')) raceSound.dq(); else if (t.has('finish')) raceSound.finish(); else if (t.has('bump')) raceSound.bump(); else if (t.has('down')) raceSound.down(); if (line.kind === 'fold') raceSound.fold(); later(pump, T(line.kind === 'flip' ? 260 : 380)); }; if (reducedMotion()) { setShown({ racers: line.racers, trackEnd: line.trackEnd, line, hop: 0 }); later(pump, behind ? 30 : 200); return; } if (line.kind === 'flip') sfx.cardFlip(); else if (line.kind === 'shuffle' || line.kind === 'start') sfx.shuffle(); const moves = line.mv ?? []; const steps = Math.max(0, ...moves.map((m) => m.path.length - 1)); // The card shows first, then the walk. setShown((sh) => ({ ...sh, line })); const hopMs = T(150); const lead = T(line.kind === 'flip' ? 320 : 120); for (let i = 1; i <= steps; i++) { later(() => { raceSound.hop(i); setShown((sh) => ({ ...sh, hop: i, racers: sh.racers.map((x) => { const m = moves.find((mv) => mv.id === x.id); if (!m) return x; const [lane, pos] = m.path[Math.min(i, m.path.length - 1)]!; return { ...x, lane, pos }; }), })); }, lead + (i - 1) * hopMs); } later(finishLine, lead + steps * hopMs + (steps ? T(60) : 0)); }; useEffect(() => { const s = st.current; if (!r) { if (s.race !== 0) { s.race = 0; s.last = 0; q.current = []; setShown({ racers: START, trackEnd: 13, line: null, hop: 0 }); } return; } if (r.no !== s.race) { s.race = r.no; s.last = 0; q.current = []; } const fresh = r.log.filter((l) => l.n > s.last); if (!fresh.length) return; s.last = fresh.at(-1)!.n; q.current.push(...fresh); if (!s.playing) pump(); }, [r?.no, r?.log.at(-1)?.n]); // eslint-disable-line react-hooks/exhaustive-deps // The lines play with timers (gaps between hops are not animations): results wait until the last line has played. useMotionHold(busy); return { shown, floats, busy }; } // ---------------------------------------------------------------- drawing const tokenLabel = (x: Racer) => `${MASCOTS[x.id].name}: ${x.status === 'finished' ? '결승' : x.status === 'dq' ? '실격' : `레인 ${x.lane + 1}, ${x.pos}번 칸${x.down ? ', 넘어짐' : ''}${x.facing === 'back' ? ', 뒤돌아 있음' : ''}`}`; /** Where each mascot stands now (finished / DQ ones on their podium slot). */ function placeOf(g: Geo, x: Racer, all: Racer[], slots: Map, ended: boolean): { cx: number; cy: number; onPodium: boolean } { const slot = slots.get(x.id); if (slot && (x.status !== 'racing' || ended)) { const b = g.slot(slot); return { cx: b.x + (g.vertical ? b.w / 2 : 30), cy: b.y + b.h / 2 + (g.vertical ? 6 : 0), onPodium: true }; } const lane = Math.max(-0.6, Math.min(3.6, x.lane)); const pos = Math.max(0.4, Math.min(13.6, x.pos)); const base = g.vertical ? { x: V_LM + lane * V_LW + V_LW / 2, y: V_FH + (pos - 1) * V_CH + V_CH / 2 } : { x: H_LM + (13 - pos) * H_CW + H_CW / 2, y: H_TM + lane * H_LH + H_LH / 2 }; // Two or more on one space: spread them a little. const mates = all.filter((y) => y.status === 'racing' && y.lane === x.lane && y.pos === x.pos); const i = mates.indexOf(x); const off = mates.length > 1 ? (i - (mates.length - 1) / 2) * (g.vertical ? 14 : 16) : 0; return { cx: base.x + (g.vertical ? off : 0), cy: base.y + (g.vertical ? 0 : off), onPodium: false }; } export function Track({ g, shown, floats, final, stars, hurdles, label, }: { g: Geo; shown: Shown; floats: Float[]; /** The race's places as the view has them (podium slots never change once taken). */ final: { top: MascotId[]; bottom: MascotId[][]; over: boolean } | null; stars: number[]; hurdles: number[]; label: string; }) { const { racers, trackEnd } = shown; const slots = new Map(); if (final) { final.top.forEach((m, i) => slots.set(m, i + 1)); let low = 4; for (const grp of final.bottom) { for (let j = grp.length - 1; j >= 0; j--) slots.set(grp[j]!, low--); } } const ended = shown.line?.kind === 'end'; const cells: ReactElement[] = []; for (let lane = 0; lane < 4; lane++) { for (let pos = 1; pos <= 13; pos++) { const b = g.cell(lane, pos); const stretch = pos <= 3; cells.push( , ); if (stars.includes(pos)) cells.push( ★ , ); if (hurdles.includes(pos)) cells.push( , ); } } // Space numbers (once per row/column). const nums: ReactElement[] = []; for (let pos = 1; pos <= 13; pos++) { const b = g.cell(0, pos); if (g.vertical) continue; nums.push( {pos} , ); } const finish = g.slot(1); const zone = g.vertical ? { x: V_LM, y: 0, w: 4 * V_LW, h: V_FH - 4 } : { x: finish.x, y: H_TM, w: H_FW, h: 4 * H_LH }; const folded = 13 - trackEnd; const foldBox = folded <= 0 ? null : g.vertical ? { x: V_LM, y: V_FH + trackEnd * V_CH, w: 4 * V_LW, h: folded * V_CH } : { x: H_LM, y: H_TM, w: folded * H_CW, h: 4 * H_LH }; return ( {/* lane colour stripes */} {MASCOT_IDS.map((m, lane) => { const a = g.cell(lane, 13); const b = g.cell(lane, 1); return g.vertical ? ( ) : ( ); })} {cells} {nums} {!g.vertical && MASCOT_IDS.map((m, lane) => { const b = g.cell(lane, 13); return ( {lane + 1} ); })} {[3, 7, 10].map((p) => { const [x1, y1, x2, y2] = g.between(p); return ; })} {[1, 2, 3, 4].map((pl) => { const b = g.slot(pl); return ( {pl}등 ); })} {/* finish line */} {g.vertical ? ( ) : ( )} {foldBox && } {racers.map((x) => { const { cx, cy, onPodium } = placeOf(g, x, racers, slots, ended); const s = g.tok; const dq = x.status === 'dq'; const turn = x.facing === 'back'; return ( {turn && !onPodium && !dq && ( ↩ )} {dq && ( 실격 )} ); })} {floats.map((f) => { const x = racers.find((y) => y.id === f.id); if (!x) return null; const { cx, cy } = placeOf(g, { ...x, lane: f.lane, pos: f.pos }, racers, slots, ended); return ( {f.text} ); })} ); } /** One sentence for screen readers about a race line. */ export function lineSpeech(l: RaceLine): string { return [l.text, ...(l.notes ?? [])].join(' '); } export const nameGa = (id: MascotId) => josa(MASCOTS[id].name, '이', '가');