clash/server/tests/action_card_tests.rs

596 lines
21 KiB
Rust

use crate::common::{TestAction, move_action, payment_response};
use common::JsonTest;
use server::action::Action;
use server::card::HandCard;
use server::city_pieces::Building::Fortress;
use server::collect::PositionCollection;
use server::content::persistent_events::{EventResponse, SelectedStructure};
use server::movement::possible_move_routes;
use server::playing_actions::PlayingAction::Construct;
use server::playing_actions::{PlayingAction, PlayingActionType};
use server::position::Position;
use server::resource_pile::ResourcePile;
use server::structure::Structure;
use server::{advance, collect, construct, cultural_influence};
mod common;
const JSON: JsonTest = JsonTest::new("action_cards");
#[test]
fn test_advance() {
JSON.test(
"advance",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(2))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::culture_tokens(1))).skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectAdvance(advance::Advance::Storage)),
),
],
);
}
#[test]
fn test_inspiration() {
JSON.test(
"inspiration",
vec![TestAction::undoable(
0,
Action::Playing(PlayingAction::ActionCard(3)),
)],
);
}
#[test]
fn test_hero_general() {
JSON.test(
"hero_general",
vec![
TestAction::undoable(
0,
move_action(vec![0, 1, 2, 3, 4, 5], Position::from_offset("C1")),
)
.skip_json(),
TestAction::not_undoable(0, Action::Response(EventResponse::SelectHandCards(vec![]))),
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(5))).skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectPositions(vec![Position::from_offset(
"C1",
)])),
)
.skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::mood_tokens(1))).skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectPositions(vec![Position::from_offset(
"C2",
)])),
),
],
);
}
#[test]
fn test_spy() {
JSON.test(
"spy",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(7))).skip_json(),
TestAction::not_undoable(0, payment_response(ResourcePile::culture_tokens(1)))
.skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![
HandCard::ActionCard(1),
HandCard::ActionCard(2),
])),
),
],
);
}
#[test]
fn test_ideas() {
JSON.test(
"ideas",
vec![TestAction::undoable(
0,
Action::Playing(PlayingAction::ActionCard(9)),
)],
);
}
#[test]
fn test_great_ideas() {
JSON.test(
"great_ideas",
vec![
TestAction::undoable(
0,
move_action(vec![0, 1, 2, 3, 4, 5], Position::from_offset("C1")),
)
.skip_json(),
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(11))),
],
);
}
#[test]
fn test_mercenaries() {
JSON.test(
"mercenaries",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(13))).skip_json(),
TestAction::not_undoable(0, payment_response(ResourcePile::ore(1))),
],
);
}
#[test]
fn test_cultural_takeover() {
JSON.test(
"cultural_takeover",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(15))).skip_json(),
TestAction::undoable(
0,
Action::Playing(PlayingAction::InfluenceCultureAttempt(
cultural_influence::InfluenceCultureAttempt::new(
SelectedStructure::new(Position::from_offset("B3"), Structure::CityCenter),
PlayingActionType::InfluenceCultureAttempt,
),
)),
)
.skip_json(),
TestAction::not_undoable(0, payment_response(ResourcePile::culture_tokens(2))),
],
);
}
#[test]
fn test_city_development() {
JSON.test(
"city_development",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(17))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::culture_tokens(1))).skip_json(),
TestAction::undoable(
0,
Action::Playing(Construct(construct::Construct::new(
Position::from_offset("C2"),
Fortress,
ResourcePile::empty(),
))),
),
],
);
}
#[test]
fn test_production_focus() {
JSON.test(
"production_focus",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(19))).skip_json(),
TestAction::undoable(
0,
Action::Playing(PlayingAction::Collect(collect::Collect::new(
Position::from_offset("C2"),
vec![
PositionCollection::new(Position::from_offset("B1"), ResourcePile::ore(1))
.times(2),
PositionCollection::new(
Position::from_offset("C3"),
ResourcePile::mood_tokens(1),
)
.times(2),
PositionCollection::new(Position::from_offset("C3"), ResourcePile::gold(1)),
],
PlayingActionType::Collect,
))),
),
],
);
}
#[test]
fn test_explorer() {
JSON.test(
"explorer",
vec![
TestAction::undoable(1, Action::Playing(PlayingAction::ActionCard(21))).skip_json(),
TestAction::undoable(1, payment_response(ResourcePile::culture_tokens(1))).skip_json(),
TestAction::not_undoable(
1,
Action::Response(EventResponse::SelectPositions(vec![Position::from_offset(
"B6",
)])),
)
.skip_json(),
TestAction::undoable(1, Action::Response(EventResponse::ExploreResolution(0)))
.skip_json(),
TestAction::undoable(
1,
Action::Response(EventResponse::SelectPositions(vec![Position::from_offset(
"D8",
)])),
),
],
);
}
#[test]
fn test_negotiations() {
JSON.test(
"negotiations",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(23))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::culture_tokens(1))).skip_json(),
TestAction::not_undoable(0, Action::Playing(PlayingAction::EndTurn))
.with_pre_assert(|game| {
assert!(
!possible_move_routes(
game.player(0),
&game,
&[0],
Position::from_offset("C2"),
None,
)
.iter()
.any(|r| r
.iter()
.any(|r| r.destination == Position::from_offset("B1")))
);
})
.skip_json(),
TestAction::not_undoable(1, Action::Playing(PlayingAction::EndTurn))
.with_pre_assert(|game| {
assert!(
!possible_move_routes(
game.player(1),
&game,
&[0],
Position::from_offset("B1"),
None,
)
.iter()
.any(|r| r
.iter()
.any(|r| r.destination == Position::from_offset("C2")))
);
})
.skip_json(),
TestAction::not_undoable(0, move_action(vec![0], Position::from_offset("B1"))),
],
);
}
#[test]
fn negotiations_protects_cities_without_units() {
for fortress in [false, true] {
let mut game = JSON.load_game("negotiations");
game.player_mut(0).units.clear();
game.player_mut(1)
.advances
.insert(advance::Advance::Tactics);
game.player_mut(0).cities[0].pieces.fortress = fortress.then_some(0);
let destination = Position::from_offset("C2");
let can_enter = |game: &server::game::Game| {
possible_move_routes(
game.player(1),
game,
&[0],
Position::from_offset("B1"),
None,
)
.iter()
.any(|routes| routes.iter().any(|route| route.destination == destination))
};
assert!(
can_enter(&game),
"The city is reachable before Negotiations"
);
let game =
server::game_api::execute(game, Action::Playing(PlayingAction::ActionCard(23)), 0);
let game =
server::game_api::execute(game, payment_response(ResourcePile::culture_tokens(1)), 0);
let game = server::game_api::execute(game, Action::Playing(PlayingAction::EndTurn), 0);
assert!(
!can_enter(&game),
"Negotiations must protect a city even without units"
);
}
}
#[test]
fn test_assassination() {
JSON.test(
"assassination",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(27))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::culture_tokens(1))).skip_json(),
TestAction::not_undoable(0, Action::Playing(PlayingAction::EndTurn)).skip_json(),
TestAction::not_undoable(1, Action::Playing(PlayingAction::EndTurn)).with_pre_assert(
|game| {
assert_eq!(game.actions_left, 2);
},
),
],
);
}
#[test]
fn test_mass_production() {
JSON.test(
"mass_production",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(29))).skip_json(),
TestAction::undoable(
0,
Action::Playing(PlayingAction::Collect(collect::Collect::new(
Position::from_offset("C2"),
vec![
PositionCollection::new(Position::from_offset("B1"), ResourcePile::ore(1)),
PositionCollection::new(Position::from_offset("B2"), ResourcePile::wood(1)),
PositionCollection::new(
Position::from_offset("C3"),
ResourcePile::mood_tokens(1),
),
],
PlayingActionType::Collect,
))),
),
],
);
}
#[test]
fn test_new_plans() {
JSON.test(
"new_plans",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(31))).skip_json(),
TestAction::not_undoable(0, payment_response(ResourcePile::culture_tokens(1)))
.skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![
HandCard::ObjectiveCard(3),
HandCard::ObjectiveCard(2),
])),
),
],
);
}
#[test]
fn test_synergies() {
JSON.test(
"synergies",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(34))).skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectAdvance(advance::Advance::Fishing)),
)
.skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::ideas(2))).skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectAdvance(advance::Advance::WarShips)),
)
.skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::ideas(2))),
],
);
}
#[test]
fn test_teach_us() {
JSON.test(
"teach_us",
vec![
TestAction::not_undoable(
0,
move_action(vec![0, 1, 2, 3, 4, 5], Position::from_offset("C1")),
)
.skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
35,
)])),
),
],
);
}
#[test]
fn test_militia() {
JSON.test(
"militia",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(37))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::culture_tokens(1))),
],
);
}
#[test]
fn test_tech_trade() {
JSON.test(
"tech_trade",
vec![TestAction::undoable(
0,
Action::Playing(PlayingAction::ActionCard(39)),
)],
);
}
#[test]
fn test_new_ideas() {
JSON.test(
"new_ideas",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(41))).skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectAdvance(advance::Advance::Storage)),
)
.skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::food(2))),
],
);
}
#[test]
fn technology_trade_only_offers_partners_within_two_spaces() {
use server::cache::Cache;
use server::game::{Game, GameContext};
use server::game_data::GameData;
for card in [39, 40] {
for unit_contact in [false, true] {
for (position, in_range) in [("C4", true), ("C5", false)] {
let mut data: serde_json::Value =
serde_json::from_str(include_str!("test_games/action_cards/tech_trade.json"))
.unwrap();
data["players"][0]["cities"] =
serde_json::json!([{ "position": "C2", "mood_state": "Happy" }]);
data["players"][0]["units"] = serde_json::json!([]);
data["players"][0]["action_cards"] = serde_json::json!([card]);
data["players"][1]["cities"] =
serde_json::json!([{ "position": "C1", "mood_state": "Happy" }]);
data["players"][1]["units"] = serde_json::json!([]);
let mut nearby = data["players"][1].clone();
nearby["id"] = serde_json::json!(2);
nearby["cities"][0]["position"] = serde_json::json!("C3");
let mut target = data["players"][1].clone();
target["id"] = serde_json::json!(3);
if unit_contact {
target["cities"] = serde_json::json!([]);
target["units"] = serde_json::json!([{ "position": position, "unit_type": "Settler", "id": 0 }]);
} else {
target["cities"][0]["position"] = serde_json::json!(position);
}
let players = data["players"].as_array_mut().unwrap();
players.insert(2, nearby);
players.insert(3, target);
for (i, player) in players.iter_mut().enumerate() {
player["id"] = serde_json::json!(i);
}
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);
// The synthetic contacts must occupy real, revealed map spaces.
for pos in ["C1", "C2", "C3", "C4", "C5"] {
game.map
.tiles
.insert(Position::from_offset(pos), server::map::Terrain::Fertile);
}
let game = server::game_api::execute(
game,
Action::Playing(PlayingAction::ActionCard(card)),
0,
);
let event = serde_json::to_value(game.current_event()).unwrap();
let choices = &event["handler"]["request"]["SelectPlayer"]["choices"];
assert_eq!(
choices,
&if in_range {
serde_json::json!([1, 2, 3])
} else {
serde_json::json!([1, 2])
},
"card {card}, unit contact {unit_contact}, position {position}"
);
}
}
}
}
#[test]
fn test_explorers_independent_benefits() {
use server::action_card::ActionCardInfo;
use server::content::persistent_events::PersistentEventRequest;
use server::map::Terrain;
use server::unit::{Unit, UnitType};
for id in [21, 22] {
for revealed in [false, true] {
for settlers in [0, 4] {
for owns_city in [false, true] {
let mut game = JSON.load_game("explorer");
if revealed {
game.map.unexplored_blocks.clear();
for terrain in game.map.tiles.values_mut() {
if terrain.is_unexplored() {
*terrain = Terrain::Fertile;
}
}
}
let position = game.player(1).cities[0].position;
let p = game.player_mut(1);
p.action_cards = vec![id];
p.units = (0..settlers)
.map(|n| Unit::new(1, position, UnitType::Settler, n))
.collect();
p.next_unit_id = settlers;
if !owns_city {
p.cities.clear();
}
let card = game.cache.get_action_card(id);
assert_eq!(
(card.civil_card.can_play)(
&game,
game.player(1),
&ActionCardInfo::new(id, None, None)
),
owns_city && (!revealed || settlers < 4),
"card {id}, revealed {revealed}, settlers {settlers}, city {owns_city}"
);
if !revealed || !owns_city || settlers == 4 {
continue;
}
for accept in [false, true] {
let actions = game.actions_left;
let culture = game.player(1).resources.culture_tokens;
let activations = game.player(1).cities[0].activations;
let mut after = server::game_api::execute(
game.clone(),
Action::Playing(PlayingAction::ActionCard(id)),
1,
);
after = server::game_api::execute(
after,
payment_response(ResourcePile::culture_tokens(1)),
1,
);
let PersistentEventRequest::SelectPositions(request) =
&after.current_event_handler().unwrap().request
else {
panic!("expected settler placement, not exploration");
};
assert_eq!(request.description, "Gain a free settler");
after = server::game_api::execute(
after,
Action::Response(EventResponse::SelectPositions(if accept {
vec![position]
} else {
vec![]
})),
1,
);
assert!(after.current_event_handler().is_none());
assert_eq!(after.player(1).units.len(), usize::from(accept));
assert_eq!(after.actions_left, actions - 1);
assert_eq!(after.player(1).resources.culture_tokens, culture - 1);
assert_eq!(after.player(1).cities[0].activations, activations);
}
}
}
}
}
}