Upstream/server/events/buildDone.js
jvanvik b18645dca1 Phase 2: client military/social screens, WebSocket push, trade + endgame, review findings pending
Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push
(docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels.
Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js,
rankings DoS, static headers, ...): fixes follow in the next commit.
NOT done: bots/balance, e2e suite, real-device checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 19:05:03 +02:00

23 lines
1.4 KiB
JavaScript

'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 });
};