- net.js: REST + one WebSocket with reconnect; single-flight refresh shared across tabs; ?slot=N - lobby.js / netfight.js: server owns presence, challenges and the fight; client shows snapshots - game.js: every workshop change goes through RC.Eco via act() (server-checked online); results, rewards and achievements come from the server (match.end) - ranking/live/cloudsave on the new REST endpoints; online tournament removed until P2-6 BREAKING CHANGE: needs the new server (protocol v1); old online saves are not migrated.
66 lines
4.0 KiB
JavaScript
66 lines
4.0 KiB
JavaScript
/* Robot Clash - online lobby over the server WebSocket (net.socket). No DOM. The server owns presence,
|
|
challenges and matches; this just mirrors its state and forwards clicks.
|
|
|
|
const lobby = RC.Lobby({ net })
|
|
lobby.enter() / leave()
|
|
lobby.onList(fn) fn([{ uid, name, flag, fame, state, room }]) everyone online except me
|
|
lobby.onIncoming(fn) fn({ from, fromName, fromFlag }) someone challenges me
|
|
lobby.onCancel(fn) fn({ reason }) my challenge / an incoming one fell through
|
|
lobby.onMatch(fn) fn(matchStartMsg) fight on (also after a reconnect)
|
|
lobby.onEnd(fn) fn(matchEndMsg) server result + rewards + new save
|
|
lobby.onError(fn) fn(code) BUSY | INVALID_ROBOT | ...
|
|
lobby.challenge(uid) / accept(from) / decline(from) / cancel() / setRoom(code|'all')
|
|
lobby.pending uid I challenged (or null) */
|
|
(function (root) {
|
|
'use strict';
|
|
const RC = (root.RC = root.RC || {});
|
|
|
|
RC.Lobby = (opt) => {
|
|
const net = opt.net, sock = net.socket;
|
|
const cbs = { list: [], incoming: [], cancel: [], match: [], end: [], error: [] };
|
|
const emit = (k, v) => cbs[k].forEach(fn => { try { fn(v); } catch (e) { console.error(e); } });
|
|
const players = new Map();
|
|
let me = null, pending = null, offs = [], incomingFrom = null;
|
|
|
|
const list = () => emit('list', [...players.values()].sort((a, b) => (b.fame | 0) - (a.fame | 0) || String(a.name).localeCompare(b.name)));
|
|
|
|
function wire() {
|
|
offs = [
|
|
sock.on('lobby', m => { players.clear(); for (const p of m.players) players.set(p.uid, p); me = m.you; list(); }),
|
|
sock.on('lobby.join', m => { players.set(m.player.uid, m.player); list(); }),
|
|
sock.on('lobby.state', m => { if (m.player.uid === net.uid) { me = m.player; return; } players.set(m.player.uid, m.player); list(); }),
|
|
sock.on('lobby.leave', m => { players.delete(m.uid); list(); }),
|
|
sock.on('challenge.in', m => { incomingFrom = m.from; emit('incoming', { from: m.from, fromName: m.name, fromFlag: m.flag, fame: m.fame }); }),
|
|
sock.on('challenge.gone', m => {
|
|
if (m.to && m.to === pending) { pending = null; emit('cancel', { reason: m.reason || 'gone', outgoing: true }); }
|
|
if (m.from && m.from === incomingFrom) { incomingFrom = null; emit('cancel', { reason: m.reason || 'gone', outgoing: false }); }
|
|
}),
|
|
sock.on('match.start', m => { pending = null; incomingFrom = null; emit('match', m); }),
|
|
sock.on('match.end', m => emit('end', m)),
|
|
sock.on('error', m => { if (['BUSY', 'NOT_AVAILABLE', 'INVALID_ROBOT', 'OPPONENT_INVALID'].includes(m.code)) pending = null; emit('error', m.code); }),
|
|
sock.on('close', () => { players.clear(); pending = null; list(); }),
|
|
];
|
|
}
|
|
|
|
return {
|
|
onList: fn => cbs.list.push(fn),
|
|
onIncoming: fn => cbs.incoming.push(fn),
|
|
onCancel: fn => cbs.cancel.push(fn),
|
|
onMatch: fn => cbs.match.push(fn),
|
|
onEnd: fn => cbs.end.push(fn),
|
|
onError: fn => cbs.error.push(fn),
|
|
get pending() { return pending; },
|
|
get me() { return me; },
|
|
get connected() { return sock.connected; },
|
|
enter() { if (!net.enabled || !net.uid) return; if (!offs.length) wire(); sock.connect(); },
|
|
leave() { offs.forEach(off => off()); offs = []; sock.close(); players.clear(); pending = null; },
|
|
challenge(uid) { if (pending) return; pending = uid; if (!sock.send({ t: 'challenge', to: uid })) pending = null; },
|
|
cancel() { if (!pending) return; sock.send({ t: 'challenge.cancel' }); pending = null; },
|
|
accept(from) { incomingFrom = null; sock.send({ t: 'challenge.accept', from }); },
|
|
decline(from) { incomingFrom = null; sock.send({ t: 'challenge.decline', from }); },
|
|
setRoom(room) { sock.send({ t: 'room', room: room || 'all' }); },
|
|
leaveMatch() { sock.send({ t: 'match.leave' }); },
|
|
};
|
|
};
|
|
})(typeof window !== 'undefined' ? window : globalThis);
|