Fix City States activation requirements and pirate token rewards

This commit is contained in:
coyotte508 2026-10-22 16:37:47 +02:00
commit 01f449998f
8 changed files with 182 additions and 17 deletions

View file

@ -348,6 +348,7 @@ pub(crate) fn activate_city_for_collection(
game: &mut Game,
origin: &EventOrigin,
) {
crate::content::civilizations::greece::record_city_states_activation(game, position);
let city = game.get_any_city_mut(position);
assert!(city.can_activate());

View file

@ -123,6 +123,24 @@ fn hellenistic_culture() -> SpecialAdvanceInfo {
.build()
}
// Keep the pre-action requirements across deferred construction rewards and reloads.
// event_info is serialized and cleared at the end of the turn.
pub(crate) fn record_city_states_activation(game: &mut Game, position: crate::position::Position) {
let city = game.get_any_city(position);
let player = city.player_index;
if city.is_activated()
&& game
.player(player)
.has_special_advance(SpecialAdvance::CityStates)
&& !game.player(player).event_info.contains_key("city_states")
{
let snapshot = serde_json::to_string(&(city.size(), city.mood_state.clone())).unwrap();
game.player_mut(player)
.event_info
.insert(format!("city_states_activation_{position}"), snapshot);
}
}
fn city_states() -> SpecialAdvanceInfo {
SpecialAdvanceInfo::builder(
SpecialAdvance::CityStates,
@ -145,22 +163,28 @@ fn city_states() -> SpecialAdvanceInfo {
let p = game.player(player_index);
let city = p.get_city(*position);
let (size, mood) = p.event_info
.get(&format!("city_states_activation_{position}"))
.and_then(|value| serde_json::from_str::<(usize, crate::city::MoodState)>(value).ok())
// Existing pending saves have already lost one mood step.
.unwrap_or((city.size(), city.mood_state.clone() + 1));
let cities = p
.cities
.iter()
.filter_map(
// mood was already decreased
|c| {
(c.position != *position
&& c.mood_state > city.mood_state
&& c.size() >= city.size())
.then_some(c.position)
},
)
.filter_map(|c| {
(c.position != *position
&& c.mood_state >= mood
&& c.size() >= size
&& c.can_activate())
.then_some(c.position)
})
.collect_vec();
if cities.is_empty() {
game.player_mut(player_index)
.event_info
.remove(&format!("city_states_activation_{position}"));
return None;
}
@ -171,12 +195,15 @@ fn city_states() -> SpecialAdvanceInfo {
))
},
|game, s, position| {
game.player_mut(s.player_index)
.event_info
.remove(&format!("city_states_activation_{position}"));
if !s.choice.is_empty() {
activate_city(s.choice[0], game, &s.origin);
increase_mood_state(game, *position, 1, &s.origin);
game.player_mut(s.player_index)
.event_info
.insert("city_states".to_string(), "used".to_string());
activate_city(s.choice[0], game, &s.origin);
increase_mood_state(game, *position, 1, &s.origin);
}
},
)

View file

@ -43,9 +43,10 @@ pub(crate) fn pirates_bonus() -> Ability {
|event| &mut event.combat_end,
103,
|game, p, i| {
if i.opponent_player(p.index, game).civilization.is_pirates() {
let destroyed = i.opponent(p.index).losses.get(&UnitType::Ship);
if i.opponent_player(p.index, game).civilization.is_pirates() && destroyed > 0 {
Some(ResourceRewardRequest::new(
p.reward_options().tokens(1),
p.reward_options().tokens(destroyed),
"Select a reward for fighting the Pirates".to_string(),
))
} else {

View file

@ -217,3 +217,62 @@ fn master() {
],
);
}
#[test]
fn city_states_uses_size_before_observatory_construction_after_reload() {
use server::cache::Cache;
use server::city::MoodState;
use server::construct::Construct;
use server::content::persistent_events::PersistentEventRequest;
use server::game::{Game, GameContext};
use server::game_api::execute;
use server::game_data::GameData;
let game = JSON.load_game("city_states");
let mut data = serde_json::to_value(game.cloned_data()).unwrap();
data["players"][0]["advances"]
.as_array_mut()
.unwrap()
.push(serde_json::json!("Math"));
let data: GameData = serde_json::from_value(data).unwrap();
let cache = Cache::new(&data.options);
let mut game = Game::from_data(data, cache, GameContext::Play);
let original = Position::from_offset("A1");
let substitute = Position::from_offset("B3");
game.player_mut(0).get_city_mut(substitute).activations = 0;
let cards_before = game.player(0).action_cards.len();
let game = execute(
game,
Action::Playing(PlayingAction::Construct(Construct::new(
original,
Building::Observatory,
ResourcePile::new(1, 1, 1, 0, 0, 0, 0),
))),
0,
);
assert_eq!(game.player(0).get_city(original).size(), 3);
assert_eq!(game.player(0).action_cards.len(), cards_before + 1);
let data = game.cloned_data();
let cache = Cache::new(&data.options);
let game = Game::from_data(data, cache, GameContext::Play);
let PersistentEventRequest::SelectPositions(request) =
&game.current_event_handler().unwrap().request
else {
panic!("expected City States choice");
};
assert_eq!(request.choices, vec![substitute]);
let game = execute(
game,
Action::Response(EventResponse::SelectPositions(vec![substitute])),
0,
);
assert_eq!(
game.player(0).get_city(original).mood_state,
MoodState::Happy
);
assert_eq!(game.player(0).get_city(substitute).activations, 1);
assert_eq!(
game.player(0).get_city(substitute).mood_state,
MoodState::Happy
);
}

View file

@ -357,7 +357,7 @@ fn test_recruit_combat() {
TestAction::undoable(
0,
Action::Response(EventResponse::ResourceReward(ResourcePile::culture_tokens(
1,
2,
))),
)
.skip_json(),
@ -479,3 +479,76 @@ fn naval_defenders_exclude_passengers_from_dice_and_combat_casualties() {
assert_eq!(req.request.choices, vec![90, 91, 92, 93]);
assert_eq!(req.request.needed, 2..=2);
}
#[test]
fn pirate_rewards_scale_with_destroyed_ships_and_allow_mixed_tokens() {
let mut game = JSON.load_game("ship_combat");
let pirates = game
.players
.iter()
.position(|p| p.civilization.is_pirates())
.unwrap();
let mut ship = game.player(1).units[0].clone();
ship.id = 90;
ship.unit_type = UnitType::Ship;
ship.position = Position::from_offset("D2");
let mut second = ship.clone();
second.id = 91;
game.player_mut(pirates).units = vec![ship, second];
game.player_mut(1).units.clear();
game.player_mut(0).resources.gold = 0;
// Both fleets roll 6s: two pirate ships destroyed even in a drawn battle.
game.dice_roll_outcomes = vec![11; 4];
let mood = game.player(0).resources.mood_tokens;
let culture = game.player(0).resources.culture_tokens;
let game = execute(
game,
move_action(vec![7, 8], Position::from_offset("D2")),
0,
);
assert!(game.player(pirates).units.is_empty());
assert_eq!(game.player(0).resources.gold, 2);
let PersistentEventRequest::ResourceReward(request) =
&game.current_event_handler().unwrap().request
else {
panic!("expected pirate token reward");
};
let mixed = ResourcePile::mood_tokens(1) + ResourcePile::culture_tokens(1);
assert!(request.reward.payment_options.is_valid_payment(&mixed));
assert!(
!request
.reward
.payment_options
.is_valid_payment(&ResourcePile::mood_tokens(1))
);
let game = execute(
game,
Action::Response(EventResponse::ResourceReward(mixed)),
0,
);
assert_eq!(game.player(0).resources.mood_tokens, mood + 1);
assert_eq!(game.player(0).resources.culture_tokens, culture + 1);
}
#[test]
fn retreat_from_pirates_without_a_kill_grants_no_reward() {
let mut game = JSON.load_game("ship_combat");
let pirates = game
.players
.iter()
.position(|p| p.civilization.is_pirates())
.unwrap();
game.player_mut(pirates).units = game.player(1).units.clone();
game.player_mut(1).units.clear();
game.dice_roll_outcomes = vec![0; 4];
let before = game.player(0).resources.clone();
let game = execute(
game,
move_action(vec![7, 8], Position::from_offset("D2")),
0,
);
let game = execute(game, Action::Response(EventResponse::ResourceReward(ResourcePile::empty())), 0);
assert_retreat_round(&game, 1);
let game = execute(game, Action::Response(EventResponse::Bool(true)), 0);
assert!(game.events.is_empty());
assert_eq!(game.player(0).resources, before);
}

View file

@ -437,3 +437,7 @@ Rule source: the official Monumental Edition rulebook, page 36, “Spaces”: ht
Viewer 0.1.156 / engine 0.4.107 lets combat estimates resolve the fixed “Pirate ship destroyed” gold reward between rounds, including zero-gold rewards. Estimates now continue from attack confirmation and tactics selection against pirates. Live game state and post-combat reward choices remain unchanged. Verified with 851 frontend/engine tests, deterministic non-mutation and tactics-step regression coverage, locale validation, type checking and production build.
Viewer 0.1.157 simplifies Great Person acquisition prompts: the Take card button carries the culture cost once, with a separate Pass button and an effect preview. Great Diplomat is labeled active when taken; hand cards show their action cost. Acquisition instructions are collapsible in event explanations, and public Great Person text no longer repeats them in the effect. Verified with 851 frontend tests, locale validation, type checking, production build and an approved local browser preview. Engine remains 0.4.107; no saved-data changes.
Viewer 0.1.158 / engine 0.4.108 checks City States against the original city's size and mood when activated, before construction changes its size. Requirements survive deferred rewards and reloads, and exhausted substitute cities are excluded. Pirate token rewards now count destroyed ships across the battle, allow a mood/culture split, and grant nothing for a retreat without kills. Existing per-ship gold rewards are retained. Completed historical actions are not rewritten.
Sources: official Monumental Edition rulebook, page 33 (City States explicitly allows size 2 growing to size 3 with a size-2 substitute) and page 27 (one gold and one mood/culture per pirate eliminated, regardless of winner). Verified with 32 Greek/combat Rust tests, 851 frontend/engine tests, 17 locales, type checking and production build. Regressions include Observatory card draw plus reload, both fleets wiped out, mixed token rewards, and zero-kill retreat.

View file

@ -1,12 +1,12 @@
{
"name": "@boardgamers/clash-web-client",
"version": "0.1.157",
"version": "0.1.158",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@boardgamers/clash-web-client",
"version": "0.1.157",
"version": "0.1.158",
"dependencies": {
"@boardgamers/protocol": "0.9.0",
"lucide-svelte": "^0.577.0",

View file

@ -1,6 +1,6 @@
{
"name": "@boardgamers/clash-web-client",
"version": "0.1.157",
"version": "0.1.158",
"private": true,
"type": "module",
"scripts": {