-- 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::' | 'alliance:' 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) );