Add Opinion Wars: week-long two-faction battles attached to posts

A new post attachment type beside polls: the composer gains a Start
Opinion War builder (same disabled-inputs opt-in as the poll builder)
that names exactly two factions; the battle runs for exactly 7 days
from post creation. Members join a side, may defect at any time
(damage already dealt stays with the faction it was dealt to), and
fight once per 24 hours per battle. A fight spends 25 Code Farm coins
and deals deterministic level-weighted damage: 100 + 10 * min(level,
20) HP, so a newcomer deals 110 and a veteran caps at 300 - no
randomness anywhere.

The battle renders on the post card as a CSS pixel-art battlefield
(box-shadow sprites: castles, faction flags, marching soldiers, a
flickering campfire; steps() animation, disabled under reduced motion)
with live HP bars, a countdown, the viewer's faction strip, top
contributors and an event ticker. Live frames ride pub/sub on
public.battle.{uid} via a relay on the service-lock owner, with the
durable opinion_war_events trail (per-war atomic seq) as the source of
truth and a 15s incremental poller as fallback. /battles lists battles
with active/ended/mine filters, search and pagination.

Every mutation is a conditional UPDATE via conditional_update_row: the
fight sequence claims the cooldown first, then spends coins, then lands
the damage, compensating earlier steps on any later refusal so a crash
costs a turn, never coins. Resolution is lazy on read (no cron):
an exactly-once CAS computes the winner in the statement, awards XP
(participation, winner bonus, top damage dealer bonus; draws pay
participation only), emits the result event and notifies fighters. The
OpinionWarService backstop resolves unviewed wars and sends
fight-ready notifications, exactly-once via a marker CAS.

Fan-out: battle notification type, four badges, audit keys
(battle.create/join/switch/fight/resolve), Devii actions (join/fight
confirm-gated), API docs group, docs prose page, sitemap and topnav
entries, REPORTABLE_TARGETS registration, post-delete cascades,
README and nested CLAUDE.md documentation.

Verified with the four-layer procedure: property checks over the full
damage domain, 1200-step stateful fuzz (hp-sum invariant, coins never
negative, resolved totals frozen), and real 8-process races proving
exactly-once semantics for concurrent fights, double-spends across two
wars, resolution XP and double-joins. Persisted tests in
tests/unit/services/opinionwar, tests/api/battles, tests/e2e/battles
and tests/api/posts/create.py.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-20 23:30:02 +02:00
co-authored by Claude Fable 5
parent 2f26dbb1e7
commit 80956ce0f4
66 changed files with 3960 additions and 1 deletions
+5
View File
@@ -89,6 +89,11 @@ def resolve_object_url(target_type: str, target_uid: str) -> str:
if not poll:
return "/feed"
return resolve_object_url("post", poll.get("post_uid", ""))
if target_type == "battle":
war = get_table("opinion_wars").find_one(uid=target_uid)
if not war:
return "/battles"
return resolve_object_url("post", war.get("post_uid", ""))
if target_type == "workspace":
instance = get_table("instances").find_one(uid=target_uid)
return f"/admin/containers/{instance['uid']}" if instance else "/admin/containers"
+3
View File
@@ -17,6 +17,7 @@ REPORTABLE_TARGETS: dict[str, str] = {
"message": "messages",
"quiz": "quizzes",
"poll": "polls",
"battle": "opinion_wars",
"award": "awards",
"user": "users",
"issue": "issue_tickets",
@@ -48,6 +49,8 @@ UNREPORTABLE_TABLES: dict[str, str] = {
"bookmarks": "private to the owner",
"follows": "relationship rows, carry no authored content",
"poll_votes": "private ballots",
"opinion_war_fighters": "membership and damage counters, carry no authored content",
"opinion_war_events": "server-composed battle log rows, not authored content",
"quiz_attempts": "private to the participant",
"quiz_answers": "private to the participant",
"sessions": "authentication state",
+1
View File
@@ -19,6 +19,7 @@ NOTIFICATION_TYPES = [
{"key": "harvest_stolen", "label": "Farm raids", "description": "Someone steals a ready build from your Code Farm"},
{"key": "award", "label": "Awards", "description": "Someone gives you an award on your profile"},
{"key": "quiz_attempt", "label": "Quiz attempts", "description": "Someone completes one of your quizzes"},
{"key": "battle", "label": "Opinion Wars", "description": "Lead changes, results and fight-ready alerts for battles you joined"},
{"key": "workspace", "label": "Dev workspaces", "description": "Idle, quota, retention and moderation notices for your dev workspaces"},
{"key": "moderation", "label": "Moderation", "description": "Acknowledgements for reports you file and decisions taken on your content"},
{"key": "system", "label": "System alerts", "description": "Platform infrastructure alerts (e.g. the AI gateway going down)"},
+18
View File
@@ -150,6 +150,16 @@ def soft_delete_engagement(target_type: str, target_uids: list, deleted_by: str)
soft_delete("poll_votes", deleted_by, stamp=stamp, poll_uid=poll["uid"])
soft_delete("poll_options", deleted_by, stamp=stamp, poll_uid=poll["uid"])
soft_delete("polls", deleted_by, stamp=stamp, post_uid=uid)
if target_type == "post" and "opinion_wars" in db.tables:
for uid in uids:
for war in db["opinion_wars"].find(post_uid=uid, deleted_at=None):
soft_delete(
"opinion_war_fighters", deleted_by, stamp=stamp, war_uid=war["uid"]
)
soft_delete(
"opinion_war_events", deleted_by, stamp=stamp, war_uid=war["uid"]
)
soft_delete("opinion_wars", deleted_by, stamp=stamp, post_uid=uid)
def delete_engagement(target_type: str, target_uids: list) -> None:
@@ -181,6 +191,14 @@ def delete_engagement(target_type: str, target_uids: list) -> None:
if "poll_options" in tables:
db["poll_options"].delete(poll_uid=poll["uid"])
db["polls"].delete(post_uid=uid)
if target_type == "post" and "opinion_wars" in tables:
for uid in uids:
for war in db["opinion_wars"].find(post_uid=uid):
if "opinion_war_fighters" in tables:
db["opinion_war_fighters"].delete(war_uid=war["uid"])
if "opinion_war_events" in tables:
db["opinion_war_events"].delete(war_uid=war["uid"])
db["opinion_wars"].delete(post_uid=uid)
def get_target_owner_uid(target_type: str, target_uid: str) -> str | None:
+92
View File
@@ -1580,6 +1580,98 @@ def init_db():
)
_index(db, "quiz_answers", "idx_quiz_answers_quiz", ["quiz_uid"])
opinion_wars = get_table("opinion_wars")
for column, example in (
("uid", ""),
("post_uid", ""),
("user_uid", ""),
("faction_a", ""),
("faction_b", ""),
("hp_a", 0),
("hp_b", 0),
("leader", ""),
("status", "active"),
("winner", ""),
("created_at", ""),
("ends_at", ""),
("resolved_at", ""),
("updated_at", ""),
("deleted_at", ""),
("deleted_by", ""),
):
if not opinion_wars.has_column(column):
opinion_wars.create_column_by_example(column, example)
_index(db, "opinion_wars", "idx_opinion_wars_post", ["post_uid"], unique=True)
_index(db, "opinion_wars", "idx_opinion_wars_status_ends", ["status", "ends_at"])
_index(
db,
"opinion_wars",
"idx_opinion_wars_live_created",
["created_at"],
where="deleted_at IS NULL",
)
opinion_war_fighters = get_table("opinion_war_fighters")
for column, example in (
("uid", ""),
("war_uid", ""),
("user_uid", ""),
("faction", ""),
("hp_a", 0),
("hp_b", 0),
("fight_count", 0),
("last_fight_at", ""),
("cooldown_notified_at", ""),
("created_at", ""),
("updated_at", ""),
("deleted_at", ""),
("deleted_by", ""),
):
if not opinion_war_fighters.has_column(column):
opinion_war_fighters.create_column_by_example(column, example)
_index(
db,
"opinion_war_fighters",
"idx_opinion_war_fighters_war_user",
["war_uid", "user_uid"],
unique=True,
)
_index(
db,
"opinion_war_fighters",
"idx_opinion_war_fighters_war_faction",
["war_uid", "faction"],
)
_index(
db,
"opinion_war_fighters",
"idx_opinion_war_fighters_last_fight",
["last_fight_at"],
)
opinion_war_events = get_table("opinion_war_events")
for column, example in (
("uid", ""),
("war_uid", ""),
("seq", 0),
("kind", ""),
("message", ""),
("payload", ""),
("actor_uid", ""),
("created_at", ""),
("deleted_at", ""),
("deleted_by", ""),
):
if not opinion_war_events.has_column(column):
opinion_war_events.create_column_by_example(column, example)
_index(
db,
"opinion_war_events",
"idx_opinion_war_events_war_seq",
["war_uid", "seq"],
unique=True,
)
_index(db, "posts", "idx_posts_user_created", ["user_uid", "created_at"])
_index(
db,
+3
View File
@@ -51,6 +51,9 @@ SOFT_DELETE_TABLES = [
"quiz_options",
"quiz_attempts",
"quiz_answers",
"opinion_wars",
"opinion_war_fighters",
"opinion_war_events",
"content_reports",
"moderation_actions",
"content_maturity",