Files
joke-app/packages/shared/src/bots.ts
EJClaw 71381087cc 야단법석 달리기(Magical Athlete): 규칙·컴퓨터 상대·화면·도움말·문서·E2E
- 규칙 모듈 packages/games/src/magical-athlete: 선수 24명(능력 처리 순서가 하나로 정해지는 선수만),
  뱀 순서 뽑기(2~3명은 선수 8명·경주마다 둘, 4~6명은 4명·하나), 몰래 동시에 출전 선수 고르기,
  경주 4번(순한 판·거친 판 번갈아, 거친 판은 화살표·돌·별 칸), 1등 3점·2등 1점(갈수록 커지는 점수표 옵션).
  능력 처리 순서 고정: 칸 효과 → 차례인 사람 → 시계 방향, 연쇄는 줄 끝, 선수당 한 차례 5번, 두 번째 도착 즉시 종료,
  경주 차례 상한. 시간 초과는 안전한 기본값(능력 안 씀), 나간 사람 선수는 자동으로 끝까지.
- 컴퓨터: 평가값 뽑기, 칸 수 기대 이득으로 능력 판단, 레벨별 실수.
- 화면: 뱀 모양 경주로 SVG(PC 11칸·휴대폰 8칸 줄), 말 이동 애니메이션, 능력 버튼, 경주 기록, 결과 표,
  도움말·30초 요약. 판이 화면보다 길면 위에서 시작(가운데 정렬이면 위가 잘려 고르기 카드를 못 누름).
- 등록(GAMES, CATALOG, BOT_GAMES, BOT_STRATEGIES, 화면 레지스트리, 아이콘·판 탭), 문서 docs/games/magical-athlete.md.
- 테스트: 규칙·능력·순서 42개(문서 예시 포함), 컴퓨터 10개, 도움말 2개, 방 통합 2개(컴퓨터 1·3명과 끝까지),
  E2E(PC 방장 + 휴대폰, 실제 뽑기·고르기·굴리기·능력으로 네 경주 끝까지).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 03:49:48 +09:00

102 lines
3.9 KiB
TypeScript

/** Computer players for solo practice (docs/06-lobby-rooms.md §12). Shared by server and web (no runtime deps). */
import type { PublicUser } from './types';
/** Bot user ids look like `bot:<n>:<ulid>`; real user ids are plain ULIDs, so they can never collide. */
export const BOT_PREFIX = 'bot:';
export const isBotId = (id: string): boolean => id.startsWith(BOT_PREFIX);
export const makeBotId = (n: number, unique: string): string => `${BOT_PREFIX}${n}:${unique}`;
/** Number shown in the bot's name ("컴퓨터 2"). */
export function botNumber(id: string): number | null {
if (!isBotId(id)) return null;
const n = Number(id.slice(BOT_PREFIX.length).split(':')[0]);
return Number.isInteger(n) && n > 0 ? n : null;
}
/** Public profile of a bot id, or null for anything else. */
export function botProfile(id: string): PublicUser | null {
const n = botNumber(id);
return n === null ? null : { id, nickname: `컴퓨터 ${n}`, avatar: null, kind: 'bot' };
}
/**
* Games that offer computer players. Real-time and drawing games (도블, 할리갈리, 그림 맞히기) don't: a bot there is
* either unbeatable or meaningless. 마피아 has talking computers (2026-10-11). 사과 게임 is a race where everyone solves
* their own copy of the board, so a computer simply races too at its level's speed (2026-10-12). 라이어 게임 has
* talking computers like 마피아 (2026-10-12).
*/
export const BOT_GAMES: readonly string[] = [
'omok',
'othello',
'chess',
'janggi',
'baduk',
'seotda',
'poker',
'yacht',
'yut',
'one-card',
'rummikub',
'gostop',
'bang',
'davinci-code',
'mahjong',
'marble',
'dalmuti',
'splendor',
'quoridor',
'las-vegas',
'cockroach-poker',
'ninety-nine',
'old-maid',
'mafia',
'apple',
'clue',
'connect-four',
'uno',
'liar',
'magical-athlete',
];
/** Players a practice room starts with when the minimum makes a thin game (마피아 4명 = 마피아 1, 경찰, 의사, 시민 1; 라이어 게임 3명). */
export const PRACTICE_PLAYERS: Readonly<Record<string, number>> = { mafia: 6, liar: 5 };
export const botsAllowed = (gameId: string): boolean => BOT_GAMES.includes(gameId);
/**
* Games where computers must talk to play at all (마피아 토론, 라이어 게임 설명·토론): their computers always speak, whatever the room's
* "컴퓨터 채팅" setting says. In every other game the computers' small talk is off unless the host turns it on.
*/
export const BOT_CHAT_REQUIRED: readonly string[] = ['mafia', 'liar'];
export const botChatRequired = (gameId: string): boolean => BOT_CHAT_REQUIRED.includes(gameId);
/**
* Computer levels 1~60 (docs/09-game-engine.md §9.1): 1~20 쉬움, 21~40 보통, 41~60 어려움. The host can pick a band
* for each computer after adding it; the exact level inside the band is drawn at random and stays hidden until the
* game is over (fairness check).
*/
export const BOT_LEVEL_MAX = 60;
export type BotBand = 'easy' | 'normal' | 'hard';
export type BotPick = 'random' | BotBand;
export const BOT_PICKS: readonly BotPick[] = ['random', 'easy', 'normal', 'hard'];
export const BOT_BANDS: Readonly<Record<BotBand, readonly [number, number]>> = { easy: [1, 20], normal: [21, 40], hard: [41, 60] };
export const BOT_PICK_LABEL: Readonly<Record<BotPick, string>> = { random: '랜덤', easy: '쉬움', normal: '보통', hard: '어려움' };
export function bandOf(level: number): BotBand {
return level <= BOT_BANDS.easy[1] ? 'easy' : level <= BOT_BANDS.normal[1] ? 'normal' : 'hard';
}
/**
* Luck in [-1, 1] as people read it. A computer's luck decides how many shuffles the server tries before a game and
* whether it keeps the best or the worst one for that computer (docs/04-stability-security.md §5.1).
*/
export function luckLabel(luck: number): string {
if (luck <= -0.6) return '아주 나쁨';
if (luck <= -0.2) return '나쁨';
if (luck < 0.2) return '보통';
return luck < 0.6 ? '좋음' : '아주 좋음';
}