채팅 연타가 몇 초씩 밀리던 문제: 브라우저가 0.55초(마피아·라이어 0.75초)에 한 줄씩만 내보내던 대기열과 게임 채팅 0.7초 간격 제한을 없애고, 방 채팅·마피아·라이어가 같은 속도 규칙(한 번에 6줄, 그 뒤 0.3초에 한 줄)을 쓰게. 사람이 치는 속도로는 기다리지 않고 바로 올라감

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-14 04:09:29 +09:00
parent cf2903dc75
commit ff3c53ab4a
17 changed files with 115 additions and 41 deletions

View File

@@ -14,7 +14,7 @@ import { BotBrain, type BotActivity } from '../ai/brain';
import type { Llm } from '../ai/llm';
import type { LogEntry, RoomStore } from './store';
import { applyLocks, type EffectiveGame, type SiteSettings } from '../admin/settings';
import { TokenBucket } from '../ws/rate-limit';
import { chatNext, chatWait } from '@bg/games/chat-pace';
export interface Conn {
userId: string;
@@ -152,7 +152,8 @@ export class Room {
private flushTimer: ReturnType<typeof setTimeout> | null = null;
private recentCs = new Map<string, number[]>();
private lastChatAt = new Map<string, number>();
private chatBuckets = new Map<string, TokenBucket>();
/** Room chat pace per person (`chat-pace.ts`). */
private chatPace = new Map<string, number>();
/** When each person last said they were typing (to drop repeats within a second). */
private typingAt = new Map<string, number>();
private consecutiveErrors = 0;
@@ -810,10 +811,9 @@ export class Room {
if (!this.isParticipant(userId)) return '방에 들어와야 채팅할 수 있어요.';
if (this.gameChatOnly()) return '게임 중에는 게임 화면의 채팅을 써 주세요.';
const now = this.now();
// A few lines in a row go through (people often send 2~3 quick lines); a flood still stops.
let bucket = this.chatBuckets.get(userId);
if (!bucket) this.chatBuckets.set(userId, (bucket = new TokenBucket(2, 4, now)));
if (!bucket.take(now)) return '조금 천천히 보내 주세요.';
// Quick lines in a row all go through at once; only a flood stops.
if (chatWait(this.chatPace.get(userId), now) > 0) return '조금 천천히 보내 주세요.';
this.chatPace.set(userId, chatNext(this.chatPace.get(userId), now));
this.typingAt.delete(userId);
const clean = text.replace(/[\u0000-\u001f\u007f]/g, ' ').trim();
if (!clean) return null;