'use strict'; /* * The game-aware half of push: which hint goes to whom. push.js knows sockets; this file knows notifications, events and * commands. Hooks fire only AFTER the transaction that caused them committed (scheduler outbox, commands.run). * * notification row (any kind) -> user: {type:'notification', id, kind} * kind incoming -> user: {type:'incoming', townId, arriveAt} * kind report -> user: {type:'report', id} * kind message | alliance_message -> user: {type:'message', id} * town_lost, town_gained, ... -> user: {type:'town', townId} (ownership changed: the old owner is not * reachable through toTown any more) * event done (build, train, research, arrive, return ...) -> owner of every POP it touched: {type:'town', townId} * command done (build, train, send, ...) -> owner of every POP it touched: {type:'town', townId} * (also the device that issued it: the client may ignore its own echo; other devices of the user learn from it) * * Not a hint source: reading (GET) never pushes. */ const isId = (v) => Number.isInteger(v) && v > 0; // notification kind -> payload key holding a POP id whose state changed for THAT user (kinds whose event does not already // carry the town in events.town_id, or whose owner changed) const TOWN_KINDS = { town_lost: 'townId', town_gained: 'townId', transport_arrived: 'townId', transport_delivered: 'toTownId', movement_returned: 'townId', reinforcements_sent_home: 'ownerTownId', garrison_arrived: 'ownerTownId', garrison_released: 'ownerTownId', // units of that POP joined / left a landing site garrison: its power draw changed }; // Command args that name a POP the command changed (the receiving POP of a movement is NOT listed: the defender gets // an 'incoming' hint, never a plain 'town' one that would give the launch away without its ETA). const COMMAND_TOWN_KEYS = ['townId', 'fromTownId', 'hostTownId', 'ownerTownId']; // Deps for createPush that need the database. function pushDeps(db) { const owner = 'SELECT o.user_id AS uid FROM towns t JOIN operators o ON o.id = t.owner_id WHERE t.id = ?'; const valid = 'SELECT 1 AS ok FROM tokens t JOIN users u ON u.id = t.user_id WHERE t.id = ? AND t.revoked_at IS NULL AND u.banned = 0'; return { townOwner: (townId) => { const r = db.get(owner, townId); return r ? r.uid : null; }, tokenValid: (tokenId) => !!db.get(valid, tokenId), }; } function bindGame({ push, db, scheduler, commands, log }) { const toTowns = (ids) => { for (const id of ids) push.toTown(id, { type: 'town', townId: id }); }; function movementTowns(movementId, ids) { if (!isId(movementId)) return; const mv = db.get('SELECT from_town_id AS f, to_town_id AS t FROM movements WHERE id = ?', movementId); if (mv) { if (isId(mv.f)) ids.add(mv.f); if (isId(mv.t)) ids.add(mv.t); } } // ---- notifications: written by events and commands via scheduler.notify scheduler.onNotify = (n) => { const p = n.payload || {}; push.toUser(n.userId, { type: 'notification', id: n.id, kind: n.type }); if (n.type === 'incoming' && isId(p.toTownId)) push.toUser(n.userId, { type: 'incoming', townId: p.toTownId, arriveAt: p.arriveAt }); else if (n.type === 'report' && isId(p.reportId)) push.toUser(n.userId, { type: 'report', id: p.reportId }); else if ((n.type === 'message' || n.type === 'alliance_message') && isId(p.messageId)) push.toUser(n.userId, { type: 'message', id: p.messageId }); const key = TOWN_KINDS[n.type]; if (key && isId(p[key])) push.toUser(n.userId, { type: 'town', townId: p[key] }); }; // ---- events: the POPs an event touched (events.town_id, or both ends of a movement) const prevOnEvent = scheduler.onEvent; scheduler.onEvent = (ev) => { if (prevOnEvent) prevOnEvent(ev); const ids = new Set(); if (isId(ev.townId)) ids.add(ev.townId); if (ev.type === 'arrive' || ev.type === 'return') movementTowns(ev.payload && ev.payload.movementId, ids); toTowns(ids); }; // ---- commands commands.hooks.onCommand = (name, args, out) => { const ids = new Set(); for (const k of COMMAND_TOWN_KEYS) if (args && isId(args[k])) ids.add(args[k]); if (out && isId(out.townId)) ids.add(out.townId); // joinWorld: the new POP if (name === 'recallMovement') movementTowns(args.movementId, ids); // also tells the target's owner the warning is gone toTowns(ids); }; if (log) log('push hooks bound'); } module.exports = { bindGame, pushDeps, TOWN_KINDS, COMMAND_TOWN_KEYS };