컴퓨터 난이도 1~60단계·방장 선택·판마다 변화·운, 공정성 확인에 표기, 업데이트 알림 팝업
- 난이도 1~60(쉬움 1~20·보통 21~40·어려움 41~60): 넣을 때 랜덤, 방장이 대기실에서 [랜덤][쉬움][보통][어려움] 선택(botLevel) → 범위 안 무작위 - 다음 판마다 -2~+2, 처음 단계 ±5·같은 범위 안(drift); 말투는 처음 단계(>30 존댓말) - humanBest 확률을 단계별로 보간(BOT_PLAY_ANCHORS: 1·10·30·50·60) - 운(-1~+1): 카드·주사위 게임에서 시작 전에 시드 후보 1~5개를 작업 스레드에서 끝까지 둬 보고 운에 맞는 것을 고름(luck.ts, luck-worker.ts). 해시 공개는 그대로 - 결과 화면 공정성 확인에 컴퓨터별 단계·운, 섞어 본 후보 수 - 업데이트 알림: 빌드 ID meta + /api/version + welcome.build → 상단 팝업 [지금 새로고침하기] [✕] - 문서 03·04 §5.1·06 §12·07 §11.1·09 §9.1, 테스트(bot-level.room, luck, version, e2e bots·pwa) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
76
apps/server/src/rooms/luck.ts
Normal file
76
apps/server/src/rooms/luck.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* The rooms' side of the luck worker (docs/04-stability-security.md §5.1). Playing out a few shuffles takes up to a
|
||||
* couple of seconds for the heaviest games (마작), so it runs on its own thread; the rooms keep running meanwhile.
|
||||
* One worker, jobs in order. If the worker fails or takes too long, the game starts with one fresh seed (no luck).
|
||||
*/
|
||||
import { createSeed, type ChaChaSeed } from '@bg/engine';
|
||||
import type { LuckJob } from '@bg/games/bots/luck';
|
||||
|
||||
type Picked = { seed: ChaChaSeed; tried: number };
|
||||
|
||||
export interface LuckSeeder {
|
||||
seed(job: LuckJob): Promise<Picked>;
|
||||
stop(): void;
|
||||
}
|
||||
|
||||
export function createLuckSeeder(opts: { timeoutMs?: number; log?: (level: 'warn' | 'error', msg: string, extra?: Record<string, unknown>) => void } = {}): LuckSeeder {
|
||||
const timeoutMs = opts.timeoutMs ?? 8000;
|
||||
let worker: Worker | null = null;
|
||||
let nextId = 1;
|
||||
const pending = new Map<number, { resolve: (p: Picked) => void; timer: ReturnType<typeof setTimeout> }>();
|
||||
const fallback = (): Picked => ({ seed: createSeed(), tried: 1 });
|
||||
|
||||
const settle = (id: number, p: Picked) => {
|
||||
const job = pending.get(id);
|
||||
if (!job) return;
|
||||
pending.delete(id);
|
||||
clearTimeout(job.timer);
|
||||
job.resolve(p);
|
||||
};
|
||||
|
||||
const reset = (why: string) => {
|
||||
opts.log?.('error', 'luck worker failed', { why });
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
for (const id of [...pending.keys()]) settle(id, fallback());
|
||||
};
|
||||
|
||||
const get = (): Worker => {
|
||||
if (worker) return worker;
|
||||
const w = new Worker(new URL('./luck-worker.ts', import.meta.url).href);
|
||||
w.onmessage = (e: MessageEvent<{ id: number; seed?: ChaChaSeed; tried?: number; error?: string }>) => {
|
||||
const { id, seed, tried, error } = e.data;
|
||||
if (error || !seed) {
|
||||
opts.log?.('warn', 'luck shuffle failed', { error });
|
||||
settle(id, fallback());
|
||||
} else settle(id, { seed, tried: tried ?? 1 });
|
||||
};
|
||||
w.onerror = (e) => reset(String((e as ErrorEvent).message ?? e));
|
||||
w.unref();
|
||||
worker = w;
|
||||
return w;
|
||||
};
|
||||
|
||||
return {
|
||||
seed(job) {
|
||||
return new Promise<Picked>((resolve) => {
|
||||
const id = nextId++;
|
||||
const timer = setTimeout(() => {
|
||||
opts.log?.('warn', 'luck shuffle timed out', { game: job.gameId });
|
||||
settle(id, fallback());
|
||||
}, timeoutMs);
|
||||
pending.set(id, { resolve, timer });
|
||||
try {
|
||||
get().postMessage({ id, job });
|
||||
} catch (err) {
|
||||
reset(String(err));
|
||||
}
|
||||
});
|
||||
},
|
||||
stop() {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
for (const id of [...pending.keys()]) settle(id, fallback());
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user