Phase 2: client military/social screens, WebSocket push, trade + endgame, review findings pending
Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push (docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels. Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js, rankings DoS, static headers, ...): fixes follow in the next commit. NOT done: bots/balance, e2e suite, real-device checks. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
parent
249818a7aa
commit
b18645dca1
87 changed files with 10201 additions and 71 deletions
82
server/tradeQueries.js
Normal file
82
server/tradeQueries.js
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
'use strict';
|
||||
/*
|
||||
* Read models of the Internet exchange: the open offers you can see and take, your own offers, one offer. Reads catch the event
|
||||
* queue up first. Lists are newest first, `after` = id of the last offer you have (older ones follow).
|
||||
* Visibility: an open offer of somebody else is listed when one of your POPs (the one in `townId`, or any) has an Internet
|
||||
* exchange and the poster's POP is within min(the offer's max distance, that exchange's reach). `acceptableFrom` names those POPs.
|
||||
*/
|
||||
const R = require('../public/js/rules.js');
|
||||
const D = require('../public/js/data.js');
|
||||
const { GameError } = require('./errors');
|
||||
const A = require('./alliances');
|
||||
const T = require('./trade');
|
||||
|
||||
function createTradeQueries({ db, clock, scheduler }) {
|
||||
const fresh = () => { scheduler.runDue(); return clock.now(); };
|
||||
const townsOf = (ids) => {
|
||||
const m = new Map();
|
||||
for (const id of ids) if (!m.has(id)) m.set(id, db.get('SELECT id, name, x, y FROM towns WHERE id = ?', id));
|
||||
return m;
|
||||
};
|
||||
|
||||
// My POPs with an exchange: [{ id, x, y, level }].
|
||||
function exchanges(operatorId, townId) {
|
||||
const rows = db.all(`SELECT t.id, t.x, t.y, b.level AS level FROM towns t JOIN buildings b ON b.town_id = t.id AND b.type = ? WHERE t.owner_id = ? AND b.level >= 1 ${townId ? 'AND t.id = ?' : ''} ORDER BY t.id`,
|
||||
...[D.TRANSPORT.building, operatorId].concat(townId ? [townId] : []));
|
||||
return rows;
|
||||
}
|
||||
|
||||
function offers(userId, { worldId, townId, give, want, after, limit }) {
|
||||
const now = fresh();
|
||||
const op = A.operatorOf(db, userId, worldId);
|
||||
if (townId && !db.get('SELECT 1 AS x FROM towns WHERE id = ? AND owner_id = ?', townId, op.id)) throw new GameError('not_found', { what: 'town' });
|
||||
const mine = exchanges(op.id, townId);
|
||||
const n = limit || 25;
|
||||
const out = [];
|
||||
let next = null;
|
||||
if (mine.length) {
|
||||
// newest first in chunks: the range filter drops rows, so keep reading until a page is full or the offers are used up
|
||||
let cur = after || 0, done = false;
|
||||
while (!done && !next) {
|
||||
const rows = db.all(`SELECT o.*, t.x AS tx, t.y AS ty FROM trade_offers o JOIN towns t ON t.id = o.town_id WHERE o.world_id = ? AND o.status = 'open' AND o.operator_id != ?
|
||||
${give ? 'AND o.give_res = ?' : ''} ${want ? 'AND o.want_res = ?' : ''} ${cur ? 'AND o.id < ?' : ''} ORDER BY o.id DESC LIMIT ?`,
|
||||
...[op.world_id, op.id].concat(give ? [give] : [], want ? [want] : [], cur ? [cur] : [], [D.TRADE.scanChunk]));
|
||||
const towns = townsOf(rows.map((r) => r.town_id));
|
||||
for (const r of rows) {
|
||||
cur = r.id;
|
||||
const from = mine.map((m) => ({ townId: m.id, distance: R.distance(m.x, m.y, r.tx, r.ty), level: m.level })).filter((m) => R.tradeInRange(m.distance, r.max_distance, m.level));
|
||||
if (!from.length) continue;
|
||||
if (out.length === n) { next = out[out.length - 1].id; break; }
|
||||
out.push(Object.assign(T.offerView(db, r, towns), { acceptableFrom: from.map((m) => ({ townId: m.townId, distance: m.distance })) }));
|
||||
}
|
||||
done = rows.length < D.TRADE.scanChunk;
|
||||
}
|
||||
}
|
||||
return { serverTime: now, items: out, next, exchangeLevels: mine.map((m) => ({ townId: m.id, level: m.level, reach: R.tradeReach(m.level) })) };
|
||||
}
|
||||
|
||||
function mineOffers(userId, { worldId, after, limit, status }) {
|
||||
const now = fresh();
|
||||
const op = A.operatorOf(db, userId, worldId);
|
||||
const n = limit || 25;
|
||||
const rows = db.all(`SELECT * FROM trade_offers WHERE (operator_id = ? OR accepted_by_operator = ?) ${status ? 'AND status = ?' : ''} ${after ? 'AND id < ?' : ''} ORDER BY id DESC LIMIT ?`,
|
||||
...[op.id, op.id].concat(status ? [status] : [], after ? [after] : [], [n + 1]));
|
||||
const towns = townsOf(rows.map((r) => r.town_id));
|
||||
const items = rows.slice(0, n).map((r) => Object.assign(T.offerView(db, r, towns), { role: r.operator_id === op.id ? 'poster' : 'acceptor' }));
|
||||
return { serverTime: now, items, next: rows.length > n ? items[items.length - 1].id : null };
|
||||
}
|
||||
|
||||
// One offer: open ones are public inside the world (to operators of it), the rest only to the two parties.
|
||||
function offer(userId, id) {
|
||||
const now = fresh();
|
||||
const o = T.getOffer(db, id);
|
||||
const op = o ? db.get('SELECT * FROM operators WHERE user_id = ? AND world_id = ?', userId, o.world_id) : null;
|
||||
if (!o || !op) throw new GameError('not_found', { what: 'offer' });
|
||||
if (o.status !== 'open' && o.operator_id !== op.id && o.accepted_by_operator !== op.id) throw new GameError('not_found', { what: 'offer' });
|
||||
return { serverTime: now, offer: Object.assign(T.offerView(db, o, townsOf([o.town_id])), { role: o.operator_id === op.id ? 'poster' : (o.accepted_by_operator === op.id ? 'acceptor' : 'viewer') }) };
|
||||
}
|
||||
|
||||
return { offers, mineOffers, offer };
|
||||
}
|
||||
|
||||
module.exports = { createTradeQueries };
|
||||
Loading…
Add table
Add a link
Reference in a new issue