Spaces:
Sleeping
Sleeping
Download packages/server/src/runService.ts from WorstCardGame/worst-card-game: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/spaces/WorstCardGame/worst-card-game/resolve/main/packages/server/src/runService.ts
- Command line
-
hf download hf://spaces/WorstCardGame/worst-card-game/packages/server/src/runService.ts
-
curl -L -o runService.ts https://huggingface.co/spaces/WorstCardGame/worst-card-game/resolve/main/packages/server/src/runService.ts
5.33 kB
| /** | |
| * 單場戰績回報 → 後端權威重播 → 算 RunSummary。 | |
| * | |
| * 不信任 client 送來的任何數值:只收「seed + 動作序列 + 該場設定」,用引擎 replay 重跑, | |
| * 再以 summarizeRun 權威算出 ATK/DEF/破關/Stage(與鬼影重播同等級防偽)。 | |
| */ | |
| import { | |
| replay, | |
| summarizeRun, | |
| validateExtremeDeck, | |
| CLASS_INIT, | |
| MAX_LEVEL, | |
| SANCTUS_MAX_LEVEL, | |
| RULE_VERSION, | |
| CARDS_HASH, | |
| type Action, | |
| type Campaign, | |
| type ClassType, | |
| type RunSummary, | |
| } from "@wcg/battle-core"; | |
| import { intInRange, validateActions } from "./ghostService.js"; | |
| import type { OnlineRunInput } from "./userRepository.js"; | |
| /** 合法職業集合由 battle-core 的 CLASS_INIT 派生 → 新增職業時這個輸入驗證點不必改。 */ | |
| const VALID_CLASSES = new Set<string>(Object.keys(CLASS_INIT)); | |
| /** 連線(pvp/pve)戰績的形態驗證+淨化(後端無法重播 → 採信,但仍限職業合法與名稱長度)。回 null=非法。 */ | |
| export function parseOnlineRun(body: unknown): OnlineRunInput | null { | |
| if (typeof body !== "object" || body === null) return null; | |
| const r = body as Record<string, unknown>; | |
| if (r.kind !== "pvp" && r.kind !== "pve") return null; | |
| if (typeof r.classType !== "string" || !VALID_CLASSES.has(r.classType)) return null; | |
| if (typeof r.cleared !== "boolean") return null; | |
| if (typeof r.enemyName !== "string" || r.enemyName.length < 1 || r.enemyName.length > 40) return null; | |
| return { kind: r.kind, classType: r.classType as ClassType, cleared: r.cleared, enemyName: r.enemyName.slice(0, 40) }; | |
| } | |
| export interface RunReport { | |
| seed: number; | |
| actions: Action[]; | |
| classType: ClassType; | |
| campaign: Campaign; | |
| maxLevel: number; | |
| hardMode?: boolean; | |
| extreme?: boolean; // 極難(insane):重播須帶;且須附自組牌組 | |
| deckOverride?: number[]; // 極難自組 30 張牌(重播洗牌須一致) | |
| ruleVersion: string; | |
| cardsHash: string; | |
| } | |
| /** 驗證回報 body 形態;回錯誤訊息或 null(通過)。 */ | |
| export function validateRunReport(body: unknown): string | null { | |
| if (typeof body !== "object" || body === null) return "body 必須是物件"; | |
| const r = body as Record<string, unknown>; | |
| if (!intInRange(r.seed, -0x80000000, 0xffffffff)) return "seed 必須是整數"; | |
| const actErr = validateActions(r.actions); | |
| if (actErr) return actErr; | |
| if (typeof r.classType !== "string" || !VALID_CLASSES.has(r.classType)) return "classType 非法"; | |
| if (r.campaign !== "slime" && r.campaign !== "sanctus") return "campaign 非法"; | |
| if (!intInRange(r.maxLevel, 1, 99)) return "maxLevel 超出範圍"; | |
| if (r.hardMode !== undefined && typeof r.hardMode !== "boolean") return "hardMode 非法"; | |
| if (r.extreme !== undefined && typeof r.extreme !== "boolean") return "extreme 非法"; | |
| // 極難:必須附「合法的自組 30 張牌」(張數/稀有度上限/副本上限/單職業,validateExtremeDeck 權威把關);非極難則不得帶 deckOverride。 | |
| if (r.extreme === true) { | |
| if (!Array.isArray(r.deckOverride) || !validateExtremeDeck(r.deckOverride as number[], r.classType as ClassType)) return "deckOverride 非法(極難須附合法 30 張牌組)"; | |
| } else if (r.deckOverride !== undefined) { | |
| return "非極難不得帶 deckOverride"; | |
| } | |
| if (typeof r.ruleVersion !== "string" || r.ruleVersion.length < 1 || r.ruleVersion.length > 32) return "缺/非法 ruleVersion"; | |
| if (typeof r.cardsHash !== "string" || r.cardsHash.length < 1 || r.cardsHash.length > 64) return "缺/非法 cardsHash"; | |
| return null; | |
| } | |
| export type RunResult = | |
| | { status: "ok"; summary: RunSummary } | |
| | { status: "version-mismatch" } // client 規則版本與 server 不同 → 不聚合(請玩家重新整理更新) | |
| | { status: "invalid"; reason: string }; | |
| /** 該 campaign 的正式最後一關(除錯用的縮短關卡數不算正式破關)。 */ | |
| function officialMaxLevel(campaign: Campaign): number { | |
| return campaign === "sanctus" ? SANCTUS_MAX_LEVEL : MAX_LEVEL; | |
| } | |
| /** 權威處理一份(已通過形態驗證的)回報。 */ | |
| export function processRunReport(report: RunReport): RunResult { | |
| if (report.ruleVersion !== RULE_VERSION || report.cardsHash !== CARDS_HASH) { | |
| return { status: "version-mismatch" }; | |
| } | |
| let final; | |
| try { | |
| final = replay(report.seed, report.actions, { | |
| classType: report.classType, | |
| campaign: report.campaign, | |
| maxLevel: report.maxLevel, | |
| hardMode: report.hardMode, // 困難會多消費 rng → 不帶會重播失準(戰績算錯/判 invalid) | |
| extreme: report.extreme, // 極難:影響數值/迴避/必殺技;deckOverride 決定洗牌 | |
| deckOverride: report.extreme ? report.deckOverride : undefined, | |
| }); | |
| } catch (e) { | |
| return { status: "invalid", reason: `replay-error: ${(e as Error).message}` }; | |
| } | |
| if (final.phase !== "VICTORY" && final.phase !== "GAME_OVER") { | |
| return { status: "invalid", reason: "該場尚未結束(phase 非 VICTORY/GAME_OVER)" }; | |
| } | |
| const summary = summarizeRun(final); | |
| // 防除錯灌水:縮短關卡數(maxLevel 非正式值)的場次,plays/peak 仍記,但不算「過關」。 | |
| const cleared = summary.cleared && report.maxLevel === officialMaxLevel(report.campaign); | |
| return { status: "ok", summary: { ...summary, cleared } }; | |
| } | |