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
This commit is contained in:
parent
249818a7aa
commit
b18645dca1
87 changed files with 10201 additions and 71 deletions
|
|
@ -19,6 +19,21 @@ class Scheduler {
|
|||
this.autoArm = autoArm !== false;
|
||||
this.timer = null; this.running = false; this.closed = false;
|
||||
this.onEvent = null; // optional hook: (event) => void after each processed event (push, tests)
|
||||
// Notification outbox (stage 6 push): every notify() row is also queued here and handed to onNotify only AFTER the
|
||||
// transaction that wrote it committed, so a rolled-back change never pushes. Nothing is queued without a listener.
|
||||
this.onNotify = null; // optional hook: ({ id, userId, worldId, type, payload }) => void, post-commit
|
||||
this.outbox = [];
|
||||
}
|
||||
|
||||
// Outbox helpers for code that owns a transaction (process() below, commands.run).
|
||||
outboxMark() { return this.outbox.length; }
|
||||
outboxRollback(mark) { if (this.outbox.length > mark) this.outbox.length = mark; }
|
||||
// Deliver queued notifications. A no-op inside a transaction: the outermost commit delivers them.
|
||||
flushOutbox() {
|
||||
if (!this.outbox.length || this.db.depth > 0) return;
|
||||
const items = this.outbox; this.outbox = [];
|
||||
if (!this.onNotify) return;
|
||||
for (const n of items) { try { this.onNotify(n); } catch (e) { this.log('onNotify hook failed: ' + e.message); } }
|
||||
}
|
||||
|
||||
register(type, fn) { this.handlers[type] = fn; }
|
||||
|
|
@ -30,9 +45,13 @@ class Scheduler {
|
|||
return Number(r.lastInsertRowid);
|
||||
}
|
||||
|
||||
// Write a notification row (call inside the transaction of the change it announces). Returns the row id.
|
||||
notify(userId, worldId, type, payload) {
|
||||
this.db.run('INSERT INTO notifications (user_id, world_id, type, payload, created_at) VALUES (?, ?, ?, ?, ?)',
|
||||
const r = this.db.run('INSERT INTO notifications (user_id, world_id, type, payload, created_at) VALUES (?, ?, ?, ?, ?)',
|
||||
userId, worldId === undefined ? null : worldId, type, JSON.stringify(payload || {}), this.clock.now());
|
||||
const id = Number(r.lastInsertRowid);
|
||||
if (this.onNotify) { this.outbox.push({ id, userId, worldId: worldId === undefined ? null : worldId, type, payload: payload || {} }); this.flushOutbox(); }
|
||||
return id;
|
||||
}
|
||||
|
||||
next() { return this.db.get("SELECT id, due_at FROM events WHERE status = 'pending' ORDER BY due_at, id LIMIT 1"); }
|
||||
|
|
@ -58,6 +77,7 @@ class Scheduler {
|
|||
process(ev) {
|
||||
const handler = this.handlers[ev.type];
|
||||
const event = { id: ev.id, worldId: ev.world_id, dueAt: ev.due_at, type: ev.type, townId: ev.town_id, payload: JSON.parse(ev.payload) };
|
||||
const mark = this.outboxMark();
|
||||
try {
|
||||
this.db.tx(() => {
|
||||
if (!handler) throw new Error('no handler for event type ' + ev.type);
|
||||
|
|
@ -65,10 +85,12 @@ class Scheduler {
|
|||
this.db.run("UPDATE events SET status = 'done', done_at = ? WHERE id = ?", this.clock.now(), ev.id);
|
||||
});
|
||||
} catch (err) {
|
||||
this.outboxRollback(mark);
|
||||
this.log('event ' + ev.id + ' (' + ev.type + ') failed: ' + (err && err.stack || err));
|
||||
this.db.run("UPDATE events SET status = 'failed', done_at = ?, error = ? WHERE id = ?", this.clock.now(), String(err && err.message || err).slice(0, 500), ev.id);
|
||||
return;
|
||||
}
|
||||
this.flushOutbox();
|
||||
if (this.onEvent) { try { this.onEvent(event); } catch (e) { this.log('onEvent hook failed: ' + e.message); } }
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue