Upstream/server/migrations/002_military.sql
jvanvik 249818a7aa 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
2026-10-11 18:32:36 +02:00

102 lines
4.4 KiB
SQL

-- 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);