/** * 타자 대결 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; 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 { 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 }; }