Files
joke-app/packages/games/src/typing/bot.ts
EJClaw 943e4be3fd 타자 대결 연습 모드: 한컴 타자연습처럼 짧은글 10문장·단어 20개·긴글 한 편 전체를 제한 시간·점수 없이
- 대결 방식에 "연습" 추가(각자 치기, 라운드 1번, 시계·점수 없음). 긴글은 고른 주제 중 한 편을 처음부터 끝까지(8줄 단락이 아님).
- 줄마다 첫 타부터 시간을 재서 줄 사이에 쉬어도 타수가 안 떨어짐(첫 타 몫 0.15초 앞당김 → 붙여 넣기 막기 그대로).
  줄 최고 타수(best) 기록. 컴퓨터도 연습에서는 첫 타를 바로 보냄.
- 화면: 라운드 대신 걸린 시간, 지금·평균·최고 타수·정확도·진행 %, 끝나면 평균·최고·정확도·틀린 줄, 참가자 옆은 타수.
  설정에서 라운드·제한 시간 숨김, 종목 이름 "연습 종목", 도움말·규칙·요약 카드·docs.
- 테스트: 연습 4개(엔진), 무작위 대국 2가지, 컴퓨터 1가지, 도움말 1개, e2e typingPractice·typingPracticeLong.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 17:09:37 +09:00

90 lines
4.7 KiB
TypeScript

/**
* 타자 대결 computer players (docs/games/typing.md §10). A live bot like 사과 게임's: it types on its own timer, sends
* its progress now and then (so its bar moves like a person's) and presses Enter when it has typed the whole item.
* It only reads its own screen (`view`).
*
* The level (1~60) sets how it types, like a person getting better:
* - speed: about 120타 a minute at level 1 … 620타 at level 60 (English: 100 … 500 letters a minute),
* - a moment to read before typing (0.9 s … 0.35 s),
* - typos: a weak computer often makes one in a sentence; a strong one rarely. In 가장 먼저 입력 and 서바이벌 a typo
* is noticed when Enter is refused, then fixed; in the other modes it stays in the line (lower accuracy).
* The computer's own pace (`persona.pace`) makes some a little quicker or slower than others of the same level.
*/
import type { LiveBotDecision, LiveBotInput } from '../bots/types';
import { keyCount } from './keys';
import type { TypingClientAction, TypingView } from './types';
type In = LiveBotInput<TypingView>;
const DEFAULT_LEVEL = 30;
export const skillOf = (level: number | undefined): number => Math.max(0, Math.min(1, ((level ?? DEFAULT_LEVEL) - 1) / 59));
/** Keystrokes a minute at this skill (before the computer's own pace). */
export const botKpm = (skill: number, lang: 'ko' | 'en') => (lang === 'ko' ? 120 + 500 * skill : 100 + 400 * skill);
/** One wrong letter somewhere in the middle of the text. */
export function withTypo(text: string, random: () => number): string {
const chars = [...text];
if (!chars.length) return 'ㅋ';
const i = Math.floor(random() * chars.length);
const ko = /[가-힣]/.test(text);
const pool = ko ? '가나다라마바사아자차카타파하' : 'abcdefghijklmnopqrstuvwxyz';
let c = pool[Math.floor(random() * pool.length)]!;
if (c === chars[i]) c = ko ? '흐' : 'q';
chars[i] = c;
return chars.join('');
}
interface Plan {
key: string;
/** When typing (after reading) began, when it will be done. */
from: number;
doneAt: number;
typo: boolean;
}
export function typingBot({ me, view: v, now, random, persona, level, memory }: In): LiveBotDecision<TypingClientAction> {
if (v.phase === 'ended') return { action: null, next: 10_000 };
if (v.phase !== 'playing') return { action: null, next: Math.max(100, (v.phaseEndsAt ?? now + 1000) - now + 60) };
const seat = v.seats.find((s) => s.id === me);
if (!seat || !seat.alive || seat.finished) return { action: null, next: 1500 };
const item = v.upcoming[0];
const shared = v.flow === 'shared';
if (!item) return { action: null, next: shared ? Math.max(100, v.curAt - now + 60) : 1000 };
if (shared && seat.solved) return { action: null, next: 700 };
const skill = skillOf(level);
const key = `${v.round}:${item.idx}`;
let plan = memory.plan as Plan | undefined;
if (!plan || plan.key !== key) {
const speed = botKpm(skill, v.options.lang) / (0.75 + 0.25 * persona.pace) * (0.85 + 0.3 * random());
const read = (900 - 550 * skill) * (0.7 + 0.6 * random());
const start = shared ? Math.max(now, v.curAt) : now;
const keys = keyCount(item.text);
const typoChance = Math.min(0.7, (0.35 - 0.31 * skill) * Math.max(0.3, keys / 40));
plan = { key, from: start + read, doneAt: start + read + (keys * 60_000) / speed, typo: random() < typoChance };
memory.plan = plan;
}
if (now < plan.doneAt) {
const frac = Math.max(0, Math.min(1, (now - plan.from) / Math.max(1, plan.doneAt - plan.from)));
const chars = [...item.text];
// 연습 times a line from its first key, so that key goes out as soon as it is typed (as a person's screen does).
const practice = v.options.mode === 'practice';
const typed = chars.slice(0, Math.max(practice && now >= plan.from ? 1 : 0, Math.floor(chars.length * frac))).join('');
let wait = Math.min(Math.round(350 + random() * 300), Math.max(60, Math.ceil(plan.doneAt - now)));
if (practice && now < plan.from) wait = Math.min(wait, Math.max(1, Math.ceil(plan.from - now)));
return typed ? { action: { type: 'progress', item: item.idx, text: typed }, next: wait } : { action: null, next: wait };
}
if (plan.typo) {
const text = withTypo(item.text, random);
if (shared) {
// Enter is refused: it looks for the mistake and fixes it.
plan.typo = false;
plan.from = now;
plan.doneAt = now + 500 + (1600 - 1000 * skill) * (0.6 + 0.8 * random());
return { action: { type: 'submit', item: item.idx, text }, next: Math.ceil(plan.doneAt - now) };
}
return { action: { type: 'submit', item: item.idx, text }, next: 120 };
}
return { action: { type: 'submit', item: item.idx, text: item.text }, next: shared ? 600 : 120 };
}