| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package BalanceOfPower::Role::Broker; | 
| 2 |  |  |  |  |  |  | $BalanceOfPower::Role::Broker::VERSION = '0.400110'; | 
| 3 | 13 |  |  | 13 |  | 4039 | use strict; | 
|  | 13 |  |  |  |  | 16 |  | 
|  | 13 |  |  |  |  | 274 |  | 
| 4 | 13 |  |  | 13 |  | 90 | use v5.10; | 
|  | 13 |  |  |  |  | 25 |  | 
| 5 | 13 |  |  | 13 |  | 37 | use Moo::Role; | 
|  | 13 |  |  |  |  | 18 |  | 
|  | 13 |  |  |  |  | 64 |  | 
| 6 | 13 |  |  | 13 |  | 2403 | use BalanceOfPower::Constants ':all'; | 
|  | 13 |  |  |  |  | 15 |  | 
|  | 13 |  |  |  |  | 5100 |  | 
| 7 | 13 |  |  | 13 |  | 62 | use BalanceOfPower::Utils qw( prev_turn ); | 
|  | 13 |  |  |  |  | 14 |  | 
|  | 13 |  |  |  |  | 10519 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | requires 'get_player'; | 
| 10 |  |  |  |  |  |  | requires 'get_nation'; | 
| 11 |  |  |  |  |  |  | requires 'get_statistics_value'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub manage_stock | 
| 14 |  |  |  |  |  |  | { | 
| 15 | 2 |  |  | 2 | 0 | 3 | my $self = shift; | 
| 16 | 2 |  |  |  |  | 2 | my $command = shift; | 
| 17 | 2 |  |  |  |  | 3 | my $player = shift; | 
| 18 | 2 |  |  |  |  | 2 | my $nation = shift; | 
| 19 | 2 |  |  |  |  | 2 | 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 |  |  |  |  | 2 | 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 |  |  | 8 | 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 |  |  |  | 13 | 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 |  |  |  | 8 | if($self->at_civil_war($nation)) | 
| 50 |  |  |  |  |  |  | { | 
| 51 | 1 | 50 |  |  |  | 3 | $player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR") | 
| 52 |  |  |  |  |  |  | if(! $dry_run); | 
| 53 | 1 |  |  |  |  | 2 | return { status => -14 }; | 
| 54 |  |  |  |  |  |  | } | 
| 55 | 2 | 100 |  |  |  | 8 | return { status => 1, | 
| 56 |  |  |  |  |  |  | command => "buy $q $nation" } if $dry_run; | 
| 57 | 1 |  |  |  |  | 6 | 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 |  |  |  |  | 2 | 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 |  |  |  |  | 5 | $player_obj->add_stocks($q, $nation); | 
| 68 | 1 |  |  |  |  | 4 | $player_obj->add_influence($influence, $nation); | 
| 69 | 1 |  |  |  |  | 4 | $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 |  |  |  |  | 3 | return { status => 1 }; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | sub sell_stock | 
| 75 |  |  |  |  |  |  | { | 
| 76 | 2 |  |  | 2 | 0 | 2 | my $self = shift; | 
| 77 | 2 |  |  |  |  | 3 | my $player = shift; | 
| 78 | 2 |  |  |  |  | 2 | my $nation = shift; | 
| 79 | 2 |  |  |  |  | 3 | my $q = shift; | 
| 80 | 2 |  | 100 |  |  | 5 | 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 |  |  |  | 7 | 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 |  |  |  | 7 | return { status => 1, | 
| 96 |  |  |  |  |  |  | command => "sell $q $nation" } if $dry_run; | 
| 97 | 1 |  |  |  |  | 5 | 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 |  |  |  |  | 4 | $player_obj->add_money($global_cost); | 
| 100 | 1 |  |  |  |  | 4 | $player_obj->add_stocks(-1 * $q, $nation); | 
| 101 | 1 |  |  |  |  | 4 | $nation_obj->get_stocks(-1 * $q); | 
| 102 | 1 |  |  |  |  | 6 | $player_obj->register_event("SOLD $q STOCKS OF $nation. COST: $global_cost ($unit_cost)"); | 
| 103 | 1 |  |  |  |  | 4 | return { status => 1 }; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  | sub issue_war_bonds | 
| 106 |  |  |  |  |  |  | { | 
| 107 | 22 |  |  | 22 | 0 | 22 | my $self = shift; | 
| 108 | 22 |  |  |  |  | 21 | my $n = shift; | 
| 109 | 22 |  |  |  |  | 18 | for(@{$self->players}) | 
|  | 22 |  |  |  |  | 58 |  | 
| 110 |  |  |  |  |  |  | { | 
| 111 | 4 |  |  |  |  | 11 | $_->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 | 4 | my $self = shift; | 
| 126 | 2 |  |  |  |  | 4 | my $nation = shift; | 
| 127 | 2 |  |  |  |  | 3 | for(@{$self->players}) | 
|  | 2 |  |  |  |  | 9 |  | 
| 128 |  |  |  |  |  |  | { | 
| 129 | 1 |  |  |  |  | 4 | $_->empty_stocks($nation); | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  | sub cash_war_bonds | 
| 133 |  |  |  |  |  |  | { | 
| 134 | 2 |  |  | 2 | 0 | 2 | my $self = shift; | 
| 135 | 2 |  |  |  |  | 2 | my $nation = shift; | 
| 136 | 2 |  |  |  |  | 3 | for(@{$self->players}) | 
|  | 2 |  |  |  |  | 6 |  | 
| 137 |  |  |  |  |  |  | { | 
| 138 | 0 |  |  |  |  | 0 | $_->cash_war_bonds($nation); | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  | sub execute_stock_orders | 
| 142 |  |  |  |  |  |  | { | 
| 143 | 45 |  |  | 45 | 0 | 46 | my $self = shift; | 
| 144 | 45 |  | 50 |  |  | 155 | my $dry_run = shift || 0; | 
| 145 | 45 |  |  |  |  | 47 | my %orders; | 
| 146 |  |  |  |  |  |  | my @acting_players; | 
| 147 | 45 |  |  |  |  | 42 | foreach my $player (@{$self->players}) | 
|  | 45 |  |  |  |  | 123 |  | 
| 148 |  |  |  |  |  |  | { | 
| 149 | 10 |  |  |  |  | 9 | my @p_orders = @{$player->stock_orders}; | 
|  | 10 |  |  |  |  | 26 |  | 
| 150 | 10 | 100 |  |  |  | 19 | if(@p_orders) | 
| 151 |  |  |  |  |  |  | { | 
| 152 | 2 | 50 |  |  |  | 5 | 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 |  |  |  |  | 29 | $player->empty_stock_orders(); | 
| 157 |  |  |  |  |  |  | } | 
| 158 | 45 | 50 |  |  |  | 82 | say "===" if($dry_run); | 
| 159 | 45 |  |  |  |  | 132 | while(@acting_players) | 
| 160 |  |  |  |  |  |  | { | 
| 161 | 2 |  |  |  |  | 39 | @acting_players = $self->shuffle("Choosing player to execute stock action", @acting_players); | 
| 162 | 2 |  |  |  |  | 4 | my $chosen_player = $acting_players[0]; | 
| 163 | 2 | 50 |  |  |  | 5 | say "Chosen player is $chosen_player" if($dry_run); | 
| 164 | 2 |  |  |  |  | 2 | my $order = shift @{$orders{$chosen_player}}; | 
|  | 2 |  |  |  |  | 4 |  | 
| 165 | 2 | 50 |  |  |  | 6 | say "Order is $order" if($dry_run); | 
| 166 | 2 |  |  |  |  | 8 | $order =~ /^(.*)\s(\d)\s(.*)$/; | 
| 167 | 2 |  |  |  |  | 5 | my $command = $1; | 
| 168 | 2 |  |  |  |  | 3 | my $q = $2; | 
| 169 | 2 |  |  |  |  | 3 | my $nation = $3; | 
| 170 | 2 | 50 |  |  |  | 8 | $self->manage_stock($command, $chosen_player, $nation, $q) if (! $dry_run); | 
| 171 | 2 | 50 |  |  |  | 3 | if(! @{$orders{$chosen_player}}) | 
|  | 2 |  |  |  |  | 21 |  | 
| 172 |  |  |  |  |  |  | { | 
| 173 | 2 |  |  |  |  | 2 | @acting_players = grep { $_ ne $chosen_player } @acting_players; | 
|  | 2 |  |  |  |  | 8 |  | 
| 174 |  |  |  |  |  |  | } | 
| 175 | 2 | 50 |  |  |  | 9 | 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; |