'use strict'; /* * Movement rows: creating them (together with their arrive/return event), reading them, return legs. * A movement is `out` until its event runs; a recall turns it into `recalled` and creates a return movement. */ const R = require('../../public/js/rules.js'); const D = require('../../public/js/data.js'); const j = (s) => JSON.parse(s || '{}'); function fromRow(r) { if (!r) return null; return { id: r.id, worldId: r.world_id, ownerId: r.owner_id, type: r.type, fromTownId: r.from_town_id, toTownId: r.to_town_id, from: { x: r.from_x, y: r.from_y }, to: { x: r.to_x, y: r.to_y }, units: j(r.units), cargo: j(r.cargo), departAt: r.depart_at, arriveAt: r.arrive_at, status: r.status, eventId: r.event_id, parentId: r.parent_id, extra: j(r.extra), }; } function getMovement(db, id) { return fromRow(db.get('SELECT * FROM movements WHERE id = ?', id)); } // Insert a movement and the event that will resolve it ('arrive' for everything but returns, 'return' for returns). // o: { worldId, ownerId, type, fromTownId, toTownId, from:{x,y}, to:{x,y}, units, cargo, departAt, arriveAt, parentId, extra } function insertMovement(ctx, o) { const { db } = ctx; const r = db.run(`INSERT INTO movements (world_id, owner_id, type, from_town_id, to_town_id, from_x, from_y, to_x, to_y, units, cargo, depart_at, arrive_at, status, parent_id, extra, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'out', ?, ?, ?)`, o.worldId, o.ownerId, o.type, o.fromTownId || null, o.toTownId || null, o.from.x, o.from.y, o.to.x, o.to.y, JSON.stringify(o.units || {}), JSON.stringify(o.cargo || {}), o.departAt, Math.round(o.arriveAt), o.parentId || null, JSON.stringify(o.extra || {}), ctx.now); const id = Number(r.lastInsertRowid); const eventId = ctx.enqueue({ worldId: o.worldId, dueAt: o.arriveAt, type: o.type === 'return' ? 'return' : 'arrive', townId: o.toTownId || null, payload: { movementId: id } }); db.run('UPDATE movements SET event_id = ? WHERE id = ?', eventId, id); return getMovement(db, id); } // Units (and cargo) heading home from where they are. `fromTown` may be null (an incumbent site or an empty tile); // `at` = where they are ({x,y}); `to` = the destination POP row. arriveAt defaults to now + travel time. function launchReturn(ctx, o) { const to = o.to; const dist = R.distance(o.at.x, o.at.y, to.x, to.y); const world = ctx.db.get('SELECT speed FROM worlds WHERE id = ?', o.worldId); const arriveAt = o.arriveAt !== undefined ? o.arriveAt : ctx.now + R.movementMs(dist, o.units, world.speed); return insertMovement(ctx, { worldId: o.worldId, ownerId: o.ownerId, type: 'return', fromTownId: o.fromTownId || null, toTownId: to.id, from: o.at, to: { x: to.x, y: to.y }, units: o.units, cargo: o.cargo, departAt: ctx.now, arriveAt, parentId: o.parentId, extra: o.extra, }); } // Transport links in use by a POP: transports it sent that are still out, plus the empty return legs of transports, plus the // open Internet exchange offers it posted (an open offer holds a link in reserve: accepting it sends the escrow on that link). function activeLinks(db, townId) { return db.get(`SELECT COUNT(*) AS n FROM movements WHERE status = 'out' AND ((type = 'transport' AND from_town_id = ?) OR (type = 'return' AND to_town_id = ? AND json_extract(extra, '$.link') = 1))`, townId, townId).n + db.get("SELECT COUNT(*) AS n FROM trade_offers WHERE town_id = ? AND status = 'open'", townId).n; } // Hooks for later stages (trade): called after a transport has been delivered. fn(ctx, movement, { added, lost }). const hooks = { transportArrived: [] }; module.exports = { fromRow, getMovement, insertMovement, launchReturn, activeLinks, hooks };