'use strict'; /* * Pure client logic of the social screens (alliance, messages, rankings): no DOM, no I/O, no clocks. Loads as a browser * script (window.UpstreamSocial) and as a Node module (test/client-social.test.js). Permissions and diplomacy moves are * never re-implemented here: they come from rules.js (allianceCan, outranks, diplomacyMove) over the tables in data.js, so * the buttons the screens show are exactly the ones the server would accept. The server still checks everything. */ (function (root, factory) { if (typeof module === 'object' && module.exports) module.exports = factory(require('./rules.js'), require('./data.js')); else root.UpstreamSocial = factory(root.UpstreamRules, root.UpstreamData); }(typeof self !== 'undefined' ? self : this, function (R, D) { const S = D.SOCIAL; // What the viewer may do in their own alliance (role from GET /alliances/mine). `memberCount` decides whether a leader // can simply leave (alone: the alliance is disbanded) or must hand over leadership first. function selfActions(role, memberCount) { const can = (a) => !!role && R.allianceCan(role, a); return { invite: can('invite'), viewInvites: can('viewInvites'), cancelInvite: can('cancelInvite'), editNotes: can('editNotes'), diplomacy: can('diplomacy'), broadcast: can('broadcast'), disband: can('disband'), transfer: can('transfer'), setRole: can('setRole'), leave: !!role, leaveNeedsTransfer: role === 'leader' && memberCount > 1, }; } // What the viewer may do to one listed member. `kick` needs the permission AND a strictly higher rank; roles are set by the // leader to diplomat/officer/member only (leadership moves through `transfer`). function memberActions(myRole, member, myOperatorId) { const self = member.operatorId === myOperatorId; const settable = S.roles.filter((r) => r !== 'leader'); return { self, message: !self, kick: !self && !!myRole && R.allianceCan(myRole, 'kick') && R.outranks(myRole, member.role), roles: !self && !!myRole && R.allianceCan(myRole, 'setRole') && member.role !== 'leader' ? settable.filter((r) => r !== member.role) : [], transfer: !self && !!myRole && R.allianceCan(myRole, 'transfer'), }; } // Relation of the own alliance to another: `relations` is the list of GET /alliances/:id/diplomacy. No row = neutral. function relationTo(relations, otherId) { const r = (relations || []).find((x) => x.other && x.other.id === otherId); return r ? { state: r.state, since: r.since, pending: r.pending || null } : { state: 'neutral', since: null, pending: null }; } // Moves offered from the current state: [{ state, kind: 'immediate' | 'proposal' | 'delayed' }]. A pair with a scheduled // change is locked (the server answers diplomacy_pending), so nothing is offered. function diplomacyOptions(current, pending) { if (pending) return []; return S.diplomacyStates.filter((s) => s !== current && R.diplomacyMove(current, s)).map((s) => ({ state: s, kind: R.diplomacyMove(current, s) })); } // Milliseconds until a scheduled pact change takes effect (never negative). const pendingRemaining = (pending, now) => (pending ? Math.max(0, pending.effectiveAt - now) : 0); // Characters as the server counts them (code points, CRLF folded to LF). The server also strips control characters, so the // counter can only over-count, never let through something the server then refuses for length. function charCount(text) { return Array.from(String(text == null ? '' : text).replace(/\r\n?/g, '\n')).length; } function composeState(text, max) { max = max || S.messageMax; const n = charCount(text); return { n, left: max - n, over: n > max, empty: !String(text == null ? '' : text).trim(), max }; } // Seconds to show for a rate_limited error (details.retryAfter / Retry-After, already parsed into err.retryAfter). const retrySeconds = (err) => Math.max(1, Math.ceil((err && err.retryAfter) || 1)); // Messages are listed newest first with `after` = the last id, so the next page holds OLDER ids. Merge without duplicates, // keep newest first. function mergeMessages(existing, incoming) { const seen = new Set(existing.map((m) => m.id)); return existing.concat(incoming.filter((m) => !seen.has(m.id))).sort((a, b) => b.id - a.id); } // A conversation reads oldest to newest. const chronological = (items) => items.slice().sort((a, b) => a.id - b.id); // The other party of a direct message from the viewer's side. const peerOf = (m) => (m.direction === 'out' ? m.to : m.from); // Received, not yet read direct or broadcast messages among `items`. const unreadIds = (items) => items.filter((m) => m.direction === 'in' && !m.readAt).map((m) => m.id); // Rankings page by rank offset: the cursor is the number of ranks already seen. Merge by rank so a refetch does not duplicate. function mergeRanks(existing, incoming) { const seen = new Set(existing.map((r) => r.rank)); return existing.concat(incoming.filter((r) => !seen.has(r.rank))).sort((a, b) => a.rank - b.rank); } // Is this ranking row the viewer's own (kind: 'operators' | 'alliances' | 'battles')? function isYou(kind, row, you) { if (!you || !row) return false; return kind === 'alliances' ? row.allianceId === you.allianceId : row.operatorId === you.operatorId; } // Battle ranking value for the tab (attack, defense or total). const battlePoints = (row, by) => (by === 'defense' ? row.defensePoints : by === 'total' ? row.attackPoints + row.defensePoints : row.attackPoints); // Where to put a number-or-null "last online": ms since, or null when unknown. const sinceMs = (at, now) => (at ? Math.max(0, now - at) : null); // Operator names may be typed or pasted: trim and collapse so " Bob " matches. The server looks names up case-insensitively. const cleanName = (s) => String(s == null ? '' : s).replace(/\s+/g, ' ').trim(); return { selfActions, memberActions, relationTo, diplomacyOptions, pendingRemaining, charCount, composeState, retrySeconds, mergeMessages, chronological, peerOf, unreadIds, mergeRanks, isYou, battlePoints, sinceMs, cleanName, }; }));