-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappiness_ui.rs
112 lines (103 loc) · 3.2 KB
/
happiness_ui.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use server::action::Action;
use server::city::City;
use server::game::Game;
use server::player::Player;
use server::playing_actions::PlayingAction;
use server::position::Position;
use server::resource_pile::ResourcePile;
use crate::client_state::{ActiveDialog, ShownPlayer, StateUpdate};
use crate::dialog_ui::active_dialog_window;
#[derive(Clone)]
pub struct IncreaseHappiness {
pub steps: Vec<(Position, u32)>,
pub cost: ResourcePile,
}
impl IncreaseHappiness {
pub fn new(steps: Vec<(Position, u32)>, cost: ResourcePile) -> IncreaseHappiness {
IncreaseHappiness { steps, cost }
}
}
pub fn add_increase_happiness(
player: &Player,
city: &City,
pos: Position,
increase_happiness: &IncreaseHappiness,
) -> IncreaseHappiness {
let mut total_cost = increase_happiness.cost.clone();
let new_steps = increase_happiness
.steps
.iter()
.map(|(p, steps)| {
let old_steps = *steps;
if *p == pos {
if let Some(r) = increase_happiness_steps(player, city, &total_cost, old_steps) {
total_cost = r.1;
return (*p, r.0);
};
}
(*p, old_steps)
})
.collect();
IncreaseHappiness::new(new_steps, total_cost)
}
fn increase_happiness_steps(
player: &Player,
city: &City,
total_cost: &ResourcePile,
old_steps: u32,
) -> Option<(u32, ResourcePile)> {
if let Some(value) =
increase_happiness_new_steps(player, city, total_cost, old_steps, old_steps + 1)
{
return Some(value);
}
if let Some(value) = increase_happiness_new_steps(player, city, total_cost, old_steps, 0) {
return Some(value);
}
None
}
fn increase_happiness_new_steps(
player: &Player,
city: &City,
total_cost: &ResourcePile,
old_steps: u32,
new_steps: u32,
) -> Option<(u32, ResourcePile)> {
if let Some(new_cost) = city.increase_happiness_cost(new_steps) {
let mut new_total = total_cost.clone();
if old_steps > 0 {
new_total -= city
.increase_happiness_cost(old_steps)
.expect("invalid steps");
}
new_total += new_cost;
if player.resources.can_afford(&new_total) {
return Some((new_steps, new_total));
}
}
None
}
pub fn increase_happiness_menu(h: &IncreaseHappiness, player: &ShownPlayer) -> StateUpdate {
active_dialog_window(player, "Increase Happiness", |ui| {
ui.label(None, &format!("Cost: {:?}", h.cost));
if ui.button(None, "Cancel") {
return StateUpdate::Cancel;
}
if ui.button(None, "Confirm") {
return StateUpdate::Execute(Action::Playing(PlayingAction::IncreaseHappiness {
happiness_increases: h.steps.clone(),
}));
}
StateUpdate::None
})
}
pub fn start_increase_happiness(game: &Game, player: &ShownPlayer) -> StateUpdate {
StateUpdate::OpenDialog(ActiveDialog::IncreaseHappiness(IncreaseHappiness::new(
game.get_player(player.index)
.cities
.iter()
.map(|c| (c.position, 0))
.collect(),
ResourcePile::empty(),
)))
}