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