122 lines
5.8 KiB
JavaScript
122 lines
5.8 KiB
JavaScript
/* Robot Clash - live multiplayer tournament over Firebase (needs net.js + ltourney.js).
|
|
Players register, tick "ready", a 30 s countdown starts once >=2 are ready, then the bracket is
|
|
frozen and each pairing is played as a normal live PvP match (game.js supplies startMatch).
|
|
No server: the bracket is computed the same way on every client from the frozen list + seed
|
|
(RC.LTourney), results are shared through /tourney/results, a missing opponent is a walkover.
|
|
|
|
const T = RC.NetTourney(net, { uid, name, flag }, {
|
|
onView(v){} // v = { phase, players, readyCount, countdown, order, results, me:{match,champion,out} }
|
|
startMatch(info){} // info = { id, host, seed, opp:{uid,name,build,paint,flag} } -> game starts the fight
|
|
});
|
|
T.join(build, paint) / T.leave() / T.setReady(bool) / T.tick(dt) / T.reportWin(round, pair, uid)
|
|
T.close()
|
|
|
|
/tourney in Firebase:
|
|
reg/<uid> = { name, flag, build, paint, ready, ts } (each writes only their own)
|
|
meta = { phase:'open'|'countdown'|'running'|'done', startAt, seed, frozen:[uid...] }
|
|
results/<round>/<pair> = winnerUid */
|
|
(function (root) {
|
|
'use strict';
|
|
const RC = (root.RC = root.RC || {});
|
|
const COUNTDOWN = 30000, WALKOVER = 60000, STALE = 15000;
|
|
|
|
RC.NetTourney = function (net, me, opt) {
|
|
const db = net.db, cb = opt || {};
|
|
let reg = {}, meta = {}, results = {};
|
|
let inMatch = false, roundStart = 0, lastRound = -1, wroteRun = false, wroteCount = false;
|
|
const subs = [];
|
|
|
|
subs.push(db.stream('tourney/reg', v => { reg = v || {}; emit(); }));
|
|
subs.push(db.stream('tourney/meta', v => { meta = v || {}; emit(); }));
|
|
subs.push(db.stream('tourney/results', v => { results = v || {}; emit(); }));
|
|
|
|
const now = () => Date.now();
|
|
const readyUids = () => Object.keys(reg).filter(u => reg[u] && reg[u].ready && now() - (reg[u].ts || 0) < STALE * 3);
|
|
const frozen = () => (meta.frozen && meta.frozen.length ? meta.frozen : null);
|
|
|
|
function view() {
|
|
const phase = meta.phase || 'open';
|
|
const players = Object.keys(reg).map(u => ({ uid: u, name: reg[u].name, flag: reg[u].flag, ready: !!reg[u].ready, me: u === me.uid }));
|
|
const v = { phase, players, readyCount: readyUids().length, countdown: null, order: null, results: results, me: {} };
|
|
if (phase === 'countdown' && meta.startAt) v.countdown = Math.max(0, Math.ceil((meta.startAt - now()) / 1000));
|
|
const fr = frozen();
|
|
if (fr && (phase === 'running' || phase === 'done')) {
|
|
const ord = RC.LTourney.order(fr, meta.seed || 1);
|
|
v.order = ord;
|
|
const m = RC.LTourney.myMatch(ord, results, me.uid);
|
|
if (!m) v.me.out = true;
|
|
else if (m.done) v.me.champion = m.champion;
|
|
else v.me.match = m;
|
|
}
|
|
return v;
|
|
}
|
|
function emit() { if (cb.onView) cb.onView(view()); drive(); }
|
|
|
|
// my own registration row (heartbeat keeps it fresh)
|
|
let myRow = null;
|
|
function writeRow() {
|
|
if (!myRow) return;
|
|
db.put('tourney/reg/' + me.uid, Object.assign({ ts: now() }, myRow));
|
|
}
|
|
function join(build, paint) {
|
|
myRow = { name: me.name, flag: me.flag || 'XX', ready: false, build: build, paint: paint };
|
|
writeRow();
|
|
}
|
|
function leave() { myRow = null; db.del('tourney/reg/' + me.uid); }
|
|
function setReady(on) { if (myRow) { myRow.ready = !!on; writeRow(); } }
|
|
|
|
// state machine - every client runs it, writes are guarded so the first one wins
|
|
function drive() {
|
|
const phase = meta.phase || 'open';
|
|
if (phase === 'open' && readyUids().length >= 2 && !wroteCount) {
|
|
wroteCount = true;
|
|
db.patch('tourney/meta', { phase: 'countdown', startAt: now() + COUNTDOWN, seed: (Math.random() * 1e9) | 0 });
|
|
}
|
|
if (phase === 'countdown') {
|
|
wroteCount = true;
|
|
if (meta.startAt && now() >= meta.startAt && !wroteRun) {
|
|
wroteRun = true;
|
|
db.patch('tourney/meta', { phase: 'running', frozen: readyUids().sort() });
|
|
}
|
|
}
|
|
if (phase === 'running') {
|
|
const fr = frozen(); if (!fr) return;
|
|
const ord = RC.LTourney.order(fr, meta.seed || 1);
|
|
const m = RC.LTourney.myMatch(ord, results, me.uid);
|
|
if (!m || m.done) { if (m && m.done && !meta.doneWrote) db.patch('tourney/meta', { phase: 'done' }); return; }
|
|
if (m.round !== lastRound) { lastRound = m.round; roundStart = now(); }
|
|
if (m.opp == null) return; // bye: bracket advances by itself
|
|
if (inMatch) return;
|
|
// opponent gone / never there -> walkover after the timer
|
|
const oppReg = reg[m.opp];
|
|
const oppGone = !oppReg || now() - (oppReg.ts || 0) > STALE;
|
|
if (oppGone && now() - roundStart > WALKOVER) { reportWin(m.round, m.pairIndex, me.uid); return; }
|
|
// both present -> the host kicks the match off once (guest joins the same match id)
|
|
if (!oppReg) return;
|
|
inMatch = true;
|
|
const id = [me.uid, m.opp].sort().join('_');
|
|
if (cb.startMatch) cb.startMatch({ id: id, host: m.host, seed: (meta.seed || 1) + m.round * 7,
|
|
round: m.round, pairIndex: m.pairIndex, oppUid: m.opp,
|
|
opp: { uid: m.opp, name: oppReg.name, build: oppReg.build, paint: oppReg.paint, flag: oppReg.flag } });
|
|
}
|
|
}
|
|
|
|
function reportWin(round, pair, uid) {
|
|
inMatch = false;
|
|
db.patch('tourney/results/' + round, { [pair]: uid });
|
|
}
|
|
function matchDone() { inMatch = false; }
|
|
|
|
return {
|
|
join, leave, setReady, reportWin, matchDone,
|
|
tick() { /* heartbeat + state progress */ writeRow(); drive(); },
|
|
view,
|
|
// host-only admin reset (clears everything for a fresh tournament)
|
|
reset() { db.del('tourney'); },
|
|
close() { subs.forEach(s => s && s.close()); },
|
|
};
|
|
};
|
|
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = RC;
|
|
})(typeof window !== 'undefined' ? window : globalThis);
|