Built: SQLite (node:sqlite) via server/db.js with checksummed migrations, injectable clock, persistent event scheduler with catch-up, tiny REST router (error envelope, idempotency keys, rate limits, bearer tokens with scrypt passwords), lazy-resource economy in public/js/rules.js + data.js, build/cancel/rename commands, seeded map + spawn ring, generated docs/openapi.json. NETWALL net.js/static.js reused. Tested: manual smoke run only (signup, town view, build, map). No unit tests or e2e yet. NOT done: military, expansion, social, client, push, deploy docs, bots/admin. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
27 lines
1.8 KiB
JavaScript
27 lines
1.8 KiB
JavaScript
'use strict';
|
|
const { reply } = require('../http');
|
|
|
|
function register(router, { queries, commands }) {
|
|
const errs = ['unauthorized', 'not_found'];
|
|
|
|
router.get('/towns/:id', { summary: 'Full state of one of your POPs: resources, power, buildings, build queue', tag: 'towns', response: { $ref: 'TownResponse' }, errors: errs },
|
|
({ user, params }) => queries.town(user.id, params.id));
|
|
|
|
router.post('/towns/:id/build', {
|
|
summary: 'Build or upgrade a building (fields f1..f18 or an infrastructure building type) by one level', tag: 'towns',
|
|
body: { type: 'object', required: ['key'], properties: { key: { type: 'string', maxLength: 32, pattern: '^[a-z0-9_]+$' } } },
|
|
response: { $ref: 'BuildResult' }, status: 201,
|
|
errors: errs.concat(['validation_failed', 'unknown_building', 'not_buildable', 'max_level', 'requirements_not_met', 'already_queued', 'queue_full', 'not_enough_resources']),
|
|
}, ({ user, params, body }) => {
|
|
return reply(201, commands.run('build', { userId: user.id, townId: params.id, key: body.key }));
|
|
});
|
|
|
|
router.del('/towns/:id/queue/:eventId', { summary: 'Cancel a running build and get 80% of its cost back', tag: 'towns', response: { $ref: 'Ok' }, errors: errs },
|
|
({ user, params }) => { commands.run('cancelBuild', { userId: user.id, townId: params.id, eventId: params.eventId }); return {}; });
|
|
|
|
router.post('/towns/:id/rename', {
|
|
summary: 'Rename a POP', tag: 'towns', body: { type: 'object', required: ['name'], properties: { name: { type: 'string', minLength: 2, maxLength: 40 } } },
|
|
response: { type: 'object', required: ['serverTime', 'name'], properties: { serverTime: { type: 'integer' }, name: { type: 'string' } } }, errors: errs.concat(['validation_failed']),
|
|
}, ({ user, params, body }) => commands.run('renameTown', { userId: user.id, townId: params.id, name: body.name }));
|
|
}
|
|
module.exports = { register };
|