Stage 0-2 server spine: db, scheduler, REST, auth, economy, map
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
This commit is contained in:
parent
9fa76d53a8
commit
b56b13880e
39 changed files with 4450 additions and 0 deletions
51
server/commands/joinWorld.js
Normal file
51
server/commands/joinWorld.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
'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 /worlds/:id/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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue