Upstream/server/commands/tradeAccept.js
jvanvik b18645dca1 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
2026-10-11 19:05:03 +02:00

54 lines
3.7 KiB
JavaScript

'use strict';
/*
* POST /trade/offers/:id/accept {townId}: take an open offer with one of your POPs. In ONE transaction:
* - the payment (the wanted resource) leaves the acceptor's POP as a transport to the poster's POP (needs a free link there,
* the usual transport rules: capacity, resources),
* - the poster's escrow leaves as a transport to the acceptor's POP (on the link the open offer held in reserve),
* - the offer becomes `accepted`; it is `completed` when both transports have been delivered (see server/trade.js).
* Any failure rolls everything back. Not your own offer; the acceptor's POP needs an Internet exchange and must be in range
* (the offer's max distance and the exchange's reach).
*/
const R = require('../../public/js/rules.js');
const { GameError } = require('../errors');
const { settle, loadWorld } = require('../state');
const { ownedTown } = require('./build');
const { launchTransport } = require('./sendTransport');
const ctxOf = require('../military/ctx');
const M = require('../military/movements');
const T = require('../trade');
module.exports = function tradeAccept(ctx0, { userId, offerId, townId }) {
const ctx = ctxOf(ctx0);
const { db, now } = ctx;
const o = T.getOffer(db, offerId);
const mine = ownedTown(db, userId, townId);
if (!o || o.world_id !== mine.worldId) throw new GameError('not_found', { what: 'offer' });
if (o.status !== 'open') throw new GameError('offer_closed', { status: o.status });
if (mine.ownerId === o.operator_id) throw new GameError('own_offer');
const poster = db.get('SELECT * FROM towns WHERE id = ?', o.town_id);
if (!poster || poster.owner_id !== o.operator_id) throw new GameError('offer_closed', { status: 'void' });
const level = R.transportLinks(mine);
const dist = R.distance(mine.x, mine.y, poster.x, poster.y);
if (level < 1) throw new GameError('no_transport_link', { links: 0, active: 0 });
if (!R.tradeInRange(dist, o.max_distance, level)) throw new GameError('trade_out_of_range', { distance: dist, maxDistance: o.max_distance, reach: R.tradeReach(level) });
settle(db, mine, now);
// acceptor -> poster: the wanted resource (validates links, capacity and resources, deducts, inserts the movement)
const pay = launchTransport(ctx, mine, poster, { [o.want_res]: o.want_amount }, { tradeId: o.id, role: 'payment' });
// poster -> acceptor: the escrow
const world = loadWorld(db, o.world_id);
const goods = M.insertMovement(ctx, {
worldId: o.world_id, ownerId: o.operator_id, type: 'transport', fromTownId: poster.id, toTownId: mine.id,
from: { x: poster.x, y: poster.y }, to: { x: mine.x, y: mine.y }, units: {}, cargo: { [o.give_res]: o.give_amount },
departAt: now, arriveAt: now + R.transportMs(dist, world.speed), extra: { tradeId: o.id, role: 'goods' },
});
db.run("UPDATE trade_offers SET status = 'accepted', accepted_by_operator = ?, accepted_town_id = ?, accepted_at = ?, goods_movement_id = ?, payment_movement_id = ? WHERE id = ?",
mine.ownerId, mine.id, now, goods.id, pay.id, o.id);
if (o.expire_event_id) db.run("UPDATE events SET status = 'cancelled', done_at = ? WHERE id = ? AND status = 'pending'", now, o.expire_event_id);
const posterUser = T.userOfOperator(db, o.operator_id);
if (posterUser) ctx.notify(posterUser, o.world_id, 'trade_accepted', { offerId: o.id, townId: poster.id, byTownId: mine.id, give: { resource: o.give_res, amount: o.give_amount }, want: { resource: o.want_res, amount: o.want_amount } });
return {
offerId: o.id, status: 'accepted', goodsMovementId: goods.id, paymentMovementId: pay.id, distance: dist,
arriveAt: goods.arriveAt, give: { resource: o.give_res, amount: o.give_amount }, want: { resource: o.want_res, amount: o.want_amount },
};
};