'use strict'; /* * Read helpers for the bots: what a client would see (own POPs with the same shape rules.js previews use, the map's towns, * pending queues). Plain reads through the server's own modules; nothing here writes. Created per run, after the data overrides * were applied (the server modules cache some constants when first required). */ module.exports = function makeView(db, world) { const R = require('../../public/js/rules.js'); const S = require('../../server/state'); const U = require('../../server/military/units'); const trainCmd = require('../../server/commands/train'); const v = { world, townRows(opId) { return db.all('SELECT * FROM towns WHERE owner_id = ? ORDER BY is_capital DESC, id', opId); }, towns(opId) { return v.townRows(opId).map((r) => S.townFromRow(db, r, world)); }, town(id) { return S.loadTown(db, id); }, pendingBuilds(townId) { return S.pendingBuilds(db, townId); }, trainQueue(townId, building) { return trainCmd.pendingBatches(db, townId, building); }, researchBusy(townId) { return !!db.get("SELECT 1 AS x FROM events WHERE town_id = ? AND type = 'researchDone' AND status = 'pending' LIMIT 1", townId); }, researchLevels(townId) { return U.researchLevels(db, townId); }, home(townId) { return U.homeUnits(db, townId); }, operator(id) { return db.get('SELECT * FROM operators WHERE id = ?', id); }, // Every POP of every other operator with what the map shows (position, owner, alliance, protection). otherTowns(opId) { return db.all(`SELECT t.id, t.x, t.y, t.owner_id AS ownerId, t.is_capital AS isCapital, o.protection_until AS prot, o.alliance_id AS ally, o.name AS opName FROM towns t JOIN operators o ON o.id = t.owner_id WHERE t.world_id = ? AND t.owner_id != ? ORDER BY t.id`, world.id, opId); }, outgoing(opId) { return db.all("SELECT id, type, to_town_id AS toTownId, to_x AS tx, to_y AS ty, arrive_at AS arriveAt, units, extra FROM movements WHERE owner_id = ? AND status = 'out' AND type != 'return' ORDER BY id", opId); }, incomingTo(townIds) { if (!townIds.length) return []; return db.all("SELECT id, type, owner_id AS ownerId, to_town_id AS toTownId, arrive_at AS arriveAt FROM movements WHERE status = 'out' AND type IN ('raid','attack','takeover','scan') AND to_town_id IN (" + townIds.map(() => '?').join(',') + ')', ...townIds); }, // New reports of this user since `cursor` ({ at, seen }; mutated). Not "id > last id": see collector.js (ids are reused after a cleanup). reportsFor(userId, cursor) { const rows = db.all(`SELECT r.id, r.type, r.created_at AS at, r.data, rr.role FROM reports r JOIN report_recipients rr ON rr.report_id = r.id WHERE rr.user_id = ? AND r.created_at >= ? AND r.type IN ('scan','raid','attack','takeover','settle','incumbent','landing') ORDER BY r.created_at, r.id`, userId, cursor.at); const out = []; for (const x of rows) { if (x.at === cursor.at && cursor.seen.has(x.id)) continue; if (x.at > cursor.at) { cursor.at = x.at; cursor.seen = new Set(); } cursor.seen.add(x.id); out.push(Object.assign(x, { data: JSON.parse(x.data) })); } return out; }, footprint(opId, now) { return U.operatorFootprint(db, opId, now); }, level(T, type) { return T.buildings[type] ? T.buildings[type].level : 0; }, fieldLevels(T) { const out = []; for (let i = 1; i <= 18; i++) { const b = T.buildings['f' + i]; if (b) out.push({ key: 'f' + i, type: b.type, level: b.level }); } return out; }, peering(opId) { return require('../../server/alliances').peeringLevel(db, opId); }, R, U, }; return v; };