feat(server): server-side match room runs the fight (P1-3)
This commit is contained in:
107
server/match/room.js
Normal file
107
server/match/room.js
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
/* One live fight, run by the server. Pure: no sockets, no timers - the caller feeds inputs and calls tick()
|
||||||
|
at 60 Hz (see runner.js). The fight itself is the same RC.Combat the browser uses offline.
|
||||||
|
|
||||||
|
const room = new Room({ id, seed, players: [{ uid, name, build, paint }, ...], send(seat, msg), onEnd(room) })
|
||||||
|
room.input(seat, raw) latest input of that seat (validated + clamped)
|
||||||
|
room.disconnect(seat) robot idles; after GRACE ticks the AI drives it
|
||||||
|
room.reconnect(seat) player takes over again
|
||||||
|
room.tick() one 1/60 s step (countdown first), snapshots every SNAP_EVERY ticks
|
||||||
|
room.ended / room.result { winner (seat or -1), reason } once over */
|
||||||
|
'use strict';
|
||||||
|
const RC = require('../../game/robot.js');
|
||||||
|
require('../../game/combat.js');
|
||||||
|
require('../../game/ai.js');
|
||||||
|
require('../../game/protocol.js');
|
||||||
|
|
||||||
|
const DT = 1 / 60;
|
||||||
|
const COUNTDOWN = Math.round(3.5 * 60); // ticks before the fight starts (matches the client countdown)
|
||||||
|
const SNAP_EVERY = 3; // 20 snapshots per second
|
||||||
|
const GRACE = 10 * 60; // ticks a disconnected player keeps his robot before the AI takes over
|
||||||
|
const HARD_CAP = 60 * 60 * 4; // safety net: no room lives longer than 4 minutes of fight
|
||||||
|
|
||||||
|
class Room {
|
||||||
|
constructor(opt) {
|
||||||
|
this.id = opt.id;
|
||||||
|
this.seed = opt.seed || 1;
|
||||||
|
this.players = opt.players; // [{ uid, name, build, paint, flag }]
|
||||||
|
this.send = opt.send || (() => {});
|
||||||
|
this.onEnd = opt.onEnd || (() => {});
|
||||||
|
const ids = RC.ARENAS.map(a => a.id);
|
||||||
|
this.arena = opt.arena || ids[this.seed % ids.length];
|
||||||
|
this.s = RC.Combat.create({
|
||||||
|
robots: this.players.map(p => RC.stats(p.build)), seed: this.seed,
|
||||||
|
names: this.players.map(p => p.name), arena: this.arena,
|
||||||
|
});
|
||||||
|
this.inputs = [RC.Proto.input(null), RC.Proto.input(null)];
|
||||||
|
this.acks = [0, 0];
|
||||||
|
this.away = [0, 0]; // ticks since disconnect (0 = connected)
|
||||||
|
this.ai = [null, null];
|
||||||
|
this.tickN = 0;
|
||||||
|
this.ended = false;
|
||||||
|
this.result = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
input(seat, raw) {
|
||||||
|
if (this.ended || (seat !== 0 && seat !== 1)) return;
|
||||||
|
this.inputs[seat] = RC.Proto.input(raw);
|
||||||
|
if (raw && Number.isInteger(raw.seq)) this.acks[seat] = raw.seq;
|
||||||
|
}
|
||||||
|
disconnect(seat) { if (!this.away[seat]) this.away[seat] = 1; }
|
||||||
|
reconnect(seat) { this.away[seat] = 0; this.ai[seat] = null; }
|
||||||
|
connected(seat) { return !this.away[seat]; }
|
||||||
|
|
||||||
|
// what a (re)joining player needs to build the fight locally
|
||||||
|
startMsg(seat) {
|
||||||
|
return {
|
||||||
|
t: 'match.start', matchId: this.id, seat, seed: this.seed, arena: this.arena,
|
||||||
|
countdown: Math.max(0, (COUNTDOWN - this.tickN) * DT),
|
||||||
|
robots: this.players.map(p => ({ uid: p.uid, name: p.name, build: p.build, paint: p.paint, flag: p.flag })),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
inputFor(seat) {
|
||||||
|
if (!this.away[seat]) return this.inputs[seat];
|
||||||
|
if (this.away[seat] < GRACE) return RC.Proto.input(null); // grace period: robot stands still
|
||||||
|
if (!this.ai[seat]) this.ai[seat] = RC.AI.create({ style: 'brawler', skill: 0.5, seed: this.seed + seat });
|
||||||
|
return this.ai[seat].think(this.s, this.s.robots[seat], this.s.robots[1 - seat], DT);
|
||||||
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
if (this.ended) return;
|
||||||
|
this.tickN++;
|
||||||
|
for (const k of [0, 1]) if (this.away[k]) this.away[k]++;
|
||||||
|
if (this.tickN <= COUNTDOWN) return;
|
||||||
|
const s = this.s;
|
||||||
|
RC.Combat.step(s, DT, [this.inputFor(0), this.inputFor(1)]);
|
||||||
|
if (!this.evBuf) this.evBuf = [];
|
||||||
|
if (s.events.length) this.evBuf.push(...s.events);
|
||||||
|
s.events.length = 0;
|
||||||
|
const over = s.result && s.endIn <= 0;
|
||||||
|
if (over || this.tickN % SNAP_EVERY === 0) this.publish();
|
||||||
|
if (over || this.tickN > COUNTDOWN + HARD_CAP) this.finish(s.result || { winner: -1, reason: 'time' });
|
||||||
|
}
|
||||||
|
|
||||||
|
publish() {
|
||||||
|
const s = this.s;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
finish(result) {
|
||||||
|
if (this.ended) return;
|
||||||
|
this.ended = true;
|
||||||
|
this.result = { winner: result.winner, reason: result.reason };
|
||||||
|
this.onEnd(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// per-seat fight summary for rewards / the result screen
|
||||||
|
summary(seat) {
|
||||||
|
const r = this.s.robots[seat];
|
||||||
|
return { hp: r.hp, maxHp: r.maxHp, dealt: r.dealt, taken: r.taken, value: r.st.value || 0,
|
||||||
|
lost: Object.keys(r.lost || {}).filter(k => r.lost[k]) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { Room, DT, COUNTDOWN, SNAP_EVERY, GRACE };
|
||||||
80
server/test/room.test.js
Normal file
80
server/test/room.test.js
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
'use strict';
|
||||||
|
const { test } = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const { Room, COUNTDOWN, GRACE } = require('../match/room');
|
||||||
|
const RC = globalThis.RC;
|
||||||
|
|
||||||
|
const players = () => [
|
||||||
|
{ uid: 'a', name: 'A', build: RC.STARTER, paint: {} },
|
||||||
|
{ uid: 'b', name: 'B', build: RC.STARTER, paint: {} },
|
||||||
|
];
|
||||||
|
|
||||||
|
function play(room, drive, maxTicks = 60 * 200) {
|
||||||
|
let n = 0;
|
||||||
|
while (!room.ended && n++ < maxTicks) { if (drive) drive(room); room.tick(); }
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('two AI-driven seats play a whole fight to exactly one result', () => {
|
||||||
|
const snaps = [[], []];
|
||||||
|
let ends = 0;
|
||||||
|
const room = new Room({ id: 'm1', seed: 7, players: players(), send: (seat, m) => snaps[seat].push(m), onEnd: () => ends++ });
|
||||||
|
const ai = [RC.AI.create({ style: 'brawler', skill: 0.7, seed: 1 }), RC.AI.create({ style: 'rammer', skill: 0.5, seed: 2 })];
|
||||||
|
play(room, r => { if (r.tickN > COUNTDOWN) for (const k of [0, 1]) r.input(k, ai[k].think(r.s, r.s.robots[k], r.s.robots[1 - k], 1 / 60)); });
|
||||||
|
assert.ok(room.ended);
|
||||||
|
assert.equal(ends, 1, 'onEnd fires once');
|
||||||
|
assert.ok([0, 1, -1].includes(room.result.winner));
|
||||||
|
assert.ok(snaps[0].length > 50 && snaps[0].length === snaps[1].length, 'both seats get the same number of snapshots');
|
||||||
|
const last = snaps[0][snaps[0].length - 1];
|
||||||
|
assert.deepEqual(last.result, room.result, 'last snapshot carries the result');
|
||||||
|
for (const s of snaps[0]) for (const r of s.robots) assert.ok(Number.isFinite(r.x) && Number.isFinite(r.hp), 'no NaN');
|
||||||
|
// seen from each seat, exactly one of them is the winner (or both see a draw)
|
||||||
|
const a = RC.Proto.localResult(0, room.result).winner, b = RC.Proto.localResult(1, room.result).winner;
|
||||||
|
if (room.result.winner === -1) assert.ok(a === -1 && b === -1);
|
||||||
|
else assert.ok((a === 0) !== (b === 0));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('nothing moves during the countdown', () => {
|
||||||
|
const room = new Room({ id: 'm2', seed: 1, players: players() });
|
||||||
|
const x0 = room.s.robots[0].x;
|
||||||
|
room.input(0, { seq: 1, throttle: 1, turn: 0, fire: false, fire2: false, fire3: false });
|
||||||
|
for (let i = 0; i < COUNTDOWN; i++) room.tick();
|
||||||
|
assert.equal(room.s.robots[0].x, x0);
|
||||||
|
for (let i = 0; i < 30; i++) room.tick();
|
||||||
|
assert.notEqual(room.s.robots[0].x, x0, 'moves once the countdown is over');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('inputs are clamped and acked per seat', () => {
|
||||||
|
const room = new Room({ id: 'm3', seed: 1, players: players() });
|
||||||
|
room.input(1, { seq: 42, throttle: 9, turn: -9, fire: true, fire2: false, fire3: false });
|
||||||
|
assert.deepEqual(room.inputs[1], { throttle: 1, turn: -1, fire: true, fire2: false, fire3: false });
|
||||||
|
assert.deepEqual(room.acks, [0, 42]);
|
||||||
|
room.input(5, { seq: 1 }); // bogus seat is ignored
|
||||||
|
assert.deepEqual(room.acks, [0, 42]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a disconnected seat stands still, then the AI takes over; the fight still ends', () => {
|
||||||
|
const room = new Room({ id: 'm4', seed: 3, players: players() });
|
||||||
|
for (let i = 0; i < COUNTDOWN; i++) room.tick();
|
||||||
|
room.input(1, { seq: 1, throttle: 1, turn: 0.3, fire: true, fire2: false, fire3: false });
|
||||||
|
room.disconnect(1);
|
||||||
|
assert.deepEqual(room.inputFor(1), RC.Proto.input(null), 'grace: neutral input');
|
||||||
|
for (let i = 0; i < GRACE; i++) room.tick();
|
||||||
|
room.inputFor(1);
|
||||||
|
assert.ok(room.ai[1], 'AI drives after the grace period');
|
||||||
|
room.reconnect(1);
|
||||||
|
assert.equal(room.ai[1], null);
|
||||||
|
assert.deepEqual(room.inputFor(1), room.inputs[1], 'reconnected: own input again');
|
||||||
|
room.disconnect(1);
|
||||||
|
play(room);
|
||||||
|
assert.ok(room.ended, 'fight ends even with one player gone');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('summary reports damage for rewards', () => {
|
||||||
|
const room = new Room({ id: 'm5', seed: 2, players: players() });
|
||||||
|
room.s.robots[0].dealt = 12; room.s.robots[0].hp = 10;
|
||||||
|
const sm = room.summary(0);
|
||||||
|
assert.equal(sm.dealt, 12);
|
||||||
|
assert.equal(sm.hp, 10);
|
||||||
|
assert.ok(sm.maxHp > 0);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user