// 리그 매치메이킹 순수 로직 (§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; }