'use strict'; /* Movements: send, recall, list (with incoming warnings), withdraw reinforcements, tile info for the send dialog. */ const { reply } = require('../http'); const { GameError } = require('../errors'); const { createMilitaryQueries } = require('../military/views'); function register(router, services) { const { commands, db, clock, scheduler } = services; const mq = createMilitaryQueries({ db, clock, scheduler }); const errs = ['unauthorized', 'not_found', 'validation_failed']; router.post('/movements', { summary: 'Send units (scan, raid, attack, takeover, reinforce, settle) or resources (transport) from one of your POPs. Target = toTownId or the tile x,y', tag: 'movements', body: { type: 'object', required: ['fromTownId', 'type'], properties: { fromTownId: { type: 'integer', minimum: 1 }, type: { type: 'string', enum: ['scan', 'raid', 'attack', 'takeover', 'reinforce', 'settle', 'transport'] }, toTownId: { type: 'integer', minimum: 1 }, x: { type: 'integer', minimum: -1000, maximum: 1000 }, y: { type: 'integer', minimum: -1000, maximum: 1000 }, units: { type: 'object', additionalProperties: true, description: 'unit id -> count' }, cargo: { type: 'object', additionalProperties: true, description: 'transport only: fiber/hardware/capital -> amount' }, target: { type: 'string', maxLength: 32, pattern: '^[a-z0-9_]+$', description: 'exploit kits: the building (f1..f18 or an infrastructure type) to damage on a win' }, wild: { type: 'array', maxItems: 2, items: { type: 'string', maxLength: 32 }, description: 'settle: the two wild slot field types of the new POP' }, confirmEndProtection: { type: 'boolean', description: 'raid/attack/takeover from a protected operator: attacking ends beginner protection' }, }, }, response: { $ref: 'MovementSent' }, status: 201, errors: errs.concat(['unknown_unit', 'not_enough_units', 'units_offline', 'invalid_target', 'target_protected', 'protection_active', 'out_of_range', 'footprint_too_low', 'cannot_takeover', 'capacity_exceeded', 'no_transport_link', 'not_enough_resources']), }, ({ user, body }) => { if (body.type === 'transport') { if (body.toTownId === undefined) throw new GameError('validation_failed', { errors: [{ path: 'toTownId', message: 'is required for a transport' }] }); return reply(201, commands.run('sendTransport', { userId: user.id, fromTownId: body.fromTownId, toTownId: body.toTownId, cargo: body.cargo })); } return reply(201, commands.run('sendMovement', { userId: user.id, fromTownId: body.fromTownId, type: body.type, toTownId: body.toTownId, x: body.x, y: body.y, units: body.units, target: body.target, wild: body.wild, confirmEndProtection: body.confirmEndProtection, })); }); router.get('/movements', { summary: 'Your active movements and other operators\' movements heading for your POPs (incoming warnings: origin and ETA only, type hidden)', tag: 'movements', query: { type: 'object', properties: { worldId: { type: 'integer', minimum: 1 } } }, response: { $ref: 'MovementList' }, errors: errs, }, ({ user, query }) => mq.movements(user.id, { worldId: query.worldId })); router.post('/movements/:id/recall', { summary: 'Turn an outbound movement around within the first 90 seconds', tag: 'movements', response: { $ref: 'RecallResult' }, errors: errs.concat(['conflict', 'recall_expired']), }, ({ user, params }) => commands.run('recallMovement', { userId: user.id, movementId: params.id })); router.post('/towns/:id/withdraw', { summary: 'Send reinforcements standing in this POP home (host kicks them out, or the units\' owner calls them back)', tag: 'movements', body: { type: 'object', required: ['ownerTownId'], properties: { ownerTownId: { type: 'integer', minimum: 1 }, units: { type: 'object', additionalProperties: true, description: 'defaults to the whole group' } } }, response: { $ref: 'WithdrawResult' }, errors: errs.concat(['not_enough_units', 'unknown_unit']), }, ({ user, params, body }) => commands.run('withdrawStationed', { userId: user.id, hostTownId: params.id, ownerTownId: body.ownerTownId, units: body.units })); router.get('/map/tile', { summary: 'Public information about one tile for the send dialog: what is there, owner, distance from your POPs, whether you could settle it', tag: 'map', query: { type: 'object', required: ['x', 'y'], properties: { x: { type: 'integer', minimum: -1000, maximum: 1000 }, y: { type: 'integer', minimum: -1000, maximum: 1000 }, worldId: { type: 'integer', minimum: 1 } } }, response: { $ref: 'TileInfo' }, errors: errs, }, ({ user, query }) => mq.tileInfo(user.id, { worldId: query.worldId, x: query.x, y: query.y })); } module.exports = { register };