Add the race screen of the daily team
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
08c3c8f6a2
commit
7ed63fff49
17 changed files with 553 additions and 9 deletions
125
website/src/features/cards/RacePanel.jsx
Normal file
125
website/src/features/cards/RacePanel.jsx
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
// Rôle : page de la Course des courants : choix du coureur, course en direct, course interrompue et résultat.
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { finishRace, loadTeam, recordRaceBoosts, startRace } from "./cardsApi.js";
|
||||
import { fillTemplate, formatCountdown } from "./dailyShare.js";
|
||||
import { RaceLive } from "./RaceLive.jsx";
|
||||
import { imageOf, nameOf, racerName, raceRanking } from "./raceRun.js";
|
||||
import { RaceZones } from "./RaceTrack.jsx";
|
||||
import { formatStardust } from "./stardustFormat.js";
|
||||
import { TeamCompanion } from "./TeamCompanion.jsx";
|
||||
|
||||
const formatTime = (milliseconds) => `${(milliseconds / 1_000).toLocaleString("fr-FR", { minimumFractionDigits: 2, maximumFractionDigits: 2 })} s`;
|
||||
const placeText = (place, content) => fillTemplate(place === 1 ? content.placeFirst : content.placeOther, { place });
|
||||
|
||||
function RaceResult({ catalog, race, nextAt, content, teamContent, titleRef }) {
|
||||
const [now] = useState(() => Date.now());
|
||||
const ranking = raceRanking(race);
|
||||
return <section className="nds-panel nebula-race-result" aria-labelledby="nebula-race-result-title" data-place={race.place}>
|
||||
<h3 id="nebula-race-result-title" ref={titleRef} tabIndex={-1}>{placeText(race.place, content)}</h3>
|
||||
<p className="nebula-race-reward">{race.reward > 0 ? fillTemplate(content.earned, { reward: formatStardust(race.reward) }) : content.noReward}</p>
|
||||
<ol className="nebula-race-ranking">{ranking.map((entry, index) => <li key={entry.index} data-player={entry.player || undefined}>
|
||||
<span>{index + 1}</span><img src={imageOf(catalog, entry.runner.cardId)} alt="" />
|
||||
<span><strong>{entry.player ? content.you : racerName(entry, content)}</strong><small>{nameOf(catalog, entry.runner.cardId, teamContent)}</small></span>
|
||||
<span>{formatTime(entry.finishedAt)}</span>
|
||||
</li>)}</ol>
|
||||
<p className="nebula-race-next">{fillTemplate(content.next, { time: formatCountdown(nextAt - now) })}</p>
|
||||
</section>;
|
||||
}
|
||||
|
||||
export function RacePanel({ catalog, runMutation, busy, content, teamContent, statLabels }) {
|
||||
const [team, setTeam] = useState(null);
|
||||
const [loadError, setLoadError] = useState("");
|
||||
const [reloadKey, setReloadKey] = useState(0);
|
||||
const [notice, setNotice] = useState("");
|
||||
// Course lancée sur cette page : elle s'anime, puis laisse la place au résultat.
|
||||
const [live, setLive] = useState(null);
|
||||
const [finished, setFinished] = useState(null);
|
||||
const titleRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
loadTeam()
|
||||
.then((result) => { if (active) { setTeam(result.team); setLoadError(""); } })
|
||||
.catch((error) => { if (active) setLoadError(error.message); });
|
||||
return () => { active = false; };
|
||||
}, [reloadKey]);
|
||||
|
||||
const showResult = !live && team?.race?.status === "finished";
|
||||
const wasLive = useRef(false);
|
||||
useEffect(() => {
|
||||
if (live) wasLive.current = true;
|
||||
else if (wasLive.current && showResult) titleRef.current?.focus();
|
||||
}, [live, showResult]);
|
||||
|
||||
if (loadError) return <section className="nds-empty-state" role="alert"><h2>{content.title}</h2><p>{loadError}</p><button className="nds-button" data-variant="secondary" type="button" onClick={() => setReloadKey((key) => key + 1)}>{teamContent.retry}</button></section>;
|
||||
if (!team) return <section className="nds-empty-state" aria-busy="true"><h2>{content.title}</h2></section>;
|
||||
|
||||
// Les refus propres à la course restent dans ce panneau ; runMutation garde le reste (erreurs réseau, solde).
|
||||
async function play(action) {
|
||||
setNotice("");
|
||||
const result = await runMutation(async () => {
|
||||
try {
|
||||
return await action();
|
||||
} catch (error) {
|
||||
if (/^(RACE|TEAM)_/.test(String(error.code || ""))) return { raceError: error };
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
if (result?.raceError) {
|
||||
setNotice(result.raceError.message);
|
||||
setLive(null);
|
||||
setReloadKey((key) => key + 1);
|
||||
}
|
||||
return result?.team || null;
|
||||
}
|
||||
|
||||
async function start(cardId) {
|
||||
const next = await play(() => startRace(team.day, cardId));
|
||||
if (!next?.race) return;
|
||||
setTeam(next);
|
||||
setFinished(null);
|
||||
setLive(next.race);
|
||||
}
|
||||
|
||||
// L'arrivée du joueur est envoyée aussitôt ; le résultat s'affiche quand tous les coureurs sont arrivés à l'écran.
|
||||
async function finish(boosts) {
|
||||
const next = await play(() => finishRace(boosts));
|
||||
if (next) setFinished(next);
|
||||
return next;
|
||||
}
|
||||
|
||||
async function over() {
|
||||
const next = finished || await finish(null);
|
||||
if (next) setTeam(next);
|
||||
setLive(null);
|
||||
}
|
||||
|
||||
async function finishInterrupted() {
|
||||
const next = await finish(null);
|
||||
if (next) setTeam(next);
|
||||
}
|
||||
|
||||
const { race } = team;
|
||||
const interrupted = !live && race?.status === "running";
|
||||
const choosing = !live && !race && team.status === "ready" && team.enabled;
|
||||
return <section className="nebula-race" aria-labelledby="nebula-race-title">
|
||||
<div className="nds-section-heading nebula-section-heading">
|
||||
<div><p className="eyebrow">{content.eyebrow}</p><h2 id="nebula-race-title">{content.title}</h2></div>
|
||||
<div className="nebula-team-heading-actions"><a className="nds-button" data-size="sm" data-variant="secondary" href="#/cards/team">{content.toTeam}</a><a className="nds-button" data-size="sm" data-variant="secondary" href="#/cards/guild">{content.back}</a></div>
|
||||
</div>
|
||||
{notice && <p className="nds-inline-notice" data-variant="warning" role="status">{notice}</p>}
|
||||
{live && <RaceLive key={live.day} catalog={catalog} race={live} companions={team.companions} content={content} teamContent={teamContent} statLabels={statLabels}
|
||||
onBoost={(boosts) => { recordRaceBoosts(boosts).catch(() => {}); }} onFinish={finish} onOver={over} />}
|
||||
{interrupted && <div className="nds-panel nebula-race-interrupted"><h3>{content.interruptedTitle}</h3><p>{content.interruptedText}</p><button className="nds-button" data-variant="primary" type="button" disabled={busy} onClick={finishInterrupted}>{content.interruptedAction}</button></div>}
|
||||
{showResult && <RaceResult catalog={catalog} race={race} nextAt={team.nextAt} content={content} teamContent={teamContent} titleRef={titleRef} />}
|
||||
{!live && !race && !choosing && <div className="nds-empty-state"><p>{team.enabled ? content.needTeam : teamContent.closed}</p>{team.enabled && <a className="nds-button" data-variant="primary" href="#/cards/team">{content.recruitFirst}</a>}</div>}
|
||||
{choosing && <>
|
||||
<p className="nebula-race-intro">{content.intro}</p>
|
||||
<RaceZones zones={team.zones} statLabels={statLabels} />
|
||||
<section className="nebula-race-choice" aria-labelledby="nebula-race-choice-title">
|
||||
<h3 id="nebula-race-choice-title">{content.chooseTitle}</h3>
|
||||
<ul className="nebula-team-list">{team.companions.map((companion) => <li key={companion.cardId}><TeamCompanion catalog={catalog} companion={companion} content={{ ...teamContent, recruitLabel: content.chooseLabel }} statLabels={statLabels} disabled={busy} actionLabel={content.chooseAction} onSelect={() => start(companion.cardId)} /></li>)}</ul>
|
||||
</section>
|
||||
</>}
|
||||
</section>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue