Stage 3-5 server (military, expansion, social), web client for stages 1-2, ops/deploy

Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic
combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances,
roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map,
alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js.
Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium
screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push,
bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
jvanvik 2026-10-11 18:32:36 +02:00
parent b56b13880e
commit 249818a7aa
111 changed files with 17472 additions and 13 deletions

View file

@ -0,0 +1,102 @@
-- Stage 3/4 (military, expansion): units at home, reinforcements, research, movements, reports, battle points.
-- Units are { unit id: count } everywhere. Times are integer ms since the epoch (UTC).
-- Units at home, owned by the town's owner. Always settle() the town before changing these (power draw changes).
CREATE TABLE town_units (
town_id INTEGER NOT NULL REFERENCES towns(id),
unit TEXT NOT NULL,
count INTEGER NOT NULL CHECK (count > 0),
PRIMARY KEY (town_id, unit)
);
-- Reinforcements: units of owner_town_id's operator standing in host_town_id. They defend the host and draw its power.
CREATE TABLE stationed (
host_town_id INTEGER NOT NULL REFERENCES towns(id),
owner_town_id INTEGER NOT NULL REFERENCES towns(id),
unit TEXT NOT NULL,
count INTEGER NOT NULL CHECK (count > 0),
PRIMARY KEY (host_town_id, owner_town_id, unit)
);
CREATE INDEX stationed_owner ON stationed(owner_town_id);
-- Research levels per POP (attack or defence of one unit type).
CREATE TABLE town_research (
town_id INTEGER NOT NULL REFERENCES towns(id),
unit TEXT NOT NULL,
stat TEXT NOT NULL, -- attack | defence
level INTEGER NOT NULL,
PRIMARY KEY (town_id, unit, stat)
);
-- When the units in a POP went offline (battery empty, net power negative). NULL / no row = online.
CREATE TABLE town_power (
town_id INTEGER PRIMARY KEY REFERENCES towns(id),
offline_since INTEGER
);
-- Everything that is travelling. type: scan | raid | attack | takeover | reinforce | settle | transport | return.
-- status: out (travelling, arrive/return event pending) | done | recalled (turned around, a return movement exists).
-- A target is a POP (to_town_id) or just a tile (settle, incumbent sites): to_x/to_y are always set.
CREATE TABLE movements (
id INTEGER PRIMARY KEY,
world_id INTEGER NOT NULL REFERENCES worlds(id),
owner_id INTEGER NOT NULL REFERENCES operators(id),
type TEXT NOT NULL,
from_town_id INTEGER REFERENCES towns(id),
to_town_id INTEGER REFERENCES towns(id),
from_x INTEGER NOT NULL,
from_y INTEGER NOT NULL,
to_x INTEGER NOT NULL,
to_y INTEGER NOT NULL,
units TEXT NOT NULL DEFAULT '{}',
cargo TEXT NOT NULL DEFAULT '{}',
depart_at INTEGER NOT NULL,
arrive_at INTEGER NOT NULL,
status TEXT NOT NULL DEFAULT 'out',
event_id INTEGER,
parent_id INTEGER, -- the outbound movement a return belongs to
extra TEXT NOT NULL DEFAULT '{}', -- research snapshot, building target, wild slots, trade metadata, ...
created_at INTEGER NOT NULL
);
CREATE INDEX movements_owner ON movements(owner_id, status);
CREATE INDEX movements_to ON movements(to_town_id, status);
CREATE INDEX movements_from ON movements(from_town_id, status);
-- A report is one row, shared by all participants (report_recipients). Kept 14 days (expires_at), cleaned by an event.
CREATE TABLE reports (
id INTEGER PRIMARY KEY,
world_id INTEGER NOT NULL REFERENCES worlds(id),
type TEXT NOT NULL,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
summary TEXT NOT NULL DEFAULT '{}',
data TEXT NOT NULL DEFAULT '{}'
);
CREATE INDEX reports_expires ON reports(expires_at);
CREATE TABLE report_recipients (
report_id INTEGER NOT NULL REFERENCES reports(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id),
role TEXT NOT NULL, -- attacker | defender | reinforcer | sender | target
read_at INTEGER,
PRIMARY KEY (report_id, user_id)
);
CREATE INDEX report_recipients_user ON report_recipients(user_id, report_id);
-- Battle points per operator, written by combat; rankings (social) read it.
CREATE TABLE operator_stats (
operator_id INTEGER PRIMARY KEY REFERENCES operators(id),
attack_points REAL NOT NULL DEFAULT 0,
defense_points REAL NOT NULL DEFAULT 0
);
-- Incumbent sites an operator has taken (one-time Hardware bonus per site; the count drives the research speed bonus).
CREATE TABLE incumbent_takes (
world_id INTEGER NOT NULL REFERENCES worlds(id),
x INTEGER NOT NULL,
y INTEGER NOT NULL,
operator_id INTEGER NOT NULL REFERENCES operators(id),
taken_at INTEGER NOT NULL,
PRIMARY KEY (world_id, x, y, operator_id)
);
CREATE INDEX incumbent_takes_operator ON incumbent_takes(operator_id);

View file

@ -0,0 +1,113 @@
-- Stage 5 (social): alliances, diplomacy between alliances, pinned notes, messages.
-- operators.alliance_id (001) stays the quick lookup; alliance_members adds the role and is kept in step with it by
-- server/alliances.js (addMember / removeMember are the only writers).
CREATE TABLE alliances (
id INTEGER PRIMARY KEY,
world_id INTEGER NOT NULL REFERENCES worlds(id),
name TEXT NOT NULL COLLATE NOCASE,
tag TEXT NOT NULL COLLATE NOCASE,
size_cap INTEGER NOT NULL, -- stored at founding, only ever raised (rules.allianceCap)
created_by INTEGER NOT NULL REFERENCES operators(id),
created_at INTEGER NOT NULL,
UNIQUE (world_id, name),
UNIQUE (world_id, tag)
);
CREATE TABLE alliance_members (
operator_id INTEGER PRIMARY KEY REFERENCES operators(id),
alliance_id INTEGER NOT NULL REFERENCES alliances(id),
role TEXT NOT NULL, -- leader | diplomat | officer | member
joined_at INTEGER NOT NULL
);
CREATE INDEX alliance_members_alliance ON alliance_members(alliance_id);
CREATE UNIQUE INDEX alliance_one_leader ON alliance_members(alliance_id) WHERE role = 'leader';
CREATE TABLE alliance_invites (
id INTEGER PRIMARY KEY,
alliance_id INTEGER NOT NULL REFERENCES alliances(id),
operator_id INTEGER NOT NULL REFERENCES operators(id), -- the invitee
invited_by INTEGER NOT NULL REFERENCES operators(id),
status TEXT NOT NULL DEFAULT 'pending', -- pending | accepted | declined | cancelled | expired
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
answered_at INTEGER
);
CREATE UNIQUE INDEX alliance_invites_pending ON alliance_invites(alliance_id, operator_id) WHERE status = 'pending';
CREATE INDEX alliance_invites_operator ON alliance_invites(operator_id, status);
CREATE TABLE alliance_notes (
id INTEGER PRIMARY KEY,
alliance_id INTEGER NOT NULL REFERENCES alliances(id),
author_id INTEGER NOT NULL REFERENCES operators(id),
text TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX alliance_notes_alliance ON alliance_notes(alliance_id, id);
-- One row per alliance pair that is not plainly neutral (a_id < b_id). state = the CURRENT relation. A unilateral cancel
-- keeps `state` until the delayed `diploChange` event (pending_event) applies pending_to at pending_at.
CREATE TABLE alliance_relations (
a_id INTEGER NOT NULL REFERENCES alliances(id),
b_id INTEGER NOT NULL REFERENCES alliances(id),
world_id INTEGER NOT NULL,
state TEXT NOT NULL, -- war | neutral | nap | confed
since INTEGER NOT NULL,
pending_to TEXT,
pending_at INTEGER,
pending_by INTEGER, -- the alliance that started the change
pending_event INTEGER, -- events.id
PRIMARY KEY (a_id, b_id),
CHECK (a_id < b_id)
);
CREATE INDEX alliance_relations_b ON alliance_relations(b_id);
CREATE TABLE alliance_proposals (
id INTEGER PRIMARY KEY,
world_id INTEGER NOT NULL,
from_id INTEGER NOT NULL REFERENCES alliances(id),
to_id INTEGER NOT NULL REFERENCES alliances(id),
state TEXT NOT NULL, -- the proposed relation
status TEXT NOT NULL DEFAULT 'pending', -- pending | accepted | declined | withdrawn | expired
proposed_by INTEGER NOT NULL REFERENCES operators(id),
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
answered_at INTEGER
);
CREATE UNIQUE INDEX alliance_proposals_pending ON alliance_proposals(from_id, to_id) WHERE status = 'pending';
CREATE INDEX alliance_proposals_to ON alliance_proposals(to_id, status);
-- Messages. No foreign key on alliance_id: broadcasts outlive a disbanded alliance (alliance_tag is a snapshot).
-- Each recipient has their own row (read / deleted); the sender's copy is hidden with sender_deleted_at.
CREATE TABLE messages (
id INTEGER PRIMARY KEY,
world_id INTEGER NOT NULL,
sender_id INTEGER NOT NULL REFERENCES operators(id),
kind TEXT NOT NULL, -- dm | alliance
to_id INTEGER REFERENCES operators(id), -- dm only
alliance_id INTEGER, -- alliance broadcast only
alliance_tag TEXT,
thread TEXT NOT NULL, -- 'dm:<lowId>:<highId>' | 'alliance:<id>'
body TEXT NOT NULL,
created_at INTEGER NOT NULL,
sender_deleted_at INTEGER
);
CREATE INDEX messages_sender ON messages(sender_id, id);
CREATE INDEX messages_thread ON messages(thread, id);
CREATE TABLE message_recipients (
message_id INTEGER NOT NULL REFERENCES messages(id),
operator_id INTEGER NOT NULL REFERENCES operators(id),
read_at INTEGER,
deleted_at INTEGER,
PRIMARY KEY (message_id, operator_id)
);
CREATE INDEX message_recipients_operator ON message_recipients(operator_id, message_id);
CREATE TABLE message_blocks (
operator_id INTEGER NOT NULL REFERENCES operators(id), -- who blocks
blocked_id INTEGER NOT NULL REFERENCES operators(id),
created_at INTEGER NOT NULL,
PRIMARY KEY (operator_id, blocked_id)
);