| 1 | package BalanceOfPower::Role::WebMaster; | ||
| 2 | $BalanceOfPower::Role::WebMaster::VERSION = '0.300010'; | ||
| 3 | use strict; | ||
| 4 | use v5.10; | ||
| 5 | use Moo::Role; | ||
| 6 | |||
| 7 | use LWP::UserAgent; | ||
| 8 | use JSON; | ||
| 9 | use Data::Dumper; | ||
| 10 | use File::Path qw(make_path remove_tree); | ||
| 11 | |||
| 12 | use BalanceOfPower::Utils qw(prev_turn next_turn); | ||
| 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 | my $self = shift; | ||
| 48 | my $game = shift; | ||
| 49 | my $site_root = $self->site_root; | ||
| 50 | my $dest_dir = "$site_root/views/generated/$game/" . $self->current_year(); | ||
| 51 | make_path $dest_dir; | ||
| 52 | open(my $hotspots, "> $dest_dir/hotspots.tt"); | ||
| 53 | print {$hotspots} $self->print_hotspots('html'); | ||
| 54 | close($hotspots); | ||
| 55 | open(my $allies, "> $dest_dir/alliances.tt"); | ||
| 56 | print {$allies} $self->print_allies(undef, 'html'); | ||
| 57 | close($allies); | ||
| 58 | open(my $influences, "> $dest_dir/influences.tt"); | ||
| 59 | print {$influences} $self->print_influences(undef, 'html'); | ||
| 60 | close($influences); | ||
| 61 | open(my $supports, "> $dest_dir/supports.tt"); | ||
| 62 | print {$supports} $self->print_military_supports(undef, 'html'); | ||
| 63 | close($supports); | ||
| 64 | open(my $reb_supports, "> $dest_dir/rebel-supports.tt"); | ||
| 65 | print {$reb_supports} $self->print_rebel_military_supports(undef, 'html'); | ||
| 66 | close($reb_supports); | ||
| 67 | open(my $whistory, "> $dest_dir/war-history.tt"); | ||
| 68 | print {$whistory} $self->print_war_history('html'); | ||
| 69 | close($whistory); | ||
| 70 | open(my $market, "> $dest_dir/market.tt"); | ||
| 71 | print {$market} $self->print_market('html'); | ||
| 72 | close($market); | ||
| 73 | $self->build_nations_statics($game, $site_root); | ||
| 74 | $self->build_players_statics($game, $site_root); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub build_post_statics | ||
| 78 | { | ||
| 79 | my $self = shift; | ||
| 80 | my $game = shift; | ||
| 81 | my $site_root = $self->site_root; | ||
| 82 | my $year = shift || $self->current_year; | ||
| 83 | my $dest_dir = "$site_root/views/generated/$game/" . next_turn($year); | ||
| 84 | make_path $dest_dir; | ||
| 85 | open(my $situation, "> $dest_dir/situation.tt"); | ||
| 86 | print {$situation} $self->print_turn_statistics($year, undef, 'html'); | ||
| 87 | close($situation); | ||
| 88 | open(my $events, "> $dest_dir/events.tt"); | ||
| 89 | print {$events} $self->print_formatted_turn_events($year, undef, 'html'); | ||
| 90 | close($events); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub build_meta_statics | ||
| 94 | { | ||
| 95 | my $self = shift; | ||
| 96 | my $game = shift; | ||
| 97 | my $site_root = $self->site_root; | ||
| 98 | my $dest_dir = "$site_root/metadata"; | ||
| 99 | make_path $dest_dir; | ||
| 100 | my %nations_to_dump; | ||
| 101 | foreach my $n (@{$self->nations}) | ||
| 102 | { | ||
| 103 | $nations_to_dump{$n->name} = { | ||
| 104 | code => $n->code, | ||
| 105 | area => $n->area }; | ||
| 106 | } | ||
| 107 | my %data = ( current_year => $self->current_year, | ||
| 108 | nations => \%nations_to_dump ); | ||
| 109 | open(my $meta, "> $dest_dir/$game.meta"); | ||
| 110 | print {$meta} Dumper(\%data); | ||
| 111 | 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 |