'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 }, }; };