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
88
server/trade.js
Normal file
88
server/trade.js
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
'use strict';
|
||||
/*
|
||||
* Internet exchange trade: shared helpers of the trade commands, the expiry event and the read model.
|
||||
*
|
||||
* Offer life cycle (table trade_offers, status):
|
||||
* open the offered amount is ESCROWED in the row (it left the POP at creation) and one transport link of the poster's POP is
|
||||
* held in reserve (military/movements.activeLinks counts open offers)
|
||||
* accepted two transports are on the road: poster -> acceptor carries the escrow, acceptor -> poster carries the payment
|
||||
* completed both legs were delivered (the `transportArrived` hook below)
|
||||
* cancelled the poster took it back: the escrow is credited to the POP again
|
||||
* expired ttl ran out (event `tradeExpire`): same
|
||||
* void the poster's POP changed hands while the offer was open: the escrow stays in that POP (it goes with the POP)
|
||||
* Nothing is created or lost: escrow is returned WITHOUT the Rack hall cap (an amount above the cap is kept, it only stops
|
||||
* growing); the only loss anywhere is what a delivery cannot store (the existing cargoLost rule of transports).
|
||||
*/
|
||||
const R = require('../public/js/rules.js');
|
||||
const D = require('../public/js/data.js');
|
||||
const { loadTown, settle } = require('./state');
|
||||
const M = require('./military/movements');
|
||||
|
||||
const COL = { fiber: 'fiber', hardware: 'hardware', capital: 'capital' };
|
||||
|
||||
// Credit `amount` of `res` to a POP (settled first, no storage cap: it is a refund of what left the POP).
|
||||
function refund(db, townId, res, amount, at) {
|
||||
const town = loadTown(db, townId);
|
||||
if (!town) return;
|
||||
settle(db, town, at);
|
||||
db.run('UPDATE towns SET ' + COL[res] + ' = ' + COL[res] + ' + ? WHERE id = ?', amount, townId);
|
||||
}
|
||||
|
||||
const getOffer = (db, id) => db.get('SELECT * FROM trade_offers WHERE id = ?', id) || null;
|
||||
|
||||
function expiryMs(speed) { return Math.round(D.TRADE.ttlHours * D.HOUR_MS / (speed || 1)); }
|
||||
|
||||
function userOfOperator(db, operatorId) { const o = db.get('SELECT user_id FROM operators WHERE id = ?', operatorId); return o ? o.user_id : null; }
|
||||
|
||||
// Close an open offer and give the escrow back to its POP. `status`: cancelled | expired | void. -> the offer row (before)
|
||||
function closeOffer(ctx, offer, status, at) {
|
||||
const { db } = ctx;
|
||||
db.run('UPDATE trade_offers SET status = ?, closed_at = ? WHERE id = ?', status, at, offer.id);
|
||||
if (offer.expire_event_id) db.run("UPDATE events SET status = 'cancelled', done_at = ? WHERE id = ? AND status = 'pending'", at, offer.expire_event_id);
|
||||
refund(db, offer.town_id, offer.give_res, offer.give_amount, at);
|
||||
return offer;
|
||||
}
|
||||
|
||||
// A POP changed hands (takeover): its open offers end and the escrow stays with the POP. Called from combat.transferTown.
|
||||
function onTownTransferred(ctx, townId, at) {
|
||||
const { db } = ctx;
|
||||
for (const o of db.all("SELECT * FROM trade_offers WHERE town_id = ? AND status = 'open'", townId)) {
|
||||
closeOffer(ctx, o, 'void', at);
|
||||
const uid = userOfOperator(db, o.operator_id);
|
||||
if (uid) ctx.notify(uid, o.world_id, 'trade_void', { offerId: o.id, townId });
|
||||
}
|
||||
}
|
||||
|
||||
// `transportArrived` consumer: both legs of an accepted offer delivered -> completed.
|
||||
function onTransportArrived(ctx, mv, res) {
|
||||
const tradeId = mv.extra && mv.extra.tradeId;
|
||||
if (!tradeId) return;
|
||||
const { db } = ctx;
|
||||
const o = getOffer(db, tradeId);
|
||||
if (!o || o.status !== 'accepted') return;
|
||||
const legs = o.legs_done + 1;
|
||||
if (legs < 2) { db.run('UPDATE trade_offers SET legs_done = ? WHERE id = ?', legs, o.id); return; }
|
||||
db.run("UPDATE trade_offers SET legs_done = ?, status = 'completed', closed_at = ? WHERE id = ?", legs, ctx.now, o.id);
|
||||
const body = { offerId: o.id, give: { resource: o.give_res, amount: o.give_amount }, want: { resource: o.want_res, amount: o.want_amount } };
|
||||
for (const opId of [o.operator_id, o.accepted_by_operator]) {
|
||||
const uid = userOfOperator(db, opId);
|
||||
if (uid) ctx.notify(uid, o.world_id, 'trade_completed', body);
|
||||
}
|
||||
}
|
||||
|
||||
// Registered once, whoever requires this module first (commands and events both do): tests and the server see the same wiring.
|
||||
if (!M.hooks.transportArrived.some((f) => f.name === 'tradeTransportArrived')) {
|
||||
M.hooks.transportArrived.push(function tradeTransportArrived(ctx, mv, res) { return onTransportArrived(ctx, mv, res); });
|
||||
}
|
||||
|
||||
function offerView(db, o, towns) {
|
||||
const t = towns && towns.get(o.town_id);
|
||||
return {
|
||||
id: o.id, status: o.status, worldId: o.world_id, townId: o.town_id, townName: t ? t.name : null, x: t ? t.x : null, y: t ? t.y : null,
|
||||
operator: { id: o.operator_id, name: (db.get('SELECT name FROM operators WHERE id = ?', o.operator_id) || {}).name || null },
|
||||
give: { resource: o.give_res, amount: o.give_amount }, want: { resource: o.want_res, amount: o.want_amount },
|
||||
maxDistance: o.max_distance, createdAt: o.created_at, expiresAt: o.expires_at, acceptedAt: o.accepted_at, closedAt: o.closed_at,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { refund, getOffer, expiryMs, closeOffer, onTownTransferred, onTransportArrived, offerView, userOfOperator };
|
||||
Loading…
Add table
Add a link
Reference in a new issue