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
+106
View File
@@ -795,3 +795,109 @@ def test_feed_includes_project_when_linked(app_server):
assert "/projects/" in item["project_link"]["url"]
break
assert found, "feed must include project info for linked posts"
def test_create_post_with_war_creates_battle(app_server):
from devplacepy.services.opinionwar import rules
s, _ = _member()
r = s.post(
f"{BASE_URL}/posts/create",
headers=JSON_audit_log,
data={
"title": _unique("auwar"),
"content": "this post carries an opinion war between two factions",
"topic": "question",
"war_faction_a": "Tabs",
"war_faction_b": "Spaces",
},
)
post_uid = r.json()["data"]["uid"]
refresh_snapshot()
war = get_table("opinion_wars").find_one(post_uid=post_uid)
assert war is not None
assert war["faction_a"] == "Tabs"
assert war["faction_b"] == "Spaces"
assert war["status"] == "active"
created = datetime.fromisoformat(war["created_at"])
ends = datetime.fromisoformat(war["ends_at"])
assert abs((ends - created).total_seconds() - rules.WAR_DURATION_DAYS * 86400) < 5
def test_create_post_with_one_faction_creates_no_war(app_server):
s, _ = _member()
r = s.post(
f"{BASE_URL}/posts/create",
headers=JSON_audit_log,
data={
"title": _unique("auwarhalf"),
"content": "one faction alone starts no war on this post",
"topic": "question",
"war_faction_a": "Loners",
},
)
post_uid = r.json()["data"]["uid"]
refresh_snapshot()
assert get_table("opinion_wars").find_one(post_uid=post_uid) is None
def test_create_post_with_equal_factions_creates_no_war(app_server):
s, _ = _member()
r = s.post(
f"{BASE_URL}/posts/create",
headers=JSON_audit_log,
data={
"title": _unique("auwareq"),
"content": "two identical faction names start no war here",
"topic": "question",
"war_faction_a": "Same",
"war_faction_b": "same",
},
)
post_uid = r.json()["data"]["uid"]
refresh_snapshot()
assert get_table("opinion_wars").find_one(post_uid=post_uid) is None
def test_create_post_with_poll_and_war_attaches_both(app_server):
s, _ = _member()
r = s.post(
f"{BASE_URL}/posts/create",
headers=JSON_audit_log,
data=[
("title", _unique("auwarpoll")),
("content", "this post carries a poll and an opinion war together"),
("topic", "question"),
("poll_question", "Which side?"),
("poll_options", "Tabs"),
("poll_options", "Spaces"),
("war_faction_a", "Tabs"),
("war_faction_b", "Spaces"),
],
)
post_uid = r.json()["data"]["uid"]
refresh_snapshot()
assert get_table("polls").find_one(post_uid=post_uid) is not None
assert get_table("opinion_wars").find_one(post_uid=post_uid) is not None
def test_feed_serializes_war_on_post(app_server):
s, _ = _member()
marker = _unique("auwarfeed")
r = s.post(
f"{BASE_URL}/posts/create",
headers=JSON_audit_log,
data={
"title": marker,
"content": "the feed must carry the serialized war for this post",
"topic": "question",
"war_faction_a": "Cats",
"war_faction_b": "Dogs",
},
)
post_uid = r.json()["data"]["uid"]
feed = s.get(f"{BASE_URL}/feed", headers=JSON_audit_log).json()
item = next(i for i in feed["posts"] if i["post"]["uid"] == post_uid)
assert item["war"] is not None
assert item["war"]["faction_a"] == "Cats"
assert item["war"]["pct_a"] == 50