freeguy2006 Claude Opus 4.8 commited on
Commit
a9712d2
·
1 Parent(s): 4444608

feat(pvp): 斷線改為「等計時器結束才判負」,非斷線即判

Browse files

- disconnect:對局中斷線只標記 setConnected(false)+通知對手 OPP_DISCONNECTED,不立即判負
- onTimeout:本回合計時器結束時若該側仍斷線 → 此時才判負(對手勝);雙方都掉 → 清房
- 緩衝期內(計時器未到)留待未來重連機制(M5.3);測試以可控 scheduler 觸發計時器驗證

server 36 測試全綠、typecheck/build 通過。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

packages/server/src/pvpHub.ts CHANGED
@@ -325,6 +325,16 @@ export class PvpHub {
325
  private onTimeout(code: string): void {
326
  const room = this.rooms.get(code);
327
  if (!room?.match || room.match.phase === "ENDED") return;
 
 
 
 
 
 
 
 
 
 
328
  room.match.timeout();
329
  this.broadcastResolve(room);
330
  }
@@ -389,11 +399,9 @@ export class PvpHub {
389
  }
390
  const side = room.match?.sideOf(playerId) ?? null;
391
  if (room.match && side && room.match.phase !== "ENDED") {
 
392
  room.match.setConnected(side, false);
393
- // MVP:對局中斷線即判負(重連/持久化列 M5.3)
394
- room.match.surrender(side, "disconnect");
395
  room.conns[OTHER[side]]?.send({ type: "OPP_DISCONNECTED" });
396
- this.broadcastResolveEndOnly(room);
397
  } else {
398
  // 等待中(只有建房者)或已結束 → 清房,避免洩漏
399
  this.cleanup(room);
 
325
  private onTimeout(code: string): void {
326
  const room = this.rooms.get(code);
327
  if (!room?.match || room.match.phase === "ENDED") return;
328
+ // 斷線方在「本回合計時器結束時」才判負(斷線當下不立即結束;緩衝期內可重連 — 重連機制列 M5.3)。
329
+ const aGone = !room.match.isConnected("A");
330
+ const bGone = !room.match.isConnected("B");
331
+ if (aGone || bGone) {
332
+ if (aGone && bGone) { this.cleanup(room); return; } // 雙方都掉 → 清房,無人勝
333
+ const goneSide: Side = aGone ? "A" : "B";
334
+ room.match.surrender(goneSide, "disconnect");
335
+ this.endMatch(room);
336
+ return;
337
+ }
338
  room.match.timeout();
339
  this.broadcastResolve(room);
340
  }
 
399
  }
400
  const side = room.match?.sideOf(playerId) ?? null;
401
  if (room.match && side && room.match.phase !== "ENDED") {
402
+ // 對局中斷線:不立即判負,標記斷線並通知對手;等本回合計時器結束(onTimeout)仍未重連才判負(緩衝/未來重連)。
403
  room.match.setConnected(side, false);
 
 
404
  room.conns[OTHER[side]]?.send({ type: "OPP_DISCONNECTED" });
 
405
  } else {
406
  // 等待中(只有建房者)或已結束 → 清房,避免洩漏
407
  this.cleanup(room);
packages/server/src/pvpMatch.ts CHANGED
@@ -174,6 +174,10 @@ export class PvpMatch {
174
  this.players[side].connected = connected;
175
  }
176
 
 
 
 
 
177
  private resolve(): void {
178
  const a: PvpSlot = { state: this.players.A.current.state, rng: this.players.A.current.rng };
179
  const b: PvpSlot = { state: this.players.B.current.state, rng: this.players.B.current.rng };
 
174
  this.players[side].connected = connected;
175
  }
176
 
177
+ isConnected(side: Side): boolean {
178
+ return this.players[side].connected;
179
+ }
180
+
181
  private resolve(): void {
182
  const a: PvpSlot = { state: this.players.A.current.state, rng: this.players.A.current.rng };
183
  const b: PvpSlot = { state: this.players.B.current.state, rng: this.players.B.current.rng };
packages/server/test/pvpHub.test.ts CHANGED
@@ -4,17 +4,20 @@ import { PvpHub, type PvpConn, type PvpServerMsg } from "../src/pvpHub.js";
4
  function harness() {
5
  const msgs: Record<"A" | "B", PvpServerMsg[]> = { A: [], B: [] };
6
  let seed = 1000;
 
 
7
  const hub = new PvpHub({
8
  genCode: () => "ROOM1",
9
  genSeed: () => (seed += 7),
10
  now: () => 0,
11
  tPlanMs: 30000,
12
- scheduler: { set: () => ({}), clear: () => {} }, // 不真的計時
13
  enableDraft: false, // 這組測試直接測戰鬥流程;draft 另有專門 describe
14
  });
15
  const connA: PvpConn = { playerId: "pa", send: (m) => msgs.A.push(m) };
16
  const connB: PvpConn = { playerId: "pb", send: (m) => msgs.B.push(m) };
17
- return { hub, msgs, connA, connB };
 
18
  }
19
  const last = (arr: PvpServerMsg[]) => arr[arr.length - 1];
20
  const types = (arr: PvpServerMsg[]) => arr.map((m) => m.type);
@@ -82,16 +85,20 @@ describe("PvpHub — 出牌 / 鎖定 / 結算廣播", () => {
82
  expect(last(msgs.B)).toMatchObject({ type: "GAME_END", outcome: "WIN", reason: "對手投降" });
83
  });
84
 
85
- it("斷線判負理由 = 各自視角", () => {
86
- const { hub, msgs } = started();
87
  hub.disconnect("pa");
 
 
 
88
  expect(last(msgs.B)).toMatchObject({ outcome: "WIN", reason: "對手離線" });
89
  });
90
 
91
- it("斷線 → 對手收 OPP_DISCONNECTED + GAME_END WIN", () => {
92
- const { hub, msgs } = started();
93
  hub.disconnect("pa");
94
  expect(types(msgs.B)).toContain("OPP_DISCONNECTED");
 
95
  expect(last(msgs.B)).toMatchObject({ type: "GAME_END", outcome: "WIN" });
96
  });
97
 
 
4
  function harness() {
5
  const msgs: Record<"A" | "B", PvpServerMsg[]> = { A: [], B: [] };
6
  let seed = 1000;
7
+ const timers = new Map<object, () => void>();
8
+ let tid = 0;
9
  const hub = new PvpHub({
10
  genCode: () => "ROOM1",
11
  genSeed: () => (seed += 7),
12
  now: () => 0,
13
  tPlanMs: 30000,
14
+ scheduler: { set: (_ms, cb) => { const h = { id: ++tid }; timers.set(h, cb); return h; }, clear: (h) => { timers.delete(h as object); } },
15
  enableDraft: false, // 這組測試直接測戰鬥流程;draft 另有專門 describe
16
  });
17
  const connA: PvpConn = { playerId: "pa", send: (m) => msgs.A.push(m) };
18
  const connB: PvpConn = { playerId: "pb", send: (m) => msgs.B.push(m) };
19
+ const fireTimers = () => { const cbs = [...timers.values()]; timers.clear(); cbs.forEach((cb) => cb()); }; // 觸發所有掛起的計時器
20
+ return { hub, msgs, connA, connB, fireTimers };
21
  }
22
  const last = (arr: PvpServerMsg[]) => arr[arr.length - 1];
23
  const types = (arr: PvpServerMsg[]) => arr.map((m) => m.type);
 
85
  expect(last(msgs.B)).toMatchObject({ type: "GAME_END", outcome: "WIN", reason: "對手投降" });
86
  });
87
 
88
+ it("斷線不立即判負;等計時器結束才判負(理由 = 各自視角)", () => {
89
+ const { hub, msgs, fireTimers } = started();
90
  hub.disconnect("pa");
91
+ expect(types(msgs.B)).toContain("OPP_DISCONNECTED");
92
+ expect(types(msgs.B)).not.toContain("GAME_END"); // 斷線當下尚未判負
93
+ fireTimers(); // 本回合計時器結束 → 此時才判負
94
  expect(last(msgs.B)).toMatchObject({ outcome: "WIN", reason: "對手離線" });
95
  });
96
 
97
+ it("斷線 → 對手收 OPP_DISCONNECTED;計時器結束 → GAME_END WIN", () => {
98
+ const { hub, msgs, fireTimers } = started();
99
  hub.disconnect("pa");
100
  expect(types(msgs.B)).toContain("OPP_DISCONNECTED");
101
+ fireTimers();
102
  expect(last(msgs.B)).toMatchObject({ type: "GAME_END", outcome: "WIN" });
103
  });
104