bug fixes

This commit is contained in:
AlphaCodingPilot 2023-08-06 15:22:45 +02:00
commit 03ea67bbb2
9 changed files with 114 additions and 65 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
target/
dist/
.vscode/
.history/

55
server/Cargo.lock generated
View file

@ -144,9 +144,12 @@ checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1"
[[package]]
name = "cc"
version = "1.0.79"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
checksum = "6c6b2562119bf28c3439f7f02db99faf0aa1a8cdfe5772a2ee155d32227239f0"
dependencies = [
"libc",
]
[[package]]
name = "cfg-if"
@ -174,9 +177,9 @@ dependencies = [
[[package]]
name = "errno"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a"
checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f"
dependencies = [
"errno-dragonfly",
"libc",
@ -308,9 +311,9 @@ dependencies = [
[[package]]
name = "itoa"
version = "1.0.8"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b02a5381cc465bd3041d84623d0fa3b66738b52b8e2fc3bab8ad63ab032f4a"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "js-sys"
@ -359,9 +362,9 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "num"
version = "0.4.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-bigint",
"num-complex",
@ -426,9 +429,9 @@ dependencies = [
[[package]]
name = "num-traits"
version = "0.2.15"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
dependencies = [
"autocfg",
]
@ -481,18 +484,18 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.63"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.29"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105"
checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965"
dependencies = [
"proc-macro2",
]
@ -543,15 +546,15 @@ dependencies = [
[[package]]
name = "ryu"
version = "1.0.14"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe232bdf6be8c8de797b22184ee71118d63780ea42ac85b61d1baa6d3b782ae9"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.167"
version = "1.0.181"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7daf513456463b42aa1d94cff7e0c24d682b429f020b9afa4f5ba5c40a22b237"
checksum = "6d3e73c93c3240c0bda063c239298e633114c69a888c3e37ca8bb33f343e9890"
dependencies = [
"serde_derive",
]
@ -569,9 +572,9 @@ dependencies = [
[[package]]
name = "serde_derive"
version = "1.0.167"
version = "1.0.181"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b69b106b68bc8054f0e974e70d19984040f8a5cf9215ca82626ea4853f82c4b9"
checksum = "be02f6cb0cd3a5ec20bbcfbcbd749f57daddb1a0882dc2e46a6c236c90b977ed"
dependencies = [
"proc-macro2",
"quote",
@ -580,9 +583,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.100"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f1e14e89be7aa4c4b78bdbdc9eb5bf8517829a600ae8eaa39a6e1d960b5185c"
checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c"
dependencies = [
"itoa",
"ryu",
@ -625,9 +628,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.23"
version = "2.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59fb7d6d8281a51045d62b8eb3a7d1ce347b76f312af50cd3dc0af39c87c1737"
checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567"
dependencies = [
"proc-macro2",
"quote",
@ -636,9 +639,9 @@ dependencies = [
[[package]]
name = "unicode-ident"
version = "1.0.10"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
[[package]]
name = "value-bag"

View file

@ -67,22 +67,16 @@ pub trait AbilityInitializerSetup: Sized {
})
}
fn add_collect_option(
self,
terrain: Terrain,
option: ResourcePile,
use_limit: Option<u32>,
) -> Self {
fn add_collect_option(self, terrain: Terrain, option: ResourcePile) -> Self {
let deinitializer_terrain = terrain.clone();
let deinitializer_option = option.clone();
let deinitializer_use_limit = use_limit;
self.add_one_time_ability_initializer(move |game, player_index| {
let player = &mut game.players[player_index];
player
.collect_options
.entry(terrain.clone())
.or_insert(Vec::new())
.push((option.clone(), use_limit));
.or_default()
.push(option.clone());
})
.add_ability_undo_deinitializer(move |game, player_index| {
let player = &mut game.players[player_index];
@ -91,15 +85,14 @@ pub trait AbilityInitializerSetup: Sized {
.get(&deinitializer_terrain)
.expect("player should have options for terrain type")
.iter()
.position(|(option, use_limit)| {
option == &deinitializer_option || use_limit == &deinitializer_use_limit
})
.position(|option| option == &deinitializer_option)
.expect("player should have previously added collect option");
player
.collect_options
.get_mut(&deinitializer_terrain)
.expect("player should have options for terrain type")
.remove(index);
//*Note that this will break if multiple effects add the same collect option
})
}
}

View file

@ -31,7 +31,7 @@ pub fn get_technologies() -> Vec<Advance> {
.build(),
//Maritime
Advance::builder("Fishing", "Your cities may Collect food from one Sea space")
.add_collect_option(Water, ResourcePile::food(1), Some(1))
.add_collect_option(Water, ResourcePile::food(1))
.with_advance_bonus(MoodToken)
.build(),
//Education

View file

@ -1,7 +1,7 @@
use crate::{resource_pile::ResourcePile, wonder::Wonder};
pub fn get_wonders() -> Vec<Wonder> {
vec![Wonder::builder("test", ResourcePile::new(3, 3, 3, 0, 0, 0, 4), vec![]).build()]
vec![Wonder::builder("test", ResourcePile::new(3, 3, 3, 0, -1, 0, 4), vec![]).build()]
}
pub fn get_wonder_by_name(name: &str) -> Option<Wonder> {

View file

@ -40,6 +40,7 @@ pub struct Game {
pub age: u32, // starts with 1
pub messages: Vec<String>,
pub dice_roll_outcomes: Vec<u8>,
pub dice_roll_log: Vec<u8>,
dropped_players: Vec<usize>,
pub wonders_left: Vec<Wonder>,
pub wonder_amount_left: usize,
@ -95,6 +96,7 @@ impl Game {
age: 1,
messages: vec![String::from("Game has started")],
dice_roll_outcomes,
dice_roll_log: Vec::new(),
dropped_players: Vec::new(),
wonders_left: wonders,
wonder_amount_left: wonder_amount,
@ -118,6 +120,7 @@ impl Game {
age: data.age,
messages: data.messages,
dice_roll_outcomes: data.dice_roll_outcomes,
dice_roll_log: data.dice_roll_log,
dropped_players: data.dropped_players,
wonders_left: data
.wonders_left
@ -158,6 +161,7 @@ impl Game {
age: self.age,
messages: self.messages,
dice_roll_outcomes: self.dice_roll_outcomes,
dice_roll_log: self.dice_roll_log,
dropped_players: self.dropped_players,
wonders_left: self
.wonders_left
@ -259,12 +263,11 @@ impl Game {
}
fn undo(&mut self, player_index: usize) {
let action = self.log[self.log_index]
.as_playing_action()
.expect("previous action should be a playing action");
let action =
serde_json::from_str::<PlayingAction>(action).expect("action should be deserializable");
action.undo(self, player_index);
match &self.log[self.log_index - 1] {
LogItem::PlayingAction(action) => serde_json::from_str::<PlayingAction>(action).expect("log item variant of type playing action should contain a serialized playing action").undo(self, player_index),
LogItem::StatusPhaseAction(_) => panic!("status phase actions can't be undone"),
LogItem::CulturalInfluenceResolutionAction(action) => self.undo_cultural_influence_resolution_action(serde_json::from_str::<bool>(action).expect("cultural influence resolution log item should contain a serialized boolean representing the confirmation action")),
}
self.log_index -= 1;
}
@ -308,6 +311,39 @@ impl Game {
)
}
fn undo_cultural_influence_resolution_action(&mut self, action: bool) {
let cultural_influence_attempt_action = self.log[self.log_index - 2].as_playing_action().expect("any log item previous to a cultural influence resolution action log item should a cultural influence attempt action log item");
let cultural_influence_attempt_action = serde_json::from_str::<PlayingAction>(cultural_influence_attempt_action).expect("any log item previous to a cultural influence resolution action log item should a cultural influence attempt action log item");
let PlayingAction::InfluenceCultureAttempt {
starting_city_position: _,
target_player_index,
target_city_position,
city_piece,
} = cultural_influence_attempt_action
else {
panic!("any log item previous to a cultural influence resolution action log item should a cultural influence attempt action log item");
};
let roll = self
.dice_roll_log
.last()
.expect("there should be a dice roll before a cultural influence resolution action");
self.state = CulturalInfluenceResolution {
roll_boost_cost: 5 - *roll as u32,
target_player_index,
target_city_position: target_city_position.clone(),
city_piece: city_piece.clone(),
};
if !action {
return;
}
self.undo_influence_culture(
self.current_player_index,
target_player_index,
&target_city_position,
&city_piece,
);
}
pub fn next_player(&mut self) {
self.current_player_index += 1;
self.current_player_index %= self.players.len();
@ -365,10 +401,12 @@ impl Game {
pub fn get_next_dice_roll(&mut self) -> u8 {
self.lock_undo();
self.dice_roll_outcomes.pop().unwrap_or_else(|| {
let dice_roll = self.dice_roll_outcomes.pop().unwrap_or_else(|| {
println!("ran out of predetermined dice roll outcomes, unseeded rng is no being used");
rand::thread_rng().gen_range(1..=12)
})
});
self.dice_roll_log.push(dice_roll);
dice_roll
}
fn add_message(&mut self, message: &str) {
@ -608,6 +646,24 @@ impl Game {
self.players[influencer_index].available_buildings -= building;
}
pub fn undo_influence_culture(
&mut self,
influencer_index: usize,
influenced_player_index: usize,
city_position: &Position,
building: &Building,
) {
self.players[influenced_player_index]
.get_city_mut(city_position)
.expect("influenced should have influenced city")
.city_pieces
.set_building(building, influenced_player_index);
self.players[influencer_index].influenced_buildings -= 1;
self.successful_cultural_influence = false;
self.players[influenced_player_index].available_buildings -= building;
self.players[influencer_index].available_buildings += building;
}
pub fn draw_new_cards(&mut self) {
//every player draws 1 action card and 1 objective card
todo!()
@ -631,6 +687,7 @@ pub struct GameData {
age: u32,
messages: Vec<String>,
dice_roll_outcomes: Vec<u8>,
dice_roll_log: Vec<u8>,
dropped_players: Vec<usize>,
wonders_left: Vec<String>,
wonder_amount_left: usize,
@ -735,6 +792,7 @@ pub mod tests {
age: 1,
messages: vec![String::from("Game has started")],
dice_roll_outcomes: vec![12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
dice_roll_log: Vec::new(),
dropped_players: Vec::new(),
wonders_left: Vec::new(),
wonder_amount_left: 0,

View file

@ -119,7 +119,6 @@ pub fn civilizations(game: Game) -> Vec<String> {
}
pub fn strip_secret(mut game: Game, player_index: Option<usize>) -> Game {
let player_index = player_index;
game.dice_roll_outcomes = Vec::new();
game.wonders_left = Vec::new();
for (i, player) in game.players.iter_mut().enumerate() {

View file

@ -64,7 +64,7 @@ pub struct Player {
pub custom_actions: HashSet<CustomActionType>,
pub wonder_cards: Vec<Wonder>,
pub available_buildings: AvailableBuildings,
pub collect_options: HashMap<Terrain, Vec<(ResourcePile, /* use limit */ Option<u32>)>>,
pub collect_options: HashMap<Terrain, Vec<ResourcePile>>,
}
impl Player {
@ -227,9 +227,9 @@ impl Player {
wonder_cards: Vec::new(),
available_buildings: AvailableBuildings::new(5, 5, 5, 5, 5, 5, 5),
collect_options: HashMap::from([
(Mountain, vec![(ResourcePile::ore(1), None)]),
(Fertile, vec![(ResourcePile::food(1), None)]),
(Forest, vec![(ResourcePile::wood(1), None)]),
(Mountain, vec![ResourcePile::ore(1)]),
(Fertile, vec![ResourcePile::food(1)]),
(Forest, vec![ResourcePile::wood(1)]),
]),
}
}
@ -475,8 +475,6 @@ impl Player {
}
}
type CollectOption = (Terrain, Vec<(ResourcePile, Option<u32>)>);
#[derive(Serialize, Deserialize)]
pub struct PlayerData {
name: Option<String>,
@ -500,5 +498,5 @@ pub struct PlayerData {
event_victory_points: f32,
wonder_cards: Vec<String>,
available_buildings: AvailableBuildings,
collect_options: Vec<CollectOption>,
collect_options: Vec<(Terrain, Vec<ResourcePile>)>,
}

View file

@ -1,5 +1,3 @@
use std::collections::HashSet;
use serde::{Deserialize, Serialize};
use crate::{
@ -44,9 +42,11 @@ pub enum PlayingAction {
impl PlayingAction {
pub fn execute(self, game: &mut Game, player_index: usize) {
let free_action = self.action_type().free;
if !free_action && game.actions_left == 0 {
panic!("Illegal action");
if !self.action_type().free {
if game.actions_left == 0 {
panic!("Illegal action");
}
game.actions_left -= 1;
}
match self {
Advance { advance, payment } => {
@ -98,7 +98,7 @@ impl PlayingAction {
panic!("Illegal action");
}
let mut total_collect = ResourcePile::empty();
for (terrain, collect) in collections.into_iter() {
for (_terrain, collect) in collections.into_iter() {
total_collect += collect;
}
game.players[player_index].gain_resources(total_collect);
@ -206,9 +206,6 @@ impl PlayingAction {
}
EndTurn => game.next_turn(),
}
if !free_action {
game.actions_left -= 1;
}
}
pub fn action_type(&self) -> ActionType {