« blib/lib/BalanceOfPower/Relations/MilitarySupport.pm
↑ Summary
lib/BalanceOfPower/Relations/MilitarySupport.pm »
| 1 | package BalanceOfPower::Role::WebMaster; | ||||||||||||||||||||
| 2 | $BalanceOfPower::Role::WebMaster::VERSION = '0.300010'; | ||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 11 | ▶ | use v5.10; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 11 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 11 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 11 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 11 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 11 | ▶ | use File::Path qw(make_path remove_tree); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 11 | ▶ | use BalanceOfPower::Utils qw(prev_turn next_turn); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | has site_root => ( | ||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||
| 18 | has api_url => ( | ||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | has admin_password => ( | ||||||||||||||||||||
| 22 | is => 'rw' | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | requires 'print_hotspots'; | ||||||||||||||||||||
| 26 | requires 'print_allies'; | ||||||||||||||||||||
| 27 | requires 'print_influences'; | ||||||||||||||||||||
| 28 | requires 'print_military_supports'; | ||||||||||||||||||||
| 29 | requires 'print_rebel_military_supports'; | ||||||||||||||||||||
| 30 | requires 'print_war_history'; | ||||||||||||||||||||
| 31 | requires 'print_turn_statistics'; | ||||||||||||||||||||
| 32 | requires 'print_formatted_turn_events'; | ||||||||||||||||||||
| 33 | requires 'print_nation_actual_situation'; | ||||||||||||||||||||
| 34 | requires 'print_borders_analysis'; | ||||||||||||||||||||
| 35 | requires 'print_near_analysis'; | ||||||||||||||||||||
| 36 | requires 'print_diplomacy'; | ||||||||||||||||||||
| 37 | requires 'print_nation_events'; | ||||||||||||||||||||
| 38 | requires 'pre_decisions_elaborations'; | ||||||||||||||||||||
| 39 | requires 'decisions'; | ||||||||||||||||||||
| 40 | requires 'post_decisions_elaborations'; | ||||||||||||||||||||
| 41 | requires 'print_market'; | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | sub build_pre_statics | ||||||||||||||||||||
| 46 | { | ||||||||||||||||||||
| 47 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
build_pre_statics: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 48 | 0 | my $game = shift; | |||||||||||||||||||
| 49 | 0 | my $site_root = $self->site_root; | |||||||||||||||||||
| 50 | 0 | my $dest_dir = "$site_root/views/generated/$game/" . $self->current_year(); | |||||||||||||||||||
| 51 | 0 | make_path $dest_dir; | |||||||||||||||||||
| 52 | 0 | open(my $hotspots, "> $dest_dir/hotspots.tt"); | |||||||||||||||||||
| 53 | 0 | print {$hotspots} $self->print_hotspots('html'); | |||||||||||||||||||
| 54 | 0 | close($hotspots); | |||||||||||||||||||
| 55 | 0 | open(my $allies, "> $dest_dir/alliances.tt"); | |||||||||||||||||||
| 56 | 0 | print {$allies} $self->print_allies(undef, 'html'); | |||||||||||||||||||
| 57 | 0 | close($allies); | |||||||||||||||||||
| 58 | 0 | open(my $influences, "> $dest_dir/influences.tt"); | |||||||||||||||||||
| 59 | 0 | print {$influences} $self->print_influences(undef, 'html'); | |||||||||||||||||||
| 60 | 0 | close($influences); | |||||||||||||||||||
| 61 | 0 | open(my $supports, "> $dest_dir/supports.tt"); | |||||||||||||||||||
| 62 | 0 | print {$supports} $self->print_military_supports(undef, 'html'); | |||||||||||||||||||
| 63 | 0 | close($supports); | |||||||||||||||||||
| 64 | 0 | open(my $reb_supports, "> $dest_dir/rebel-supports.tt"); | |||||||||||||||||||
| 65 | 0 | print {$reb_supports} $self->print_rebel_military_supports(undef, 'html'); | |||||||||||||||||||
| 66 | 0 | close($reb_supports); | |||||||||||||||||||
| 67 | 0 | open(my $whistory, "> $dest_dir/war-history.tt"); | |||||||||||||||||||
| 68 | 0 | print {$whistory} $self->print_war_history('html'); | |||||||||||||||||||
| 69 | 0 | close($whistory); | |||||||||||||||||||
| 70 | 0 | open(my $market, "> $dest_dir/market.tt"); | |||||||||||||||||||
| 71 | 0 | print {$market} $self->print_market('html'); | |||||||||||||||||||
| 72 | 0 | close($market); | |||||||||||||||||||
| 73 | 0 | $self->build_nations_statics($game, $site_root); | |||||||||||||||||||
| 74 | 0 | $self->build_players_statics($game, $site_root); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | sub build_post_statics | ||||||||||||||||||||
| 78 | { | ||||||||||||||||||||
| 79 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
build_post_statics: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 80 | 0 | my $game = shift; | |||||||||||||||||||
| 81 | 0 | my $site_root = $self->site_root; | |||||||||||||||||||
| 82 | 0 | ▶ | my $year = shift || $self->current_year; | ||||||||||||||||||
|
Condition0%0/3
shift() || $self->current_year
MC/DC0%0/2
shift() || $self->current_year
shift()
$self->current_year
Truth table0%0/3
A || B
A= shift()B= $self->current_year
| |||||||||||||||||||||
| 83 | 0 | my $dest_dir = "$site_root/views/generated/$game/" . next_turn($year); | |||||||||||||||||||
| 84 | 0 | make_path $dest_dir; | |||||||||||||||||||
| 85 | 0 | open(my $situation, "> $dest_dir/situation.tt"); | |||||||||||||||||||
| 86 | 0 | print {$situation} $self->print_turn_statistics($year, undef, 'html'); | |||||||||||||||||||
| 87 | 0 | close($situation); | |||||||||||||||||||
| 88 | 0 | open(my $events, "> $dest_dir/events.tt"); | |||||||||||||||||||
| 89 | 0 | print {$events} $self->print_formatted_turn_events($year, undef, 'html'); | |||||||||||||||||||
| 90 | 0 | close($events); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub build_meta_statics | ||||||||||||||||||||
| 94 | { | ||||||||||||||||||||
| 95 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
build_meta_statics: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 96 | 0 | my $game = shift; | |||||||||||||||||||
| 97 | 0 | my $site_root = $self->site_root; | |||||||||||||||||||
| 98 | 0 | my $dest_dir = "$site_root/metadata"; | |||||||||||||||||||
| 99 | 0 | make_path $dest_dir; | |||||||||||||||||||
| 100 | 0 | my %nations_to_dump; | |||||||||||||||||||
| 101 | 0 | foreach my $n (@{$self->nations}) | |||||||||||||||||||
| 102 | { | ||||||||||||||||||||
| 103 | 0 | $nations_to_dump{$n->name} = { | |||||||||||||||||||
| 104 | code => $n->code, | ||||||||||||||||||||
| 105 | area => $n->area }; | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | 0 | my %data = ( current_year => $self->current_year, | |||||||||||||||||||
| 108 | nations => \%nations_to_dump ); | ||||||||||||||||||||
| 109 | 0 | open(my $meta, "> $dest_dir/$game.meta"); | |||||||||||||||||||
| 110 | 0 | print {$meta} Dumper(\%data); | |||||||||||||||||||
| 111 | 0 | close($meta); | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | sub build_nations_statics | ||||||||||||||||||||
| 115 | { | ||||||||||||||||||||
| 116 | my $self = shift; | ||||||||||||||||||||
| 117 | my $game = shift; | ||||||||||||||||||||
| 118 | my $site_root = $self->site_root; | ||||||||||||||||||||
| 119 | foreach my $code (keys $self->nation_codes) | ||||||||||||||||||||
| 120 | { | ||||||||||||||||||||
| 121 | my $nation = $self->nation_codes->{$code}; | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | my $dest_dir = "$site_root/views/generated/$game/" . $self->current_year() . "/n/$code"; | ||||||||||||||||||||
| 124 | make_path($dest_dir); | ||||||||||||||||||||
| 125 | open(my $status, "> $dest_dir/actual.tt"); | ||||||||||||||||||||
| 126 | print {$status} $self->print_nation_actual_situation($nation, 1, 'html'); | ||||||||||||||||||||
| 127 | close($status); | ||||||||||||||||||||
| 128 | open(my $borders, "> $dest_dir/borders.tt"); | ||||||||||||||||||||
| 129 | print {$borders} $self->print_borders_analysis($nation, 'html'); | ||||||||||||||||||||
| 130 | close($borders); | ||||||||||||||||||||
| 131 | open(my $near, "> $dest_dir/near.tt"); | ||||||||||||||||||||
| 132 | print {$near} $self->print_near_analysis($nation, 'html'); | ||||||||||||||||||||
| 133 | close($near); | ||||||||||||||||||||
| 134 | open(my $diplomacy, "> $dest_dir/diplomacy.tt"); | ||||||||||||||||||||
| 135 | print {$diplomacy} $self->print_diplomacy($nation, 'html'); | ||||||||||||||||||||
| 136 | close($diplomacy); | ||||||||||||||||||||
| 137 | open(my $events, "> $dest_dir/events.tt"); | ||||||||||||||||||||
| 138 | print {$events} $self->print_nation_events($nation, prev_turn($self->current_year()), undef, 'html'); | ||||||||||||||||||||
| 139 | close($events); | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | $dest_dir = "$site_root/metadata/$game/n"; | ||||||||||||||||||||
| 142 | make_path($dest_dir); | ||||||||||||||||||||
| 143 | open(my $nation_meta, "> $dest_dir/" . $code . ".data"); | ||||||||||||||||||||
| 144 | my $civil_war = $self->at_civil_war($nation); | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | my $commands = $self->build_commands(); | ||||||||||||||||||||
| 147 | my $exec = $commands->set_executive($nation); | ||||||||||||||||||||
| 148 | my @allowed = $exec->allowed_orders(); | ||||||||||||||||||||
| 149 | my %command_matrix; | ||||||||||||||||||||
| 150 | for(@allowed) | ||||||||||||||||||||
| 151 | { | ||||||||||||||||||||
| 152 | my $com_name = $_; | ||||||||||||||||||||
| 153 | my $com = $exec->commands->{$com_name}; | ||||||||||||||||||||
| 154 | if($com->has_argument) | ||||||||||||||||||||
| 155 | { | ||||||||||||||||||||
| 156 | my @targets = $com->get_available_targets(); | ||||||||||||||||||||
| 157 | if(@targets) | ||||||||||||||||||||
| 158 | { | ||||||||||||||||||||
| 159 | $command_matrix{$com_name}->{targets} = \@targets; | ||||||||||||||||||||
| 160 | $command_matrix{$com_name}->{argument} = $com->has_argument(); | ||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | else | ||||||||||||||||||||
| 164 | { | ||||||||||||||||||||
| 165 | $command_matrix{$com_name}->{argument} = $com->has_argument(); | ||||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | my $nation_obj = $self->get_nation($nation); | ||||||||||||||||||||
| 169 | my %nation_hash = ( stocks => $nation_obj->available_stocks, | ||||||||||||||||||||
| 170 | internal_production => $nation_obj->production_for_domestic, | ||||||||||||||||||||
| 171 | export_production => $nation_obj->production_for_export, | ||||||||||||||||||||
| 172 | prestige => $nation_obj->prestige, | ||||||||||||||||||||
| 173 | army => $nation_obj->army, | ||||||||||||||||||||
| 174 | civil_war => $civil_war, | ||||||||||||||||||||
| 175 | commands => \%command_matrix ); | ||||||||||||||||||||
| 176 | print {$nation_meta} Dumper(\%nation_hash); | ||||||||||||||||||||
| 177 | close($nation_meta); | ||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | sub build_players_statics | ||||||||||||||||||||
| 181 | { | ||||||||||||||||||||
| 182 | my $self = shift; | ||||||||||||||||||||
| 183 | my $game = shift; | ||||||||||||||||||||
| 184 | my $site_root = $self->site_root; | ||||||||||||||||||||
| 185 | say "Generating player statics"; | ||||||||||||||||||||
| 186 | foreach my $p (@{$self->players}) | ||||||||||||||||||||
| 187 | { | ||||||||||||||||||||
| 188 | my $dest_dir = "$site_root/views/generated/$game/" . $self->current_year() . '/p/' . $p->name; | ||||||||||||||||||||
| 189 | make_path($dest_dir); | ||||||||||||||||||||
| 190 | open(my $stocks, "> $dest_dir/stocks.tt"); | ||||||||||||||||||||
| 191 | print {$stocks} $self->print_stocks($p->name, 'html'); | ||||||||||||||||||||
| 192 | close($stocks); | ||||||||||||||||||||
| 193 | open(my $events, "> $dest_dir/events.tt"); | ||||||||||||||||||||
| 194 | print {$events} $self->print_stock_events($p->name, prev_turn($self->current_year()), "My market events", 3, 'html'); | ||||||||||||||||||||
| 195 | close($events); | ||||||||||||||||||||
| 196 | open(my $ranking, "> $dest_dir/ranking.tt"); | ||||||||||||||||||||
| 197 | print {$ranking} $self->print_all_stocks('html'); | ||||||||||||||||||||
| 198 | close($ranking); | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | $dest_dir = "$site_root/metadata/$game/p"; | ||||||||||||||||||||
| 201 | make_path($dest_dir); | ||||||||||||||||||||
| 202 | open(my $metawallet, "> $dest_dir/" . $p->name . "-wallet.data"); | ||||||||||||||||||||
| 203 | my %wallet; | ||||||||||||||||||||
| 204 | for(keys %{$p->wallet}) | ||||||||||||||||||||
| 205 | { | ||||||||||||||||||||
| 206 | my $code = $self->get_nation($_)->code; | ||||||||||||||||||||
| 207 | $wallet{$code} = $p->wallet->{$_}; | ||||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | print {$metawallet} Dumper(\%wallet); | ||||||||||||||||||||
| 210 | close($metawallet); | ||||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | sub clean_statics | ||||||||||||||||||||
| 215 | { | ||||||||||||||||||||
| 216 | my $self = shift; | ||||||||||||||||||||
| 217 | my $game = shift; | ||||||||||||||||||||
| 218 | my $preservation_window = shift; | ||||||||||||||||||||
| 219 | my $site_root = $self->site_root; | ||||||||||||||||||||
| 220 | my $delete_year = $self->current_year; | ||||||||||||||||||||
| 221 | say "Current year: $delete_year"; | ||||||||||||||||||||
| 222 | say "---"; | ||||||||||||||||||||
| 223 | for(my $i = $preservation_window; $i > 0; $i--) | ||||||||||||||||||||
| 224 | { | ||||||||||||||||||||
| 225 | $delete_year = prev_turn($delete_year); | ||||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | my $deleting = 1; | ||||||||||||||||||||
| 228 | while($deleting) | ||||||||||||||||||||
| 229 | { | ||||||||||||||||||||
| 230 | my $directory_to_delete = "$site_root/views/generated/$game/$delete_year"; | ||||||||||||||||||||
| 231 | if (-e $directory_to_delete and -d $directory_to_delete) | ||||||||||||||||||||
| 232 | { | ||||||||||||||||||||
| 233 | say "Deleting $directory_to_delete"; | ||||||||||||||||||||
| 234 | remove_tree($directory_to_delete); | ||||||||||||||||||||
| 235 | if($delete_year =~ /1$/) | ||||||||||||||||||||
| 236 | { | ||||||||||||||||||||
| 237 | my $parent_directory_to_delete = $directory_to_delete; | ||||||||||||||||||||
| 238 | $parent_directory_to_delete =~ s/\/1$//; | ||||||||||||||||||||
| 239 | say "Deleting $parent_directory_to_delete"; | ||||||||||||||||||||
| 240 | remove_tree($parent_directory_to_delete); | ||||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | $delete_year = prev_turn($delete_year); | ||||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | else | ||||||||||||||||||||
| 245 | { | ||||||||||||||||||||
| 246 | $deleting = 0; | ||||||||||||||||||||
| 247 | } | ||||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | sub server_available | ||||||||||||||||||||
| 252 | { | ||||||||||||||||||||
| 253 | my $self = shift; | ||||||||||||||||||||
| 254 | my $alive = $self->get_web_data('/keepalive', 1); | ||||||||||||||||||||
| 255 | if($alive && $alive eq 'OK') | ||||||||||||||||||||
| 256 | { | ||||||||||||||||||||
| 257 | return 1; | ||||||||||||||||||||
| 258 | } | ||||||||||||||||||||
| 259 | else | ||||||||||||||||||||
| 260 | { | ||||||||||||||||||||
| 261 | return 0; | ||||||||||||||||||||
| 262 | } | ||||||||||||||||||||
| 263 | } | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | sub generate_whole_turn | ||||||||||||||||||||
| 266 | { | ||||||||||||||||||||
| 267 | my $self = shift; | ||||||||||||||||||||
| 268 | my $game = shift; | ||||||||||||||||||||
| 269 | my $turn = shift; | ||||||||||||||||||||
| 270 | my $site_root = shift; | ||||||||||||||||||||
| 271 | $self->pre_decisions_elaborations($turn); | ||||||||||||||||||||
| 272 | $self->build_pre_statics($game, $site_root); | ||||||||||||||||||||
| 273 | $self->decisions(); | ||||||||||||||||||||
| 274 | $self->post_decisions_elaborations(); | ||||||||||||||||||||
| 275 | $self->build_post_statics($game, $site_root); | ||||||||||||||||||||
| 276 | } | ||||||||||||||||||||
| 277 | sub generate_web_interactive_turn | ||||||||||||||||||||
| 278 | { | ||||||||||||||||||||
| 279 | my $self = shift; | ||||||||||||||||||||
| 280 | my $game = shift; | ||||||||||||||||||||
| 281 | my $site_root = shift; | ||||||||||||||||||||
| 282 | $self->manage_stock_orders($game); | ||||||||||||||||||||
| 283 | $self->manage_influence_orders($game); | ||||||||||||||||||||
| 284 | $self->decisions(); | ||||||||||||||||||||
| 285 | $self->post_decisions_elaborations(); | ||||||||||||||||||||
| 286 | $self->build_post_statics($game, $site_root); | ||||||||||||||||||||
| 287 | $self->pre_decisions_elaborations(next_turn($self->current_year)); | ||||||||||||||||||||
| 288 | $self->manage_web_players($game); | ||||||||||||||||||||
| 289 | $self->build_pre_statics($game, $site_root); | ||||||||||||||||||||
| 290 | } | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | sub manage_web_players | ||||||||||||||||||||
| 293 | { | ||||||||||||||||||||
| 294 | my $self = shift; | ||||||||||||||||||||
| 295 | my $game = shift; | ||||||||||||||||||||
| 296 | my $players = $self->get_web_data("/api/$game/users"); | ||||||||||||||||||||
| 297 | if($players) | ||||||||||||||||||||
| 298 | { | ||||||||||||||||||||
| 299 | for(@{$players}) | ||||||||||||||||||||
| 300 | { | ||||||||||||||||||||
| 301 | $self->create_player($_); | ||||||||||||||||||||
| 302 | } | ||||||||||||||||||||
| 303 | } | ||||||||||||||||||||
| 304 | } | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | sub manage_stock_orders | ||||||||||||||||||||
| 307 | { | ||||||||||||||||||||
| 308 | my $self = shift; | ||||||||||||||||||||
| 309 | my $game = shift; | ||||||||||||||||||||
| 310 | foreach my $p (@{$self->players}) | ||||||||||||||||||||
| 311 | { | ||||||||||||||||||||
| 312 | my $password = $self->admin_password; | ||||||||||||||||||||
| 313 | my $user = $p->name; | ||||||||||||||||||||
| 314 | my $call = "/api/$game/stock-orders?player=$user&password=$password"; | ||||||||||||||||||||
| 315 | my $orders = $self->get_web_data($call); | ||||||||||||||||||||
| 316 | for(@{$orders}) | ||||||||||||||||||||
| 317 | { | ||||||||||||||||||||
| 318 | $self->get_player($p->name)->add_stock_order($_); | ||||||||||||||||||||
| 319 | } | ||||||||||||||||||||
| 320 | } | ||||||||||||||||||||
| 321 | } | ||||||||||||||||||||
| 322 | sub manage_influence_orders | ||||||||||||||||||||
| 323 | { | ||||||||||||||||||||
| 324 | my $self = shift; | ||||||||||||||||||||
| 325 | my $game = shift; | ||||||||||||||||||||
| 326 | foreach my $p (@{$self->players}) | ||||||||||||||||||||
| 327 | { | ||||||||||||||||||||
| 328 | my $password = $self->admin_password; | ||||||||||||||||||||
| 329 | my $user = $p->name; | ||||||||||||||||||||
| 330 | my $call = "/api/$game/influence-orders?player=$user&password=$password"; | ||||||||||||||||||||
| 331 | my $orders = $self->get_web_data($call); | ||||||||||||||||||||
| 332 | for(@{$orders}) | ||||||||||||||||||||
| 333 | { | ||||||||||||||||||||
| 334 | $self->get_player($p->name)->add_control_order($_->{nation}, $_->{command}); | ||||||||||||||||||||
| 335 | } | ||||||||||||||||||||
| 336 | } | ||||||||||||||||||||
| 337 | } | ||||||||||||||||||||
| 338 | |||||||||||||||||||||
| 339 | sub get_web_data | ||||||||||||||||||||
| 340 | { | ||||||||||||||||||||
| 341 | my $self = shift; | ||||||||||||||||||||
| 342 | my $call = shift; | ||||||||||||||||||||
| 343 | my $bare = shift || 0; | ||||||||||||||||||||
| 344 | my $ua = LWP::UserAgent->new; | ||||||||||||||||||||
| 345 | $ua->timeout(10); | ||||||||||||||||||||
| 346 | $ua->env_proxy; | ||||||||||||||||||||
| 347 | my $response = $ua->get($self->api_url . $call); | ||||||||||||||||||||
| 348 | |||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | if ($response->is_success) { | ||||||||||||||||||||
| 351 | if($bare) | ||||||||||||||||||||
| 352 | { | ||||||||||||||||||||
| 353 | return $response->decoded_content; | ||||||||||||||||||||
| 354 | } | ||||||||||||||||||||
| 355 | else | ||||||||||||||||||||
| 356 | { | ||||||||||||||||||||
| 357 | my $json = JSON->new->allow_nonref; | ||||||||||||||||||||
| 358 | return $json->decode( $response->decoded_content ); | ||||||||||||||||||||
| 359 | } | ||||||||||||||||||||
| 360 | } | ||||||||||||||||||||
| 361 | else { | ||||||||||||||||||||
| 362 | return undef; | ||||||||||||||||||||
| 363 | } | ||||||||||||||||||||
| 364 | } | ||||||||||||||||||||
| 365 | |||||||||||||||||||||
| 366 | 1; | ||||||||||||||||||||
| 367 | |||||||||||||||||||||
| 368 | |||||||||||||||||||||