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"use client";import { useEffect, useRef, useState } from "react";import "@/styles/demos/packs.css";import { play } from "@/lib/sound";import { PACK_CARDS, PACK_SIZE, TOTAL_CARDS, collect, mulberry32, openPack, rarityOf, type PackCard, type Rarity } from "@/lib/games/packs";import { CardArt } from "../CardArt";import { HowTo, useHowTo } from "../HowTo";import { useConsoleInput, useStep } from "../input";import { withBase } from "@/lib/base"; const T = { en: { title: "Card Packs", found: "Cards found", sealed: "A sealed pack", open: "Open the pack", another: "Open another pack", opening: "Opening...", hintSealed: "Click the pack or press Enter to open it", hintOpen: "Enter or click: open another pack · Esc: back", fresh: "New", rarity: { common: "Common", uncommon: "Uncommon", rare: "Rare" } as Record<Rarity, string>, pack: "Pack", cards: "Cards in this pack", done: "You found all 18 creatures!", }, es: { title: "Sobres de cartas", found: "Cartas encontradas", sealed: "Un sobre cerrado", open: "Abrir el sobre", another: "Abrir otro sobre", opening: "Abriendo...", hintSealed: "Haz clic en el sobre o pulsa Enter para abrirlo", hintOpen: "Enter o clic: abrir otro sobre · Esc: volver", fresh: "Nueva", rarity: { common: "Común", uncommon: "Poco común", rare: "Rara" } as Record<Rarity, string>, pack: "Sobre", cards: "Cartas de este sobre", done: "¡Encontraste las 18 criaturas!", },} as const; const byId = new Map(PACK_CARDS.map((c) => [c.id, c]));const seed = () => Math.floor(Math.random() * 2 ** 32);const reduced = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;const TEAR_MS = 780; function Face({ card, locale }: { card: PackCard; locale: "en" | "es" }) { return ( <div className="pk-face"> <div className="pk-window"><CardArt id={card.id} /></div> <span className="pk-name">{card.name[locale]}</span> </div> );} export function PacksDemo({ locale }: { locale: "en" | "es" }) { const t = T[locale]; const step = useStep(); const how = useHowTo("tcg"); const [phase, setPhase] = useState<"sealed" | "tearing" | "open">("sealed"); const [pack, setPack] = useState<string[]>([]); const [fresh, setFresh] = useState<string[]>([]); const [owned, setOwned] = useState<string[]>([]); const [opened, setOpened] = useState(0); const timer = useRef<number | undefined>(undefined); const live = useRef({ phase, owned }); live.current = { phase, owned }; useEffect(() => () => window.clearTimeout(timer.current), []); const openIt = () => { const c = live.current; if (c.phase === "tearing") return; step("open"); play("flip"); setPhase("tearing"); window.clearTimeout(timer.current); timer.current = window.setTimeout(() => { const cards = openPack(mulberry32(seed())); const next = collect(live.current.owned, cards); step("collect"); setFresh(cards.filter((id) => !live.current.owned.includes(id))); setOwned(next); setPack(cards); setOpened((n) => n + 1); setPhase("open"); play(cards.some((id) => rarityOf(id) === "rare") ? "epic" : "confirm"); }, reduced() ? 0 : TEAR_MS); }; useConsoleInput((a) => { if (how.openRef.current) { if (a === "a" || a === "b") how.hide(); return; } if (a === "a") openIt(); }); const best = pack.length ? byId.get(pack[pack.length - 1])! : null; const complete = owned.length >= TOTAL_CARDS; return ( <div className="dm dm-pk" data-phase={phase}> <header className="pk-bar"> <b className="pk-title">{t.title}</b> <span className="pk-count" aria-live="polite"><i aria-hidden="true" />{t.found}: <strong>{owned.length}/{TOTAL_CARDS}</strong></span> </header> <div className="pk-stage"> {phase !== "open" ? ( <button type="button" className="pk-pack" data-tear={phase === "tearing"} onClick={openIt} aria-label={phase === "tearing" ? t.opening : t.open} disabled={phase === "tearing"}> <span className="pk-burst" aria-hidden="true" /> <span className="pk-body"> <picture> <source srcSet={withBase("/art/v3/games/card-back.avif")} type="image/avif" /> {} <img src={withBase("/art/v3/games/card-back.webp")} alt="" width={492} height={762} decoding="async" draggable={false} /> </picture> <span className="pk-foil" aria-hidden="true" /> <span className="pk-label" aria-hidden="true"><b>{t.title}</b><small>{PACK_SIZE}</small></span> </span> <span className="pk-top" aria-hidden="true" /> </button> ) : ( <ul className="pk-fan" aria-label={t.cards}> {pack.map((id, i) => { const card = byId.get(id)!; const r = rarityOf(id); return ( <li key={`${opened}-${id}`} className="pk-card" data-r={r} style={{ "--i": i, "--n": pack.length } as React.CSSProperties}> <Face card={card} locale={locale} /> <span className="pk-rar">{t.rarity[r]}</span> {fresh.includes(id) && <span className="pk-new">{t.fresh}</span>} </li> ); })} </ul> )} </div> <div className="pk-foot"> {phase === "open" ? ( <> <p className="pk-line" aria-live="polite">{complete ? t.done : best ? <em>{best.name[locale]}: {best.line[locale]}</em> : null}</p> <button type="button" className="pk-btn" onClick={openIt} autoFocus={false}>{t.another}</button> </> ) : ( <> <p className="pk-line">{phase === "tearing" ? t.opening : t.sealed}</p> <button type="button" className="pk-btn" onClick={openIt} disabled={phase === "tearing"}>{t.open}</button> </> )} </div> <p className="dm-hint pk-hint">{phase === "open" ? t.hintOpen : t.hintSealed}</p> <HowTo id="tcg" locale={locale} how={how} /> </div> );}
Recreación simplificada hecha para este sitio, no es el código del proyecto.