recruit integration test & fmt
This commit is contained in:
parent
20793879ca
commit
5edc5488f0
8 changed files with 117 additions and 39 deletions
|
|
@ -17,7 +17,7 @@ use crate::{
|
|||
status_phase::StatusPhaseState::{self, *},
|
||||
unit::{
|
||||
Unit,
|
||||
UnitType,
|
||||
UnitType::{self, *},
|
||||
},
|
||||
utils,
|
||||
wonder::Wonder,
|
||||
|
|
@ -684,22 +684,29 @@ impl Game {
|
|||
let player = &mut self.players[player_index];
|
||||
let mut replaced_units_undo_context = Vec::new();
|
||||
for unit in replaced_units {
|
||||
let unit = player.take_unit(unit).expect("the player should have the replaced units");
|
||||
let unit = player
|
||||
.take_unit(unit)
|
||||
.expect("the player should have the replaced units");
|
||||
player.available_units += &unit.unit_type;
|
||||
replaced_units_undo_context.push(unit);
|
||||
}
|
||||
self.replaced_units_undo_context = if replaced_leader.is_some() || !replaced_units_undo_context.is_empty() {
|
||||
Some((replaced_units_undo_context, replaced_leader))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.replaced_units_undo_context =
|
||||
if replaced_leader.is_some() || !replaced_units_undo_context.is_empty() {
|
||||
Some((replaced_units_undo_context, replaced_leader))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
for unit_type in units {
|
||||
player.available_units -= &unit_type;
|
||||
let position = player
|
||||
let city = player
|
||||
.get_city(city_position)
|
||||
.expect("player should have a city at the recruitment position")
|
||||
.port_position
|
||||
.expect("there should be a port in the city");
|
||||
.expect("player should have a city at the recruitment position");
|
||||
let position = match &unit_type {
|
||||
Ship => city
|
||||
.port_position
|
||||
.expect("there should be a port in the city"),
|
||||
_ => city_position,
|
||||
};
|
||||
let unit = Unit::new(player_index, position, unit_type, player.next_unit_id);
|
||||
player.units.push(unit);
|
||||
player.next_unit_id += 1;
|
||||
|
|
@ -737,7 +744,10 @@ impl Game {
|
|||
}
|
||||
let player = &mut self.players[player_index];
|
||||
for _ in 0..units.len() {
|
||||
let unit = player.units.pop().expect("the player should have the recruited units when undoing");
|
||||
let unit = player
|
||||
.units
|
||||
.pop()
|
||||
.expect("the player should have the recruited units when undoing");
|
||||
player.available_units += &unit.unit_type;
|
||||
player.next_unit_id -= 1;
|
||||
}
|
||||
|
|
@ -751,7 +761,9 @@ impl Game {
|
|||
player.units.push(unit);
|
||||
}
|
||||
if let Some(replaced_leader) = replaced_leader {
|
||||
let replaced_leader = civilizations::get_leader_by_name(&replaced_leader, &player.civilization.name).expect("there should be a replaced leader in context data");
|
||||
let replaced_leader =
|
||||
civilizations::get_leader_by_name(&replaced_leader, &player.civilization.name)
|
||||
.expect("there should be a replaced leader in context data");
|
||||
(replaced_leader.player_initializer)(self, player_index);
|
||||
(replaced_leader.player_one_time_initializer)(self, player_index);
|
||||
let player = &mut self.players[player_index];
|
||||
|
|
|
|||
|
|
@ -30,6 +30,6 @@ pub mod position;
|
|||
pub mod resource_pile;
|
||||
mod special_advance;
|
||||
mod status_phase;
|
||||
mod unit;
|
||||
pub mod unit;
|
||||
mod utils;
|
||||
mod wonder;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ use crate::{
|
|||
ChangeGovernmentType, CompleteObjectives, DetermineFirstPlayer, FreeAdvance, RaseSize1City,
|
||||
StatusPhaseAction,
|
||||
},
|
||||
utils, unit::Units,
|
||||
unit::Units,
|
||||
utils,
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
|
|
|
|||
|
|
@ -732,9 +732,8 @@ impl Player {
|
|||
|
||||
pub fn take_unit(&mut self, id: u32) -> Option<Unit> {
|
||||
Some(
|
||||
self.units.remove(
|
||||
self.units.iter().position(|unit| unit.id == id)?
|
||||
)
|
||||
self.units
|
||||
.remove(self.units.iter().position(|unit| unit.id == id)?),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ use crate::{
|
|||
game::{Game, GameState::*},
|
||||
map::Terrain,
|
||||
position::Position,
|
||||
resource_pile::ResourcePile, unit::UnitType,
|
||||
resource_pile::ResourcePile,
|
||||
unit::UnitType,
|
||||
};
|
||||
|
||||
pub const PORT_CHOICES: [ResourcePile; 3] = [
|
||||
|
|
@ -113,7 +114,7 @@ impl PlayingAction {
|
|||
let city = player.get_city(city_position).expect("Illegal action");
|
||||
let cost = player.construct_cost(&city_piece, city);
|
||||
assert!(
|
||||
city.can_construct(&city_piece, player) && payment.can_afford(&cost),
|
||||
city.can_construct(&city_piece, player) && cost.is_valid_payment(&payment),
|
||||
"Illegal action"
|
||||
);
|
||||
if matches!(&city_piece, Port) {
|
||||
|
|
@ -153,12 +154,27 @@ impl PlayingAction {
|
|||
city.activate();
|
||||
game.players[player_index].gain_resources(total_collect);
|
||||
}
|
||||
Recruit { units, city_position, payment, leader_index, replaced_units } => {
|
||||
let cost = units.iter().map(UnitType::cost).sum();
|
||||
Recruit {
|
||||
units,
|
||||
city_position,
|
||||
payment,
|
||||
leader_index,
|
||||
replaced_units,
|
||||
} => {
|
||||
let cost = units.iter().map(UnitType::cost).sum::<ResourcePile>();
|
||||
let player = &mut game.players[player_index];
|
||||
assert!(player.can_recruit(&units, city_position, leader_index, &replaced_units) && payment.can_afford(&cost));
|
||||
assert!(
|
||||
player.can_recruit(&units, city_position, leader_index, &replaced_units)
|
||||
&& cost.is_valid_payment(&payment)
|
||||
);
|
||||
player.loose_resources(payment);
|
||||
game.recruit(player_index, units, city_position, leader_index, replaced_units);
|
||||
game.recruit(
|
||||
player_index,
|
||||
units,
|
||||
city_position,
|
||||
leader_index,
|
||||
replaced_units,
|
||||
);
|
||||
}
|
||||
IncreaseHappiness {
|
||||
happiness_increases,
|
||||
|
|
@ -295,7 +311,13 @@ impl PlayingAction {
|
|||
let total_collect = collections.into_iter().map(|(_, collect)| collect).sum();
|
||||
game.players[player_index].loose_resources(total_collect);
|
||||
}
|
||||
Recruit { units, city_position, payment, leader_index, replaced_units: _ } => {
|
||||
Recruit {
|
||||
units,
|
||||
city_position,
|
||||
payment,
|
||||
leader_index,
|
||||
replaced_units: _,
|
||||
} => {
|
||||
game.players[player_index].loose_resources(payment);
|
||||
game.undo_recruit(player_index, &units, city_position, leader_index);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -217,6 +217,22 @@ impl ResourcePile {
|
|||
|| self.mood_tokens > 0 && other.mood_tokens > 0
|
||||
|| self.culture_tokens > 0 && other.culture_tokens > 0
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_valid_payment(&self, payment: &Self) -> bool {
|
||||
payment.can_afford(self) && self.resource_amount() == payment.resource_amount()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn resource_amount(&self) -> u32 {
|
||||
self.food
|
||||
+ self.wood
|
||||
+ self.ore
|
||||
+ self.ideas
|
||||
+ self.gold as u32
|
||||
+ self.mood_tokens
|
||||
+ self.culture_tokens
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for ResourcePile {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use std::{
|
||||
ops::{AddAssign, SubAssign}, fmt::Display,
|
||||
fmt::Display,
|
||||
ops::{AddAssign, SubAssign},
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
|
@ -19,6 +20,7 @@ pub struct Unit {
|
|||
}
|
||||
|
||||
impl Unit {
|
||||
#[must_use]
|
||||
pub fn new(player_index: usize, position: Position, unit_type: UnitType, id: u32) -> Self {
|
||||
Self {
|
||||
player_index,
|
||||
|
|
@ -42,6 +44,7 @@ pub enum UnitType {
|
|||
}
|
||||
|
||||
impl UnitType {
|
||||
#[must_use]
|
||||
pub fn cost(&self) -> ResourcePile {
|
||||
match self {
|
||||
Settler | Elephant => ResourcePile::food(2),
|
||||
|
|
@ -52,6 +55,7 @@ impl UnitType {
|
|||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_land_based(&self) -> bool {
|
||||
!matches!(self, Ship)
|
||||
}
|
||||
|
|
@ -155,14 +159,15 @@ impl Display for Units {
|
|||
unit_types.push(format!(
|
||||
"{} {}",
|
||||
self.settlers,
|
||||
if self.settlers == 1 { "settler" } else { "settlers" }
|
||||
if self.settlers == 1 {
|
||||
"settler"
|
||||
} else {
|
||||
"settlers"
|
||||
}
|
||||
));
|
||||
}
|
||||
if self.infantry > 0 {
|
||||
unit_types.push(format!(
|
||||
"{} infantry",
|
||||
self.infantry,
|
||||
));
|
||||
unit_types.push(format!("{} infantry", self.infantry,));
|
||||
}
|
||||
if self.ships > 0 {
|
||||
unit_types.push(format!(
|
||||
|
|
@ -172,22 +177,25 @@ impl Display for Units {
|
|||
));
|
||||
}
|
||||
if self.cavalry > 0 {
|
||||
unit_types.push(format!(
|
||||
"{} cavalry",
|
||||
self.cavalry,
|
||||
));
|
||||
unit_types.push(format!("{} cavalry", self.cavalry,));
|
||||
}
|
||||
if self.elephants > 0 {
|
||||
unit_types.push(format!(
|
||||
"{} {}",
|
||||
self.elephants,
|
||||
if self.elephants == 1 { "elephant" } else { "elephants" }
|
||||
if self.elephants == 1 {
|
||||
"elephant"
|
||||
} else {
|
||||
"elephants"
|
||||
}
|
||||
));
|
||||
}
|
||||
if self.leaders > 0 {
|
||||
unit_types.push(
|
||||
if self.leaders == 1 { String::from("a leader") } else { format!("{} leaders", self.leaders) }
|
||||
);
|
||||
unit_types.push(if self.leaders == 1 {
|
||||
String::from("a leader")
|
||||
} else {
|
||||
format!("{} leaders", self.leaders)
|
||||
});
|
||||
}
|
||||
write!(f, "{}", utils::format_list(&unit_types, "no units"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use server::{
|
|||
playing_actions::PlayingAction::*,
|
||||
position::Position,
|
||||
resource_pile::ResourcePile,
|
||||
unit::UnitType::*,
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
|
@ -151,6 +152,25 @@ fn basic_actions() {
|
|||
.expect("player should have a city at this position")
|
||||
.is_activated());
|
||||
assert_eq!(0, game.actions_left);
|
||||
let mut game = game_api::execute_action(game, Action::Playing(EndTurn), 0);
|
||||
let player = &mut game.players[0];
|
||||
player.gain_resources(ResourcePile::food(2));
|
||||
let recruit_action = Action::Playing(Recruit {
|
||||
units: vec![Settler],
|
||||
city_position,
|
||||
payment: ResourcePile::food(2),
|
||||
leader_index: None,
|
||||
replaced_units: Vec::new(),
|
||||
});
|
||||
let game = game_api::execute_action(game, recruit_action, 0);
|
||||
let player = &game.players[0];
|
||||
assert_eq!(1, player.units.len());
|
||||
assert_eq!(1, player.next_unit_id);
|
||||
assert_eq!(ResourcePile::ore(1), player.resources);
|
||||
assert!(player
|
||||
.get_city(city_position)
|
||||
.expect("player should have a city at this position")
|
||||
.is_activated());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue