Tarot readings: AI-written, drawn on the server, sold as units
A new Kártyajóslás page: choose a spread (1, 3 or 5 cards), ask a question, pick the cards from the fanned deck and watch them turn over; the reading is written by the detailed-analysis model from the cards, their positions and base meanings, the question and the reader's Sun, Moon and Ascendant. The server shuffles and draws, so nobody can draw again for free. A reading is a 'tarot' analysis (worker, retries, fallback, refunds and admin regeneration as for analyses), uses one 'tarot' unit, and survives recalculations; deleting the account deletes it. Packages carry 2/5/10 readings, the new +5 kártyajóslás addon costs 1 000 Ft. The card artwork comes from the tarot design studio (celestial astrolabe style); the deck JSON is shared by the frontend and the prompt. A public example is at /kartyajoslas/pelda. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,327 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { api, ApiError, errorText } from "@/lib/api";
|
||||
import { SPREADS, TAROT_QUESTION_MAX, tarotCard, type SpreadId, type TarotReading } from "@/lib/tarot";
|
||||
import type { PayMethod, UnitBalance } from "@/lib/types";
|
||||
|
||||
import { PackageChooser } from "../PackageCards";
|
||||
import { TarotCard, TarotCardBack } from "./TarotCard";
|
||||
import { TarotLive } from "./TarotLive";
|
||||
|
||||
/** Face-down cards in the fan to pick from. */
|
||||
const FAN = 21;
|
||||
/** Pause between two cards turning over. */
|
||||
const REVEAL_MS = 650;
|
||||
|
||||
type Phase = "setup" | "picking" | "drawing" | "revealing" | "done";
|
||||
|
||||
const day = (iso: string) =>
|
||||
new Date(iso).toLocaleDateString("hu-HU", { timeZone: "Europe/Budapest", month: "long", day: "numeric" });
|
||||
|
||||
/**
|
||||
* The tarot table: choose a spread and ask a question, pick the cards from
|
||||
* the shuffled deck, watch them turn over, then read what they say. The
|
||||
* server draws the cards (picking only chooses the moment); each reading
|
||||
* uses one from the user's readings.
|
||||
*/
|
||||
export function TarotTable({
|
||||
readings: initialReadings,
|
||||
left: initialLeft,
|
||||
isAdmin,
|
||||
canPay,
|
||||
}: {
|
||||
readings: TarotReading[];
|
||||
left: number;
|
||||
isAdmin: boolean;
|
||||
canPay: PayMethod | null;
|
||||
}) {
|
||||
const [readings, setReadings] = useState(initialReadings);
|
||||
const [left, setLeft] = useState(initialLeft);
|
||||
const [spread, setSpread] = useState<SpreadId>("trinity");
|
||||
const [question, setQuestion] = useState("");
|
||||
const [phase, setPhase] = useState<Phase>("setup");
|
||||
const [picked, setPicked] = useState<number[]>([]);
|
||||
const [current, setCurrent] = useState<TarotReading | null>(null);
|
||||
const [revealed, setRevealed] = useState(0);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [buying, setBuying] = useState(false);
|
||||
|
||||
const chosen = SPREADS.find((s) => s.id === spread) ?? SPREADS[1];
|
||||
const writing = readings.some((r) => r.status === "pending" || r.status === "running");
|
||||
|
||||
// The cards turn over one by one.
|
||||
useEffect(() => {
|
||||
if (phase !== "revealing" || !current) return;
|
||||
if (revealed >= current.cards.length) {
|
||||
const t = setTimeout(() => setPhase("done"), REVEAL_MS);
|
||||
return () => clearTimeout(t);
|
||||
}
|
||||
const t = setTimeout(() => setRevealed((n) => n + 1), revealed === 0 ? 350 : REVEAL_MS);
|
||||
return () => clearTimeout(t);
|
||||
}, [phase, revealed, current]);
|
||||
|
||||
const draw = useCallback(
|
||||
async () => {
|
||||
setPhase("drawing");
|
||||
setError(null);
|
||||
try {
|
||||
const reading = await api<TarotReading>("/tarot/readings", {
|
||||
body: { spread, question: question.trim() || null },
|
||||
});
|
||||
setCurrent(reading);
|
||||
setReadings((all) => [reading, ...all]);
|
||||
setLeft((n) => n - reading.credits_charged);
|
||||
setRevealed(0);
|
||||
setPhase("revealing");
|
||||
} catch (err) {
|
||||
setPhase("setup");
|
||||
setPicked([]);
|
||||
if (err instanceof ApiError && err.status === 402) {
|
||||
setLeft(0);
|
||||
setBuying(true);
|
||||
} else {
|
||||
setError(errorText(err));
|
||||
}
|
||||
}
|
||||
},
|
||||
[spread, question],
|
||||
);
|
||||
|
||||
function pick(i: number) {
|
||||
if (phase !== "picking" || picked.includes(i) || picked.length >= chosen.cards) return;
|
||||
const next = [...picked, i];
|
||||
setPicked(next);
|
||||
if (next.length === chosen.cards) void draw();
|
||||
}
|
||||
|
||||
function start(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
if (left < 1) {
|
||||
setBuying(true);
|
||||
return;
|
||||
}
|
||||
setPicked([]);
|
||||
setCurrent(null);
|
||||
setPhase("picking");
|
||||
}
|
||||
|
||||
function again() {
|
||||
setPhase("setup");
|
||||
setPicked([]);
|
||||
setCurrent(null);
|
||||
setQuestion("");
|
||||
}
|
||||
|
||||
const finished = useCallback((r: TarotReading) => {
|
||||
setReadings((all) => all.map((x) => (x.id === r.id ? r : x)));
|
||||
if (r.status === "failed" && r.credits_charged > 0) setLeft((n) => n + r.credits_charged);
|
||||
}, []);
|
||||
|
||||
async function bought() {
|
||||
try {
|
||||
const me = await api<{ analyses_left: UnitBalance }>("/me");
|
||||
setLeft(me.analyses_left.tarot);
|
||||
} catch {
|
||||
setLeft((n) => n + 5);
|
||||
}
|
||||
setBuying(false);
|
||||
}
|
||||
|
||||
const slots = current?.cards.length ?? chosen.cards;
|
||||
const earlier = readings.filter((r) => r.id !== current?.id);
|
||||
|
||||
return (
|
||||
<div className="tarot">
|
||||
{phase === "setup" && (
|
||||
<section className="card" aria-labelledby="tarot-setup-title">
|
||||
<h2 id="tarot-setup-title">Új jóslás</h2>
|
||||
{buying ? (
|
||||
<div className="field">
|
||||
<p>
|
||||
Elfogytak a jóslásaid. A csomagokban is van néhány, vagy vegyél ötöt egyben, és a saját kérdéseidre húzhatsz
|
||||
újra.
|
||||
</p>
|
||||
<PackageChooser ids={["addon_tarot", "single", "couple"]} canPay={canPay} onBought={bought} />
|
||||
<p className="hint">
|
||||
Kíváncsi vagy, milyen egy jóslás? <Link href="/kartyajoslas/pelda">Nézd meg a példát →</Link>
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<form className="field" style={{ gap: 18 }} onSubmit={start}>
|
||||
<fieldset className="tarot-spreads">
|
||||
<legend className="label">Kirakás</legend>
|
||||
{SPREADS.map((s) => (
|
||||
<label key={s.id} className={s.id === spread ? "tarot-spread is-active" : "tarot-spread"}>
|
||||
<input
|
||||
type="radio"
|
||||
name="spread"
|
||||
value={s.id}
|
||||
checked={s.id === spread}
|
||||
onChange={() => setSpread(s.id)}
|
||||
className="sr-only"
|
||||
/>
|
||||
<span className="tarot-spread-name">{s.name}</span>
|
||||
<span className="hint">{s.about}</span>
|
||||
</label>
|
||||
))}
|
||||
</fieldset>
|
||||
<div className="field">
|
||||
<label htmlFor="tarot-question">Kérdésed (nem kötelező)</label>
|
||||
<textarea
|
||||
id="tarot-question"
|
||||
className="inp"
|
||||
rows={2}
|
||||
maxLength={TAROT_QUESTION_MAX}
|
||||
value={question}
|
||||
placeholder="Pl. Mire figyeljek, ha munkahelyet váltanék? Kérdés nélkül általános útmutatást kapsz."
|
||||
onChange={(e) => setQuestion(e.target.value)}
|
||||
/>
|
||||
<span className="hint">
|
||||
Nyitott kérdés működik a legjobban („mire figyeljek”, „mit tanulhatok”), nem igen–nem kérdés.
|
||||
</span>
|
||||
</div>
|
||||
<div className="form-actions">
|
||||
<button type="submit" className="btn btn-primary" disabled={writing}>
|
||||
Keverés és húzás
|
||||
</button>
|
||||
<span className="hint">
|
||||
{writing
|
||||
? "Várd meg, amíg az előző jóslásod elkészül."
|
||||
: left > 0
|
||||
? `Még ${left} jóslásod van.`
|
||||
: "Nincs több jóslásod."}
|
||||
</span>
|
||||
</div>
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</form>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
|
||||
{phase !== "setup" && (
|
||||
<section className="card tarot-stage" aria-labelledby="tarot-stage-title">
|
||||
<div className="tarot-stage-head">
|
||||
<span className="eyebrow">{current?.spread_name ?? chosen.name}</span>
|
||||
<h2 id="tarot-stage-title">
|
||||
{phase === "picking"
|
||||
? `Válassz ${chosen.cards} lapot a pakliból`
|
||||
: phase === "drawing"
|
||||
? "A lapok…"
|
||||
: "A kirakásod"}
|
||||
</h2>
|
||||
{question.trim() && <p className="tarot-question">„{question.trim()}”</p>}
|
||||
<p className="hint" role="status">
|
||||
{phase === "picking" &&
|
||||
`Hangolódj a kérdésedre, és válassz ösztönösen. ${picked.length}/${chosen.cards}`}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<ol className="tarot-slots" data-count={slots}>
|
||||
{Array.from({ length: slots }).map((_, i) => {
|
||||
const drawn = current?.cards[i];
|
||||
const card = drawn ? tarotCard(drawn.card) : undefined;
|
||||
const flipped = !!card && i < revealed;
|
||||
return (
|
||||
<li key={i} className="tarot-slot">
|
||||
<div className={flipped ? "tarot-flip is-flipped" : "tarot-flip"}>
|
||||
{i < picked.length || drawn ? (
|
||||
<div className="tarot-flip-inner">
|
||||
<TarotCardBack className="tarot-face tarot-back" />
|
||||
{card && <TarotCard card={card} reversed={drawn?.reversed} className="tarot-face tarot-front" />}
|
||||
</div>
|
||||
) : (
|
||||
<div className="tarot-slot-empty" aria-hidden="true" />
|
||||
)}
|
||||
</div>
|
||||
<span className="tarot-slot-title">{drawn?.position ?? `${i + 1}. lap`}</span>
|
||||
{flipped && card && (
|
||||
<span className="hint">
|
||||
{card.nameHu}
|
||||
{drawn?.reversed ? " · fordított" : ""}
|
||||
</span>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ol>
|
||||
|
||||
{phase === "picking" && (
|
||||
<div className="tarot-fan" role="group" aria-label="A megkevert pakli">
|
||||
{Array.from({ length: FAN }).map((_, i) => (
|
||||
<button
|
||||
key={i}
|
||||
type="button"
|
||||
className={picked.includes(i) ? "tarot-fan-card is-picked" : "tarot-fan-card"}
|
||||
style={{ "--i": i - (FAN - 1) / 2 } as React.CSSProperties}
|
||||
onClick={() => pick(i)}
|
||||
disabled={picked.includes(i)}
|
||||
aria-label={`${i + 1}. lap kiválasztása`}
|
||||
>
|
||||
<TarotCardBack />
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{phase === "picking" && (
|
||||
<div className="form-actions">
|
||||
<button type="button" className="btn btn-ghost" onClick={again}>
|
||||
Mégsem
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{phase === "done" && current && (
|
||||
<>
|
||||
<TarotLive key={current.id} initial={current} isAdmin={isAdmin} onChange={finished} />
|
||||
<div className="form-actions">
|
||||
<button type="button" className="btn" onClick={again}>
|
||||
Új jóslás
|
||||
</button>
|
||||
<Link href={`/kartyajoslas/${current.id}`} className="btn btn-ghost">
|
||||
Külön oldalon
|
||||
</Link>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
|
||||
{earlier.length > 0 && (
|
||||
<section className="card" aria-labelledby="tarot-history-title">
|
||||
<h2 id="tarot-history-title">Korábbi jóslásaim</h2>
|
||||
<ul className="tarot-history">
|
||||
{earlier.map((r) => (
|
||||
<li key={r.id}>
|
||||
<Link href={`/kartyajoslas/${r.id}`} className="tarot-history-row">
|
||||
<span className="tarot-history-cards" aria-hidden="true">
|
||||
{r.cards.slice(0, 5).map((c, i) => {
|
||||
const card = tarotCard(c.card);
|
||||
return card ? <TarotCard key={i} card={card} reversed={c.reversed} className="tarot-card-xs" /> : null;
|
||||
})}
|
||||
</span>
|
||||
<span className="tarot-history-text">
|
||||
<strong>{r.content?.title ?? r.question ?? r.spread_name}</strong>
|
||||
<span className="hint">
|
||||
{day(r.created_at)} · {r.spread_name}
|
||||
{r.question && r.content ? ` · „${r.question}”` : ""}
|
||||
{r.status === "pending" || r.status === "running" ? " · készül" : ""}
|
||||
{r.status === "failed" ? " · nem sikerült" : ""}
|
||||
</span>
|
||||
</span>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user