'use strict'; const { GameError } = require('../errors'); const { reply } = require('../http'); function register(router, { queries, commands, db }) { router.get('/me', { summary: 'The current user, their operators and POPs', tag: 'me', response: { $ref: 'Me' }, errors: ['unauthorized'] }, ({ user }) => queries.me(user.id)); // An account without an operator (the world was reset, or a new one opened) founds its POP here. Signup does the same inline. router.post('/me/join', { summary: 'Join a world with this account: creates the operator and its capital POP (default: the first running world)', tag: 'me', status: 201, body: { type: 'object', properties: { worldId: { type: 'integer', minimum: 1 } } }, response: { $ref: 'Me' }, errors: ['unauthorized', 'validation_failed', 'not_found', 'conflict', 'world_full'], }, ({ user, body }) => { const w = body.worldId !== undefined ? db.get("SELECT id FROM worlds WHERE id = ? AND status = 'running'", body.worldId) : db.get("SELECT id FROM worlds WHERE status = 'running' ORDER BY id LIMIT 1"); if (!w) throw new GameError('not_found', { what: 'world' }); commands.run('joinWorld', { userId: user.id, worldId: w.id }); return reply(201, queries.me(user.id)); }); router.get('/notifications', { summary: 'Notifications for this user (cursor: after = last seen id)', tag: 'me', query: { type: 'object', properties: { after: { type: 'integer', minimum: 0 }, limit: { type: 'integer', minimum: 1, maximum: 100 } } }, response: { $ref: 'NotificationList' }, errors: ['unauthorized', 'validation_failed'], }, ({ user, query }) => queries.notifications(user.id, query)); } module.exports = { register };