'use strict'; /* A Backbone node level finished. Payload { siteId, toLevel, by }. Idempotent: only acts while the site's build points at THIS * event. The level lands on the SITE (a capture during the build does not stop it); level maxLevel makes the holder alliance the * winner (once per world) and schedules the end of the world. */ const D = require('../../public/js/data.js'); const { loadWorld } = require('../state'); const E = require('../endgame'); module.exports = function backboneDone(ctx, ev) { const { db } = ctx; const site = E.siteRow(db, ev.payload.siteId); if (!site || site.build_event_id !== ev.id) return; const world = loadWorld(db, site.world_id); if (!world || world.status === 'ended') return; const level = site.build_to; db.run('UPDATE landing_sites SET level = ?, build_to = NULL, build_event_id = NULL, build_started_at = NULL, build_due_at = NULL, build_cost = NULL WHERE id = ?', level, site.id); const fresh = E.siteRow(db, site.id); if (fresh.holder_alliance_id && (level % D.ENDGAME.notifyEvery === 0 || level >= D.ENDGAME.maxLevel)) { E.notifyAlliance(ctx, fresh.holder_alliance_id, 'backbone_level', { siteId: site.id, index: site.idx, level }); } E.maybeWin(ctx, fresh, ev.dueAt); };