map change

This commit is contained in:
2026-11-15 20:05:02 +01:00
parent 319436a5f9
commit 3e565cf8c7
15 changed files with 242 additions and 26 deletions

View File

@@ -77,6 +77,9 @@
if (outgoing) return;
outgoing = uid;
const p = prof();
// a rematch reuses the match id, so clear whatever a crashed fight left behind -
// otherwise the waiting side would pick up the old seed (and the old arena) at once
await db.del(`matches/${matchId(me(), uid)}`);
await beat('searching');
await db.put(`challenges/${uid}/${me()}`, { from: me(), fromName: p.name, fromFlag: p.flag || 'XX', build: p.build, paint: p.paint, seed: (Math.random() * 1e9) | 0, ts: { '.sv': 'timestamp' } });
// wait for the other side to create the match (they host if their uid < mine)
@@ -85,7 +88,7 @@
const watch = db.stream(`matches/${id}/meta/seed`, seed => {
if (seed == null) return;
clearTimeout(give); watch.close(); outgoing = null;
createMatch(uid); // add my own robot to the match meta
createMatch(uid, false); // add my own robot; the seed is already set by the other side
watchMatch(id, uid, host);
});
}
@@ -93,17 +96,20 @@
async function accept(from) {
const id = matchId(me(), from), host = isHost(me(), from);
await db.del(`challenges/${me()}/${from}`);
await createMatch(from);
await createMatch(from, true); // whoever accepts rolls the seed (and with it the arena)
watchMatch(id, from, host);
}
function decline(from) { db.del(`challenges/${me()}/${from}`); }
function cancel() { if (outgoing) { db.del(`challenges/${outgoing}/${me()}`); outgoing = null; beat('idle'); } }
async function createMatch(oppUid) {
// setSeed: only the side that accepts writes the seed, so both read the exact same value.
// It is rolled fresh for every match - a seed derived from the match id would hand the same
// pair of players the same arena for ever.
async function createMatch(oppUid, setSeed) {
const id = matchId(me(), oppUid), p = prof();
const seed = (parseInt(id.slice(-6), 36) % 1e9) || 1; // same on both sides
// meta holds both players' robots; written once, guarded so the first writer wins
await db.patch(`matches/${id}/meta`, { seed, [me()]: { name: p.name, flag: p.flag || 'XX', build: p.build, paint: p.paint } });
const meta = { [me()]: { name: p.name, flag: p.flag || 'XX', build: p.build, paint: p.paint } };
if (setSeed) meta.seed = ((Math.random() * 1e9) | 0) || 1;
await db.patch(`matches/${id}/meta`, meta);
await beat('inmatch');
}