Spaces:
Sleeping
Sleeping
Commit ·
ab413b5
1
Parent(s): d76c4b0
feat(pvp): draft 5/3/2+史傳去重+每抽60s逾時、規劃100s、鎖定鈕不位移
Browse files- draft 組成改 5 普通+3 史詩+2 傳說(10 抽,牌組 20);史詩/傳說選過不再出現
- draft 每抽 60 秒逾時自動選第 1 張(補上原本缺的 draft 逾時 + 計時器清理)
- 對戰每回合規劃時間 30→100 秒;WS choiceIndex 守衛 <8 改 <3
- 前端 draft 顯示倒數;鎖定鈕在 disabled/hover 維持垂直置中,不再位移
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- packages/battle-core/src/pvpDraft.ts +15 -6
- packages/battle-core/test/pvp-draft.test.ts +20 -12
- packages/server/src/pvpHub.ts +39 -4
- packages/server/src/server.ts +5 -3
- packages/server/test/pvpHub.test.ts +6 -5
- packages/web/src/PvpScreen.tsx +6 -3
- packages/web/src/pvpClient.ts +7 -2
- packages/web/src/styles.css +6 -0
packages/battle-core/src/pvpDraft.ts
CHANGED
|
@@ -9,13 +9,13 @@ import { REWARD_POOL_NORMAL, REWARD_POOL_EPIC, REWARD_POOL_LEGEND, STARTING_DECK
|
|
| 9 |
|
| 10 |
export type DraftTier = "normal" | "epic" | "legend";
|
| 11 |
|
| 12 |
-
/**
|
| 13 |
export const PVP_DRAFT_PLAN: readonly DraftTier[] = [
|
| 14 |
-
"normal", "normal", "normal", "normal", "epic", "epic", "legend", "legend",
|
| 15 |
];
|
| 16 |
-
export const PVP_DRAFT_PICKS = PVP_DRAFT_PLAN.length; //
|
| 17 |
export const PVP_DRAFT_CHOICES = 3; // 每抽三選一
|
| 18 |
-
export const PVP_DRAFT_COUNTS = { normal:
|
| 19 |
|
| 20 |
/**
|
| 21 |
* PvP draft 排除卡:33 斷劍(升級鏈起點,避免局中經 ADVANCE 升級成 34;對應 Q-PVP-10)。
|
|
@@ -30,13 +30,22 @@ export function draftPoolFor(tier: DraftTier): readonly number[] {
|
|
| 30 |
return tier === "epic" ? REWARD_POOL_EPIC : tier === "legend" ? REWARD_POOL_LEGEND : PVP_DRAFT_POOL_NORMAL;
|
| 31 |
}
|
| 32 |
|
| 33 |
-
/**
|
|
|
|
|
|
|
|
|
|
| 34 |
export function draftChoices(
|
| 35 |
rng: RngState,
|
| 36 |
pickIndex: number,
|
|
|
|
| 37 |
): { tier: DraftTier; choices: number[]; rng: RngState } {
|
| 38 |
const tier = PVP_DRAFT_PLAN[pickIndex] ?? "normal";
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return { tier, choices: shuffled.slice(0, PVP_DRAFT_CHOICES), rng: next };
|
| 41 |
}
|
| 42 |
|
|
|
|
| 9 |
|
| 10 |
export type DraftTier = "normal" | "epic" | "legend";
|
| 11 |
|
| 12 |
+
/** 10 抽的稀有度順序:5 普通 → 3 史詩 → 2 傳說。 */
|
| 13 |
export const PVP_DRAFT_PLAN: readonly DraftTier[] = [
|
| 14 |
+
"normal", "normal", "normal", "normal", "normal", "epic", "epic", "epic", "legend", "legend",
|
| 15 |
];
|
| 16 |
+
export const PVP_DRAFT_PICKS = PVP_DRAFT_PLAN.length; // 10
|
| 17 |
export const PVP_DRAFT_CHOICES = 3; // 每抽三選一
|
| 18 |
+
export const PVP_DRAFT_COUNTS = { normal: 5, epic: 3, legend: 2 } as const;
|
| 19 |
|
| 20 |
/**
|
| 21 |
* PvP draft 排除卡:33 斷劍(升級鏈起點,避免局中經 ADVANCE 升級成 34;對應 Q-PVP-10)。
|
|
|
|
| 30 |
return tier === "epic" ? REWARD_POOL_EPIC : tier === "legend" ? REWARD_POOL_LEGEND : PVP_DRAFT_POOL_NORMAL;
|
| 31 |
}
|
| 32 |
|
| 33 |
+
/**
|
| 34 |
+
* 第 pickIndex(0..9)抽的三選一(決定性、消耗一次 rng)。
|
| 35 |
+
* picked = 該側目前已選清單;史詩/傳說會排除「已選過」的卡(同稀有不重複),普通仍可重複。
|
| 36 |
+
*/
|
| 37 |
export function draftChoices(
|
| 38 |
rng: RngState,
|
| 39 |
pickIndex: number,
|
| 40 |
+
picked: readonly number[] = [],
|
| 41 |
): { tier: DraftTier; choices: number[]; rng: RngState } {
|
| 42 |
const tier = PVP_DRAFT_PLAN[pickIndex] ?? "normal";
|
| 43 |
+
let pool = draftPoolFor(tier);
|
| 44 |
+
if (tier === "epic" || tier === "legend") {
|
| 45 |
+
const taken = new Set(picked);
|
| 46 |
+
pool = pool.filter((id) => !taken.has(id)); // 史詩/傳說:選過的不再出現
|
| 47 |
+
}
|
| 48 |
+
const [shuffled, next] = shuffle(pool, rng);
|
| 49 |
return { tier, choices: shuffled.slice(0, PVP_DRAFT_CHOICES), rng: next };
|
| 50 |
}
|
| 51 |
|
packages/battle-core/test/pvp-draft.test.ts
CHANGED
|
@@ -12,10 +12,10 @@ import {
|
|
| 12 |
import { STARTING_DECK, REWARD_POOL_NORMAL, REWARD_POOL_EPIC, REWARD_POOL_LEGEND } from "../src/cards.js";
|
| 13 |
|
| 14 |
describe("PvP Draft", () => {
|
| 15 |
-
it("計畫為
|
| 16 |
-
expect(PVP_DRAFT_PICKS).toBe(
|
| 17 |
-
expect(PVP_DRAFT_PLAN.filter((t) => t === "normal").length).toBe(
|
| 18 |
-
expect(PVP_DRAFT_PLAN.filter((t) => t === "epic").length).toBe(
|
| 19 |
expect(PVP_DRAFT_PLAN.filter((t) => t === "legend").length).toBe(2);
|
| 20 |
});
|
| 21 |
|
|
@@ -35,17 +35,25 @@ describe("PvP Draft", () => {
|
|
| 35 |
}
|
| 36 |
});
|
| 37 |
|
| 38 |
-
it("
|
| 39 |
-
const
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
expect(deck.slice(0, STARTING_DECK.length)).toEqual([...STARTING_DECK]);
|
| 42 |
});
|
| 43 |
|
| 44 |
it("validateDraftPicks:合法組成通過、非法拒絕", () => {
|
| 45 |
-
expect(validateDraftPicks([0, 1, 2, 3, 13, 14, 24, 25])).toBe(true); //
|
| 46 |
-
expect(validateDraftPicks([0, 0, 0, 0, 13, 14, 24, 25])).toBe(true); // 允許重複
|
| 47 |
-
expect(validateDraftPicks([0, 1, 2, 3,
|
| 48 |
-
expect(validateDraftPicks([0, 1, 2, 3, 13, 14, 24])).toBe(false); // 只有
|
| 49 |
-
expect(validateDraftPicks([0, 1, 2, 3, 13, 14, 24, 34])).toBe(false); // 34
|
|
|
|
| 50 |
});
|
| 51 |
});
|
|
|
|
| 12 |
import { STARTING_DECK, REWARD_POOL_NORMAL, REWARD_POOL_EPIC, REWARD_POOL_LEGEND } from "../src/cards.js";
|
| 13 |
|
| 14 |
describe("PvP Draft", () => {
|
| 15 |
+
it("計畫為 5 普通 + 3 史詩 + 2 傳說、共 10 抽", () => {
|
| 16 |
+
expect(PVP_DRAFT_PICKS).toBe(10);
|
| 17 |
+
expect(PVP_DRAFT_PLAN.filter((t) => t === "normal").length).toBe(5);
|
| 18 |
+
expect(PVP_DRAFT_PLAN.filter((t) => t === "epic").length).toBe(3);
|
| 19 |
expect(PVP_DRAFT_PLAN.filter((t) => t === "legend").length).toBe(2);
|
| 20 |
});
|
| 21 |
|
|
|
|
| 35 |
}
|
| 36 |
});
|
| 37 |
|
| 38 |
+
it("draftChoices:史詩/傳說排除已選(同稀有不重複)", () => {
|
| 39 |
+
const first = draftChoices(makeRng(7), 8); // index 8 = 傳說
|
| 40 |
+
const picked = first.choices[0]!;
|
| 41 |
+
const next = draftChoices(first.rng, 9, [picked]); // index 9 = 傳說,已選傳入
|
| 42 |
+
expect(next.choices).not.toContain(picked);
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
it("buildPvpDeck = 基礎牌組 + 10 選擇(共 20 張)", () => {
|
| 46 |
+
const deck = buildPvpDeck([0, 1, 2, 3, 5, 13, 14, 15, 24, 25]);
|
| 47 |
+
expect(deck.length).toBe(STARTING_DECK.length + 10);
|
| 48 |
expect(deck.slice(0, STARTING_DECK.length)).toEqual([...STARTING_DECK]);
|
| 49 |
});
|
| 50 |
|
| 51 |
it("validateDraftPicks:合法組成通過、非法拒絕", () => {
|
| 52 |
+
expect(validateDraftPicks([0, 1, 2, 3, 5, 13, 14, 15, 24, 25])).toBe(true); // 5N+3E+2L
|
| 53 |
+
expect(validateDraftPicks([0, 0, 0, 0, 0, 13, 14, 15, 24, 25])).toBe(true); // 普通允許重複
|
| 54 |
+
expect(validateDraftPicks([0, 1, 2, 3, 5, 6, 13, 14, 24, 25])).toBe(false); // 6N+2E+2L
|
| 55 |
+
expect(validateDraftPicks([0, 1, 2, 3, 5, 13, 14, 15, 24])).toBe(false); // 只有 9 張
|
| 56 |
+
expect(validateDraftPicks([0, 1, 2, 3, 5, 13, 14, 15, 24, 34])).toBe(false); // 34 升級卡、不在任何池
|
| 57 |
+
expect(validateDraftPicks([0, 1, 2, 3, 33, 13, 14, 15, 24, 25])).toBe(false); // 33 PvP 普通池已排除
|
| 58 |
});
|
| 59 |
});
|
packages/server/src/pvpHub.ts
CHANGED
|
@@ -12,8 +12,8 @@ export type PvpServerMsg =
|
|
| 12 |
| { type: "ROOM_CREATED"; code: string }
|
| 13 |
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 14 |
// 伺服器權威 draft:每抽由伺服器發 3 張(client 只回 choiceIndex),seed 不外洩
|
| 15 |
-
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[] }
|
| 16 |
-
| { type: "DRAFT_WAIT" } // 你已完成
|
| 17 |
| { type: "PHASE_START"; turnNo: number; isOvertime: boolean; view: PvpView; durationMs: number }
|
| 18 |
| { type: "PLAY_RESULT"; events: GameEvent[]; view: PvpView }
|
| 19 |
| { type: "LOCKED"; view: PvpView } // 確認「你」已鎖定(更新自己畫面)
|
|
@@ -43,6 +43,7 @@ interface RoomDraft {
|
|
| 43 |
picked: Record<Side, number[]>;
|
| 44 |
choices: Record<Side, number[]>; // 該側當前 3 選項
|
| 45 |
done: Record<Side, boolean>;
|
|
|
|
| 46 |
}
|
| 47 |
|
| 48 |
interface Room {
|
|
@@ -56,6 +57,8 @@ interface Room {
|
|
| 56 |
|
| 57 |
export interface PvpHubOptions {
|
| 58 |
tPlanMs?: number;
|
|
|
|
|
|
|
| 59 |
genCode?: () => string;
|
| 60 |
genSeed?: () => number;
|
| 61 |
now?: () => number;
|
|
@@ -74,6 +77,7 @@ export class PvpHub {
|
|
| 74 |
private rooms = new Map<string, Room>();
|
| 75 |
private playerRoom = new Map<string, string>();
|
| 76 |
private tPlanMs: number;
|
|
|
|
| 77 |
private genCode: () => string;
|
| 78 |
private genSeed: () => number;
|
| 79 |
private now: () => number;
|
|
@@ -82,6 +86,7 @@ export class PvpHub {
|
|
| 82 |
|
| 83 |
constructor(opts: PvpHubOptions = {}) {
|
| 84 |
this.tPlanMs = opts.tPlanMs ?? 30000;
|
|
|
|
| 85 |
this.genSeed = opts.genSeed ?? (() => Math.floor(Math.random() * 0x7fffffff));
|
| 86 |
this.now = opts.now ?? (() => Date.now());
|
| 87 |
this.scheduler = opts.scheduler ?? defaultScheduler;
|
|
@@ -141,6 +146,7 @@ export class PvpHub {
|
|
| 141 |
picked: { A: [], B: [] },
|
| 142 |
choices: { A: [], B: [] },
|
| 143 |
done: { A: false, B: false },
|
|
|
|
| 144 |
};
|
| 145 |
this.sendDraftChoice(room, "A");
|
| 146 |
this.sendDraftChoice(room, "B");
|
|
@@ -148,7 +154,8 @@ export class PvpHub {
|
|
| 148 |
|
| 149 |
private sendDraftChoice(room: Room, side: Side): void {
|
| 150 |
const d = room.draft!;
|
| 151 |
-
|
|
|
|
| 152 |
d.rng[side] = r.rng;
|
| 153 |
d.choices[side] = r.choices;
|
| 154 |
room.conns[side]?.send({
|
|
@@ -157,7 +164,11 @@ export class PvpHub {
|
|
| 157 |
total: PVP_DRAFT_PICKS,
|
| 158 |
tier: r.tier,
|
| 159 |
choices: r.choices,
|
|
|
|
| 160 |
});
|
|
|
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
/** 玩家在 draft 階段選第 choiceIndex 張(0..2)。只能從伺服器發的 3 張裡選。 */
|
|
@@ -168,10 +179,16 @@ export class PvpHub {
|
|
| 168 |
if (!room?.draft) return;
|
| 169 |
const side = this.sideOfConn(room, playerId);
|
| 170 |
if (!side) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
const d = room.draft;
|
| 172 |
-
if (d.done[side]) return;
|
| 173 |
const choices = d.choices[side];
|
| 174 |
if (!Number.isInteger(choiceIndex) || choiceIndex < 0 || choiceIndex >= choices.length) return;
|
|
|
|
| 175 |
d.picked[side].push(choices[choiceIndex]!);
|
| 176 |
d.pickIndex[side] += 1;
|
| 177 |
if (d.pickIndex[side] < PVP_DRAFT_PICKS) {
|
|
@@ -183,8 +200,25 @@ export class PvpHub {
|
|
| 183 |
}
|
| 184 |
}
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
private finishDraft(room: Room): void {
|
| 187 |
const d = room.draft!;
|
|
|
|
|
|
|
| 188 |
room.decks = { A: buildPvpDeck(d.picked.A), B: buildPvpDeck(d.picked.B) };
|
| 189 |
room.draft = undefined;
|
| 190 |
this.startMatch(room);
|
|
@@ -338,6 +372,7 @@ export class PvpHub {
|
|
| 338 |
|
| 339 |
private cleanup(room: Room): void {
|
| 340 |
if (room.timer) this.scheduler.clear(room.timer);
|
|
|
|
| 341 |
for (const side of ["A", "B"] as Side[]) {
|
| 342 |
const c = room.conns[side];
|
| 343 |
if (c) this.playerRoom.delete(c.playerId);
|
|
|
|
| 12 |
| { type: "ROOM_CREATED"; code: string }
|
| 13 |
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 14 |
// 伺服器權威 draft:每抽由伺服器發 3 張(client 只回 choiceIndex),seed 不外洩
|
| 15 |
+
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 16 |
+
| { type: "DRAFT_WAIT" } // 你已完成 10 抽,等待對手
|
| 17 |
| { type: "PHASE_START"; turnNo: number; isOvertime: boolean; view: PvpView; durationMs: number }
|
| 18 |
| { type: "PLAY_RESULT"; events: GameEvent[]; view: PvpView }
|
| 19 |
| { type: "LOCKED"; view: PvpView } // 確認「你」已鎖定(更新自己畫面)
|
|
|
|
| 43 |
picked: Record<Side, number[]>;
|
| 44 |
choices: Record<Side, number[]>; // 該側當前 3 選項
|
| 45 |
done: Record<Side, boolean>;
|
| 46 |
+
timer: Partial<Record<Side, TimerHandle>>; // 每側選牌逾時計時器
|
| 47 |
}
|
| 48 |
|
| 49 |
interface Room {
|
|
|
|
| 57 |
|
| 58 |
export interface PvpHubOptions {
|
| 59 |
tPlanMs?: number;
|
| 60 |
+
/** PvP draft 每抽時限(ms,預設 60000);逾時自動選第 1 張。 */
|
| 61 |
+
tDraftMs?: number;
|
| 62 |
genCode?: () => string;
|
| 63 |
genSeed?: () => number;
|
| 64 |
now?: () => number;
|
|
|
|
| 77 |
private rooms = new Map<string, Room>();
|
| 78 |
private playerRoom = new Map<string, string>();
|
| 79 |
private tPlanMs: number;
|
| 80 |
+
private tDraftMs: number;
|
| 81 |
private genCode: () => string;
|
| 82 |
private genSeed: () => number;
|
| 83 |
private now: () => number;
|
|
|
|
| 86 |
|
| 87 |
constructor(opts: PvpHubOptions = {}) {
|
| 88 |
this.tPlanMs = opts.tPlanMs ?? 30000;
|
| 89 |
+
this.tDraftMs = opts.tDraftMs ?? 60000;
|
| 90 |
this.genSeed = opts.genSeed ?? (() => Math.floor(Math.random() * 0x7fffffff));
|
| 91 |
this.now = opts.now ?? (() => Date.now());
|
| 92 |
this.scheduler = opts.scheduler ?? defaultScheduler;
|
|
|
|
| 146 |
picked: { A: [], B: [] },
|
| 147 |
choices: { A: [], B: [] },
|
| 148 |
done: { A: false, B: false },
|
| 149 |
+
timer: {},
|
| 150 |
};
|
| 151 |
this.sendDraftChoice(room, "A");
|
| 152 |
this.sendDraftChoice(room, "B");
|
|
|
|
| 154 |
|
| 155 |
private sendDraftChoice(room: Room, side: Side): void {
|
| 156 |
const d = room.draft!;
|
| 157 |
+
// 史詩/傳說去重需傳入已選清單;普通仍可重複(由 draftChoices 內部判斷)
|
| 158 |
+
const r = draftChoices(d.rng[side], d.pickIndex[side], d.picked[side]);
|
| 159 |
d.rng[side] = r.rng;
|
| 160 |
d.choices[side] = r.choices;
|
| 161 |
room.conns[side]?.send({
|
|
|
|
| 164 |
total: PVP_DRAFT_PICKS,
|
| 165 |
tier: r.tier,
|
| 166 |
choices: r.choices,
|
| 167 |
+
durationMs: this.tDraftMs,
|
| 168 |
});
|
| 169 |
+
// 每抽重設逾時計時器(逾時自動選第 1 張)
|
| 170 |
+
this.clearDraftTimer(room, side);
|
| 171 |
+
d.timer[side] = this.scheduler.set(this.tDraftMs, () => this.onDraftTimeout(room.code, side));
|
| 172 |
}
|
| 173 |
|
| 174 |
/** 玩家在 draft 階段選第 choiceIndex 張(0..2)。只能從伺服器發的 3 張裡選。 */
|
|
|
|
| 179 |
if (!room?.draft) return;
|
| 180 |
const side = this.sideOfConn(room, playerId);
|
| 181 |
if (!side) return;
|
| 182 |
+
this.applyDraftPick(room, side, choiceIndex);
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
/** 套用一次選牌(玩家手動或逾時自動共用)。 */
|
| 186 |
+
private applyDraftPick(room: Room, side: Side, choiceIndex: number): void {
|
| 187 |
const d = room.draft;
|
| 188 |
+
if (!d || d.done[side]) return;
|
| 189 |
const choices = d.choices[side];
|
| 190 |
if (!Number.isInteger(choiceIndex) || choiceIndex < 0 || choiceIndex >= choices.length) return;
|
| 191 |
+
this.clearDraftTimer(room, side);
|
| 192 |
d.picked[side].push(choices[choiceIndex]!);
|
| 193 |
d.pickIndex[side] += 1;
|
| 194 |
if (d.pickIndex[side] < PVP_DRAFT_PICKS) {
|
|
|
|
| 200 |
}
|
| 201 |
}
|
| 202 |
|
| 203 |
+
/** draft 逾時:該側自動選第 1 張(index 0)。 */
|
| 204 |
+
private onDraftTimeout(code: string, side: Side): void {
|
| 205 |
+
const room = this.rooms.get(code);
|
| 206 |
+
if (!room?.draft || room.draft.done[side]) return;
|
| 207 |
+
this.applyDraftPick(room, side, 0);
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
private clearDraftTimer(room: Room, side: Side): void {
|
| 211 |
+
const t = room.draft?.timer[side];
|
| 212 |
+
if (t) {
|
| 213 |
+
this.scheduler.clear(t);
|
| 214 |
+
room.draft!.timer[side] = undefined;
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
private finishDraft(room: Room): void {
|
| 219 |
const d = room.draft!;
|
| 220 |
+
this.clearDraftTimer(room, "A");
|
| 221 |
+
this.clearDraftTimer(room, "B");
|
| 222 |
room.decks = { A: buildPvpDeck(d.picked.A), B: buildPvpDeck(d.picked.B) };
|
| 223 |
room.draft = undefined;
|
| 224 |
this.startMatch(room);
|
|
|
|
| 372 |
|
| 373 |
private cleanup(room: Room): void {
|
| 374 |
if (room.timer) this.scheduler.clear(room.timer);
|
| 375 |
+
if (room.draft) { this.clearDraftTimer(room, "A"); this.clearDraftTimer(room, "B"); }
|
| 376 |
for (const side of ["A", "B"] as Side[]) {
|
| 377 |
const c = room.conns[side];
|
| 378 |
if (c) this.playerRoom.delete(c.playerId);
|
packages/server/src/server.ts
CHANGED
|
@@ -17,8 +17,10 @@ export interface ServerOptions {
|
|
| 17 |
requireVerified?: boolean;
|
| 18 |
/** 提供前端建置目錄路徑時,同進程一併服務靜態檔(單容器部署,如 HF Spaces)。 */
|
| 19 |
webRoot?: string;
|
| 20 |
-
/** PvP PLAN 相時限(ms)。 */
|
| 21 |
tPlanMs?: number;
|
|
|
|
|
|
|
| 22 |
/** 是否啟用開局 draft(預設 true)。 */
|
| 23 |
enableDraft?: boolean;
|
| 24 |
}
|
|
@@ -33,7 +35,7 @@ interface PvpClientMsg {
|
|
| 33 |
export function buildServer(opts: ServerOptions): FastifyInstance {
|
| 34 |
const { repo, requireVerified = false, webRoot } = opts;
|
| 35 |
const app = Fastify({ logger: false, bodyLimit: 1_000_000 });
|
| 36 |
-
const hub = new PvpHub({ tPlanMs: opts.tPlanMs ??
|
| 37 |
|
| 38 |
app.get("/api/health", async () => ({ ok: true, ghosts: repo.count() }));
|
| 39 |
|
|
@@ -95,7 +97,7 @@ export function buildServer(opts: ServerOptions): FastifyInstance {
|
|
| 95 |
}
|
| 96 |
case "DRAFT_PICK": {
|
| 97 |
const ci = Number(msg.choiceIndex);
|
| 98 |
-
if (Number.isInteger(ci) && ci >= 0 && ci <
|
| 99 |
break;
|
| 100 |
}
|
| 101 |
case "LOCK": hub.lock(playerId); break;
|
|
|
|
| 17 |
requireVerified?: boolean;
|
| 18 |
/** 提供前端建置目錄路徑時,同進程一併服務靜態檔(單容器部署,如 HF Spaces)。 */
|
| 19 |
webRoot?: string;
|
| 20 |
+
/** PvP PLAN 相時限(ms,預設 100000)。 */
|
| 21 |
tPlanMs?: number;
|
| 22 |
+
/** PvP draft 每抽時限(ms,預設 60000)。 */
|
| 23 |
+
tDraftMs?: number;
|
| 24 |
/** 是否啟用開局 draft(預設 true)。 */
|
| 25 |
enableDraft?: boolean;
|
| 26 |
}
|
|
|
|
| 35 |
export function buildServer(opts: ServerOptions): FastifyInstance {
|
| 36 |
const { repo, requireVerified = false, webRoot } = opts;
|
| 37 |
const app = Fastify({ logger: false, bodyLimit: 1_000_000 });
|
| 38 |
+
const hub = new PvpHub({ tPlanMs: opts.tPlanMs ?? 100000, tDraftMs: opts.tDraftMs ?? 60000, enableDraft: opts.enableDraft });
|
| 39 |
|
| 40 |
app.get("/api/health", async () => ({ ok: true, ghosts: repo.count() }));
|
| 41 |
|
|
|
|
| 97 |
}
|
| 98 |
case "DRAFT_PICK": {
|
| 99 |
const ci = Number(msg.choiceIndex);
|
| 100 |
+
if (Number.isInteger(ci) && ci >= 0 && ci < 3) hub.draftPick(playerId, ci);
|
| 101 |
break;
|
| 102 |
}
|
| 103 |
case "LOCK": hub.lock(playerId); break;
|
packages/server/test/pvpHub.test.ts
CHANGED
|
@@ -141,7 +141,7 @@ describe("PvpHub — 開局 draft(伺服器權威三選一)", () => {
|
|
| 141 |
return { hub, msgs, connA, connB };
|
| 142 |
}
|
| 143 |
|
| 144 |
-
it("加入後先 DRAFT_CHOICE(非 PHASE_START);雙方各選
|
| 145 |
const { hub, msgs, connA, connB } = draftHarness();
|
| 146 |
hub.createRoom(connA);
|
| 147 |
hub.join("ROOM1", connB);
|
|
@@ -149,21 +149,22 @@ describe("PvpHub — 開局 draft(伺服器權威三選一)", () => {
|
|
| 149 |
expect(types(msgs.A)).toContain("DRAFT_CHOICE");
|
| 150 |
expect(types(msgs.A)).not.toContain("PHASE_START"); // draft 尚未完成
|
| 151 |
const first = msgs.A.find((m) => m.type === "DRAFT_CHOICE") as Extract<PvpServerMsg, { type: "DRAFT_CHOICE" }>;
|
| 152 |
-
expect(first.total).toBe(
|
| 153 |
expect(first.pickIndex).toBe(0);
|
| 154 |
expect(first.choices.length).toBe(3);
|
|
|
|
| 155 |
|
| 156 |
-
for (let i = 0; i <
|
| 157 |
expect(types(msgs.A)).toContain("DRAFT_WAIT"); // A 完成、等 B
|
| 158 |
expect(types(msgs.A)).not.toContain("PHASE_START");
|
| 159 |
-
for (let i = 0; i <
|
| 160 |
|
| 161 |
expect(types(msgs.A)).toContain("PHASE_START");
|
| 162 |
expect(types(msgs.B)).toContain("PHASE_START");
|
| 163 |
const ps = msgs.A.find((m) => m.type === "PHASE_START") as Extract<PvpServerMsg, { type: "PHASE_START" }>;
|
| 164 |
expect(ps.view.you.hp).toBe(200);
|
| 165 |
expect(ps.view.you.hand.length).toBe(5);
|
| 166 |
-
expect(ps.view.you.deckCount).toBe(
|
| 167 |
});
|
| 168 |
|
| 169 |
it("draftPick 越界(choiceIndex 超出 3)被忽略、不前進", () => {
|
|
|
|
| 141 |
return { hub, msgs, connA, connB };
|
| 142 |
}
|
| 143 |
|
| 144 |
+
it("加入後先 DRAFT_CHOICE(非 PHASE_START);雙方各選 10 張後才開戰", () => {
|
| 145 |
const { hub, msgs, connA, connB } = draftHarness();
|
| 146 |
hub.createRoom(connA);
|
| 147 |
hub.join("ROOM1", connB);
|
|
|
|
| 149 |
expect(types(msgs.A)).toContain("DRAFT_CHOICE");
|
| 150 |
expect(types(msgs.A)).not.toContain("PHASE_START"); // draft 尚未完成
|
| 151 |
const first = msgs.A.find((m) => m.type === "DRAFT_CHOICE") as Extract<PvpServerMsg, { type: "DRAFT_CHOICE" }>;
|
| 152 |
+
expect(first.total).toBe(10);
|
| 153 |
expect(first.pickIndex).toBe(0);
|
| 154 |
expect(first.choices.length).toBe(3);
|
| 155 |
+
expect(first.durationMs).toBe(60000); // 每抽 60 秒
|
| 156 |
|
| 157 |
+
for (let i = 0; i < 10; i++) hub.draftPick("pa", 0);
|
| 158 |
expect(types(msgs.A)).toContain("DRAFT_WAIT"); // A 完成、等 B
|
| 159 |
expect(types(msgs.A)).not.toContain("PHASE_START");
|
| 160 |
+
for (let i = 0; i < 10; i++) hub.draftPick("pb", 0);
|
| 161 |
|
| 162 |
expect(types(msgs.A)).toContain("PHASE_START");
|
| 163 |
expect(types(msgs.B)).toContain("PHASE_START");
|
| 164 |
const ps = msgs.A.find((m) => m.type === "PHASE_START") as Extract<PvpServerMsg, { type: "PHASE_START" }>;
|
| 165 |
expect(ps.view.you.hp).toBe(200);
|
| 166 |
expect(ps.view.you.hand.length).toBe(5);
|
| 167 |
+
expect(ps.view.you.deckCount).toBe(15); // 基礎 10 + draft 10 = 20,抽 5 → 牌庫 15
|
| 168 |
});
|
| 169 |
|
| 170 |
it("draftPick 越界(choiceIndex 超出 3)被忽略、不前進", () => {
|
packages/web/src/PvpScreen.tsx
CHANGED
|
@@ -22,7 +22,7 @@ export function PvpScreen({ onExit }: { onExit: () => void }) {
|
|
| 22 |
if (state.status === "lobby") {
|
| 23 |
main = <Lobby pvp={pvp} onExit={onExit} />;
|
| 24 |
} else if (state.status === "drafting" && state.draft) {
|
| 25 |
-
main = <DraftView draft={state.draft} onPick={pvp.draftPick} onExit={onExit} />;
|
| 26 |
} else if (state.status === "draftWait") {
|
| 27 |
main = (
|
| 28 |
<Centered>
|
|
@@ -125,17 +125,20 @@ const TIER_LABEL: Record<DraftTier, string> = { normal: "普通", epic: "史詩"
|
|
| 125 |
|
| 126 |
/** 伺服器權威 draft:三選一由伺服器發牌,client 只回 choiceIndex(改機也跳不出這 3 張)。 */
|
| 127 |
function DraftView({
|
| 128 |
-
draft, onPick, onExit,
|
| 129 |
}: {
|
| 130 |
draft: { pickIndex: number; total: number; tier: DraftTier; choices: number[] };
|
|
|
|
| 131 |
onPick: (choiceIndex: number) => void;
|
| 132 |
onExit: () => void;
|
| 133 |
}) {
|
|
|
|
| 134 |
return (
|
| 135 |
<Centered>
|
| 136 |
<h2 className="title">組牌 Draft</h2>
|
| 137 |
<p className="subtitle">
|
| 138 |
第 {draft.pickIndex + 1} / {draft.total} 抽 · {TIER_LABEL[draft.tier]}卡(三選一)
|
|
|
|
| 139 |
</p>
|
| 140 |
<div className="hand">
|
| 141 |
{draft.choices.map((id, i) => (
|
|
@@ -144,7 +147,7 @@ function DraftView({
|
|
| 144 |
</button>
|
| 145 |
))}
|
| 146 |
</div>
|
| 147 |
-
<p className="msg">基礎牌組 + 三選一挑
|
| 148 |
<button className="btn" onClick={onExit}>返回</button>
|
| 149 |
</Centered>
|
| 150 |
);
|
|
|
|
| 22 |
if (state.status === "lobby") {
|
| 23 |
main = <Lobby pvp={pvp} onExit={onExit} />;
|
| 24 |
} else if (state.status === "drafting" && state.draft) {
|
| 25 |
+
main = <DraftView draft={state.draft} deadlineTs={state.draftDeadlineTs} onPick={pvp.draftPick} onExit={onExit} />;
|
| 26 |
} else if (state.status === "draftWait") {
|
| 27 |
main = (
|
| 28 |
<Centered>
|
|
|
|
| 125 |
|
| 126 |
/** 伺服器權威 draft:三選一由伺服器發牌,client 只回 choiceIndex(改機也跳不出這 3 張)。 */
|
| 127 |
function DraftView({
|
| 128 |
+
draft, deadlineTs, onPick, onExit,
|
| 129 |
}: {
|
| 130 |
draft: { pickIndex: number; total: number; tier: DraftTier; choices: number[] };
|
| 131 |
+
deadlineTs?: number;
|
| 132 |
onPick: (choiceIndex: number) => void;
|
| 133 |
onExit: () => void;
|
| 134 |
}) {
|
| 135 |
+
const left = useCountdown(deadlineTs);
|
| 136 |
return (
|
| 137 |
<Centered>
|
| 138 |
<h2 className="title">組牌 Draft</h2>
|
| 139 |
<p className="subtitle">
|
| 140 |
第 {draft.pickIndex + 1} / {draft.total} 抽 · {TIER_LABEL[draft.tier]}卡(三選一)
|
| 141 |
+
{left != null ? ` · ⏳ ${left}s` : ""}
|
| 142 |
</p>
|
| 143 |
<div className="hand">
|
| 144 |
{draft.choices.map((id, i) => (
|
|
|
|
| 147 |
</button>
|
| 148 |
))}
|
| 149 |
</div>
|
| 150 |
+
<p className="msg">基礎牌組 + 三選一挑 5 普通 + 3 史詩 + 2 傳說(共 {draft.total} 抽)· 逾時自動選第 1 張</p>
|
| 151 |
<button className="btn" onClick={onExit}>返回</button>
|
| 152 |
</Centered>
|
| 153 |
);
|
packages/web/src/pvpClient.ts
CHANGED
|
@@ -21,7 +21,7 @@ export interface PvpView {
|
|
| 21 |
type ServerMsg =
|
| 22 |
| { type: "ROOM_CREATED"; code: string }
|
| 23 |
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 24 |
-
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[] }
|
| 25 |
| { type: "DRAFT_WAIT" }
|
| 26 |
| { type: "PHASE_START"; turnNo: number; isOvertime: boolean; view: PvpView; durationMs: number }
|
| 27 |
| { type: "PLAY_RESULT"; events: GameEvent[]; view: PvpView }
|
|
@@ -42,6 +42,7 @@ export interface PvpState {
|
|
| 42 |
view?: PvpView;
|
| 43 |
deadlineTs?: number;
|
| 44 |
draft?: { pickIndex: number; total: number; tier: DraftTier; choices: number[] };
|
|
|
|
| 45 |
oppLocked: boolean;
|
| 46 |
lastResolve?: PvpResolveResult;
|
| 47 |
outcome?: "WIN" | "LOSE" | "DRAW";
|
|
@@ -105,7 +106,11 @@ function reduce(s: PvpState, m: ServerMsg): PvpState {
|
|
| 105 |
case "MATCH_FOUND":
|
| 106 |
return { ...s, you: m.you, error: undefined }; // 之後接 DRAFT_CHOICE(draft)或 PHASE_START(無 draft)
|
| 107 |
case "DRAFT_CHOICE":
|
| 108 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
case "DRAFT_WAIT":
|
| 110 |
return { ...s, status: "draftWait", draft: undefined };
|
| 111 |
case "PHASE_START":
|
|
|
|
| 21 |
type ServerMsg =
|
| 22 |
| { type: "ROOM_CREATED"; code: string }
|
| 23 |
| { type: "MATCH_FOUND"; matchId: string; you: Side }
|
| 24 |
+
| { type: "DRAFT_CHOICE"; pickIndex: number; total: number; tier: DraftTier; choices: number[]; durationMs: number }
|
| 25 |
| { type: "DRAFT_WAIT" }
|
| 26 |
| { type: "PHASE_START"; turnNo: number; isOvertime: boolean; view: PvpView; durationMs: number }
|
| 27 |
| { type: "PLAY_RESULT"; events: GameEvent[]; view: PvpView }
|
|
|
|
| 42 |
view?: PvpView;
|
| 43 |
deadlineTs?: number;
|
| 44 |
draft?: { pickIndex: number; total: number; tier: DraftTier; choices: number[] };
|
| 45 |
+
draftDeadlineTs?: number;
|
| 46 |
oppLocked: boolean;
|
| 47 |
lastResolve?: PvpResolveResult;
|
| 48 |
outcome?: "WIN" | "LOSE" | "DRAW";
|
|
|
|
| 106 |
case "MATCH_FOUND":
|
| 107 |
return { ...s, you: m.you, error: undefined }; // 之後接 DRAFT_CHOICE(draft)或 PHASE_START(無 draft)
|
| 108 |
case "DRAFT_CHOICE":
|
| 109 |
+
return {
|
| 110 |
+
...s, status: "drafting",
|
| 111 |
+
draft: { pickIndex: m.pickIndex, total: m.total, tier: m.tier, choices: m.choices },
|
| 112 |
+
draftDeadlineTs: Date.now() + m.durationMs,
|
| 113 |
+
};
|
| 114 |
case "DRAFT_WAIT":
|
| 115 |
return { ...s, status: "draftWait", draft: undefined };
|
| 116 |
case "PHASE_START":
|
packages/web/src/styles.css
CHANGED
|
@@ -288,6 +288,12 @@ html, body, #root {
|
|
| 288 |
border-color: var(--gold);
|
| 289 |
box-shadow: 0 0 26px rgba(217, 178, 90, .45);
|
| 290 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
/* ===== 覆蓋層 ===== */
|
| 293 |
.overlay {
|
|
|
|
| 288 |
border-color: var(--gold);
|
| 289 |
box-shadow: 0 0 26px rgba(217, 178, 90, .45);
|
| 290 |
}
|
| 291 |
+
/* 鎖定(disabled)/active 也維持垂直置中,蓋過 .btn:disabled:hover 的 transform:none,按鈕不位移 */
|
| 292 |
+
.end-turn:active,
|
| 293 |
+
.end-turn:disabled,
|
| 294 |
+
.end-turn:disabled:hover {
|
| 295 |
+
transform: translateY(-50%);
|
| 296 |
+
}
|
| 297 |
|
| 298 |
/* ===== 覆蓋層 ===== */
|
| 299 |
.overlay {
|