/* Robot Clash - live multiplayer tournament bracket maths (no DOM, no network, runs in Node). The networked part (registration, ready/countdown, match sync) lives in game.js/lobby.js and drives this pure logic, so the hard-to-test bit here is small and fully covered by tests. Every client computes the SAME bracket from the same inputs (frozen player list + seed), so no server is needed to agree on who plays whom. Results come in from the server as a map results[round][pairIndex] = winnerUid; a bye (odd player out) advances on its own. RC.LTourney.order(uids, seed) -> seeded slot order, padded with null byes to a power of two RC.LTourney.rounds(order, results) -> [[slot,...] per round]; later rounds fill from results+byes RC.LTourney.myMatch(order, results, me) -> for the player `me`: { done:true, champion } when the bracket is settled, or { round, pairIndex, opp (uid|null=bye), host (bool) } for the match to play now, or null when `me` is out / not in it. */ (function (root) { 'use strict'; const RC = (root.RC = root.RC || {}); function rng(seed) { let s = (seed || 1) >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; } const pow2 = n => { let p = 1; while (p < n) p *= 2; return Math.max(2, p); }; // deterministic seeded order, padded to a power of two with null (= bye) function order(uids, seed) { const a = (uids || []).filter(Boolean).slice(); const rand = rng(seed); for (let i = a.length - 1; i > 0; i--) { const j = (rand() * (i + 1)) | 0; const t = a[i]; a[i] = a[j]; a[j] = t; } const size = pow2(a.length); while (a.length < size) a.push(null); return a; } // winner of one pair: a reported result, or the only real player (bye), else undefined (not yet) function pairWinner(a, b, res) { if (a && !b) return a; if (b && !a) return b; if (!a && !b) return null; // empty pair stays empty return res === a || res === b ? res : undefined; } function rounds(ord, results) { results = results || {}; const out = [ord.slice()]; let cur = ord; let r = 0; while (cur.length > 1) { const res = results[r] || {}; const next = []; let complete = true; for (let i = 0; i < cur.length; i += 2) { const w = pairWinner(cur[i], cur[i + 1], res[i / 2]); if (w === undefined) complete = false; next.push(w === undefined ? null : w); } out.push(next); if (!complete) break; // round not finished -> stop building further cur = next; r++; } return out; } function myMatch(ord, results, me) { if (!me || ord.indexOf(me) < 0) return null; results = results || {}; let cur = ord, r = 0; while (cur.length > 1) { const i = cur.indexOf(me); if (i < 0) return null; // knocked out in an earlier round const other = i % 2 === 0 ? i + 1 : i - 1; const opp = cur[other], res = (results[r] || {})[Math.floor(i / 2)]; const w = pairWinner(me, opp, res); if (w === undefined) { // my match this round is not decided yet -> this is the one to play return { round: r, pairIndex: Math.floor(i / 2), opp: opp || null, host: opp ? me < opp : true }; // lower uid hosts (matches netfight rule) } if (w !== me) return null; // I lost // I advanced: rebuild next round and go on const next = []; for (let k = 0; k < cur.length; k += 2) { const ww = pairWinner(cur[k], cur[k + 1], (results[r] || {})[k / 2]); next.push(ww === undefined ? null : ww); } cur = next; r++; } return { done: true, champion: cur[0] || null }; } RC.LTourney = { order, rounds, myMatch, pow2 }; if (typeof module !== 'undefined' && module.exports) module.exports = RC; })(typeof window !== 'undefined' ? window : globalThis);