feat(server): achievements pay out server-side, snapshots carry opponent connection state

This commit is contained in:
Benjamin Oezcan
2026-11-19 02:09:12 +01:00
parent 85a19ba242
commit 0202a08b8d
5 changed files with 13 additions and 4 deletions

View File

@@ -3,6 +3,7 @@
'use strict'; 'use strict';
const RC = require('../game/robot.js'); // pulls in parts.js, sets globalThis.RC const RC = require('../game/robot.js'); // pulls in parts.js, sets globalThis.RC
require('../game/eco.js'); require('../game/eco.js');
require('../game/progress.js');
const { OnlineSaves, KV } = require('./db'); const { OnlineSaves, KV } = require('./db');
const Eco = RC.Eco; 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 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 })); const r = Eco.pvpReward(Object.assign({}, o, { money: s.money, prizeMul: ev }));
Eco.applyReward(s, r); 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); 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))); } function ranks(n = 20) { return OnlineSaves.top(Math.max(1, Math.min(100, n | 0))); }

View File

@@ -124,7 +124,7 @@ function createLobby({ runner, economy, matches, now = () => Date.now() }) {
const won = res.winner === seat; 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)); 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, 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'); const p = players.get(pl.uid); if (p) setState(p, 'idle');
}); });
} }

View File

@@ -94,7 +94,8 @@ class Room {
s.events = this.evBuf.slice(-48); // events since the last snapshot s.events = this.evBuf.slice(-48); // events since the last snapshot
const snap = RC.Proto.packSnap(s, this.tickN, this.acks); const snap = RC.Proto.packSnap(s, this.tickN, this.acks);
s.events = []; this.evBuf.length = 0; 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) { finish(result) {

View File

@@ -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); if (ea.result.winner === -1) assert.ok(la === -1 && lb === -1);
else assert.ok((la === 0) !== (lb === 0), 'exactly one player won'); else assert.ok((la === 0) !== (lb === 0), 'exactly one player won');
assert.equal(ea.save.games, 1); 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.equal(eb.save.games, 1);
assert.ok(ca.inbox.some(m => m.t === 'snap'), 'snapshots arrived before the end'); assert.ok(ca.inbox.some(m => m.t === 'snap'), 'snapshots arrived before the end');

View File

@@ -34,7 +34,7 @@ test('input clamps ranges and coerces flags', () => {
function fakeSnap() { function fakeSnap() {
return { 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' }, result: { winner: 0, reason: 'ko' },
press: { st: 'idle', y: 1 }, crane: { st: 'grab', x: 0, z: 0, who: 1 }, 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 }], 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.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.result.winner, 1, 'seat 0 won -> for seat 1 the winner is the opponent (local 1)');
assert.equal(l.ack, 7); assert.equal(l.ack, 7);
assert.deepEqual(l.away, [true, false], 'own connection first');
assert.equal(l.crane.who, 0); assert.equal(l.crane.who, 0);
assert.equal(l.rockets[0].owner, 1); assert.equal(l.rockets[0].owner, 1);
assert.equal(l.mines[0].owner, 0); assert.equal(l.mines[0].owner, 0);