'use strict'; /* * What the Alerts feed shows for the notification types of trade, the end game, the world and the social screens * (alliances, diplomacy, messages). Pure: no DOM, no App. make(ctx) returns { type: (notification, payload) -> { icon, title, * detail, go, params } } and test/client-alerts.test.js feeds it payloads in the shapes the server writes. In the browser the * bottom of this file registers the describers in App.alertKinds (the military ones live in milstate.js, build_done in * screens/alerts.js). Unknown payload fields never throw: a describer falls back to something readable. * * ctx = { t(key, vars), num(n), resName(res), townName(id) -> string | null, clockTime(ms), dayTime(ms) } */ (function (root, factory) { if (typeof module === 'object' && module.exports) module.exports = { make: factory }; else { root.UpstreamAlertKinds = { make: factory }; if (root.App && root.UpstreamI18n) { const App = root.App, mil = () => App.mil; const kinds = factory({ t: (k, v) => App.t(k, v), num: (n) => App.fmt.num(n), resName: (r) => App.resName(r), townName: (id) => (mil() ? mil().townName(id) : null), clockTime: (ms) => mil().clockTime(ms), dayTime: (ms) => mil().dayTime(ms), }); for (const k of Object.keys(kinds)) if (!App.alertKinds[k]) App.alertKinds[k] = kinds[k]; } } }(typeof self !== 'undefined' ? self : this, function make(c) { const t = c.t; const RES = ['fiber', 'hardware', 'capital']; const amt = (a) => (a && a.resource ? c.num(a.amount) + ' ' + c.resName(a.resource) : ''); const cargoText = (cargo) => RES.filter((r) => cargo && cargo[r] > 0).map((r) => c.num(cargo[r]) + ' ' + c.resName(r)).join(', '); const town = (id) => c.townName(id) || '#' + id; const tag = (o) => (o ? (o.tag ? '[' + o.tag + ']' : (o.name || '')) : ''); const site = (p) => (Number.isInteger(p.index) ? p.index + 1 : '?'); const known = (prefix, key) => { const k = prefix + key; const s = t(k); return s === k ? String(key) : s; }; const there = (p) => (p.x !== undefined ? t('alerts.at', { x: p.x, y: p.y }) : ''); const worldGo = (p) => ({ go: 'world', params: p && Number.isInteger(p.index) ? { site: p.index } : null }); return { // ---- trade trade_accepted: (n, p) => ({ icon: 'swap', title: t('alerts.trade_accepted', { give: amt(p.give), want: amt(p.want) }), detail: t('alerts.build_done.in', { town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }), trade_completed: (n, p) => ({ icon: 'swap', title: t('alerts.trade_completed', { give: amt(p.give), want: amt(p.want) }), detail: '', go: 'trade', params: { tab: 'mine' } }), trade_expired: (n, p) => ({ icon: 'clock', title: t('alerts.trade_expired'), detail: t('alerts.trade_expired.back', { what: amt(p.returned), town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }), trade_refund_clipped: (n, p) => ({ icon: 'alert', title: t('alerts.trade_refund_clipped', { what: amt({ resource: p.resource, amount: p.lost }) }), detail: t('alerts.trade_refund_clipped.why', { town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }), trade_void: (n, p) => ({ icon: 'alert', title: t('alerts.trade_void'), detail: t('alerts.build_done.in', { town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }), // ---- end game backbone_open: (n, p) => Object.assign({ icon: 'b-backbone_node', title: t('alerts.backbone_open'), detail: t('alerts.backbone_open.n', { n: (p.sites || []).length || 7 }) }, worldGo(null)), backbone_taken: (n, p) => Object.assign({ icon: 'flag', title: t('alerts.backbone_taken', { n: site(p) }), detail: [t('alerts.level', { n: p.level || 0 }), p.from ? t('alerts.backbone_taken.from', { tag: tag(p.from) }) : t('alerts.backbone_taken.incumbent'), there(p)].filter(Boolean).join(' · ') }, worldGo(p)), backbone_lost: (n, p) => Object.assign({ icon: 'alert', title: t('alerts.backbone_lost', { n: site(p), tag: tag(p.by) }), detail: [t('alerts.level', { n: p.level || 0 }), there(p)].filter(Boolean).join(' · ') }, worldGo(p)), backbone_incoming: (n, p) => Object.assign({ icon: 'alert', title: t('alerts.backbone_incoming', { n: site(p) }), detail: [p.arriveAt ? t('alerts.backbone_incoming.at', { time: c.clockTime(p.arriveAt) }) : '', there(p)].filter(Boolean).join(' · ') }, worldGo(p)), backbone_level: (n, p) => Object.assign({ icon: 'b-backbone_node', title: t('alerts.backbone_level', { n: site(p), level: p.level }), detail: '' }, worldGo(p)), backbone_delivered: (n, p) => ({ icon: 'box', title: t('alerts.backbone_delivered'), detail: cargoText(p.cargo), go: 'world', params: null }), backbone_ship_bounced: (n, p) => ({ icon: 'alert', title: t('alerts.backbone_ship_bounced'), detail: cargoText(p.cargo), go: 'movements', params: null }), backbone_won: (n, p) => ({ icon: 'trophy', title: t('alerts.backbone_won', { name: p.name || tag(p) }), detail: p.endsAt ? t('alerts.backbone_won.ends', { time: c.dayTime(p.endsAt) }) : '', go: 'world', params: null }), world_ended: (n, p) => ({ icon: 'trophy', title: t('alerts.world_ended'), detail: p.winner && p.winner.name ? t('alerts.world_ended.winner', { name: p.winner.name }) : t('alerts.world_ended.nowinner'), go: 'world', params: null }), // ---- alliance alliance_invite: (n, p) => ({ icon: 'users', title: t('alerts.alliance_invite', { name: p.name || tag(p) }), detail: t('alerts.alliance_invite.by', { name: p.by || '?' }), go: 'alliance', params: null }), alliance_invite_declined: (n, p) => ({ icon: 'users', title: t('alerts.alliance_invite_declined', { name: p.by || '?' }), detail: '', go: 'alliance', params: null }), alliance_member_joined: (n, p) => ({ icon: 'users', title: t('alerts.alliance_member_joined', { name: p.name || '?' }), detail: '', go: 'alliance', params: null }), alliance_member_left: (n, p) => ({ icon: 'users', title: t(p.kicked ? 'alerts.alliance_member_kicked' : 'alerts.alliance_member_left', { name: p.name || '?' }), detail: '', go: 'alliance', params: null }), alliance_kicked: (n, p) => ({ icon: 'alert', title: t('alerts.alliance_kicked', { name: p.name || tag(p) }), detail: '', go: 'alliance', params: null }), alliance_role: (n, p) => ({ icon: 'users', title: t('alerts.alliance_role', { role: known('soc.role.', p.role) }), detail: p.from ? t('alerts.alliance_role.was', { role: known('soc.role.', p.from) }) : '', go: 'alliance', params: null }), alliance_leader: (n, p) => ({ icon: 'users', title: t('alerts.alliance_leader', { name: p.name || '?' }), detail: p.from ? t('alerts.alliance_leader.from', { name: p.from }) : '', go: 'alliance', params: null }), alliance_disbanded: (n, p) => ({ icon: 'alert', title: t('alerts.alliance_disbanded', { name: p.name || tag(p) }), detail: '', go: 'alliance', params: null }), // ---- diplomacy (diplomacy_changed has two payload shapes: { other: {tag,..} } and, when an alliance was disbanded, { otherTag, reason }) diplomacy_proposal: (n, p) => ({ icon: 'handshake', title: t('alerts.diplomacy_proposal', { tag: tag(p.from), state: known('soc.state.', p.state) }), detail: p.expiresAt ? t('alerts.diplomacy_proposal.until', { time: c.dayTime(p.expiresAt) }) : '', go: 'alliance', params: null }), diplomacy_declined: (n, p) => ({ icon: 'handshake', title: t('alerts.diplomacy_declined', { tag: tag(p.by), state: known('soc.state.', p.state) }), detail: '', go: 'alliance', params: null }), diplomacy_changed: (n, p) => ({ icon: 'handshake', title: t('alerts.diplomacy_changed', { tag: p.other ? tag(p.other) : (p.otherTag ? '[' + p.otherTag + ']' : '?'), to: known('soc.state.', p.to) }), detail: [p.from ? t('alerts.diplomacy_changed.was', { state: known('soc.state.', p.from) }) : '', p.reason === 'disbanded' ? t('alerts.diplomacy_changed.disbanded') : ''].filter(Boolean).join(' · '), go: 'alliance', params: null, }), diplomacy_cancel_pending: (n, p) => ({ icon: 'clock', title: t('alerts.diplomacy_cancel_pending', { tag: tag(p.other), state: known('soc.state.', p.from) }), detail: p.effectiveAt ? t('alerts.diplomacy_cancel_pending.at', { time: c.dayTime(p.effectiveAt) }) : '', go: 'alliance', params: null }), diplomacy_cancel_revoked: (n, p) => ({ icon: 'handshake', title: t('alerts.diplomacy_cancel_revoked', { tag: tag(p.other), state: known('soc.state.', p.state) }), detail: '', go: 'alliance', params: null }), // ---- messages message: (n, p) => ({ icon: 'mail', title: t('alerts.message', { name: p.from && p.from.name ? p.from.name : '?' }), detail: '', go: 'messages', params: null }), alliance_message: (n, p) => ({ icon: 'mail', title: t('alerts.alliance_message', { name: typeof p.from === 'string' ? p.from : '?' }), detail: p.tag ? '[' + p.tag + ']' : '', go: 'messages', params: null }), }; }));