Spaces:
Sleeping
Sleeping
Commit ·
ff3cdfd
1
Parent(s): 8972f03
feat(pvp): 對戰畫面與戰績顯示對手登入名(訪客退回「對手」)
Browse files- server /ws/pvp 握手時由 session cookie 解析 displayName → conn.name
- pvpHub: PvpConn 加 name;MATCH_FOUND 加 oppName(對手名),開局下發雙方
- web: pvpClient 收 oppName 進 state;PvpScreen 對手 HP 面板/同時結算列/
戰績回報改用對手名,訪客或未登入退回「對手」
- 補 pvpHub 測試:雙方互看名字 + 訪客(無 name) oppName=undefined
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
packages/server/src/pvpHub.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type PvpServerMsg =
|
|
| 14 |
| { type: "ROOM_CREATED"; code: string }
|
| 15 |
| { type: "QUEUED" } // 已加入快速配對佇列,等待對手
|
| 16 |
| { type: "PICK_CLASS" } // 進房後請選職業(雙方都選好才開局/draft)
|
| 17 |
-
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 18 |
// 伺服器權威 draft:每抽由伺服器發 3 張(client 只回 choiceIndex),seed 不外洩
|
| 19 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 20 |
| { type: "DRAFT_WAIT" } // 你已完成 10 抽,等待對手
|
|
@@ -33,6 +33,7 @@ export type PvpServerMsg =
|
|
| 33 |
|
| 34 |
export interface PvpConn {
|
| 35 |
playerId: string;
|
|
|
|
| 36 |
send: (m: PvpServerMsg) => void;
|
| 37 |
}
|
| 38 |
|
|
@@ -201,8 +202,8 @@ export class PvpHub {
|
|
| 201 |
}
|
| 202 |
|
| 203 |
private announceMatch(room: Room): void {
|
| 204 |
-
room.conns.A?.send({ type: "MATCH_FOUND", matchId: room.code, you: "A" });
|
| 205 |
-
room.conns.B?.send({ type: "MATCH_FOUND", matchId: room.code, you: "B" });
|
| 206 |
}
|
| 207 |
|
| 208 |
// ===== 伺服器權威 draft(三選一由伺服器發牌,client 只回 choiceIndex,seed 不外洩)=====
|
|
|
|
| 14 |
| { type: "ROOM_CREATED"; code: string }
|
| 15 |
| { type: "QUEUED" } // 已加入快速配對佇列,等待對手
|
| 16 |
| { type: "PICK_CLASS" } // 進房後請選職業(雙方都選好才開局/draft)
|
| 17 |
+
| { type: "MATCH_FOUND"; matchId: string; you: Side; oppName?: string } // oppName=對手登入顯示名(訪客為 undefined)
|
| 18 |
// 伺服器權威 draft:每抽由伺服器發 3 張(client 只回 choiceIndex),seed 不外洩
|
| 19 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 20 |
| { type: "DRAFT_WAIT" } // 你已完成 10 抽,等待對手
|
|
|
|
| 33 |
|
| 34 |
export interface PvpConn {
|
| 35 |
playerId: string;
|
| 36 |
+
name?: string; // 登入者顯示名(連線握手時由 session cookie 解析;訪客為 undefined)→ 下發給對手當「對手名」
|
| 37 |
send: (m: PvpServerMsg) => void;
|
| 38 |
}
|
| 39 |
|
|
|
|
| 202 |
}
|
| 203 |
|
| 204 |
private announceMatch(room: Room): void {
|
| 205 |
+
room.conns.A?.send({ type: "MATCH_FOUND", matchId: room.code, you: "A", oppName: room.conns.B?.name });
|
| 206 |
+
room.conns.B?.send({ type: "MATCH_FOUND", matchId: room.code, you: "B", oppName: room.conns.A?.name });
|
| 207 |
}
|
| 208 |
|
| 209 |
// ===== 伺服器權威 draft(三選一由伺服器發牌,client 只回 choiceIndex,seed 不外洩)=====
|
packages/server/src/server.ts
CHANGED
|
@@ -11,7 +11,7 @@ import type { GhostRepository } from "./repository.js";
|
|
| 11 |
import type { UserRepository } from "./userRepository.js";
|
| 12 |
import type { OAuthProvider } from "./oauth.js";
|
| 13 |
import { validateGhostShape, verifyGhost } from "./ghostService.js";
|
| 14 |
-
import { registerAuthRoutes } from "./authRoutes.js";
|
| 15 |
import { registerDiscordInteractions } from "./discordInteractions.js";
|
| 16 |
import { PvpHub, type PvpConn } from "./pvpHub.js";
|
| 17 |
import { CoopHub, type CoopConn } from "./coopHub.js";
|
|
@@ -204,8 +204,17 @@ export function buildServer(opts: ServerOptions): FastifyInstance {
|
|
| 204 |
wsConns++;
|
| 205 |
const gate = makeMsgGate();
|
| 206 |
const playerId = randomUUID();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
const conn: PvpConn = {
|
| 208 |
playerId,
|
|
|
|
| 209 |
send: (m) => {
|
| 210 |
try {
|
| 211 |
socket.send(JSON.stringify(m));
|
|
|
|
| 11 |
import type { UserRepository } from "./userRepository.js";
|
| 12 |
import type { OAuthProvider } from "./oauth.js";
|
| 13 |
import { validateGhostShape, verifyGhost } from "./ghostService.js";
|
| 14 |
+
import { registerAuthRoutes, currentUserId } from "./authRoutes.js";
|
| 15 |
import { registerDiscordInteractions } from "./discordInteractions.js";
|
| 16 |
import { PvpHub, type PvpConn } from "./pvpHub.js";
|
| 17 |
import { CoopHub, type CoopConn } from "./coopHub.js";
|
|
|
|
| 204 |
wsConns++;
|
| 205 |
const gate = makeMsgGate();
|
| 206 |
const playerId = randomUUID();
|
| 207 |
+
// 從 session cookie 解析登入者顯示名 → 當作 PvP 對手名(訪客/未登入=undefined,前端退回「對手」)
|
| 208 |
+
let displayName: string | undefined;
|
| 209 |
+
if (userRepo) {
|
| 210 |
+
try {
|
| 211 |
+
const uid = currentUserId(req, userRepo);
|
| 212 |
+
displayName = uid ? userRepo.getById(uid)?.displayName : undefined;
|
| 213 |
+
} catch { /* 解析失敗就當匿名 */ }
|
| 214 |
+
}
|
| 215 |
const conn: PvpConn = {
|
| 216 |
playerId,
|
| 217 |
+
name: displayName,
|
| 218 |
send: (m) => {
|
| 219 |
try {
|
| 220 |
socket.send(JSON.stringify(m));
|
packages/server/test/pvpHub.test.ts
CHANGED
|
@@ -49,6 +49,30 @@ describe("PvpHub — 房間碼配對", () => {
|
|
| 49 |
expect(ps.view.you.hand.length).toBe(5);
|
| 50 |
});
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
it("加入不存在的房 → ERROR", () => {
|
| 53 |
const { hub, msgs, connB } = harness();
|
| 54 |
hub.join("NOPE", connB);
|
|
|
|
| 49 |
expect(ps.view.you.hand.length).toBe(5);
|
| 50 |
});
|
| 51 |
|
| 52 |
+
it("MATCH_FOUND 帶對手登入名(oppName=對方 name;雙方互看)", () => {
|
| 53 |
+
const { hub, msgs } = harness();
|
| 54 |
+
const connA: PvpConn = { playerId: "pa", name: "Ray", send: (m) => msgs.A.push(m) };
|
| 55 |
+
const connB: PvpConn = { playerId: "pb", name: "小明", send: (m) => msgs.B.push(m) };
|
| 56 |
+
hub.createRoom(connA);
|
| 57 |
+
hub.join("ROOM1", connB);
|
| 58 |
+
hub.setClass("pa", "WARRIOR");
|
| 59 |
+
hub.setClass("pb", "WARRIOR");
|
| 60 |
+
const mfA = msgs.A.find((m) => m.type === "MATCH_FOUND") as Extract<PvpServerMsg, { type: "MATCH_FOUND" }>;
|
| 61 |
+
const mfB = msgs.B.find((m) => m.type === "MATCH_FOUND") as Extract<PvpServerMsg, { type: "MATCH_FOUND" }>;
|
| 62 |
+
expect(mfA.oppName).toBe("小明"); // A 收到的是「對手 B」的名字
|
| 63 |
+
expect(mfB.oppName).toBe("Ray"); // B 收到的是「對手 A」的名字
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
it("訪客(conn 無 name)→ MATCH_FOUND.oppName 為 undefined(前端退回「對手」)", () => {
|
| 67 |
+
const { hub, msgs, connA, connB } = harness(); // harness 的 conn 不帶 name=匿名/訪客
|
| 68 |
+
hub.createRoom(connA);
|
| 69 |
+
hub.join("ROOM1", connB);
|
| 70 |
+
hub.setClass("pa", "WARRIOR");
|
| 71 |
+
hub.setClass("pb", "WARRIOR");
|
| 72 |
+
const mfA = msgs.A.find((m) => m.type === "MATCH_FOUND") as Extract<PvpServerMsg, { type: "MATCH_FOUND" }>;
|
| 73 |
+
expect(mfA.oppName).toBeUndefined();
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
it("加入不存在的房 → ERROR", () => {
|
| 77 |
const { hub, msgs, connB } = harness();
|
| 78 |
hub.join("NOPE", connB);
|
packages/web/src/PvpScreen.tsx
CHANGED
|
@@ -7,14 +7,14 @@ import { Centered, Bar, useCountdown, DraftView, WaitRoom, RoomLobby, PileBar, P
|
|
| 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);
|
| 12 |
useEffect(() => {
|
| 13 |
if (reportedRef.current || state.status !== "ended" || state.outcome === undefined) return;
|
| 14 |
reportedRef.current = true;
|
| 15 |
const cls = (state.view?.you.classType ?? "WARRIOR") as ClassType;
|
| 16 |
-
onResult?.("pvp", "對手", cls, state.outcome === "WIN");
|
| 17 |
-
}, [state.status, state.outcome, state.view, onResult]);
|
| 18 |
|
| 19 |
// 同時結算揭示:每收到一次 RESOLVE_RESULT 就顯示 2.2 秒揭示覆蓋層
|
| 20 |
const [reveal, setReveal] = useState<PvpResolveResult | null>(null);
|
|
@@ -108,12 +108,12 @@ export function PvpScreen({ autoJoinCode, onExit, onResult }: { autoJoinCode?: s
|
|
| 108 |
<>
|
| 109 |
{main}
|
| 110 |
{state.reconnecting && <div className="reconnect-banner">連線中斷,重連中…</div>}
|
| 111 |
-
{reveal && <ResolveReveal key={reveal.turnNo} result={reveal} you={state.you ?? "A"} />}
|
| 112 |
</>
|
| 113 |
);
|
| 114 |
}
|
| 115 |
|
| 116 |
-
function ResolveReveal({ result, you }: { result: PvpResolveResult; you: Side }) {
|
| 117 |
const me = you === "A" ? "a" : "b";
|
| 118 |
const op = you === "A" ? "b" : "a";
|
| 119 |
const dealt = you === "A" ? result.dmgAtoB : result.dmgBtoA;
|
|
@@ -136,7 +136,7 @@ function ResolveReveal({ result, you }: { result: PvpResolveResult; you: Side })
|
|
| 136 |
shown={shown}
|
| 137 |
/>
|
| 138 |
<RevealRow
|
| 139 |
-
label=
|
| 140 |
before={result.snapshotBefore[op].hp}
|
| 141 |
after={result.hpAfter[op]}
|
| 142 |
max={result.snapshotBefore[op].maxHp + result.snapshotBefore[op].tempMaxHp}
|
|
@@ -216,7 +216,7 @@ function PvpBattle({ pvp }: { pvp: ReturnType<typeof usePvp> }) {
|
|
| 216 |
{/* 對手(PLAN 相只露 HP + 鎖定燈) */}
|
| 217 |
<div className="enemy">
|
| 218 |
<div className="enemy-info">
|
| 219 |
-
<div className="name">對手 {state.oppLocked ? "🔒 已鎖定" : "規劃中…"}</div>
|
| 220 |
<Bar value={v.opponent.hp} max={oppEffMax} label={`HP ${v.opponent.hp}/${oppEffMax}${v.opponent.tempMaxHp ? `(臨時+${v.opponent.tempMaxHp})` : ""}`} />
|
| 221 |
</div>
|
| 222 |
</div>
|
|
|
|
| 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);
|
| 12 |
useEffect(() => {
|
| 13 |
if (reportedRef.current || state.status !== "ended" || state.outcome === undefined) return;
|
| 14 |
reportedRef.current = true;
|
| 15 |
const cls = (state.view?.you.classType ?? "WARRIOR") as ClassType;
|
| 16 |
+
onResult?.("pvp", state.oppName || "對手", cls, state.outcome === "WIN");
|
| 17 |
+
}, [state.status, state.outcome, state.view, state.oppName, onResult]);
|
| 18 |
|
| 19 |
// 同時結算揭示:每收到一次 RESOLVE_RESULT 就顯示 2.2 秒揭示覆蓋層
|
| 20 |
const [reveal, setReveal] = useState<PvpResolveResult | null>(null);
|
|
|
|
| 108 |
<>
|
| 109 |
{main}
|
| 110 |
{state.reconnecting && <div className="reconnect-banner">連線中斷,重連中…</div>}
|
| 111 |
+
{reveal && <ResolveReveal key={reveal.turnNo} result={reveal} you={state.you ?? "A"} oppName={state.oppName || "對手"} />}
|
| 112 |
</>
|
| 113 |
);
|
| 114 |
}
|
| 115 |
|
| 116 |
+
function ResolveReveal({ result, you, oppName }: { result: PvpResolveResult; you: Side; oppName: string }) {
|
| 117 |
const me = you === "A" ? "a" : "b";
|
| 118 |
const op = you === "A" ? "b" : "a";
|
| 119 |
const dealt = you === "A" ? result.dmgAtoB : result.dmgBtoA;
|
|
|
|
| 136 |
shown={shown}
|
| 137 |
/>
|
| 138 |
<RevealRow
|
| 139 |
+
label={oppName}
|
| 140 |
before={result.snapshotBefore[op].hp}
|
| 141 |
after={result.hpAfter[op]}
|
| 142 |
max={result.snapshotBefore[op].maxHp + result.snapshotBefore[op].tempMaxHp}
|
|
|
|
| 216 |
{/* 對手(PLAN 相只露 HP + 鎖定燈) */}
|
| 217 |
<div className="enemy">
|
| 218 |
<div className="enemy-info">
|
| 219 |
+
<div className="name">{state.oppName || "對手"} {state.oppLocked ? "🔒 已鎖定" : "規劃中…"}</div>
|
| 220 |
<Bar value={v.opponent.hp} max={oppEffMax} label={`HP ${v.opponent.hp}/${oppEffMax}${v.opponent.tempMaxHp ? `(臨時+${v.opponent.tempMaxHp})` : ""}`} />
|
| 221 |
</div>
|
| 222 |
</div>
|
packages/web/src/pvpClient.ts
CHANGED
|
@@ -26,7 +26,7 @@ type ServerMsg =
|
|
| 26 |
| { type: "ROOM_CREATED"; code: string }
|
| 27 |
| { type: "QUEUED" }
|
| 28 |
| { type: "PICK_CLASS" }
|
| 29 |
-
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 30 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 31 |
| { type: "DRAFT_WAIT" }
|
| 32 |
| { type: "PHASE_START"; turnNo: number; isOvertime: boolean; view: PvpView; durationMs: number }
|
|
@@ -49,6 +49,7 @@ export interface PvpState {
|
|
| 49 |
connected: boolean;
|
| 50 |
code?: string;
|
| 51 |
you?: Side;
|
|
|
|
| 52 |
view?: PvpView;
|
| 53 |
deadlineTs?: number;
|
| 54 |
draft?: { pickIndex: number; total: number; tier: DraftTier; choices: number[] };
|
|
@@ -161,7 +162,7 @@ function reduce(s: PvpState, m: ServerMsg): PvpState {
|
|
| 161 |
case "PICK_CLASS":
|
| 162 |
return { ...s, status: "picking", error: undefined }; // 配對成功 → 房內選職業
|
| 163 |
case "MATCH_FOUND":
|
| 164 |
-
return { ...s, you: m.you, error: undefined }; // 之後接 DRAFT_CHOICE(draft)或 PHASE_START(無 draft)
|
| 165 |
case "DRAFT_CHOICE":
|
| 166 |
return {
|
| 167 |
...s, status: "drafting",
|
|
|
|
| 26 |
| { type: "ROOM_CREATED"; code: string }
|
| 27 |
| { type: "QUEUED" }
|
| 28 |
| { type: "PICK_CLASS" }
|
| 29 |
+
| { type: "MATCH_FOUND"; matchId: string; you: Side; oppName?: string }
|
| 30 |
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 31 |
| { type: "DRAFT_WAIT" }
|
| 32 |
| { type: "PHASE_START"; turnNo: number; isOvertime: boolean; view: PvpView; durationMs: number }
|
|
|
|
| 49 |
connected: boolean;
|
| 50 |
code?: string;
|
| 51 |
you?: Side;
|
| 52 |
+
oppName?: string; // 對手登入顯示名(MATCH_FOUND 時收到;訪客為 undefined → UI 退回「對手」)
|
| 53 |
view?: PvpView;
|
| 54 |
deadlineTs?: number;
|
| 55 |
draft?: { pickIndex: number; total: number; tier: DraftTier; choices: number[] };
|
|
|
|
| 162 |
case "PICK_CLASS":
|
| 163 |
return { ...s, status: "picking", error: undefined }; // 配對成功 → 房內選職業
|
| 164 |
case "MATCH_FOUND":
|
| 165 |
+
return { ...s, you: m.you, oppName: m.oppName, error: undefined }; // 之後接 DRAFT_CHOICE(draft)或 PHASE_START(無 draft)
|
| 166 |
case "DRAFT_CHOICE":
|
| 167 |
return {
|
| 168 |
...s, status: "drafting",
|