test for conquer city
This commit is contained in:
parent
91c922e0e3
commit
5feebf0077
6 changed files with 69 additions and 8 deletions
|
|
@ -109,7 +109,7 @@ impl City {
|
|||
self.city_pieces.wonders.push(wonder);
|
||||
}
|
||||
|
||||
pub fn conquer(&mut self, new_player: &mut Player, old_player: &mut Player) {
|
||||
pub fn conquer(mut self, new_player: &mut Player, old_player: &mut Player) {
|
||||
let new_player_name = new_player.name();
|
||||
self.player = new_player_name.clone();
|
||||
self.mood_state = Angry;
|
||||
|
|
@ -134,6 +134,7 @@ impl City {
|
|||
self.city_pieces.buildings(Some(&new_player.name())).len() as u32;
|
||||
new_player.influenced_buildings -= previously_influenced_building;
|
||||
self.city_pieces.change_player(new_player_name);
|
||||
new_player.cities.push(self)
|
||||
}
|
||||
|
||||
pub fn raze(self, player: &mut Player, game: &mut Game) {
|
||||
|
|
@ -376,7 +377,9 @@ impl CityPieces {
|
|||
|
||||
fn change_player(&mut self, new_player: String) {
|
||||
for b in self.buildings(None) {
|
||||
self.set_building(&b, new_player.clone());
|
||||
if (!matches!(b, Building::Obelisk)) {
|
||||
self.set_building(&b, new_player.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -415,7 +418,7 @@ pub struct CityPiecesData {
|
|||
wonders: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
pub enum MoodState {
|
||||
Happy,
|
||||
Neutral,
|
||||
|
|
|
|||
39
server/src/city_tests.rs
Normal file
39
server/src/city_tests.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use crate::city::{Building, City, MoodState};
|
||||
use crate::civilization::Civilization;
|
||||
use crate::hexagon::Position;
|
||||
use crate::player::Player;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
use crate::wonder::Wonder;
|
||||
|
||||
#[test]
|
||||
fn conquer_test() {
|
||||
let mut old = Player::new(Civilization::new("old civ", vec![], vec![]));
|
||||
old.set_name(String::from("old"));
|
||||
let mut new = Player::new(Civilization::new("new civ", vec![], vec![]));
|
||||
new.set_name(String::from("new"));
|
||||
|
||||
let wonder = Wonder::builder("wonder", ResourcePile::default(), vec![]).build();
|
||||
|
||||
let position = Position::new(0, 0, 0);
|
||||
old.cities.push(City::new(old.name(), position.clone()));
|
||||
old.with_city(&position, |p, c| {
|
||||
c.build_wonder(wonder, p);
|
||||
c.increase_size(&Building::Academy, p);
|
||||
c.increase_size(&Building::Obelisk, p);
|
||||
});
|
||||
|
||||
assert_eq!(6.0, old.victory_points());
|
||||
|
||||
old.conquer_city(&position, &mut new);
|
||||
|
||||
let c = new.get_city(&position).unwrap();
|
||||
assert_eq!(String::from("new"), c.player);
|
||||
assert_eq!(MoodState::Angry, c.mood_state);
|
||||
|
||||
assert_eq!(3.0, old.victory_points());
|
||||
assert_eq!(3.0, new.victory_points());
|
||||
assert_eq!(0, old.wonders.len());
|
||||
assert_eq!(1, new.wonders.len());
|
||||
assert_eq!(1, old.influenced_buildings);
|
||||
assert_eq!(0, new.influenced_buildings);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||
pub struct Position {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
mod advance;
|
||||
mod army;
|
||||
mod city;
|
||||
#[cfg(test)]
|
||||
mod city_tests;
|
||||
mod civilization;
|
||||
mod content;
|
||||
mod events;
|
||||
|
|
|
|||
|
|
@ -314,6 +314,23 @@ impl Player {
|
|||
self.events = Some(events);
|
||||
}
|
||||
|
||||
pub fn conquer_city(&mut self, position: &Position, new_player: &mut Player) {
|
||||
self.take_city(&position)
|
||||
.expect("player should own city")
|
||||
.conquer(new_player, self);
|
||||
}
|
||||
|
||||
pub fn with_city<F>(&mut self, position: &Position, action: F)
|
||||
where
|
||||
F: FnOnce(&mut Player, &mut City),
|
||||
{
|
||||
let mut city = self
|
||||
.take_city(position)
|
||||
.expect("player should have this city");
|
||||
action(self, &mut city);
|
||||
self.cities.push(city);
|
||||
}
|
||||
|
||||
pub fn game_event_tokens(&self) -> u8 {
|
||||
self.game_event_tokens
|
||||
}
|
||||
|
|
@ -392,7 +409,7 @@ impl Player {
|
|||
Some(&mut self.cities[position])
|
||||
}
|
||||
|
||||
pub fn take_city(&mut self, position: &Position) -> Option<City> {
|
||||
fn take_city(&mut self, position: &Position) -> Option<City> {
|
||||
Some(
|
||||
self.cities.remove(
|
||||
self.cities
|
||||
|
|
|
|||
|
|
@ -82,9 +82,9 @@ impl PlayingAction {
|
|||
player.gain_resources(building_bonus);
|
||||
}
|
||||
player.loose_resources(payment);
|
||||
let mut city = player.take_city(&city.position).expect("city should exist");
|
||||
city.increase_size(&building, player);
|
||||
player.cities.push(city);
|
||||
player.with_city(&city.position, |p, c| {
|
||||
c.increase_size(&building, p);
|
||||
});
|
||||
}
|
||||
IncreaseHappiness { cities } => {
|
||||
for (city, steps) in cities {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue