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
20 lines
1 KiB
JavaScript
20 lines
1 KiB
JavaScript
'use strict';
|
|
/* World lifecycle helpers. A world is a row in `worlds`; everything about it (speed, size, seed) is data. */
|
|
const crypto = require('crypto');
|
|
const D = require('../public/js/data.js');
|
|
|
|
function createWorld(db, now, o) {
|
|
o = o || {};
|
|
const seed = o.seed !== undefined ? o.seed : crypto.randomInt(1, 2 ** 31 - 1);
|
|
const r = db.run('INSERT INTO worlds (name, speed, map_radius, seed, start_at, end_at, status, preset, config, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
o.name || D.WORLD_DEFAULTS.name, o.speed || D.WORLD_DEFAULTS.speed, o.mapRadius || D.WORLD_DEFAULTS.mapRadius, seed, now, o.endAt || null, 'running', o.preset || D.WORLD_DEFAULTS.preset, JSON.stringify(o.config || {}), now);
|
|
return Number(r.lastInsertRowid);
|
|
}
|
|
|
|
// First boot: a database without any world gets one (WORLD_SPEED / WORLD_RADIUS from the environment).
|
|
function ensureWorld(db, now, o) {
|
|
if (db.get('SELECT 1 AS x FROM worlds LIMIT 1')) return null;
|
|
return createWorld(db, now, o);
|
|
}
|
|
|
|
module.exports = { createWorld, ensureWorld };
|