Files
tetrig/server/src/matchmaking.js
tkrmagid 8fcef8e720 서버: Cloudflare Workers 멀티플레이 백엔드 (Phase 4~6 서버 측)
- 게스트/이메일/Google 계정, 게스트 기록 이전, 닉네임 정책
- GameRoomDO: 커스텀 1:1·FFA, 가비지 중계·배율·장기전, 타겟팅, KO, 재접속 유예, 리매치 투표
- MatchmakerDO + Glicko-2 + TR/18랭크 + 리그 결과 기록
- 리더보드·솔로 기록·시즌/RD 감쇠 Cron
- 단위 테스트 17개 + 종단 테스트 스크립트

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 09:27:15 +09:00

39 lines
1.4 KiB
JavaScript

// 리그 매치메이킹 순수 로직 (§8.3). MatchmakerDO 가 대기열 스냅샷을 넘겨 짝을 받는다.
import { MM_EXPAND_EVERY_MS, MM_EXPAND_FACTOR, MM_WINDOW_RD } from "./rules.js";
/** 대기 시간에 따라 넓어지는 탐색 반경 (R 기준 반폭). */
export function windowFor(entry, now) {
const steps = Math.floor(Math.max(0, now - entry.joinedAt) / MM_EXPAND_EVERY_MS);
return MM_WINDOW_RD * entry.rd * Math.pow(MM_EXPAND_FACTOR, steps);
}
/** 두 사람의 구간 [R-w, R+w] 가 겹치면 성사. */
export function compatible(a, b, now) {
return Math.abs(a.r - b.r) <= windowFor(a, now) + windowFor(b, now);
}
/**
* queue: [{uid, r, rd, joinedAt}] — 오래 기다린 순서로 우선 매칭, 그 안에서는 R 차이가 가장 작은 상대.
* 반환: [[a, b], …]
*/
export function findPairs(queue, now) {
const waiting = [...queue].sort((x, y) => x.joinedAt - y.joinedAt);
const used = new Set();
const pairs = [];
for (const a of waiting) {
if (used.has(a.uid)) continue;
let best = null;
for (const b of waiting) {
if (b.uid === a.uid || used.has(b.uid)) continue;
if (!compatible(a, b, now)) continue;
if (!best || Math.abs(a.r - b.r) < Math.abs(a.r - best.r)) best = b;
}
if (best) {
used.add(a.uid);
used.add(best.uid);
pairs.push([a, best]);
}
}
return pairs;
}