line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package BalanceOfPower::Role::Broker; |
2
|
|
|
|
|
|
|
$BalanceOfPower::Role::Broker::VERSION = '0.400105'; |
3
|
13
|
|
|
13
|
|
4173
|
use strict; |
|
13
|
|
|
|
|
19
|
|
|
13
|
|
|
|
|
290
|
|
4
|
13
|
|
|
13
|
|
93
|
use v5.10; |
|
13
|
|
|
|
|
29
|
|
5
|
13
|
|
|
13
|
|
39
|
use Moo::Role; |
|
13
|
|
|
|
|
16
|
|
|
13
|
|
|
|
|
67
|
|
6
|
13
|
|
|
13
|
|
2583
|
use BalanceOfPower::Constants ':all'; |
|
13
|
|
|
|
|
20
|
|
|
13
|
|
|
|
|
5080
|
|
7
|
13
|
|
|
13
|
|
63
|
use BalanceOfPower::Utils qw( prev_turn ); |
|
13
|
|
|
|
|
18
|
|
|
13
|
|
|
|
|
10773
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
requires 'get_player'; |
10
|
|
|
|
|
|
|
requires 'get_nation'; |
11
|
|
|
|
|
|
|
requires 'get_statistics_value'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub manage_stock |
14
|
|
|
|
|
|
|
{ |
15
|
2
|
|
|
2
|
0
|
4
|
my $self = shift; |
16
|
2
|
|
|
|
|
3
|
my $command = shift; |
17
|
2
|
|
|
|
|
3
|
my $player = shift; |
18
|
2
|
|
|
|
|
3
|
my $nation = shift; |
19
|
2
|
|
|
|
|
3
|
my $q = shift; |
20
|
2
|
100
|
|
|
|
7
|
if($command eq 'buy') |
|
|
50
|
|
|
|
|
|
21
|
|
|
|
|
|
|
{ |
22
|
1
|
|
|
|
|
3
|
return $self->buy_stock($player, $nation, $q); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
elsif($command eq 'sell') |
25
|
|
|
|
|
|
|
{ |
26
|
1
|
|
|
|
|
3
|
return $self->sell_stock($player, $nation, $q); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub buy_stock |
31
|
|
|
|
|
|
|
{ |
32
|
3
|
|
|
3
|
0
|
4
|
my $self = shift; |
33
|
3
|
|
|
|
|
4
|
my $player = shift; |
34
|
3
|
|
|
|
|
3
|
my $nation = shift; |
35
|
3
|
|
|
|
|
5
|
my $q = shift; |
36
|
3
|
|
100
|
|
|
12
|
my $dry_run = shift || 0; |
37
|
3
|
|
|
|
|
7
|
my $player_obj = $self->get_player($player); |
38
|
3
|
|
|
|
|
7
|
my $nation_obj = $self->get_nation($nation); |
39
|
3
|
50
|
|
|
|
12
|
if($nation_obj->available_stocks < $q) |
40
|
|
|
|
|
|
|
{ |
41
|
0
|
0
|
|
|
|
0
|
$player_obj->register_event("STOCKS IN QUANTITY $q FOR $nation NOT AVAILABLE") |
42
|
|
|
|
|
|
|
if(! $dry_run); |
43
|
0
|
|
|
|
|
0
|
return { status => -11 }; |
44
|
|
|
|
|
|
|
} |
45
|
3
|
50
|
|
|
|
6
|
if($q > MAX_BUY_STOCK) |
46
|
|
|
|
|
|
|
{ |
47
|
0
|
|
|
|
|
0
|
return { status => -15 }; |
48
|
|
|
|
|
|
|
} |
49
|
3
|
100
|
|
|
|
9
|
if($self->at_civil_war($nation)) |
50
|
|
|
|
|
|
|
{ |
51
|
1
|
50
|
|
|
|
8
|
$player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR") |
52
|
|
|
|
|
|
|
if(! $dry_run); |
53
|
1
|
|
|
|
|
3
|
return { status => -14 }; |
54
|
|
|
|
|
|
|
} |
55
|
2
|
100
|
|
|
|
10
|
return { status => 1, |
56
|
|
|
|
|
|
|
command => "buy $q $nation" } if $dry_run; |
57
|
1
|
|
|
|
|
4
|
my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d'); |
58
|
1
|
|
|
|
|
1
|
my $global_cost = $unit_cost * $q; |
59
|
1
|
|
|
|
|
3
|
my $influence = $q * STOCK_INFLUENCE_FACTOR; |
60
|
1
|
50
|
|
|
|
6
|
if($player_obj->money < $global_cost) |
61
|
|
|
|
|
|
|
{ |
62
|
0
|
0
|
|
|
|
0
|
$player_obj->register_event("NOT ENOUGH MONEY TO BUY STOCKS OF $nation FOR $global_cost") |
63
|
|
|
|
|
|
|
if(! $dry_run); |
64
|
0
|
|
|
|
|
0
|
return { status => -12 }; |
65
|
|
|
|
|
|
|
} |
66
|
1
|
|
|
|
|
5
|
$player_obj->add_money(-1 * $global_cost); |
67
|
1
|
|
|
|
|
4
|
$player_obj->add_stocks($q, $nation); |
68
|
1
|
|
|
|
|
4
|
$player_obj->add_influence($influence, $nation); |
69
|
1
|
|
|
|
|
5
|
$nation_obj->get_stocks($q); |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
6
|
$player_obj->register_event("BOUGHT $q STOCKS OF $nation. COST: $global_cost ($unit_cost)"); |
72
|
1
|
|
|
|
|
4
|
return { status => 1 }; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
sub sell_stock |
75
|
|
|
|
|
|
|
{ |
76
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
77
|
2
|
|
|
|
|
2
|
my $player = shift; |
78
|
2
|
|
|
|
|
2
|
my $nation = shift; |
79
|
2
|
|
|
|
|
3
|
my $q = shift; |
80
|
2
|
|
100
|
|
|
7
|
my $dry_run = shift || 0; |
81
|
2
|
|
|
|
|
5
|
my $player_obj = $self->get_player($player); |
82
|
2
|
|
|
|
|
5
|
my $nation_obj = $self->get_nation($nation); |
83
|
2
|
50
|
|
|
|
6
|
if($player_obj->stocks($nation) < $q) |
84
|
|
|
|
|
|
|
{ |
85
|
0
|
0
|
|
|
|
0
|
$player_obj->register_event("THERE AREN'T $q STOCKS OF $nation TO SELL") |
86
|
|
|
|
|
|
|
if(! $dry_run); |
87
|
0
|
|
|
|
|
0
|
return { status => -13 }; |
88
|
|
|
|
|
|
|
} |
89
|
2
|
50
|
|
|
|
8
|
if($self->at_civil_war($nation)) |
90
|
|
|
|
|
|
|
{ |
91
|
0
|
0
|
|
|
|
0
|
$player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR") |
92
|
|
|
|
|
|
|
if(! $dry_run); |
93
|
0
|
|
|
|
|
0
|
return { status => -14 }; |
94
|
|
|
|
|
|
|
} |
95
|
2
|
100
|
|
|
|
9
|
return { status => 1, |
96
|
|
|
|
|
|
|
command => "sell $q $nation" } if $dry_run; |
97
|
1
|
|
|
|
|
6
|
my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d'); |
98
|
1
|
|
|
|
|
2
|
my $global_cost = $unit_cost * $q; |
99
|
1
|
|
|
|
|
3
|
$player_obj->add_money($global_cost); |
100
|
1
|
|
|
|
|
4
|
$player_obj->add_stocks(-1 * $q, $nation); |
101
|
1
|
|
|
|
|
5
|
$nation_obj->get_stocks(-1 * $q); |
102
|
1
|
|
|
|
|
7
|
$player_obj->register_event("SOLD $q STOCKS OF $nation. COST: $global_cost ($unit_cost)"); |
103
|
1
|
|
|
|
|
3
|
return { status => 1 }; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
sub issue_war_bonds |
106
|
|
|
|
|
|
|
{ |
107
|
22
|
|
|
22
|
0
|
28
|
my $self = shift; |
108
|
22
|
|
|
|
|
24
|
my $n = shift; |
109
|
22
|
|
|
|
|
21
|
for(@{$self->players}) |
|
22
|
|
|
|
|
65
|
|
110
|
|
|
|
|
|
|
{ |
111
|
4
|
|
|
|
|
9
|
$_->issue_war_bonds($n); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
sub discard_war_bonds |
115
|
|
|
|
|
|
|
{ |
116
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
117
|
2
|
|
|
|
|
2
|
my $nation = shift; |
118
|
2
|
|
|
|
|
2
|
for(@{$self->players}) |
|
2
|
|
|
|
|
5
|
|
119
|
|
|
|
|
|
|
{ |
120
|
0
|
|
|
|
|
0
|
$_->discard_war_bonds($nation); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
sub empty_stocks |
124
|
|
|
|
|
|
|
{ |
125
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
126
|
2
|
|
|
|
|
3
|
my $nation = shift; |
127
|
2
|
|
|
|
|
4
|
for(@{$self->players}) |
|
2
|
|
|
|
|
8
|
|
128
|
|
|
|
|
|
|
{ |
129
|
1
|
|
|
|
|
4
|
$_->empty_stocks($nation); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
sub cash_war_bonds |
133
|
|
|
|
|
|
|
{ |
134
|
2
|
|
|
2
|
0
|
4
|
my $self = shift; |
135
|
2
|
|
|
|
|
3
|
my $nation = shift; |
136
|
2
|
|
|
|
|
3
|
for(@{$self->players}) |
|
2
|
|
|
|
|
9
|
|
137
|
|
|
|
|
|
|
{ |
138
|
0
|
|
|
|
|
0
|
$_->cash_war_bonds($nation); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
sub execute_stock_orders |
142
|
|
|
|
|
|
|
{ |
143
|
45
|
|
|
45
|
0
|
49
|
my $self = shift; |
144
|
45
|
|
50
|
|
|
180
|
my $dry_run = shift || 0; |
145
|
45
|
|
|
|
|
51
|
my %orders; |
146
|
|
|
|
|
|
|
my @acting_players; |
147
|
45
|
|
|
|
|
51
|
foreach my $player (@{$self->players}) |
|
45
|
|
|
|
|
143
|
|
148
|
|
|
|
|
|
|
{ |
149
|
10
|
|
|
|
|
13
|
my @p_orders = @{$player->stock_orders}; |
|
10
|
|
|
|
|
28
|
|
150
|
10
|
100
|
|
|
|
28
|
if(@p_orders) |
151
|
|
|
|
|
|
|
{ |
152
|
2
|
50
|
|
|
|
4
|
say "Player " . $player->name . " has stock orders" if($dry_run); |
153
|
2
|
|
|
|
|
6
|
$orders{$player->name} = \@p_orders; |
154
|
2
|
|
|
|
|
5
|
push @acting_players, $player->name; |
155
|
|
|
|
|
|
|
} |
156
|
10
|
|
|
|
|
33
|
$player->empty_stock_orders(); |
157
|
|
|
|
|
|
|
} |
158
|
45
|
50
|
|
|
|
107
|
say "===" if($dry_run); |
159
|
45
|
|
|
|
|
151
|
while(@acting_players) |
160
|
|
|
|
|
|
|
{ |
161
|
2
|
|
|
|
|
38
|
@acting_players = $self->shuffle("Choosing player to execute stock action", @acting_players); |
162
|
2
|
|
|
|
|
3
|
my $chosen_player = $acting_players[0]; |
163
|
2
|
50
|
|
|
|
5
|
say "Chosen player is $chosen_player" if($dry_run); |
164
|
2
|
|
|
|
|
3
|
my $order = shift @{$orders{$chosen_player}}; |
|
2
|
|
|
|
|
5
|
|
165
|
2
|
50
|
|
|
|
4
|
say "Order is $order" if($dry_run); |
166
|
2
|
|
|
|
|
10
|
$order =~ /^(.*)\s(\d)\s(.*)$/; |
167
|
2
|
|
|
|
|
3
|
my $command = $1; |
168
|
2
|
|
|
|
|
4
|
my $q = $2; |
169
|
2
|
|
|
|
|
3
|
my $nation = $3; |
170
|
2
|
50
|
|
|
|
11
|
$self->manage_stock($command, $chosen_player, $nation, $q) if (! $dry_run); |
171
|
2
|
50
|
|
|
|
4
|
if(! @{$orders{$chosen_player}}) |
|
2
|
|
|
|
|
8
|
|
172
|
|
|
|
|
|
|
{ |
173
|
2
|
|
|
|
|
4
|
@acting_players = grep { $_ ne $chosen_player } @acting_players; |
|
2
|
|
|
|
|
6
|
|
174
|
|
|
|
|
|
|
} |
175
|
2
|
50
|
|
|
|
11
|
say "---" if($dry_run); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
sub print_stock_events |
179
|
|
|
|
|
|
|
{ |
180
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
181
|
0
|
|
|
|
|
|
my $player = shift; |
182
|
0
|
|
|
|
|
|
my $player_obj = $self->get_player($player); |
183
|
0
|
|
|
|
|
|
my $y = shift; |
184
|
0
|
|
|
|
|
|
my $title = shift; |
185
|
0
|
|
|
|
|
|
my $backlog = shift; |
186
|
0
|
|
|
|
|
|
my $mode = shift; |
187
|
0
|
|
|
|
|
|
return $player_obj->print_turn_events($y, $title, $backlog, $mode); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
1; |