Found while resetting the live test world: joinWorld's comment promised POST /worlds/:id/join but no route existed, so after reset-world (accounts kept) nobody could found a POP again. Added POST /me/join (default: first running world; 409 if already joined), a join card on the POP screen, en/nb strings, API.md + openapi. Client no longer polls /movements while it has no operator (that 404ed). Tested: npm test 209/209 (new test/me-join.test.js incl. response contract), openapi check, npm run e2e (new check: account without a POP sees the join card and founds one). NOT tested: joining a second/other world (only the default-world path in the browser), nb text of the card visually. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
51 lines
2.8 KiB
JavaScript
51 lines
2.8 KiB
JavaScript
'use strict';
|
|
/* Create an operator for a user in a world, with a capital POP placed by the spawn ring. Not a player-facing route
|
|
* on its own: signup and `POST /me/join` call it. */
|
|
const crypto = require('crypto');
|
|
const R = require('../../public/js/rules.js');
|
|
const D = require('../../public/js/data.js');
|
|
const { GameError } = require('../errors');
|
|
const { loadWorld } = require('../state');
|
|
|
|
module.exports = function joinWorld(ctx, { userId, worldId }) {
|
|
const { db, now } = ctx;
|
|
const world = loadWorld(db, worldId);
|
|
if (!world || world.status !== 'running') throw new GameError('not_found', { what: 'world' });
|
|
const user = db.get('SELECT id, username FROM users WHERE id = ?', userId);
|
|
if (!user) throw new GameError('not_found', { what: 'user' });
|
|
if (db.get('SELECT 1 AS x FROM operators WHERE world_id = ? AND user_id = ?', worldId, userId)) throw new GameError('conflict', { what: 'already joined' });
|
|
|
|
const towns = db.all('SELECT x, y FROM towns WHERE world_id = ?', worldId);
|
|
const spot = R.pickSpawn(world.seed, world.map_radius, towns.length, towns);
|
|
if (!spot) throw new GameError('world_full');
|
|
|
|
let asn;
|
|
for (let i = 0; i < 50; i++) {
|
|
asn = crypto.randomInt(D.AS_RANGE.min, D.AS_RANGE.max);
|
|
if (!db.get('SELECT 1 AS x FROM operators WHERE world_id = ? AND as_number = ?', worldId, asn)) break;
|
|
asn = null;
|
|
}
|
|
if (!asn) throw new GameError('internal_error');
|
|
|
|
const protectionMs = Math.round(D.PROTECTION_HOURS * D.HOUR_MS / world.speed);
|
|
const op = db.run('INSERT INTO operators (world_id, user_id, name, as_number, protection_until, created_at) VALUES (?, ?, ?, ?, ?, ?)',
|
|
worldId, userId, user.username, asn, now + protectionMs, now);
|
|
const operatorId = Number(op.lastInsertRowid);
|
|
|
|
const townId = createTown(db, { worldId, ownerId: operatorId, name: user.username + ' POP 1', x: spot.x, y: spot.y, isCapital: true, now });
|
|
return { operatorId, townId, x: spot.x, y: spot.y, asNumber: asn };
|
|
};
|
|
|
|
// A town with the starting buildings and resources. Also used by expansion (stage 4) with different `start` data.
|
|
function createTown(db, { worldId, ownerId, name, x, y, isCapital, now, buildings, res }) {
|
|
const start = res || D.START.resources;
|
|
const t = db.run(`INSERT INTO towns (world_id, owner_id, name, x, y, is_capital, loyalty, loyalty_at, fiber, hardware, capital,
|
|
battery, footprint, res_at, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?)`,
|
|
worldId, ownerId, name, x, y, isCapital ? 1 : 0, D.START.loyalty, now, start.fiber, start.hardware, start.capital, D.START.battery, now, now);
|
|
const id = Number(t.lastInsertRowid);
|
|
const b = buildings || R.initialBuildings();
|
|
for (const key of Object.keys(b)) db.run('INSERT INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', id, key, b[key].type, b[key].level);
|
|
return id;
|
|
}
|
|
|
|
module.exports.createTown = createTown;
|