'use strict'; /* Alliances, invites, pinned notes and diplomacy (stage 5). Fixed paths are registered before /alliances/:id. */ const { reply } = require('../http'); const { GameError } = require('../errors'); const { createSocialQueries } = require('../socialQueries'); const D = require('../../public/js/data.js'); const S = D.SOCIAL; const int1 = { type: 'integer', minimum: 1 }; const page = { after: { type: 'integer', minimum: 0 }, limit: { type: 'integer', minimum: 1, maximum: 100 } }; const worldQ = { worldId: int1 }; function register(router, services) { const { commands } = services; const social = createSocialQueries(services); const run = (name, args) => commands.run(name, args); const base = ['unauthorized', 'not_found', 'validation_failed']; const roleErrs = base.concat(['forbidden']); router.get('/alliances/mine', { summary: 'Your alliance, role and permissions (null alliance when you are not in one)', tag: 'alliances', query: { type: 'object', properties: worldQ }, response: { $ref: 'AllianceMine' }, errors: ['unauthorized', 'not_found', 'validation_failed'], }, ({ user, query }) => social.mine(user.id, query.worldId)); router.get('/alliances', { summary: 'Alliance directory of a world, optional name/tag search (cursor: after = last id)', tag: 'alliances', query: { type: 'object', properties: Object.assign({ q: { type: 'string', maxLength: 40 } }, page, worldQ) }, response: { $ref: 'AllianceList' }, errors: base, }, ({ user, query }) => social.list(user.id, query)); router.post('/alliances', { summary: 'Found an alliance (needs Peering office 1+); you become its leader', tag: 'alliances', status: 201, body: { type: 'object', required: ['name', 'tag'], properties: { name: { type: 'string', minLength: S.nameLen[0], maxLength: S.nameLen[1] * 2 }, tag: { type: 'string', minLength: S.tagLen[0], maxLength: S.tagLen[1] }, worldId: int1 } }, response: { $ref: 'AllianceCreated' }, errors: base.concat(['peering_office_required', 'already_in_alliance', 'alliance_name_taken', 'alliance_tag_taken']), }, ({ user, body }) => reply(201, run('allianceCreate', { userId: user.id, worldId: body.worldId, name: body.name, tag: body.tag }))); router.get('/alliance-invites', { summary: 'Open invites addressed to you', tag: 'alliances', response: { $ref: 'ReceivedInviteList' }, errors: ['unauthorized'], }, ({ user }) => social.invitesReceived(user.id)); const answerErrs = ['unauthorized', 'not_found', 'conflict', 'invite_expired', 'already_in_alliance', 'peering_office_required', 'alliance_full']; router.post('/alliance-invites/:id/accept', { summary: 'Accept an invite (needs Peering office 1+ and no alliance yet)', tag: 'alliances', response: { $ref: 'Changed' }, errors: answerErrs, }, ({ user, params }) => run('allianceInviteAnswer', { userId: user.id, inviteId: params.id, accept: true })); router.post('/alliance-invites/:id/decline', { summary: 'Decline an invite', tag: 'alliances', response: { $ref: 'Changed' }, errors: answerErrs, }, ({ user, params }) => run('allianceInviteAnswer', { userId: user.id, inviteId: params.id, accept: false })); router.get('/alliances/:id', { summary: 'Public alliance profile: size, leader, footprint, your role and relation to it', tag: 'alliances', response: { $ref: 'AllianceDetail' }, errors: ['unauthorized', 'not_found'], }, ({ user, params }) => social.get(user.id, params.id)); router.get('/alliances/:id/members', { summary: 'Member list with role, POP count, footprint and last online (members only)', tag: 'alliances', response: { $ref: 'AllianceMemberList' }, errors: ['unauthorized', 'not_found', 'forbidden'], }, ({ user, params }) => social.members(user.id, params.id)); router.get('/alliances/:id/invites', { summary: 'Pending invites sent by this alliance (needs viewInvites)', tag: 'alliances', response: { $ref: 'AllianceInviteList' }, errors: ['unauthorized', 'not_found', 'forbidden'], }, ({ user, params }) => social.invitesSent(user.id, params.id)); router.post('/alliances/:id/invite', { summary: 'Invite an operator by id or name (needs invite). Repeating it returns the pending invite', tag: 'alliances', status: 201, body: { type: 'object', properties: { operatorId: int1, name: { type: 'string', minLength: 1, maxLength: 40 } } }, response: { $ref: 'InviteResult' }, errors: roleErrs.concat(['already_in_alliance', 'alliance_full', 'conflict']), }, ({ user, params, body }) => { if ((body.operatorId === undefined) === (body.name === undefined)) throw new GameError('validation_failed', { errors: [{ path: 'operatorId', message: 'give exactly one of operatorId, name' }] }); const r = run('allianceInvite', { userId: user.id, allianceId: params.id, operatorId: body.operatorId, name: body.name }); return reply(r.changed ? 201 : 200, r); }); router.del('/alliances/:id/invites/:inviteId', { summary: 'Cancel a pending invite (needs cancelInvite)', tag: 'alliances', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'forbidden', 'conflict'], }, ({ user, params }) => run('allianceInviteCancel', { userId: user.id, allianceId: params.id, inviteId: params.inviteId })); router.post('/alliances/:id/leave', { summary: 'Leave the alliance. The leader must transfer first unless alone (then it is disbanded)', tag: 'alliances', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'leader_must_transfer'], }, ({ user, params }) => run('allianceLeave', { userId: user.id, allianceId: params.id })); router.post('/alliances/:id/kick', { summary: 'Remove a member you outrank (needs kick)', tag: 'alliances', body: { type: 'object', required: ['operatorId'], properties: { operatorId: int1 } }, response: { $ref: 'Changed' }, errors: roleErrs, }, ({ user, params, body }) => run('allianceKick', { userId: user.id, allianceId: params.id, operatorId: body.operatorId })); router.post('/alliances/:id/role', { summary: 'Set a member role: diplomat, officer or member (leader only)', tag: 'alliances', body: { type: 'object', required: ['operatorId', 'role'], properties: { operatorId: int1, role: { type: 'string', enum: ['diplomat', 'officer', 'member'] } } }, response: { $ref: 'Changed' }, errors: roleErrs, }, ({ user, params, body }) => run('allianceRole', { userId: user.id, allianceId: params.id, operatorId: body.operatorId, role: body.role })); router.post('/alliances/:id/transfer', { summary: 'Hand leadership to another member; you become an officer (leader only)', tag: 'alliances', body: { type: 'object', required: ['operatorId'], properties: { operatorId: int1 } }, response: { $ref: 'Changed' }, errors: roleErrs, }, ({ user, params, body }) => run('allianceTransfer', { userId: user.id, allianceId: params.id, operatorId: body.operatorId })); router.post('/alliances/:id/disband', { summary: 'Disband the alliance (leader only)', tag: 'alliances', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'forbidden'], }, ({ user, params }) => run('allianceDisband', { userId: user.id, allianceId: params.id })); // ---- pinned notes const noteBody = { type: 'object', required: ['text'], properties: { text: { type: 'string', minLength: 1, maxLength: S.noteMax } } }; router.get('/alliances/:id/notes', { summary: 'Pinned notes (members only)', tag: 'alliances', response: { $ref: 'AllianceNoteList' }, errors: ['unauthorized', 'not_found', 'forbidden'], }, ({ user, params }) => social.notes(user.id, params.id)); router.post('/alliances/:id/notes', { summary: 'Pin a note (needs editNotes)', tag: 'alliances', status: 201, body: noteBody, response: { $ref: 'AllianceNoteResponse' }, errors: roleErrs.concat(['notes_full']), }, ({ user, params, body }) => reply(201, run('allianceNoteAdd', { userId: user.id, allianceId: params.id, text: body.text }))); router.post('/alliances/:id/notes/:noteId', { summary: 'Edit a pinned note (needs editNotes)', tag: 'alliances', body: noteBody, response: { $ref: 'AllianceNoteResponse' }, errors: roleErrs, }, ({ user, params, body }) => run('allianceNoteEdit', { userId: user.id, allianceId: params.id, noteId: params.noteId, text: body.text })); router.del('/alliances/:id/notes/:noteId', { summary: 'Remove a pinned note (needs editNotes)', tag: 'alliances', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'forbidden'], }, ({ user, params }) => run('allianceNoteDelete', { userId: user.id, allianceId: params.id, noteId: params.noteId })); // ---- diplomacy router.get('/alliances/:id/diplomacy', { summary: 'Relations with other alliances (with scheduled changes) and open proposals (members only)', tag: 'diplomacy', response: { $ref: 'Diplomacy' }, errors: ['unauthorized', 'not_found', 'forbidden'], }, ({ user, params }) => social.diplomacy(user.id, params.id)); router.post('/alliances/:id/diplomacy', { summary: 'Change the relation to another alliance (needs diplomacy): war applies at once from neutral, pacts need acceptance, cancelling a pact takes 24 h / world speed', tag: 'diplomacy', body: { type: 'object', required: ['targetId', 'state'], properties: { targetId: int1, state: { type: 'string', enum: S.diplomacyStates } } }, response: { $ref: 'DiplomacyResult' }, errors: roleErrs.concat(['diplomacy_invalid', 'diplomacy_pending', 'proposal_exists']), }, ({ user, params, body }) => run('diploPropose', { userId: user.id, allianceId: params.id, targetId: body.targetId, state: body.state })); router.del('/alliances/:id/diplomacy/:otherId/pending', { summary: 'Take back a scheduled cancel before it takes effect (the alliance that started it, needs diplomacy)', tag: 'diplomacy', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'forbidden'], }, ({ user, params }) => run('diploRevoke', { userId: user.id, allianceId: params.id, otherId: params.otherId })); const propErrs = ['unauthorized', 'not_found', 'forbidden', 'conflict', 'proposal_expired', 'diplomacy_invalid', 'diplomacy_pending']; router.post('/diplomacy/proposals/:proposalId/accept', { summary: 'Accept a diplomatic proposal addressed to your alliance (needs diplomacy)', tag: 'diplomacy', response: { $ref: 'Changed' }, errors: propErrs, }, ({ user, params }) => run('diploAnswer', { userId: user.id, proposalId: params.proposalId, accept: true })); router.post('/diplomacy/proposals/:proposalId/decline', { summary: 'Decline a diplomatic proposal addressed to your alliance (needs diplomacy)', tag: 'diplomacy', response: { $ref: 'Changed' }, errors: propErrs, }, ({ user, params }) => run('diploAnswer', { userId: user.id, proposalId: params.proposalId, accept: false })); router.del('/diplomacy/proposals/:proposalId', { summary: 'Withdraw a proposal your alliance made (needs diplomacy)', tag: 'diplomacy', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'forbidden', 'conflict'], }, ({ user, params }) => run('diploWithdraw', { userId: user.id, proposalId: params.proposalId })); } module.exports = { register };