'use strict'; /* A building level finished. Payload: { key, type, toLevel, userId }. Each build event adds exactly ONE level to what the * building is at the moment it finishes (relative, not absolute): exploit kits may have knocked the building down while the * upgrade was in flight, and the finished level must not undo that. Never lowers a level; `toLevel` is the level the * upgrade was queued for and is only an upper bound. Events run once (status), so +1 is safe. */ 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]; let level; if (!cur) { level = 1; db.run('INSERT INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', town.id, key, type, level); } else { level = cur.level < toLevel ? cur.level + 1 : cur.level; if (level !== cur.level) db.run('UPDATE buildings SET level = ? WHERE town_id = ? AND key = ?', level, 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 }); };