Built: - Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds). - End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x). - Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in. - e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English. Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented). NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
63 lines
4.9 KiB
JavaScript
63 lines
4.9 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 Rack hall must have room for the goods when they arrive
|
|
* (projected: stock after paying, plus production, minus other cargo already on its way to this POP), else `no_room_for_goods`
|
|
* (goods that do not fit on arrival are destroyed, so the trade is refused up front; economics are otherwise unchanged); 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, loadTown } = 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);
|
|
settle(db, loadTown(db, poster.id), now); // the escrow leaves the poster's Rack hall now: settle under the cap it shaped (rules.storageCapOf)
|
|
|
|
// 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 arriveAt = now + R.transportMs(dist, world.speed);
|
|
const room = R.resourcesAt(mine, arriveAt); // `mine` is settled and already paid; production until arrival is projected
|
|
let booked = 0; // cargo of this resource already on its way here will take room first
|
|
for (const row of db.all("SELECT cargo FROM movements WHERE status = 'out' AND to_town_id = ? AND type IN ('transport', 'return')", mine.id)) booked += JSON.parse(row.cargo || '{}')[o.give_res] || 0;
|
|
const free = Math.floor(room.caps[o.give_res] - room.amounts[o.give_res] - booked);
|
|
if (free < o.give_amount) throw new GameError('no_room_for_goods', { resource: o.give_res, amount: o.give_amount, room: Math.max(0, free) });
|
|
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, 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 },
|
|
};
|
|
};
|