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
33 lines
2.1 KiB
JavaScript
33 lines
2.1 KiB
JavaScript
'use strict';
|
|
/*
|
|
* POST /trade/offers: "give X of A for Y of B, max distance Z" from one of your POPs with an Internet exchange. The offered
|
|
* amount leaves the POP now (escrow), one transport link stays reserved for it, and the offer expires after TRADE.ttlHours
|
|
* (divided by world speed). Both amounts must fit one transport link and differ by at most 1:2.
|
|
*/
|
|
const R = require('../../public/js/rules.js');
|
|
const D = require('../../public/js/data.js');
|
|
const { GameError } = require('../errors');
|
|
const { settle, spend } = require('../state');
|
|
const { ownedTown } = require('./build');
|
|
const M = require('../military/movements');
|
|
const T = require('../trade');
|
|
|
|
module.exports = function tradeOffer(ctx, { userId, townId, giveRes, giveAmount, wantRes, wantAmount, maxDistance }) {
|
|
const { db, now, scheduler } = ctx;
|
|
const town = ownedTown(db, userId, townId);
|
|
const terms = R.tradeTermsCheck({ giveRes, giveAmount, wantRes, wantAmount, maxDistance });
|
|
if (!terms.ok) throw new GameError(terms.code, terms);
|
|
const links = R.transportLinks(town);
|
|
const inUse = M.activeLinks(db, town.id);
|
|
if (links < 1 || inUse >= links) throw new GameError('no_transport_link', { links, active: inUse });
|
|
settle(db, town, now);
|
|
if (R.resourcesAt(town, now).amounts[giveRes] + 1e-6 < giveAmount) throw new GameError('not_enough_resources', { resource: giveRes, want: giveAmount });
|
|
spend(db, town, { [giveRes]: giveAmount });
|
|
const expiresAt = now + T.expiryMs(town.speed);
|
|
const id = Number(db.run(`INSERT INTO trade_offers (world_id, town_id, operator_id, give_res, give_amount, want_res, want_amount, max_distance, status, created_at, expires_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'open', ?, ?)`, town.worldId, town.id, town.ownerId, giveRes, giveAmount, wantRes, wantAmount, maxDistance, now, expiresAt).lastInsertRowid);
|
|
const ev = scheduler.enqueue({ worldId: town.worldId, dueAt: expiresAt, type: 'tradeExpire', townId: town.id, payload: { offerId: id } });
|
|
db.run('UPDATE trade_offers SET expire_event_id = ? WHERE id = ?', ev, id);
|
|
const o = T.getOffer(db, id);
|
|
return { offer: T.offerView(db, o, new Map([[town.id, town]])) };
|
|
};
|