'use strict'; /* * Incremental ingestion of what the server wrote down: reports (battles, scans, settles), and the notifications that carry * numbers the ledger needs (cargo lost at delivery, decommissioned units). Reports are cleaned after 14 days, so the harness * ingests at least once per simulated day and keeps its own totals. Everything here only READS the database. */ const R = require('../../public/js/rules.js'); const D = require('../../public/js/data.js'); const { bump, addInto, DAY } = require('./util'); const RES = D.RESOURCES; const lootValue = (l) => RES.reduce((s, r) => s + ((l && l[r]) || 0), 0); const unitsValue = (u) => Object.keys(u || {}).reduce((s, k) => s + (R.unitDef(k) ? R.unitValue(k) * u[k] : 0), 0); function newOpStats() { return { raids: 0, raidWins: 0, raidLoot: 0, raidLostValue: 0, attacks: 0, attackWins: 0, hit: 0, hitLoot: 0, defLostValue: 0, scans: 0, scanOk: 0, incumbent: 0, incumbentWins: 0, incumbentHw: 0 }; } class Collector { constructor(db) { this.db = db; // Cursors are (created_at, ids seen at that instant), not plain ids: the report cleanup deletes every expired report and SQLite then // reuses low ids (rowid without AUTOINCREMENT), so "id > last id" would silently skip new reports in a long run. this.rep = { at: 0, seen: new Set() }; this.noteCursor = 0; this.foundedTowns = 0; this.unitsLost = {}; // unit -> count, every way of losing units that the ledger knows about this.unitsConsumed = {}; // crews/teams used up (not battle losses) this.byType = {}; // report type -> count this.battles = { raid: { n: 0, att: 0, loot: 0, lootRes: { fiber: 0, hardware: 0, capital: 0 }, attLost: 0, defLost: 0 }, attack: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 }, takeover: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 } }; this.raidSamples = []; // { loot, attLostValue, defLostValue, A, D, win, targetAgeDays, undefended } this.takeovers = { attempted: 0, dropped: 0, blocked: {}, captured: 0, loyaltyDropped: 0 }; this.scans = { n: 0, ok: 0, failed: 0, detected: 0, revealed: 0 }; this.incumbent = { n: 0, wins: 0, hardware: 0, attLostValue: 0 }; this.landing = { n: 0, taken: 0, attLostValue: 0 }; this.movementFailed = {}; this.settles = { ok: 0 }; this.siege = { n: 0, levels: 0 }; this.ops = new Map(); // operatorId -> opStats this.notes = {}; // notification type -> count this.cargoLostTotal = { fiber: 0, hardware: 0, capital: 0 }; this.cargoLostEvents = 0; this.decommissioned = {}; this.unitsOfflineNotes = 0; this.opCreated = new Map(); this.bounces = 0; } op(id) { if (!this.ops.has(id)) this.ops.set(id, newOpStats()); return this.ops.get(id); } created(opId) { if (!this.opCreated.has(opId)) { const o = this.db.get('SELECT created_at FROM operators WHERE id = ?', opId); this.opCreated.set(opId, o ? o.created_at : 0); } return this.opCreated.get(opId); } ingest(ledger) { const db = this.db; for (const rep of db.all('SELECT id, type, created_at, data FROM reports WHERE created_at >= ? ORDER BY created_at, id', this.rep.at)) { if (rep.created_at === this.rep.at && this.rep.seen.has(rep.id)) continue; if (rep.created_at > this.rep.at) { this.rep.at = rep.created_at; this.rep.seen = new Set(); } this.rep.seen.add(rep.id); bump(this.byType, rep.type); const d = JSON.parse(rep.data); switch (rep.type) { case 'raid': case 'attack': case 'takeover': this.battle(rep, d); break; case 'scan': this.scan(d); break; case 'scan_detected': this.scans.detected += 1; if (d.identityRevealed) this.scans.revealed += 1; break; case 'incumbent': this.incumbentReport(d); break; case 'landing': this.landingReport(d); break; case 'settle': if (d.success) { this.foundedTowns += 1; bump(this.unitsConsumed, D.EXPANSION.settleUnit, D.EXPANSION.crews); this.settles.ok += 1; } break; case 'movement_failed': bump(this.movementFailed, d.movementType + ':' + d.reason); this.bounces += 1; break; default: break; } } const rows = db.all("SELECT id, type, payload FROM notifications WHERE id > ? AND type IN ('transport_arrived','movement_returned','units_decommissioned','units_offline') ORDER BY id", this.noteCursor); for (const n of rows) { this.noteCursor = n.id; const p = JSON.parse(n.payload); bump(this.notes, n.type); if (n.type === 'units_decommissioned') { addInto(this.decommissioned, p.units); addInto(this.unitsLost, p.units); } else if (n.type === 'units_offline') this.unitsOfflineNotes += 1; else if (p.cargoLost) { let any = false; for (const r of RES) if (p.cargoLost[r]) { this.cargoLostTotal[r] += p.cargoLost[r]; ledger.sinkLost[r] += p.cargoLost[r]; any = true; } if (any) { ledger.lostEvents += 1; this.cargoLostEvents += 1; } } } // notifications are numbered independently of the filter above: move the cursor past rows we skipped const top = db.get('SELECT MAX(id) AS m FROM notifications').m || 0; if (top > this.noteCursor) this.noteCursor = top; } battle(rep, d) { const kind = rep.type; const b = this.battles[kind]; b.n += 1; const win = d.outcome === 'attacker_won'; const attLost = d.attacker.units.lost || {}; const attLostVal = unitsValue(attLost); let defLost = {}; for (const g of d.defender.groups || []) addInto(defLost, g.lost); const defLostVal = unitsValue(defLost); addInto(this.unitsLost, attLost); addInto(this.unitsLost, defLost); const loot = lootValue(d.loot); if (win) b.att += 1; b.loot += loot; b.attLost += attLostVal; b.defLost += defLostVal; if (kind === 'raid') addInto(b.lootRes, d.loot); const aId = d.attacker.operator.id, dId = d.defender.operator.id; const a = this.op(aId), df = this.op(dId); if (kind === 'raid') { a.raids += 1; if (win) a.raidWins += 1; a.raidLoot += loot; a.raidLostValue += attLostVal; } else { a.attacks += 1; if (win) a.attackWins += 1; a.raidLoot += loot; a.raidLostValue += attLostVal; } df.hit += 1; df.hitLoot += loot; df.defLostValue += defLostVal; if (d.siege) { this.siege.n += 1; this.siege.levels += d.siege.from - d.siege.to; } if (d.takeover) { const t = d.takeover; this.takeovers.attempted += 1; if (t.blocked) bump(this.takeovers.blocked, t.blocked); if (t.drop > 0) { this.takeovers.dropped += 1; this.takeovers.loyaltyDropped += t.drop; bump(this.unitsConsumed, D.EXPANSION.conquestUnit, 1); } if (t.captured) this.takeovers.captured += 1; } if (kind === 'raid') { const defenderHome = (d.defender.groups || []).reduce((s, g) => s + R.defencePower(g.sent, {}), 0); this.raidSamples.push({ loot, attLost: attLostVal, defLost: defLostVal, A: d.power.attack, D: d.power.defence, win, targetAgeDays: (rep.created_at - this.created(dId)) / DAY, undefended: defenderHome <= 0, at: rep.created_at, attacker: aId, defender: dId, }); } } scan(d) { this.scans.n += 1; if (d.success) this.scans.ok += 1; else this.scans.failed += 1; addInto(this.unitsLost, (d.scanners && d.scanners.lost) || {}); } incumbentReport(d) { this.incumbent.n += 1; if (d.outcome === 'attacker_won') this.incumbent.wins += 1; if (d.bonus) this.incumbent.hardware += d.bonus.hardware || 0; const lost = (d.attacker.units && d.attacker.units.lost) || {}; this.incumbent.attLostValue += unitsValue(lost); addInto(this.unitsLost, lost); const a = this.op(d.attacker.operator.id); a.incumbent += 1; if (d.outcome === 'attacker_won') a.incumbentWins += 1; if (d.bonus) a.incumbentHw += d.bonus.hardware || 0; } landingReport(d) { this.landing.n += 1; if (d.taken) this.landing.taken += 1; const lost = (d.attacker.units && d.attacker.units.lost) || {}; this.landing.attLostValue += unitsValue(lost); addInto(this.unitsLost, lost); } } module.exports = { Collector, lootValue, unitsValue };