컴퓨터 난이도 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:
@@ -1,5 +1,5 @@
|
||||
/** Wires DB, auth, rooms, HTTP and WebSocket into one Bun server (docs/02 §2). */
|
||||
import { existsSync } from 'node:fs';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { join, normalize } from 'node:path';
|
||||
import type { Server } from 'bun';
|
||||
import { GAMES } from '@bg/games';
|
||||
@@ -11,6 +11,7 @@ import { SESSION_COOKIE, Sessions } from './auth/sessions';
|
||||
import { Users, toPublicUser } from './auth/users';
|
||||
import { createHttpApp, type HttpDeps } from './http/app';
|
||||
import { RoomManager } from './rooms/manager';
|
||||
import { createLuckSeeder } from './rooms/luck';
|
||||
import type { BotDelayInput } from './rooms/bots';
|
||||
import { RoomStore } from './rooms/store';
|
||||
import { Replays } from './rooms/replay';
|
||||
@@ -38,6 +39,8 @@ export interface StartOptions {
|
||||
botDelay?: (input: BotDelayInput) => number;
|
||||
/** Upper bound for waiting on Bun's server.stop() (tests shorten it). */
|
||||
stopWaitMs?: number;
|
||||
/** Computers' luck on a worker thread (docs/04 §5.1); default on. */
|
||||
luck?: boolean;
|
||||
}
|
||||
|
||||
export function startServer(opts: StartOptions) {
|
||||
@@ -60,10 +63,21 @@ export function startServer(opts: StartOptions) {
|
||||
// Computers talk and play like people with a local model on the GPU when AI_URL is set (docs/06 §12.1, docs/11 §8).
|
||||
const ai = config.ai ? new Llm({ url: config.ai.url, model: config.ai.model, log }) : null;
|
||||
if (ai) void ai.warm().then((ok) => log('info', 'ai model', { model: config.ai!.model, ready: ok }));
|
||||
const rooms = new RoomManager({ store, games: GAMES, users: { publicUser }, log, settings, botDelay: opts.botDelay, ai, offlineSince: (id): number | null => gateway.offlineSince(id) });
|
||||
const luck = opts.luck === false ? null : createLuckSeeder({ log });
|
||||
const rooms = new RoomManager({
|
||||
store,
|
||||
games: GAMES,
|
||||
users: { publicUser },
|
||||
log,
|
||||
settings,
|
||||
botDelay: opts.botDelay,
|
||||
ai,
|
||||
luckSeed: luck ? (job) => luck.seed(job) : undefined,
|
||||
offlineSince: (id): number | null => gateway.offlineSince(id),
|
||||
});
|
||||
const restored = rooms.restoreAll();
|
||||
log('info', 'rooms restored', restored);
|
||||
const gateway = new Gateway({ rooms, me: publicUser, log, onClose: (id) => users.touch(id) });
|
||||
const gateway = new Gateway({ rooms, me: publicUser, log, onClose: (id) => users.touch(id), build: () => build });
|
||||
const removeGuest = (id: string) => {
|
||||
rooms.leaveAll(id);
|
||||
gateway.closeUser(id);
|
||||
@@ -85,8 +99,13 @@ export function startServer(opts: StartOptions) {
|
||||
server?.requestIP(req)?.address ||
|
||||
'unknown';
|
||||
|
||||
const staticDir = opts.staticDir === undefined ? join(import.meta.dir, '../../web/dist') : opts.staticDir;
|
||||
const serveStatic = staticDir && existsSync(staticDir);
|
||||
// The web build being served (docs/07 §11.1): pages that were opened with an older one offer to refresh.
|
||||
const build = serveStatic ? webBuildId(staticDir!) : null;
|
||||
const app = createHttpApp({
|
||||
config,
|
||||
build,
|
||||
users,
|
||||
sessions,
|
||||
rooms,
|
||||
@@ -121,8 +140,6 @@ export function startServer(opts: StartOptions) {
|
||||
},
|
||||
});
|
||||
|
||||
const staticDir = opts.staticDir === undefined ? join(import.meta.dir, '../../web/dist') : opts.staticDir;
|
||||
const serveStatic = staticDir && existsSync(staticDir);
|
||||
|
||||
const server: Server<WsData> = Bun.serve<WsData>({
|
||||
port: config.port,
|
||||
@@ -171,6 +188,7 @@ export function startServer(opts: StartOptions) {
|
||||
clearInterval(sweeper);
|
||||
gateway.shutdown();
|
||||
rooms.shutdown();
|
||||
luck?.stop();
|
||||
// Bun's stop() promise can stay pending after server-initiated WS closes even though the
|
||||
// listener is already closed, so bound the wait.
|
||||
await Promise.race([server.stop(true), Bun.sleep(opts.stopWaitMs ?? 1000)]);
|
||||
@@ -180,6 +198,15 @@ export function startServer(opts: StartOptions) {
|
||||
return { server, stop, rooms, users, sessions, db, gateway, settings, roles, purgeIdleGuests };
|
||||
}
|
||||
|
||||
/** The build id the web build wrote into its index.html (`<meta name="bg-build">`, vite.config.ts), or null. */
|
||||
export function webBuildId(root: string): string | null {
|
||||
try {
|
||||
return /<meta name="bg-build" content="([0-9a-f]+)"/.exec(readFileSync(join(root, 'index.html'), 'utf8'))?.[1] ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function serveFile(root: string, pathname: string): Promise<Response> {
|
||||
const safe = normalize(decodeURIComponent(pathname)).replace(/^(\.\.[/\\])+/, '');
|
||||
const candidate = join(root, safe);
|
||||
|
||||
Reference in New Issue
Block a user