'use strict'; /* Shared pieces of the diplomacy commands and the delayed `diploChange` event. */ const A = require('./alliances'); const brief = (a) => ({ id: a.id, tag: a.tag, name: a.name }); // Everybody in both alliances hears about a relation that changed. function announceChange(ctx, x, y, from, to, extra) { for (const [me, other] of [[x, y], [y, x]]) { A.notifyAlliance(ctx, me.id, 'diplomacy_changed', Object.assign({ allianceId: me.id, other: brief(other), from, to }, extra || {})); } } // Make `to` the current relation of the pair right now, drop proposals that this overtakes, tell everyone. function applyChange(ctx, x, y, to, extra) { const { db, now } = ctx; const cur = A.getRelation(db, x.id, y.id); const from = cur ? cur.state : 'neutral'; A.setRelation(db, x.world_id, x.id, y.id, to, now); A.withdrawProposals(db, x.id, y.id, now); announceChange(ctx, x, y, from, to, extra); return from; } module.exports = { brief, announceChange, applyChange };