Spaces:
Sleeping
Sleeping
Commit ·
ba79b8e
1
Parent(s): ff3cdfd
fix(online): PvP對手/合作隊友顯示勇者名,不再用 Google 帳號名
Browse filesclient 連線時把本地勇者名(heroName)隨 CREATE_ROOM/QUICK_MATCH/JOIN 送出;server 以 cleanHeroName 設 conn.name → PvP 沿用 oppName、coop MATCH_FOUND 新增 oppName→CoopScreen 顯示。訪客(未設名)退回「對手」/「隊友」。typecheck 綠、測試 607 全綠、本機端到端驗證對手名=勇者名。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
packages/server/src/coopHub.ts
CHANGED
|
@@ -22,7 +22,7 @@ export type CoopServerMsg =
|
|
| 22 |
| { type: "QUEUED" }
|
| 23 |
| { type: "BOSS_SELECT"; bosses: BossCatalogEntry[]; selections: { A: string | null; B: string | null } } // 選 boss 共識階段
|
| 24 |
| { type: "PICK_CLASS"; youClass: ClassType | null; youConfirmed: boolean; allyClass: ClassType | null; allyConfirmed: boolean } // 選職業:雙方目前選擇+是否已按「確定」鎖定(隊友的選擇即時可見,雙方都確定才繼續)
|
| 25 |
-
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 26 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 27 |
| { type: "DRAFT_WAIT" }
|
| 28 |
| { type: "COOP_PHASE"; view: CoopView; events: GameEvent[]; durationMs: number } // 戰鬥主更新(順序制)
|
|
@@ -37,6 +37,7 @@ export type CoopServerMsg =
|
|
| 37 |
|
| 38 |
export interface CoopConn {
|
| 39 |
playerId: string;
|
|
|
|
| 40 |
send: (m: CoopServerMsg) => void;
|
| 41 |
}
|
| 42 |
|
|
@@ -164,7 +165,7 @@ export class CoopHub {
|
|
| 164 |
/** 房間成立 → 先進「選 boss 共識」階段(房間建立後從選 boss 開始;雙方選同一個才進選職業)。 */
|
| 165 |
private announceAndPick(room: Room): void {
|
| 166 |
(["A", "B"] as Side[]).forEach((side) => {
|
| 167 |
-
room.conns[side]?.send({ type: "MATCH_FOUND", matchId: room.code, you: side });
|
| 168 |
});
|
| 169 |
this.broadcastBossSelect(room);
|
| 170 |
}
|
|
|
|
| 22 |
| { type: "QUEUED" }
|
| 23 |
| { type: "BOSS_SELECT"; bosses: BossCatalogEntry[]; selections: { A: string | null; B: string | null } } // 選 boss 共識階段
|
| 24 |
| { type: "PICK_CLASS"; youClass: ClassType | null; youConfirmed: boolean; allyClass: ClassType | null; allyConfirmed: boolean } // 選職業:雙方目前選擇+是否已按「確定」鎖定(隊友的選擇即時可見,雙方都確定才繼續)
|
| 25 |
+
| { type: "MATCH_FOUND"; matchId: string; you: Side; oppName?: string } // oppName=隊友勇者名(訪客 undefined)
|
| 26 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 27 |
| { type: "DRAFT_WAIT" }
|
| 28 |
| { type: "COOP_PHASE"; view: CoopView; events: GameEvent[]; durationMs: number } // 戰鬥主更新(順序制)
|
|
|
|
| 37 |
|
| 38 |
export interface CoopConn {
|
| 39 |
playerId: string;
|
| 40 |
+
name?: string; // 玩家勇者名(連線時由 client 送來)→ 下發給隊友當「隊友名」
|
| 41 |
send: (m: CoopServerMsg) => void;
|
| 42 |
}
|
| 43 |
|
|
|
|
| 165 |
/** 房間成立 → 先進「選 boss 共識」階段(房間建立後從選 boss 開始;雙方選同一個才進選職業)。 */
|
| 166 |
private announceAndPick(room: Room): void {
|
| 167 |
(["A", "B"] as Side[]).forEach((side) => {
|
| 168 |
+
room.conns[side]?.send({ type: "MATCH_FOUND", matchId: room.code, you: side, oppName: room.conns[OTHER[side]]?.name });
|
| 169 |
});
|
| 170 |
this.broadcastBossSelect(room);
|
| 171 |
}
|
packages/server/src/server.ts
CHANGED
|
@@ -235,6 +235,8 @@ export function buildServer(opts: ServerOptions): FastifyInstance {
|
|
| 235 |
}
|
| 236 |
// 整段以 try/catch 包覆:任一對局運算(引擎 throw、未知卡 id 等)只結束該連線,絕不讓例外逃出回呼崩潰全服。
|
| 237 |
try {
|
|
|
|
|
|
|
| 238 |
switch (msg.type) {
|
| 239 |
case "CREATE_ROOM": hub.createRoom(conn); break;
|
| 240 |
case "QUICK_MATCH": hub.quickMatch(conn); break;
|
|
@@ -293,6 +295,8 @@ export function buildServer(opts: ServerOptions): FastifyInstance {
|
|
| 293 |
let msg: CoopClientMsg;
|
| 294 |
try { msg = JSON.parse(raw.toString()); } catch { return; }
|
| 295 |
try {
|
|
|
|
|
|
|
| 296 |
switch (msg.type) {
|
| 297 |
case "CREATE_ROOM": coopHub.createRoom(conn); break;
|
| 298 |
case "QUICK_MATCH": coopHub.quickMatch(conn); break;
|
|
|
|
| 235 |
}
|
| 236 |
// 整段以 try/catch 包覆:任一對局運算(引擎 throw、未知卡 id 等)只結束該連線,絕不讓例外逃出回呼崩潰全服。
|
| 237 |
try {
|
| 238 |
+
// 勇者名(client 連線時帶;覆寫連線當下從 session 取的顯示名)→ 下發給對手當「對手名」
|
| 239 |
+
{ const raw2 = (msg as { heroName?: unknown }).heroName; if (typeof raw2 === "string" && raw2.trim()) { const hn = cleanHeroName(raw2); if (hn) conn.name = hn; } }
|
| 240 |
switch (msg.type) {
|
| 241 |
case "CREATE_ROOM": hub.createRoom(conn); break;
|
| 242 |
case "QUICK_MATCH": hub.quickMatch(conn); break;
|
|
|
|
| 295 |
let msg: CoopClientMsg;
|
| 296 |
try { msg = JSON.parse(raw.toString()); } catch { return; }
|
| 297 |
try {
|
| 298 |
+
// 勇者名(client 連線時帶)→ 下發給隊友當「隊友名」
|
| 299 |
+
{ const raw2 = (msg as { heroName?: unknown }).heroName; if (typeof raw2 === "string" && raw2.trim()) { const hn = cleanHeroName(raw2); if (hn) conn.name = hn; } }
|
| 300 |
switch (msg.type) {
|
| 301 |
case "CREATE_ROOM": coopHub.createRoom(conn); break;
|
| 302 |
case "QUICK_MATCH": coopHub.quickMatch(conn); break;
|
packages/web/src/App.tsx
CHANGED
|
@@ -849,11 +849,11 @@ export function App() {
|
|
| 849 |
|
| 850 |
if (screen === "pvp") {
|
| 851 |
// 先建房/加入,配對成功後才於房內選職業(PvpScreen 處理 picking 階段)。
|
| 852 |
-
return <PvpScreen autoJoinCode={pvpJoinCode} onExit={() => { setScreen("online"); setPvpJoinCode(""); }} onResult={recordOnline} />;
|
| 853 |
}
|
| 854 |
|
| 855 |
if (screen === "coop") {
|
| 856 |
-
return <CoopScreen autoJoinCode={coopJoinCode} onExit={() => { setScreen("online"); setCoopJoinCode(""); }} onResult={recordOnline} />;
|
| 857 |
}
|
| 858 |
|
| 859 |
if (screen === "profile") {
|
|
|
|
| 849 |
|
| 850 |
if (screen === "pvp") {
|
| 851 |
// 先建房/加入,配對成功後才於房內選職業(PvpScreen 處理 picking 階段)。
|
| 852 |
+
return <PvpScreen autoJoinCode={pvpJoinCode} onExit={() => { setScreen("online"); setPvpJoinCode(""); }} onResult={recordOnline} heroName={heroName} />;
|
| 853 |
}
|
| 854 |
|
| 855 |
if (screen === "coop") {
|
| 856 |
+
return <CoopScreen autoJoinCode={coopJoinCode} onExit={() => { setScreen("online"); setCoopJoinCode(""); }} onResult={recordOnline} heroName={heroName} />;
|
| 857 |
}
|
| 858 |
|
| 859 |
if (screen === "profile") {
|
packages/web/src/CoopScreen.tsx
CHANGED
|
@@ -13,8 +13,8 @@ const FLOAT_LIFE_MS = 1800; // 單一飄字存活=漸淡時長(與 Bar 的 -
|
|
| 13 |
type FloatTarget = "boss" | "you" | "ally";
|
| 14 |
type CoopFloat = { id: number; text: string; target: FloatTarget; cls: string };
|
| 15 |
|
| 16 |
-
export function CoopScreen({ autoJoinCode, onExit, onResult }: { autoJoinCode?: string; onExit: () => void; onResult?: (kind: "pvp" | "pve", enemyName: string, classType: ClassType, cleared: boolean) => void }) {
|
| 17 |
-
const coop = useCoop();
|
| 18 |
const { state } = coop;
|
| 19 |
const [bossConfirmed, setBossConfirmed] = useState(false); // 選定 Boss 後先看資訊,確認才進選職業
|
| 20 |
useEffect(() => { if (state.status !== "picking") setBossConfirmed(false); }, [state.status]);
|
|
@@ -111,7 +111,7 @@ export function CoopScreen({ autoJoinCode, onExit, onResult }: { autoJoinCode?:
|
|
| 111 |
<div className="coop-end-board">
|
| 112 |
<div className="ally-bar"><span className="end-label">{fv.boss.name}</span><Bar value={fv.boss.hp} max={fv.boss.maxHp} label={`HP ${fv.boss.hp}/${fv.boss.maxHp}`} /></div>
|
| 113 |
<div className="ally-bar"><span className="end-label">你({classLabel(fv.you.classType)})</span><Bar value={fv.you.hp} max={fv.you.maxHp + fv.you.tempMaxHp} label={`HP ${fv.you.hp}/${fv.you.maxHp + fv.you.tempMaxHp}`} /></div>
|
| 114 |
-
<div className="ally-bar"><span className="end-label">隊友({classLabel(fv.ally.classType)})</span><Bar value={fv.ally.hp} max={fv.ally.maxHp + fv.ally.tempMaxHp} label={`HP ${fv.ally.hp}/${fv.ally.maxHp + fv.ally.tempMaxHp}`} /></div>
|
| 115 |
</div>
|
| 116 |
)}
|
| 117 |
{state.oppWantsRematch && !state.rematchRequested && <p className="msg">隊友想再來一局</p>}
|
|
@@ -357,7 +357,7 @@ function CoopBattle({ coop }: { coop: ReturnType<typeof useCoop> }) {
|
|
| 357 |
<span className="reveal-amp">+</span>
|
| 358 |
<div className="reveal-side">
|
| 359 |
<img src={`${RES}/characters/class_${v.ally.classType.toLowerCase()}.webp`} alt="" />
|
| 360 |
-
<span>隊友 · {classLabel(v.ally.classType)}</span>
|
| 361 |
</div>
|
| 362 |
</div>
|
| 363 |
<div className="reveal-role">你是{v.order[0] === you.side ? "先手" : "後手"}</div>
|
|
@@ -396,7 +396,7 @@ function CoopBattle({ coop }: { coop: ReturnType<typeof useCoop> }) {
|
|
| 396 |
|
| 397 |
{/* 隊友 */}
|
| 398 |
<div className={`ally-bar${shake.ally ? " shake" : ""}${allyActing ? " acting" : ""}`}>
|
| 399 |
-
隊友({classLabel(v.ally.classType)}){v.ally.ghost ? "👻 鬼魂" : v.ally.down ? "💀 已倒下" : ""}{v.ally.lastStand ? " ⚔️ 絕境反擊" : ""}{!v.ally.connected ? "(離線)" : ""}
|
| 400 |
<Bar value={v.ally.hp} max={v.ally.maxHp + v.ally.tempMaxHp} floats={allyFloats} floatDurMs={FLOAT_LIFE_MS} label={`HP ${v.ally.hp}/${v.ally.maxHp + v.ally.tempMaxHp} ATK ${v.ally.atk}${v.ally.classType === "BARBARIAN" ? "" : ` 🛡 ${v.ally.def}`} 手牌 ${v.ally.handCount}`} />
|
| 401 |
</div>
|
| 402 |
|
|
|
|
| 13 |
type FloatTarget = "boss" | "you" | "ally";
|
| 14 |
type CoopFloat = { id: number; text: string; target: FloatTarget; cls: string };
|
| 15 |
|
| 16 |
+
export function CoopScreen({ autoJoinCode, onExit, onResult, heroName }: { autoJoinCode?: string; onExit: () => void; onResult?: (kind: "pvp" | "pve", enemyName: string, classType: ClassType, cleared: boolean) => void; heroName?: string }) {
|
| 17 |
+
const coop = useCoop(heroName);
|
| 18 |
const { state } = coop;
|
| 19 |
const [bossConfirmed, setBossConfirmed] = useState(false); // 選定 Boss 後先看資訊,確認才進選職業
|
| 20 |
useEffect(() => { if (state.status !== "picking") setBossConfirmed(false); }, [state.status]);
|
|
|
|
| 111 |
<div className="coop-end-board">
|
| 112 |
<div className="ally-bar"><span className="end-label">{fv.boss.name}</span><Bar value={fv.boss.hp} max={fv.boss.maxHp} label={`HP ${fv.boss.hp}/${fv.boss.maxHp}`} /></div>
|
| 113 |
<div className="ally-bar"><span className="end-label">你({classLabel(fv.you.classType)})</span><Bar value={fv.you.hp} max={fv.you.maxHp + fv.you.tempMaxHp} label={`HP ${fv.you.hp}/${fv.you.maxHp + fv.you.tempMaxHp}`} /></div>
|
| 114 |
+
<div className="ally-bar"><span className="end-label">{state.oppName || "隊友"}({classLabel(fv.ally.classType)})</span><Bar value={fv.ally.hp} max={fv.ally.maxHp + fv.ally.tempMaxHp} label={`HP ${fv.ally.hp}/${fv.ally.maxHp + fv.ally.tempMaxHp}`} /></div>
|
| 115 |
</div>
|
| 116 |
)}
|
| 117 |
{state.oppWantsRematch && !state.rematchRequested && <p className="msg">隊友想再來一局</p>}
|
|
|
|
| 357 |
<span className="reveal-amp">+</span>
|
| 358 |
<div className="reveal-side">
|
| 359 |
<img src={`${RES}/characters/class_${v.ally.classType.toLowerCase()}.webp`} alt="" />
|
| 360 |
+
<span>{state.oppName || "隊友"} · {classLabel(v.ally.classType)}</span>
|
| 361 |
</div>
|
| 362 |
</div>
|
| 363 |
<div className="reveal-role">你是{v.order[0] === you.side ? "先手" : "後手"}</div>
|
|
|
|
| 396 |
|
| 397 |
{/* 隊友 */}
|
| 398 |
<div className={`ally-bar${shake.ally ? " shake" : ""}${allyActing ? " acting" : ""}`}>
|
| 399 |
+
{state.oppName || "隊友"}({classLabel(v.ally.classType)}){v.ally.ghost ? "👻 鬼魂" : v.ally.down ? "💀 已倒下" : ""}{v.ally.lastStand ? " ⚔️ 絕境反擊" : ""}{!v.ally.connected ? "(離線)" : ""}
|
| 400 |
<Bar value={v.ally.hp} max={v.ally.maxHp + v.ally.tempMaxHp} floats={allyFloats} floatDurMs={FLOAT_LIFE_MS} label={`HP ${v.ally.hp}/${v.ally.maxHp + v.ally.tempMaxHp} ATK ${v.ally.atk}${v.ally.classType === "BARBARIAN" ? "" : ` 🛡 ${v.ally.def}`} 手牌 ${v.ally.handCount}`} />
|
| 401 |
</div>
|
| 402 |
|
packages/web/src/PvpScreen.tsx
CHANGED
|
@@ -4,8 +4,8 @@ import { usePvp, type Side } from "./pvpClient.js";
|
|
| 4 |
import { isTagLocked, CardView, ClassSelect, PilePanel, PILE_LABEL, usePlayGate, useHandAnimation, Hand, CardPreview, ScryOverlay, DiscardPrompt } from "./App.js";
|
| 5 |
import { Centered, Bar, useCountdown, DraftView, WaitRoom, RoomLobby, PileBar, PlayerStatsRow } from "./onlineUi.js";
|
| 6 |
|
| 7 |
-
export function PvpScreen({ autoJoinCode, onExit, onResult }: { autoJoinCode?: string; onExit: () => void; onResult?: (kind: "pvp" | "pve", enemyName: string, classType: ClassType, cleared: boolean) => void }) {
|
| 8 |
-
const pvp = usePvp();
|
| 9 |
const { state } = pvp;
|
| 10 |
// 一場 PvP 結束(有勝負)→ 記一場戰績(pvp;敵人=對手登入顯示名,訪客退回「對手」;DRAW 不算 cleared)。只記一次。
|
| 11 |
const reportedRef = useRef(false);
|
|
|
|
| 4 |
import { isTagLocked, CardView, ClassSelect, PilePanel, PILE_LABEL, usePlayGate, useHandAnimation, Hand, CardPreview, ScryOverlay, DiscardPrompt } from "./App.js";
|
| 5 |
import { Centered, Bar, useCountdown, DraftView, WaitRoom, RoomLobby, PileBar, PlayerStatsRow } from "./onlineUi.js";
|
| 6 |
|
| 7 |
+
export function PvpScreen({ autoJoinCode, onExit, onResult, heroName }: { autoJoinCode?: string; onExit: () => void; onResult?: (kind: "pvp" | "pve", enemyName: string, classType: ClassType, cleared: boolean) => void; heroName?: string }) {
|
| 8 |
+
const pvp = usePvp(heroName);
|
| 9 |
const { state } = pvp;
|
| 10 |
// 一場 PvP 結束(有勝負)→ 記一場戰績(pvp;敵人=對手登入顯示名,訪客退回「對手」;DRAW 不算 cleared)。只記一次。
|
| 11 |
const reportedRef = useRef(false);
|
packages/web/src/coopClient.ts
CHANGED
|
@@ -40,7 +40,7 @@ type ServerMsg =
|
|
| 40 |
| { type: "QUEUED" }
|
| 41 |
| { type: "BOSS_SELECT"; bosses: BossCatalogEntry[]; selections: { A: string | null; B: string | null } }
|
| 42 |
| { type: "PICK_CLASS"; youClass: ClassType | null; youConfirmed: boolean; allyClass: ClassType | null; allyConfirmed: boolean }
|
| 43 |
-
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 44 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 45 |
| { type: "DRAFT_WAIT" }
|
| 46 |
| { type: "COOP_PHASE"; view: CoopView; events: GameEvent[]; durationMs: number }
|
|
@@ -60,6 +60,7 @@ export interface CoopState {
|
|
| 60 |
connected: boolean;
|
| 61 |
code?: string;
|
| 62 |
you?: Side;
|
|
|
|
| 63 |
bossCatalog?: BossCatalogEntry[]; // 選 boss 地圖:可選/即將推出的全部 Boss
|
| 64 |
bossSelections?: { A: string | null; B: string | null }; // 雙方目前的選擇
|
| 65 |
chosenBossId?: string; // 雙方共識鎖定的 Boss(供選職業前的介紹頁顯示)
|
|
@@ -104,8 +105,10 @@ export interface CoopApi {
|
|
| 104 |
rematch: () => void;
|
| 105 |
}
|
| 106 |
|
| 107 |
-
export function useCoop(): CoopApi {
|
| 108 |
const wsRef = useRef<WebSocket | null>(null);
|
|
|
|
|
|
|
| 109 |
const tokenRef = useRef<string | null>(null); // 重連 token(對局中存著;斷線用它重綁)
|
| 110 |
const statusRef = useRef<CoopStatus>("lobby"); // 鏡像 status,供 onclose 閉包判斷是否該重連
|
| 111 |
const attemptsRef = useRef(0);
|
|
@@ -154,10 +157,10 @@ export function useCoop(): CoopApi {
|
|
| 154 |
|
| 155 |
return {
|
| 156 |
state,
|
| 157 |
-
createRoom: useCallback(() => send({ type: "CREATE_ROOM" }), [send]),
|
| 158 |
-
quickMatch: useCallback(() => send({ type: "QUICK_MATCH" }), [send]),
|
| 159 |
cancelQueue: useCallback(() => send({ type: "CANCEL_QUEUE" }), [send]),
|
| 160 |
-
join: useCallback((code: string) => send({ type: "JOIN", code }), [send]),
|
| 161 |
selectBoss: useCallback((bossId: string) => send({ type: "SELECT_BOSS", bossId }), [send]),
|
| 162 |
backToBoss: useCallback(() => send({ type: "BACK_TO_BOSS" }), [send]),
|
| 163 |
// 樂觀更新:點選即時高亮+改選即取消自己的確定(server 一定接受合法職業,回傳 PICK_CLASS 會再校正)
|
|
@@ -187,7 +190,7 @@ function reduce(s: CoopState, m: ServerMsg): CoopState {
|
|
| 187 |
return { ...s, status, bossCatalog: m.bosses, bossSelections: m.selections, chosenBossId: matched ?? s.chosenBossId, outcome: undefined, canRematch: undefined, rematchRequested: false, oppWantsRematch: false, error: undefined };
|
| 188 |
}
|
| 189 |
case "PICK_CLASS": return { ...s, status: "picking", youClass: m.youClass, youConfirmed: m.youConfirmed, allyClass: m.allyClass, allyConfirmed: m.allyConfirmed, error: undefined };
|
| 190 |
-
case "MATCH_FOUND": return { ...s, you: m.you, error: undefined };
|
| 191 |
case "DRAFT_CHOICE":
|
| 192 |
return { ...s, status: "drafting", draft: { pickIndex: m.pickIndex, total: m.total, tier: m.tier, choices: m.choices }, draftDeadlineTs: Date.now() + m.durationMs };
|
| 193 |
case "DRAFT_WAIT": return { ...s, status: "draftWait", draft: undefined, draftDeadlineTs: undefined };
|
|
|
|
| 40 |
| { type: "QUEUED" }
|
| 41 |
| { type: "BOSS_SELECT"; bosses: BossCatalogEntry[]; selections: { A: string | null; B: string | null } }
|
| 42 |
| { type: "PICK_CLASS"; youClass: ClassType | null; youConfirmed: boolean; allyClass: ClassType | null; allyConfirmed: boolean }
|
| 43 |
+
| { type: "MATCH_FOUND"; matchId: string; you: Side; oppName?: string }
|
| 44 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 45 |
| { type: "DRAFT_WAIT" }
|
| 46 |
| { type: "COOP_PHASE"; view: CoopView; events: GameEvent[]; durationMs: number }
|
|
|
|
| 60 |
connected: boolean;
|
| 61 |
code?: string;
|
| 62 |
you?: Side;
|
| 63 |
+
oppName?: string; // 隊友勇者名(MATCH_FOUND 收到;訪客 undefined → UI 退回「隊友」)
|
| 64 |
bossCatalog?: BossCatalogEntry[]; // 選 boss 地圖:可選/即將推出的全部 Boss
|
| 65 |
bossSelections?: { A: string | null; B: string | null }; // 雙方目前的選擇
|
| 66 |
chosenBossId?: string; // 雙方共識鎖定的 Boss(供選職業前的介紹頁顯示)
|
|
|
|
| 105 |
rematch: () => void;
|
| 106 |
}
|
| 107 |
|
| 108 |
+
export function useCoop(heroName?: string): CoopApi {
|
| 109 |
const wsRef = useRef<WebSocket | null>(null);
|
| 110 |
+
const heroNameRef = useRef<string | undefined>(heroName); // 勇者名(連線訊息帶給 server → 下發給隊友當「隊友名」)
|
| 111 |
+
heroNameRef.current = heroName;
|
| 112 |
const tokenRef = useRef<string | null>(null); // 重連 token(對局中存著;斷線用它重綁)
|
| 113 |
const statusRef = useRef<CoopStatus>("lobby"); // 鏡像 status,供 onclose 閉包判斷是否該重連
|
| 114 |
const attemptsRef = useRef(0);
|
|
|
|
| 157 |
|
| 158 |
return {
|
| 159 |
state,
|
| 160 |
+
createRoom: useCallback(() => send({ type: "CREATE_ROOM", heroName: heroNameRef.current }), [send]),
|
| 161 |
+
quickMatch: useCallback(() => send({ type: "QUICK_MATCH", heroName: heroNameRef.current }), [send]),
|
| 162 |
cancelQueue: useCallback(() => send({ type: "CANCEL_QUEUE" }), [send]),
|
| 163 |
+
join: useCallback((code: string) => send({ type: "JOIN", code, heroName: heroNameRef.current }), [send]),
|
| 164 |
selectBoss: useCallback((bossId: string) => send({ type: "SELECT_BOSS", bossId }), [send]),
|
| 165 |
backToBoss: useCallback(() => send({ type: "BACK_TO_BOSS" }), [send]),
|
| 166 |
// 樂觀更新:點選即時高亮+改選即取消自己的確定(server 一定接受合法職業,回傳 PICK_CLASS 會再校正)
|
|
|
|
| 190 |
return { ...s, status, bossCatalog: m.bosses, bossSelections: m.selections, chosenBossId: matched ?? s.chosenBossId, outcome: undefined, canRematch: undefined, rematchRequested: false, oppWantsRematch: false, error: undefined };
|
| 191 |
}
|
| 192 |
case "PICK_CLASS": return { ...s, status: "picking", youClass: m.youClass, youConfirmed: m.youConfirmed, allyClass: m.allyClass, allyConfirmed: m.allyConfirmed, error: undefined };
|
| 193 |
+
case "MATCH_FOUND": return { ...s, you: m.you, oppName: m.oppName, error: undefined };
|
| 194 |
case "DRAFT_CHOICE":
|
| 195 |
return { ...s, status: "drafting", draft: { pickIndex: m.pickIndex, total: m.total, tier: m.tier, choices: m.choices }, draftDeadlineTs: Date.now() + m.durationMs };
|
| 196 |
case "DRAFT_WAIT": return { ...s, status: "draftWait", draft: undefined, draftDeadlineTs: undefined };
|
packages/web/src/pvpClient.ts
CHANGED
|
@@ -80,8 +80,10 @@ export interface PvpApi {
|
|
| 80 |
rematch: () => void;
|
| 81 |
}
|
| 82 |
|
| 83 |
-
export function usePvp(): PvpApi {
|
| 84 |
const wsRef = useRef<WebSocket | null>(null);
|
|
|
|
|
|
|
| 85 |
const tokenRef = useRef<string | null>(null); // 重連 token(對局中存著;斷線用它重綁)
|
| 86 |
const statusRef = useRef<PvpStatus>("lobby"); // 鏡像 status,供 onclose 閉包判斷是否該重連
|
| 87 |
const attemptsRef = useRef(0); // 重連退避計數
|
|
@@ -138,10 +140,10 @@ export function usePvp(): PvpApi {
|
|
| 138 |
|
| 139 |
return {
|
| 140 |
state,
|
| 141 |
-
createRoom: useCallback(() => send({ type: "CREATE_ROOM" }), [send]),
|
| 142 |
-
quickMatch: useCallback(() => send({ type: "QUICK_MATCH" }), [send]),
|
| 143 |
cancelQueue: useCallback(() => send({ type: "CANCEL_QUEUE" }), [send]),
|
| 144 |
-
join: useCallback((code: string) => send({ type: "JOIN", code }), [send]),
|
| 145 |
setClass: useCallback((classType: ClassType) => send({ type: "SET_CLASS", classType }), [send]),
|
| 146 |
draftPick: useCallback((choiceIndex: number, pickIndex: number) => send({ type: "DRAFT_PICK", choiceIndex, pickIndex }), [send]),
|
| 147 |
play: useCallback((handIndex: number) => send({ type: "PLAY_CARD", handIndex }), [send]),
|
|
|
|
| 80 |
rematch: () => void;
|
| 81 |
}
|
| 82 |
|
| 83 |
+
export function usePvp(heroName?: string): PvpApi {
|
| 84 |
const wsRef = useRef<WebSocket | null>(null);
|
| 85 |
+
const heroNameRef = useRef<string | undefined>(heroName); // 勇者名(連線訊息帶給 server → 下發給對手當「對手名」)
|
| 86 |
+
heroNameRef.current = heroName;
|
| 87 |
const tokenRef = useRef<string | null>(null); // 重連 token(對局中存著;斷線用它重綁)
|
| 88 |
const statusRef = useRef<PvpStatus>("lobby"); // 鏡像 status,供 onclose 閉包判斷是否該重連
|
| 89 |
const attemptsRef = useRef(0); // 重連退避計數
|
|
|
|
| 140 |
|
| 141 |
return {
|
| 142 |
state,
|
| 143 |
+
createRoom: useCallback(() => send({ type: "CREATE_ROOM", heroName: heroNameRef.current }), [send]),
|
| 144 |
+
quickMatch: useCallback(() => send({ type: "QUICK_MATCH", heroName: heroNameRef.current }), [send]),
|
| 145 |
cancelQueue: useCallback(() => send({ type: "CANCEL_QUEUE" }), [send]),
|
| 146 |
+
join: useCallback((code: string) => send({ type: "JOIN", code, heroName: heroNameRef.current }), [send]),
|
| 147 |
setClass: useCallback((classType: ClassType) => send({ type: "SET_CLASS", classType }), [send]),
|
| 148 |
draftPick: useCallback((choiceIndex: number, pickIndex: number) => send({ type: "DRAFT_PICK", choiceIndex, pickIndex }), [send]),
|
| 149 |
play: useCallback((handIndex: number) => send({ type: "PLAY_CARD", handIndex }), [send]),
|