From 0202a08b8dd10e0c5ffbfafd8f5a09913a95e2cb Mon Sep 17 00:00:00 2001 From: Benjamin Oezcan Date: Thu, 19 Nov 2026 02:09:12 +0100 Subject: [PATCH] feat(server): achievements pay out server-side, snapshots carry opponent connection state --- server/economy.js | 6 +++++- server/lobby.js | 2 +- server/match/room.js | 3 ++- server/test/lobby.test.js | 3 +++ tests/protocol.test.js | 3 ++- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/economy.js b/server/economy.js index 552bd99..05146d7 100644 --- a/server/economy.js +++ b/server/economy.js @@ -3,6 +3,7 @@ 'use strict'; const RC = require('../game/robot.js'); // pulls in parts.js, sets globalThis.RC require('../game/eco.js'); +require('../game/progress.js'); const { OnlineSaves, KV } = require('./db'); const Eco = RC.Eco; @@ -51,8 +52,11 @@ function reward(uid, o) { const ev = live.event && live.event.prizeMul && Date.now() >= (live.event.from || 0) && Date.now() <= (live.event.to || Infinity) ? live.event.prizeMul : 1; const r = Eco.pvpReward(Object.assign({}, o, { money: s.money, prizeMul: ev })); Eco.applyReward(s, r); + // achievements pay credits -> decided here, never on the client (same rules: game/progress.js) + const ach = RC.Progress.check(s, { won: r.won, reason: o.reason, hpPct: (o.hp || 0) / (o.maxHp || 1), dealt: o.dealt || 0, lost: (o.lost || []).length }); + for (const a of ach) s.money = Math.min(1e9, s.money + (a.reward | 0)); store(uid, s); - return Object.assign(r, { save: view(s) }); + return Object.assign(r, { ach: ach.map(a => ({ id: a.id, reward: a.reward })), save: view(s) }); } function ranks(n = 20) { return OnlineSaves.top(Math.max(1, Math.min(100, n | 0))); } diff --git a/server/lobby.js b/server/lobby.js index 2593238..927b199 100644 --- a/server/lobby.js +++ b/server/lobby.js @@ -124,7 +124,7 @@ function createLobby({ runner, economy, matches, now = () => Date.now() }) { const won = res.winner === seat; const r = economy.reward(pl.uid, Object.assign({ won, winner: res.winner === -1 ? -1 : won ? 0 : 1, reason: res.reason }, sm)); tell(pl.uid, { t: 'match.end', matchId: room.id, seat, result: res, summary: sm, - reward: { money: r.money, fame: r.fame, lines: r.lines }, save: r.save }); + reward: { money: r.money, fame: r.fame, lines: r.lines, ach: r.ach }, save: r.save }); const p = players.get(pl.uid); if (p) setState(p, 'idle'); }); } diff --git a/server/match/room.js b/server/match/room.js index f509ef8..96befef 100644 --- a/server/match/room.js +++ b/server/match/room.js @@ -94,7 +94,8 @@ class Room { s.events = this.evBuf.slice(-48); // events since the last snapshot const snap = RC.Proto.packSnap(s, this.tickN, this.acks); s.events = []; this.evBuf.length = 0; - for (const seat of [0, 1]) this.send(seat, Object.assign({ t: 'snap' }, snap)); + const away = [!!this.away[0], !!this.away[1]]; + for (const seat of [0, 1]) this.send(seat, Object.assign({ t: 'snap', away }, snap)); } finish(result) { diff --git a/server/test/lobby.test.js b/server/test/lobby.test.js index c15fd21..ef2441f 100644 --- a/server/test/lobby.test.js +++ b/server/test/lobby.test.js @@ -86,6 +86,9 @@ test('full live match over WebSocket: both get start, snapshots, and mirrored re if (ea.result.winner === -1) assert.ok(la === -1 && lb === -1); else assert.ok((la === 0) !== (lb === 0), 'exactly one player won'); assert.equal(ea.save.games, 1); + assert.ok(Array.isArray(ea.reward.ach), 'achievements decided on the server'); + const winner = la === 0 ? ea : lb === 0 ? eb : null; + if (winner) assert.ok(winner.reward.ach.some(x => x.id === 'first_win'), 'first win pays out'); assert.equal(eb.save.games, 1); assert.ok(ca.inbox.some(m => m.t === 'snap'), 'snapshots arrived before the end'); diff --git a/tests/protocol.test.js b/tests/protocol.test.js index 56a72a5..82c981e 100644 --- a/tests/protocol.test.js +++ b/tests/protocol.test.js @@ -34,7 +34,7 @@ test('input clamps ranges and coerces flags', () => { function fakeSnap() { return { - tick: 10, ack: [4, 7], time: 1, left: 100, pitOpen: 0, pitAlarm: false, endIn: 0, + tick: 10, ack: [4, 7], away: [false, true], time: 1, left: 100, pitOpen: 0, pitAlarm: false, endIn: 0, result: { winner: 0, reason: 'ko' }, press: { st: 'idle', y: 1 }, crane: { st: 'grab', x: 0, z: 0, who: 1 }, rockets: [{ kind: 'rocket', owner: 0, x: 1, z: 2, y: 0.5, a: 0 }], @@ -61,6 +61,7 @@ test('toLocal for seat 1 mirrors every robot index', () => { assert.equal(l.robots[1].grabbedBy, null); assert.equal(l.result.winner, 1, 'seat 0 won -> for seat 1 the winner is the opponent (local 1)'); assert.equal(l.ack, 7); + assert.deepEqual(l.away, [true, false], 'own connection first'); assert.equal(l.crane.who, 0); assert.equal(l.rockets[0].owner, 1); assert.equal(l.mines[0].owner, 0);