From 09c2e4bdb31ae545840497e94dc1cb8273d194f1 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Tue, 24 Nov 2026 02:07:45 +0000 Subject: [PATCH] Reserve reveal priority above tactics for Strategist; cover real Martyr collision --- docs/strategist-tactics-priorities.md | 43 +++++++ server/src/ability_initializer.rs | 15 ++- server/src/combat_listeners.rs | 9 +- server/src/content/civilizations/persia.rs | 2 +- server/src/content/tactics_cards.rs | 3 + server/src/events.rs | 11 ++ server/src/tactics_card.rs | 6 + server/tests/combat_preparation_tests.rs | 135 +++++++++++++++++++++ 8 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 docs/strategist-tactics-priorities.md diff --git a/docs/strategist-tactics-priorities.md b/docs/strategist-tactics-priorities.md new file mode 100644 index 00000000..f0235370 --- /dev/null +++ b/docs/strategist-tactics-priorities.md @@ -0,0 +1,43 @@ +# Strategist / tactics listener priority bands + +Release 0.1.240 / engine 0.4.171 includes a deterministic fix for Check CI run +45306584337. The random-actions test exposed a genuine registration panic, not a +flaky assertion: `LeaderAbility("Strategist")` and attacking `TacticsCard(24)` +both registered priority 100 in `combat_round_start_reveal_tactics`. +Card 24 is **Negotiations / Martyr** (civil / tactics halves). + +`Event` requires distinct origins to have distinct priorities. Persistent response +cursors also use priorities, so dropping this check, overwriting a listener, or +allowing arbitrary ties would be unsafe. Both tactics cards are temporarily +installed on **both** combatants; Cyrus need not be the owner of the colliding +card. Same-origin registrations are a separate existing objective mechanism. + +## Ordering contract + +- Defender tactics base priorities occupy `[0, 100)`. +- Attacker tactics keep their existing +100 offset, occupying `[100, 200)`. +- Reveal abilities occupy priorities `>= 200`; Strategist uses 200. +- Priority is descending **within each participant**. Participant order, combat + phases, cancellation timing, attacker-before-defender card ordering and card + response priorities are unchanged. This is not a new global event scheduler. +- Strategist remains in the reveal phase, before that participant's card + cancellation/effects, not after reveal or in the later tactics-effect phase. +- Previously saved Strategist responses at 100 match by exact origin as well as + the old priority and resume at 200. An attacking card must not consume them. + +Construction checks every tactics base priority against its band; registration +checks reveal abilities against the pre-tactics band and retains the ordinary +origin/priority uniqueness invariant. Current tactics base priorities are 0–10; +Strategist is the only non-card listener on the reveal-cancellation event. The +catalog regression registers every distinct attacker/defender card pair alongside +**every leader**, on both participants, and deinitializes every registration. + +The real-combat regression reproduces the original panic before the fix. It covers +attacking and defending Cyrus, human defenders, both Strategist answers, normal +first-round preparation and a serialized later-round continuation, reload, and old +priority-100 pending decisions. Accepted replacement adds +2; declining preserves +Martyr's successive sacrifice requests for both players. Replacing Martyr itself +must suppress both its cancellation and its sacrifices, so its sacrifice listener +now observes the existing `is_active(..., AllPlayers)` ignored-card check too. +No random tests are suppressed, no fixtures are rewritten, and Aggressor / Great +Architect history boundaries are unchanged. diff --git a/server/src/ability_initializer.rs b/server/src/ability_initializer.rs index 8334d210..f9ed8607 100644 --- a/server/src/ability_initializer.rs +++ b/server/src/ability_initializer.rs @@ -300,7 +300,14 @@ pub(crate) trait AbilityInitializerSetup: Sized { if let Some(ref c) = phase.player.handler && let Some(ref action) = c.response && (c.priority == priority - || (c.priority == priority - p.priority_delta && c.origin == p.origin)) + || (c.priority == priority - p.priority_delta && c.origin == p.origin) + // A pre-fix saved Strategist decision used 100. + // Match by origin too, never let an attacker card + // consume that response merely because it uses 100. + || (c.priority == 100 + && priority == crate::tactics_card::BEFORE_TACTICS_PRIORITY + && c.origin == p.origin + && p.origin == EventOrigin::LeaderAbility("Strategist".into()))) { let mut current = phase.clone(); current @@ -1074,6 +1081,12 @@ where E: Fn(&mut PlayerEvents) -> &mut Event + 'static + Clone + Sync + Send, F: Fn(&mut T, &U, &V, &mut W, &EventPlayer) + 'static + Clone + Sync + Send, { + if matches!(setup.get_key(), EventOrigin::TacticsCard(_)) { + assert!( + (0..crate::tactics_card::TACTICS_PRIORITY_BAND).contains(&priority), + "Tactics base priority must fit its reserved band" + ); + } let deinitialize_event = event.clone(); let initializer = move |game: &mut Game, p: &EventPlayer, prio_delta: i32| { let e = event(&mut p.get_mut(game).events); diff --git a/server/src/combat_listeners.rs b/server/src/combat_listeners.rs index 08396b76..f598c7a5 100644 --- a/server/src/combat_listeners.rs +++ b/server/src/combat_listeners.rs @@ -702,8 +702,13 @@ fn add_tactics_listener( if let Some(card) = card { for p in players { match role { - // avoid clash in priority - and attacker card requests should come first anyway - CombatRole::Attacker => card.listeners.init_with_prio_delta(game, *p, 100), + // Keep attacker card requests before defender card requests, but + // below reveal abilities (see the reserved tactics bands). + CombatRole::Attacker => card.listeners.init_with_prio_delta( + game, + *p, + crate::tactics_card::TACTICS_PRIORITY_BAND, + ), CombatRole::Defender => card.listeners.init(game, *p), } } diff --git a/server/src/content/civilizations/persia.rs b/server/src/content/civilizations/persia.rs index 161fd0c7..2505cc1f 100644 --- a/server/src/content/civilizations/persia.rs +++ b/server/src/content/civilizations/persia.rs @@ -47,7 +47,7 @@ fn cyrus() -> LeaderInfo { Some(ResourceRewardRequest::new(p.reward_options().tokens(amount), "King of Persia · Regional tribute".into())) }), can_activate_leader_city).build(), LeaderAbility::builder("Strategist", "When revealing a tactics card, Cyrus may use +2 combat value instead of the card's effects, even if the opponent cancels the card.") - .add_bool_request(|e| &mut e.combat_round_start_reveal_tactics, 100, |game, p, r| { + .add_bool_request(|e| &mut e.combat_round_start_reveal_tactics, crate::tactics_card::BEFORE_TACTICS_PRIORITY, |game, p, r| { (r.combat.has_leader(r.combat.role(p.index),game) && get_combat_strength(p.index,r).tactics_card.is_some()).then(|| "Strategist · Replace your tactics card's effects with +2 combat value?".into()) }, |game,s,r| { if s.choice { r.combat.stats.ignored_tactics_cards.push(get_combat_strength(s.player_index,r).tactics_card.unwrap()); update_combat_strength(game,s.player_index,r,|_,_,strength,_| { strength.extra_combat_value+=2; strength.roll_log.push("Strategist adds +2 combat value".into()); }); } }).build()) } diff --git a/server/src/content/tactics_cards.rs b/server/src/content/tactics_cards.rs index 4d482dfe..79377316 100644 --- a/server/src/content/tactics_cards.rs +++ b/server/src/content/tactics_cards.rs @@ -377,6 +377,9 @@ pub(crate) fn martyr(id: u8) -> TacticsCard { |event| &mut event.combat_round_start_tactics, 0, move |game, p, s| { + if !s.is_active(p.index, id, TacticsCardTarget::AllPlayers) { + return None; + } Some(UnitsRequest::new( p.index, s.combat.fighting_units(game, p.index), diff --git a/server/src/events.rs b/server/src/events.rs index 12b7b0bc..a671bea1 100644 --- a/server/src/events.rs +++ b/server/src/events.rs @@ -191,6 +191,17 @@ where ) where F: Fn(&mut T, &U, &V, &mut W, &EventPlayer) + 'static + Sync + Send, { + // Reveal abilities must precede both card bands, never share their + // priorities. Keep the ordinary uniqueness check as well: equal + // priorities are not interchangeable listener identities on resume. + if self.name == "combat_round_start_reveal_tactics" + && !matches!(player.origin, EventOrigin::TacticsCard(_)) + { + assert!( + priority >= crate::tactics_card::BEFORE_TACTICS_PRIORITY, + "Reveal abilities must precede the reserved tactics bands" + ); + } // objectives can have the same key, but you still can only fulfill one of them at a time let key = &player.origin; if let Some(old) = self diff --git a/server/src/tactics_card.rs b/server/src/tactics_card.rs index dff9bb99..9000ba02 100644 --- a/server/src/tactics_card.rs +++ b/server/src/tactics_card.rs @@ -15,6 +15,12 @@ use action_card::discard_action_card; use std::fmt::Display; use std::sync::Arc; +// Persistent listeners run in descending priority within each participant. +// Reserve [0, 100) for defender cards and [100, 200) for attacker cards; +// reveal abilities such as Strategist must run before either card's effects. +pub(crate) const TACTICS_PRIORITY_BAND: i32 = 100; +pub(crate) const BEFORE_TACTICS_PRIORITY: i32 = 2 * TACTICS_PRIORITY_BAND; + #[derive(Clone, PartialEq, Eq, Copy)] pub enum TacticsCardTarget { ActivePlayer, diff --git a/server/tests/combat_preparation_tests.rs b/server/tests/combat_preparation_tests.rs index db13fd1e..ca6379bd 100644 --- a/server/tests/combat_preparation_tests.rs +++ b/server/tests/combat_preparation_tests.rs @@ -429,3 +429,138 @@ fn undo_redo_of_a_branched_battle_keeps_new_preparation() { expected["finalized_combat_from"] ); } + +// Real Negotiations (24) has the Martyr tactics half. Its cancellation listener +// is installed on BOTH combatants, including Cyrus even when he plays no card. +#[test] +fn strategist_and_attacking_martyr_have_distinct_reveal_priorities() { + for cyrus in [0, 1] { + let mut v = fixture("tactics_cards/peltasts"); + v["players"][cyrus]["civilization"] = json!("Persia"); + v["players"][cyrus]["units"][0]["unit_type"] = json!({"Leader":"Cyrus"}); + v["players"][0]["action_cards"] = json!([24]); + v["players"][1]["action_cards"] = json!([6]); + let g = tactics(attack(v), 0, Some(24)); + let g = tactics(g, 1, Some(6)); + assert_eq!(g.active_player(), cyrus); + assert!( + request(&g).get("BoolRequest").is_some(), + "{:?}", + request(&g) + ); + } +} + +#[test] +fn every_leader_and_pair_of_tactics_cards_fit_the_reserved_priority_bands() { + let cache = Cache::new(&Default::default()); + let cards: Vec<_> = cache + .get_action_cards() + .iter() + .filter_map(|c| c.tactics_card.clone()) + .collect(); + assert!(cards.iter().any(|card| card.id == 24)); + assert!( + cache + .get_civilizations() + .iter() + .any(|civ| civ.name == "Persia") + ); + for civ in cache.get_civilizations() { + for leader in &civ.leaders { + let mut v = fixture("tactics_cards/peltasts"); + v["players"][0]["civilization"] = json!(civ.name); + v["players"][0]["units"][0]["unit_type"] = json!({"Leader":leader.leader}); + let mut g = load(v); + for attacker in &cards { + for defender in &cards { + if attacker.id == defender.id { + continue; + } + // Production installs BOTH cards on BOTH combatants. + for player in [0, 1] { + attacker.listeners.init_with_prio_delta(&mut g, player, 100); + defender.listeners.init(&mut g, player); + } + for player in [0, 1] { + attacker.listeners.deinit(&mut g, player); + defender.listeners.deinit(&mut g, player); + } + } + } + } + } +} + +#[test] +fn strategist_precedes_martyr_effects_on_first_and_later_rounds_and_resumes_old_priority() { + for cyrus in [0, 1] { + for round_number in [1, 2] { + for replace in [false, true] { + let mut v = fixture("tactics_cards/peltasts"); + v["players"][cyrus]["civilization"] = json!("Persia"); + v["players"][cyrus]["units"][0]["unit_type"] = json!({"Leader":"Cyrus"}); + v["players"][0]["action_cards"] = json!([24]); + v["players"][1]["action_cards"] = json!([6]); + let g = tactics(attack(v), 0, Some(24)); + let mut v = data(&g); + if round_number == 2 { + let r = &mut v["events"][0]["event_type"]["CombatRoundStart"]; + r["combat"]["stats"]["round"] = json!(2); + r.as_object_mut().unwrap().remove("preparation"); + r["phase"] = json!("Default"); + v["events"][0]["players_used"] = json!([0]); + } + let g = tactics(load(v), 1, Some(6)); + assert_eq!(g.active_player(), cyrus); + assert!(request(&g).get("BoolRequest").is_some()); + let mut v = data(&g); + let round = &v["events"][0]["event_type"]["CombatRoundStart"]; + assert_eq!(round["phase"], json!("RevealTacticsCard")); + assert_eq!(round["attacker_strength"]["tactics_card"], json!(24)); + assert_eq!(round["defender_strength"]["tactics_card"], json!(6)); + // Old pending decisions are still consumable by Strategist, + // not by the attacker card now occupying priority 100. + if round_number == 2 { + v["events"][0]["handler"]["priority"] = json!(100); + v["events"][0]["last_priority_used"] = json!(100); + } + let g = play(load(v), cyrus, json!({"Response":{"Bool":replace}})); + let log = data(&g)["log"].to_string(); + if replace { + assert!( + data(&g) + .to_string() + .contains("Strategist adds +2 combat value") + ); + } + if replace && cyrus == 0 { + // Replacing Martyr disables cancellation AND sacrifice. + assert!(!log.contains("sacrifices")); + assert!(!request(&g).to_string().contains("sacrifice")); + assert!( + data(&g) + .to_string() + .contains("High Morale added 2 combat value") + ); + } else { + let v = data(&g); + let round = &v["events"][0]["event_type"]["CombatRoundStart"]; + assert_eq!(round["phase"], json!("TacticsCard")); + assert!(round["defender_strength"]["tactics_card"].is_null()); + if replace { + assert_eq!(round["defender_strength"]["extra_combat_value"], json!(2)); + } + assert!(request(&g).to_string().contains("sacrifice")); + let before = g.player(0).units.len(); + let g = play(g, 0, json!({"Response":{"SelectUnits":[2]}})); + assert_eq!(g.player(0).units.len(), before - 1); + assert!(request(&g).to_string().contains("sacrifice")); + assert_eq!(g.active_player(), 1); + let g = play(g, 1, json!({"Response":{"SelectUnits":[1]}})); + assert!(!g.player(1).units.iter().any(|u| u.id == 1)); + } + } + } + } +}