'use strict'; /* A building level finished. Payload: { key, type, toLevel, userId }. Idempotent: a level never goes down. */ const { loadTown, settle } = require('../state'); module.exports = function buildDone(ctx, ev) { const { db } = ctx; const { key, type, toLevel, userId } = ev.payload; const town = loadTown(db, ev.townId); if (!town) return; settle(db, town, ev.dueAt); // values up to the moment of completion, at the OLD rates const cur = town.buildings[key]; if (!cur) db.run('INSERT INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', town.id, key, type, toLevel); else if (cur.level < toLevel) db.run('UPDATE buildings SET level = ? WHERE town_id = ? AND key = ?', toLevel, town.id, key); require('../military/power').reschedule(ctx, town.id, ev.dueAt); // building draw / battery cap changed: units' power check if (userId) ctx.notify(userId, ev.worldId, 'build_done', { townId: town.id, key, type, level: toLevel }); };