1
1
use crate :: action_buttons:: { base_or_custom_action, base_or_custom_available} ;
2
2
use crate :: client_state:: { ActiveDialog , StateUpdate } ;
3
- use crate :: dialog_ui:: {
4
- cancel_button, ok_button, BaseOrCustomAction , BaseOrCustomDialog , OkTooltip ,
5
- } ;
3
+ use crate :: dialog_ui:: { BaseOrCustomAction , BaseOrCustomDialog } ;
4
+ use crate :: payment_ui:: { new_payment, payment_dialog, Payment } ;
6
5
use crate :: render_context:: RenderContext ;
7
- use crate :: resource_ui:: show_resource_pile;
8
6
use server:: action:: Action ;
9
7
use server:: city:: City ;
10
8
use server:: content:: custom_actions:: { CustomAction , CustomActionType } ;
11
9
use server:: player:: Player ;
12
10
use server:: playing_actions:: { IncreaseHappiness , PlayingAction , PlayingActionType } ;
13
11
use server:: position:: Position ;
14
- use server:: resource:: ResourceType ;
15
- use server:: resource_pile:: ResourcePile ;
16
12
17
13
#[ derive( Clone ) ]
18
14
pub struct IncreaseHappinessConfig {
19
15
pub steps : Vec < ( Position , u32 ) > ,
20
- pub cost : ResourcePile ,
16
+ pub payment : Payment ,
21
17
pub custom : BaseOrCustomDialog ,
22
18
}
23
19
@@ -26,10 +22,23 @@ impl IncreaseHappinessConfig {
26
22
let steps = p. cities . iter ( ) . map ( |c| ( c. position , 0 ) ) . collect ( ) ;
27
23
IncreaseHappinessConfig {
28
24
steps,
29
- cost : ResourcePile :: empty ( ) ,
25
+ payment : Self :: happiness_payment ( p , & [ ( p . cities [ 0 ] . position , 0 ) ] ) ,
30
26
custom,
31
27
}
32
28
}
29
+
30
+ fn happiness_payment ( p : & Player , new_steps : & [ ( Position , u32 ) ] ) -> Payment {
31
+ let payment = new_steps
32
+ . iter ( )
33
+ . map ( |( pos, steps) | {
34
+ let city = p. get_city ( * pos) . unwrap ( ) ;
35
+ p. increase_happiness_cost ( city, * steps) . unwrap ( )
36
+ } )
37
+ . reduce ( |a, b| a + b)
38
+ . unwrap ( ) ;
39
+
40
+ new_payment ( & payment, & p. resources , "Increase happiness" , false )
41
+ }
33
42
}
34
43
35
44
pub fn can_play_increase_happiness ( rc : & RenderContext ) -> bool {
@@ -65,95 +74,80 @@ pub fn increase_happiness_click(
65
74
) -> StateUpdate {
66
75
if let Some ( city) = rc. shown_player . get_city ( pos) {
67
76
StateUpdate :: OpenDialog ( ActiveDialog :: IncreaseHappiness ( add_increase_happiness (
68
- city, h,
77
+ rc,
78
+ city,
79
+ h. clone ( ) ,
69
80
) ) )
70
81
} else {
71
82
StateUpdate :: None
72
83
}
73
84
}
74
85
75
86
pub fn add_increase_happiness (
87
+ rc : & RenderContext ,
76
88
city : & City ,
77
- increase_happiness : & IncreaseHappinessConfig ,
89
+ mut increase_happiness : IncreaseHappinessConfig ,
78
90
) -> IncreaseHappinessConfig {
79
- let mut total_cost = increase_happiness. cost . clone ( ) ;
80
- let new_steps = increase_happiness
91
+ let new_steps: Vec < ( Position , u32 ) > = increase_happiness
81
92
. steps
82
93
. iter ( )
83
94
. map ( |( p, steps) | {
84
95
let old_steps = * steps;
85
96
if * p == city. position {
86
- if let Some ( r) = increase_happiness_steps ( city, & total_cost, old_steps) {
87
- total_cost = r. 1 ;
88
- return ( * p, r. 0 ) ;
97
+ if let Some ( r) = increase_happiness_steps ( rc, city, old_steps) {
98
+ return ( * p, r) ;
89
99
} ;
90
100
}
91
101
( * p, old_steps)
92
102
} )
93
103
. collect ( ) ;
94
104
95
- IncreaseHappinessConfig {
96
- steps : new_steps,
97
- cost : total_cost,
98
- custom : increase_happiness. custom . clone ( ) ,
99
- }
105
+ increase_happiness. payment =
106
+ IncreaseHappinessConfig :: happiness_payment ( rc. shown_player , & new_steps) ;
107
+ increase_happiness. steps = new_steps;
108
+ increase_happiness
100
109
}
101
110
102
- fn increase_happiness_steps (
103
- city : & City ,
104
- total_cost : & ResourcePile ,
105
- old_steps : u32 ,
106
- ) -> Option < ( u32 , ResourcePile ) > {
107
- if let Some ( value) = increase_happiness_new_steps ( city, total_cost, old_steps, old_steps + 1 ) {
111
+ fn increase_happiness_steps ( rc : & RenderContext , city : & City , old_steps : u32 ) -> Option < u32 > {
112
+ if let Some ( value) = increase_happiness_new_steps ( rc, city, old_steps + 1 ) {
108
113
return Some ( value) ;
109
114
}
110
- if let Some ( value) = increase_happiness_new_steps ( city , total_cost , old_steps , 0 ) {
115
+ if let Some ( value) = increase_happiness_new_steps ( rc , city , 0 ) {
111
116
return Some ( value) ;
112
117
}
113
118
None
114
119
}
115
120
116
- fn increase_happiness_new_steps (
117
- city : & City ,
118
- total_cost : & ResourcePile ,
119
- old_steps : u32 ,
120
- new_steps : u32 ,
121
- ) -> Option < ( u32 , ResourcePile ) > {
122
- if let Some ( new_cost) = city. increase_happiness_cost ( new_steps) {
123
- let mut new_total = total_cost. clone ( ) ;
124
- if old_steps > 0 {
125
- new_total -= city
126
- . increase_happiness_cost ( old_steps)
127
- . expect ( "invalid steps" ) ;
128
- }
129
- new_total += new_cost;
130
- return Some ( ( new_steps, new_total) ) ;
131
- }
132
- None
121
+ fn increase_happiness_new_steps ( rc : & RenderContext , city : & City , new_steps : u32 ) -> Option < u32 > {
122
+ rc. shown_player
123
+ . increase_happiness_cost ( city, new_steps)
124
+ . map ( |_| new_steps)
133
125
}
134
126
135
127
pub fn increase_happiness_menu ( rc : & RenderContext , h : & IncreaseHappinessConfig ) -> StateUpdate {
136
- show_resource_pile ( rc, & h. cost , & [ ResourceType :: MoodTokens ] ) ;
137
-
138
- let tooltip = if rc. shown_player . resources . can_afford ( & h. cost ) {
139
- OkTooltip :: Valid ( "Increase happiness" . to_string ( ) )
140
- } else {
141
- OkTooltip :: Invalid ( "Not enough resources" . to_string ( ) )
142
- } ;
143
- if ok_button ( rc, tooltip) {
144
- let i = IncreaseHappiness {
145
- happiness_increases : h. steps . clone ( ) ,
146
- } ;
147
- let action = match & h. custom . custom {
148
- BaseOrCustomAction :: Base => PlayingAction :: IncreaseHappiness ( i) ,
149
- BaseOrCustomAction :: Custom { .. } => {
150
- PlayingAction :: Custom ( CustomAction :: VotingIncreaseHappiness ( i) )
151
- }
152
- } ;
153
- return StateUpdate :: Execute ( Action :: Playing ( action) ) ;
154
- }
155
- if cancel_button ( rc) {
156
- return StateUpdate :: Cancel ;
157
- }
158
- StateUpdate :: None
128
+ payment_dialog (
129
+ rc,
130
+ & h. payment ,
131
+ |payment| {
132
+ ActiveDialog :: IncreaseHappiness ( IncreaseHappinessConfig {
133
+ steps : h. steps . clone ( ) ,
134
+ payment,
135
+ custom : h. custom . clone ( ) ,
136
+ } )
137
+ } ,
138
+ true ,
139
+ |payment| {
140
+ let i = IncreaseHappiness {
141
+ happiness_increases : h. steps . clone ( ) ,
142
+ payment,
143
+ } ;
144
+ let action = match & h. custom . custom {
145
+ BaseOrCustomAction :: Base => PlayingAction :: IncreaseHappiness ( i) ,
146
+ BaseOrCustomAction :: Custom { .. } => {
147
+ PlayingAction :: Custom ( CustomAction :: VotingIncreaseHappiness ( i) )
148
+ }
149
+ } ;
150
+ StateUpdate :: execute ( Action :: Playing ( action) )
151
+ } ,
152
+ )
159
153
}
0 commit comments