Skip to content

Commit 33779ef

Browse files
authored
cleanup (#91)
* cleanup * log cleanup * log cleanup * log cleanup * log cleanup * log cleanup
1 parent 0a39858 commit 33779ef

7 files changed

+269
-147
lines changed

client/src/construct_ui.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use server::city_pieces::Building;
1010
use server::content::custom_actions::CustomAction;
1111
use server::game::Game;
1212
use server::map::{Map, Terrain};
13-
use server::playing_actions::{Construct, PlayingAction};
13+
use server::playing_actions::{Construct, PlayingAction, Recruit};
1414
use server::position::Position;
1515
use server::resource_pile::PaymentOptions;
1616
use server::unit::UnitType;
@@ -126,13 +126,13 @@ pub fn pay_construction_dialog(
126126
game.get_any_city(cp.city_position).unwrap(),
127127
),
128128
ConstructionProject::Units(r) => StateUpdate::execute_activation(
129-
Action::Playing(PlayingAction::Recruit {
129+
Action::Playing(PlayingAction::Recruit(Recruit {
130130
city_position: cp.city_position,
131131
units: r.amount.units.clone().to_vec(),
132132
payment: cp.payment.to_resource_pile(),
133133
replaced_units: r.replaced_units.clone(),
134134
leader_index: r.amount.leader_index,
135-
}),
135+
})),
136136
vec![],
137137
game.get_any_city(cp.city_position).unwrap(),
138138
),

client/src/influence_ui.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use macroquad::ui::Ui;
55
use server::action::Action;
66
use server::city_pieces::Building;
77
use server::game::{CulturalInfluenceResolution, Game};
8-
use server::playing_actions::PlayingAction;
8+
use server::playing_actions::{InfluenceCultureAttempt, PlayingAction};
99
use server::position::Position;
1010

1111
pub fn add_influence_button(
@@ -34,12 +34,12 @@ pub fn add_influence_button(
3434
format!("Attempt Influence {building_name} for {cost}"),
3535
) {
3636
return StateUpdate::Execute(Action::Playing(
37-
PlayingAction::InfluenceCultureAttempt {
37+
PlayingAction::InfluenceCultureAttempt(InfluenceCultureAttempt {
3838
starting_city_position: start_position,
3939
target_player_index: menu.city_owner_index,
4040
target_city_position: menu.city_position,
4141
city_piece: building.clone(),
42-
},
42+
}),
4343
));
4444
}
4545
}

server/src/game.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -618,13 +618,7 @@ impl Game {
618618

619619
fn undo_cultural_influence_resolution_action(&mut self, action: bool) {
620620
let cultural_influence_attempt_action = self.action_log[self.action_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");
621-
let PlayingAction::InfluenceCultureAttempt {
622-
starting_city_position: _,
623-
target_player_index,
624-
target_city_position,
625-
city_piece,
626-
} = cultural_influence_attempt_action
627-
else {
621+
let PlayingAction::InfluenceCultureAttempt(c) = cultural_influence_attempt_action else {
628622
panic!("any log item previous to a cultural influence resolution action log item should a cultural influence attempt action log item");
629623
};
630624
let roll =
@@ -633,6 +627,9 @@ impl Game {
633627
) / 2
634628
+ 1;
635629
let roll_boost_cost = 5 - roll as u32;
630+
let city_piece = c.city_piece;
631+
let target_player_index = c.target_player_index;
632+
let target_city_position = c.target_city_position;
636633
self.state = GameState::CulturalInfluenceResolution(CulturalInfluenceResolution {
637634
roll_boost_cost,
638635
target_player_index,

server/src/log.rs

+163-46
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55

66
use crate::action::PlayActionCard;
77
use crate::player::Player;
8-
use crate::playing_actions::Construct;
8+
use crate::playing_actions::{Construct, InfluenceCultureAttempt, Recruit};
99
use crate::status_phase::{ChangeGovernmentType, RazeSize1City};
1010
use crate::{
1111
action::{Action, CombatAction},
@@ -79,41 +79,170 @@ pub fn format_action_log_item(action: &Action, game: &Game) -> String {
7979
Action::Playing(action) => format_playing_action_log_item(action, game),
8080
Action::StatusPhase(action) => format_status_phase_action_log_item(action, game),
8181
Action::Movement(action) => format_movement_action_log_item(action, game),
82-
Action::CulturalInfluenceResolution(action) => format!("{} {}", game.players[game.active_player()].get_name(), match action {
83-
true => format!("paid {} culture tokens to increased the dice roll and proceed with the cultural influence", game.dice_roll_log.last().expect("there should have been at least one dice roll before a cultural influence resolution action") / 2 + 1),
84-
false => String::from("declined to increase the dice roll"),
85-
}),
82+
Action::CulturalInfluenceResolution(action) => {
83+
format_cultural_influence_resolution_log_item(game, *action)
84+
}
8685
Action::Combat(action) => format_combat_action_log_item(action, game),
87-
Action::PlaceSettler(position) => format!("{} placed a settler in the city at {position}", game.players[game.state.settler_placer().expect("the game should be in the place settler state")].get_name()),
86+
Action::PlaceSettler(position) => format_place_settler_log_item(game, *position),
8887
Action::Undo | Action::Redo => {
8988
panic!("undoing or redoing actions should not be written to the log")
9089
}
9190
}
9291
}
9392

93+
fn format_place_settler_log_item(game: &Game, position: Position) -> String {
94+
let player = game.players[game
95+
.state
96+
.settler_placer()
97+
.expect("the game should be in the place settler state")]
98+
.get_name();
99+
format!("{player} placed a settler in the city at {position}")
100+
}
101+
102+
fn format_cultural_influence_resolution_log_item(game: &Game, success: bool) -> String {
103+
let player = game.players[game.active_player()].get_name();
104+
let outcome = if success {
105+
let price = game.dice_roll_log.last()
106+
.expect("there should have been at least one dice roll before a cultural influence resolution action");
107+
format!("paid {} culture tokens to increase the dice roll and proceed with the cultural influence", price / 2 + 1)
108+
} else {
109+
String::from("declined to increase the dice roll")
110+
};
111+
format!("{player} {outcome}")
112+
}
113+
94114
fn format_playing_action_log_item(action: &PlayingAction, game: &Game) -> String {
95115
let player = &game.players[game.active_player()];
96116
let player_name = player.get_name();
97117
match action {
98-
PlayingAction::Advance { advance, payment } => format!("{player_name} paid {payment} to get the {advance} advance"),
99-
PlayingAction::FoundCity { settler } => format!("{player_name} founded a city at {}", player.get_unit(*settler).expect("The player should have the settler").position),
118+
PlayingAction::Advance { advance, payment } => {
119+
format!("{player_name} paid {payment} to get the {advance} advance")
120+
}
121+
PlayingAction::FoundCity { settler } => format!(
122+
"{player_name} founded a city at {}",
123+
player
124+
.get_unit(*settler)
125+
.expect("The player should have the settler")
126+
.position
127+
),
100128
PlayingAction::Construct(c) => format_construct_log_item(game, player, &player_name, c),
101-
PlayingAction::Collect { city_position, collections } => format_collect_log_item(player, &player_name, *city_position, collections),
102-
PlayingAction::Recruit { units, city_position, payment, leader_index, replaced_units } => format!("{player_name} paid {payment} to recruit {}{} in the city at {city_position}{}{}", units.iter().cloned().collect::<Units>(), leader_index.map_or(String::new(), |leader_index| format!(" {} {} as his leader", if player.available_leaders.len() > 1 { "choosing" } else { "getting" }, &player.available_leaders[leader_index].name)), if player.get_city(*city_position).expect("there should be a city at the given position").is_activated() { format!(" making it {:?}", player.get_city(*city_position).expect("there should be a city at the given position").mood_state.clone() - 1) } else { String::new() }, format_args!("{}{}", match replaced_units.len() { 0 => "", 1 => " and replaces the unit at ", _ => " and replaces units at " }, utils::format_list(&replaced_units.iter().map(|unit_id| player.get_unit(*unit_id).expect("the player should have the replaced units").position.to_string()).unique().collect::<Vec<String>>(), ""))),
129+
PlayingAction::Collect {
130+
city_position,
131+
collections,
132+
} => format_collect_log_item(player, &player_name, *city_position, collections),
133+
PlayingAction::Recruit(r) => format_recruit_log_item(player, &player_name, r),
103134
PlayingAction::MoveUnits => format!("{player_name} used a move units action"),
104-
PlayingAction::IncreaseHappiness { happiness_increases } => {
105-
let happiness_increases = happiness_increases.iter().filter_map(|(position, steps)| if *steps > 0 { Some(format!("the city at {position} by {steps} steps, making it {:?}", player.get_city(*position).expect("player should have a city at this position").mood_state.clone() + *steps)) } else { None }).collect::<Vec<String>>();
106-
format!("{player_name} increased happiness in {}", utils::format_list(&happiness_increases, "no city"))
107-
},
108-
PlayingAction::InfluenceCultureAttempt { starting_city_position, target_player_index, target_city_position, city_piece } => format!("{player_name} tried to influence culture the {city_piece:?} in the city at {target_city_position} by {}{}", if target_player_index == &game.active_player() { String::from("himself")} else { game.players[*target_player_index].get_name() }, if starting_city_position != target_city_position { format!(" with the city at {starting_city_position}")} else { String::new() }),
135+
PlayingAction::IncreaseHappiness {
136+
happiness_increases,
137+
} => format_happiness_increase(player, &player_name, happiness_increases),
138+
PlayingAction::InfluenceCultureAttempt(c) => {
139+
format_cultural_influence_attempt_log_item(game, &player_name, c)
140+
}
109141
PlayingAction::Custom(action) => action.format_log_item(game, &player_name),
110-
PlayingAction::EndTurn => format!("{player_name} ended his turn{}", match game.actions_left {
111-
0 => String::new(),
112-
actions_left => format!(" with {actions_left} actions left"),
113-
}),
142+
PlayingAction::EndTurn => format!(
143+
"{player_name} ended his turn{}",
144+
match game.actions_left {
145+
0 => String::new(),
146+
actions_left => format!(" with {actions_left} actions left"),
147+
}
148+
),
114149
}
115150
}
116151

152+
fn format_cultural_influence_attempt_log_item(
153+
game: &Game,
154+
player_name: &String,
155+
c: &InfluenceCultureAttempt,
156+
) -> String {
157+
let target_player_index = c.target_player_index;
158+
let target_city_position = c.target_city_position;
159+
let starting_city_position = c.starting_city_position;
160+
let city_piece = &c.city_piece;
161+
let player = if target_player_index == game.active_player() {
162+
String::from("himself")
163+
} else {
164+
game.players[target_player_index].get_name()
165+
};
166+
let city = if starting_city_position != target_city_position {
167+
format!(" with the city at {starting_city_position}")
168+
} else {
169+
String::new()
170+
};
171+
format!("{player_name} tried to influence culture the {city_piece:?} in the city at {target_city_position} by {player}{city}")
172+
}
173+
174+
fn format_happiness_increase(
175+
player: &Player,
176+
player_name: &String,
177+
happiness_increases: &[(Position, u32)],
178+
) -> String {
179+
let happiness_increases = happiness_increases
180+
.iter()
181+
.filter_map(|(position, steps)| {
182+
if *steps > 0 {
183+
Some(format!(
184+
"the city at {position} by {steps} steps, making it {:?}",
185+
player
186+
.get_city(*position)
187+
.expect("player should have a city at this position")
188+
.mood_state
189+
.clone()
190+
+ *steps
191+
))
192+
} else {
193+
None
194+
}
195+
})
196+
.collect::<Vec<String>>();
197+
format!(
198+
"{player_name} increased happiness in {}",
199+
utils::format_list(&happiness_increases, "no city")
200+
)
201+
}
202+
203+
fn format_recruit_log_item(player: &Player, player_name: &String, r: &Recruit) -> String {
204+
let leader_index = r.leader_index;
205+
let city_position = &r.city_position;
206+
let units = &r.units;
207+
let payment = &r.payment;
208+
let replaced_units = &r.replaced_units;
209+
let units_str = units.iter().cloned().collect::<Units>();
210+
let leader_str = leader_index.map_or(String::new(), |leader_index| {
211+
format!(
212+
" {} {} as his leader",
213+
if player.available_leaders.len() > 1 {
214+
"choosing"
215+
} else {
216+
"getting"
217+
},
218+
&player.available_leaders[leader_index].name
219+
)
220+
});
221+
let mood = format_mood_change(player, *city_position);
222+
let replace_str = match replaced_units.len() {
223+
0 => "",
224+
1 => " and replaces the unit at ",
225+
_ => " and replaces units at ",
226+
};
227+
let replace_pos = utils::format_list(
228+
&replaced_units
229+
.iter()
230+
.map(|unit_id| {
231+
player
232+
.get_unit(*unit_id)
233+
.expect("the player should have the replaced units")
234+
.position
235+
.to_string()
236+
})
237+
.unique()
238+
.collect::<Vec<String>>(),
239+
"",
240+
);
241+
format!(
242+
"{player_name} paid {payment} to recruit {units_str}{leader_str} in the city at {city_position}{mood}{replace_str}{replace_pos}"
243+
)
244+
}
245+
117246
fn format_collect_log_item(
118247
player: &Player,
119248
player_name: &String,
@@ -144,23 +273,7 @@ fn format_collect_log_item(
144273
} else {
145274
String::new()
146275
};
147-
let mood = if player
148-
.get_city(city_position)
149-
.expect("there should be a city at the given position")
150-
.is_activated()
151-
{
152-
format!(
153-
" making it {:?}",
154-
player
155-
.get_city(city_position)
156-
.expect("there should be a city at the given position")
157-
.mood_state
158-
.clone()
159-
- 1
160-
)
161-
} else {
162-
String::new()
163-
};
276+
let mood = format_mood_change(player, city_position);
164277
format!("{player_name} collects {res}{total} in the city at {city_position}{mood}")
165278
}
166279

@@ -195,29 +308,33 @@ fn format_construct_log_item(
195308
let payment = &c.payment;
196309
let city_position = &c.city_position;
197310

198-
let mood = if player
199-
.get_city(*city_position)
311+
let mood = format_mood_change(player, *city_position);
312+
let temple = if let Some(temple_bonus) = &c.temple_bonus {
313+
format!(" and chooses to get {temple_bonus}")
314+
} else {
315+
String::new()
316+
};
317+
format!("{player_name} paid {payment} to construct a {city_piece:?} in the city at {city_position}{port_pos}{mood}{temple}")
318+
}
319+
320+
fn format_mood_change(player: &Player, city_position: Position) -> String {
321+
if player
322+
.get_city(city_position)
200323
.expect("there should be a city at the given position")
201324
.is_activated()
202325
{
203326
format!(
204327
" making it {:?}",
205328
player
206-
.get_city(*city_position)
329+
.get_city(city_position)
207330
.expect("there should be a city at the given position")
208331
.mood_state
209332
.clone()
210333
- 1
211334
)
212335
} else {
213336
String::new()
214-
};
215-
let temple = if let Some(temple_bonus) = &c.temple_bonus {
216-
format!(" and chooses to get {temple_bonus}")
217-
} else {
218-
String::new()
219-
};
220-
format!("{player_name} paid {payment} to construct a {city_piece:?} in the city at {city_position}{port_pos}{mood}{temple}")
337+
}
221338
}
222339

223340
fn format_movement_action_log_item(action: &MovementAction, game: &Game) -> String {

0 commit comments

Comments
 (0)