| 1 | package BalanceOfPower::Role::Broker; | ||||||||||||||
| 2 | 11 | $BalanceOfPower::Role::Broker::VERSION = '0.200000'; | |||||||||||||
| 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 | 11 | ▶ | use BalanceOfPower::Constants ':all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 11 | ▶ | use BalanceOfPower::Utils qw( prev_turn ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 11 | requires 'get_player'; | |||||||||||||
| 10 | 11 | requires 'get_nation'; | |||||||||||||
| 11 | 11 | requires 'get_statistics_value'; | |||||||||||||
| 12 | |||||||||||||||
| 13 | sub manage_stock | ||||||||||||||
| 14 | { | ||||||||||||||
| 15 | 2 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
manage_stock: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 16 | 2 | my $command = shift; | |||||||||||||
| 17 | 2 | my $player = shift; | |||||||||||||
| 18 | 2 | my $nation = shift; | |||||||||||||
| 19 | 2 | my $q = shift; | |||||||||||||
| 20 | 2 | ▶ | if($command eq 'buy') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 21 | { | ||||||||||||||
| 22 | 1 | return $self->buy_stock($player, $nation, $q); | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | elsif($command eq 'sell') | ||||||||||||||
| 25 | { | ||||||||||||||
| 26 | 1 | return $self->sell_stock($player, $nation, $q); | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | sub buy_stock | ||||||||||||||
| 31 | { | ||||||||||||||
| 32 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
buy_stock: called CC=10 SCAR=25.1
Pod
undocumented
| |||||||||||||||
| 33 | 3 | my $player = shift; | |||||||||||||
| 34 | 3 | my $nation = shift; | |||||||||||||
| 35 | 3 | my $q = shift; | |||||||||||||
| 36 | 3 | ▶ | my $dry_run = shift || 0; | ||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 37 | 3 | my $player_obj = $self->get_player($player); | |||||||||||||
| 38 | 3 | my $nation_obj = $self->get_nation($nation); | |||||||||||||
| 39 | 3 | ▶ | if($nation_obj->available_stocks < $q) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | { | ||||||||||||||
| 41 | 0 | ▶ | $player_obj->register_event("STOCKS IN QUANTITY $q FOR $nation NOT AVAILABLE") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | if(! $dry_run); | ||||||||||||||
| 43 | 0 | return { status => -11 }; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | 3 | ▶ | if($q > MAX_BUY_STOCK) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | { | ||||||||||||||
| 47 | 0 | return { status => -15 }; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | 3 | ▶ | if($self->at_civil_war($nation)) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | { | ||||||||||||||
| 51 | 1 | ▶ | $player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | if(! $dry_run); | ||||||||||||||
| 53 | 1 | return { status => -14 }; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | 2 | ▶ | return { status => 1, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | command => "buy $q $nation" } if $dry_run; | ||||||||||||||
| 57 | 1 | my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d'); | |||||||||||||
| 58 | 1 | my $global_cost = $unit_cost * $q; | |||||||||||||
| 59 | 1 | my $influence = $q * STOCK_INFLUENCE_FACTOR; | |||||||||||||
| 60 | 1 | ▶ | if($player_obj->money < $global_cost) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | { | ||||||||||||||
| 62 | 0 | ▶ | $player_obj->register_event("NOT ENOUGH MONEY TO BUY STOCKS OF $nation FOR $global_cost") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | if(! $dry_run); | ||||||||||||||
| 64 | 0 | return { status => -12 }; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | 1 | $player_obj->add_money(-1 * $global_cost); | |||||||||||||
| 67 | 1 | $player_obj->add_stocks($q, $nation); | |||||||||||||
| 68 | 1 | $player_obj->add_influence($influence, $nation); | |||||||||||||
| 69 | 1 | $nation_obj->get_stocks($q); | |||||||||||||
| 70 | |||||||||||||||
| 71 | 1 | $player_obj->register_event("BOUGHT $q STOCKS OF $nation. COST: $global_cost ($unit_cost)"); | |||||||||||||
| 72 | 1 | return { status => 1 }; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | sub sell_stock | ||||||||||||||
| 75 | { | ||||||||||||||
| 76 | 2 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
sell_stock: called CC=7 SCAR=21.2
Pod
undocumented
| |||||||||||||||
| 77 | 2 | my $player = shift; | |||||||||||||
| 78 | 2 | my $nation = shift; | |||||||||||||
| 79 | 2 | my $q = shift; | |||||||||||||
| 80 | 2 | ▶ | my $dry_run = shift || 0; | ||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 81 | 2 | my $player_obj = $self->get_player($player); | |||||||||||||
| 82 | 2 | my $nation_obj = $self->get_nation($nation); | |||||||||||||
| 83 | 2 | ▶ | if($player_obj->stocks($nation) < $q) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | { | ||||||||||||||
| 85 | 0 | ▶ | $player_obj->register_event("THERE AREN'T $q STOCKS OF $nation TO SELL") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | if(! $dry_run); | ||||||||||||||
| 87 | 0 | return { status => -13 }; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | 2 | ▶ | if($self->at_civil_war($nation)) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | { | ||||||||||||||
| 91 | 0 | ▶ | $player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | if(! $dry_run); | ||||||||||||||
| 93 | 0 | return { status => -14 }; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | 2 | ▶ | return { status => 1, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | command => "sell $q $nation" } if $dry_run; | ||||||||||||||
| 97 | 1 | my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d'); | |||||||||||||
| 98 | 1 | my $global_cost = $unit_cost * $q; | |||||||||||||
| 99 | 1 | $player_obj->add_money($global_cost); | |||||||||||||
| 100 | 1 | $player_obj->add_stocks(-1 * $q, $nation); | |||||||||||||
| 101 | 1 | $nation_obj->get_stocks(-1 * $q); | |||||||||||||
| 102 | 1 | $player_obj->register_event("SOLD $q STOCKS OF $nation. COST: $global_cost ($unit_cost)"); | |||||||||||||
| 103 | 1 | return { status => 1 }; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | sub issue_war_bonds | ||||||||||||||
| 106 | { | ||||||||||||||
| 107 | 22 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
issue_war_bonds: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 108 | 22 | my $n = shift; | |||||||||||||
| 109 | 22 | for(@{$self->players}) | |||||||||||||
| 110 | { | ||||||||||||||
| 111 | 4 | $_->issue_war_bonds($n); | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | } | ||||||||||||||
| 114 | sub discard_war_bonds | ||||||||||||||
| 115 | { | ||||||||||||||
| 116 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
discard_war_bonds: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 117 | 3 | my $nation = shift; | |||||||||||||
| 118 | 3 | for(@{$self->players}) | |||||||||||||
| 119 | { | ||||||||||||||
| 120 | 1 | $_->discard_war_bonds($nation); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | } | ||||||||||||||
| 123 | sub empty_stocks | ||||||||||||||
| 124 | { | ||||||||||||||
| 125 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
empty_stocks: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||
| 126 | 1 | my $nation = shift; | |||||||||||||
| 127 | 1 | for(@{$self->players}) | |||||||||||||
| 128 | { | ||||||||||||||
| 129 | 0 | $_->empty_stocks($nation); | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | } | ||||||||||||||
| 132 | sub cash_war_bonds | ||||||||||||||
| 133 | { | ||||||||||||||
| 134 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
cash_war_bonds: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 135 | 3 | my $nation = shift; | |||||||||||||
| 136 | 3 | for(@{$self->players}) | |||||||||||||
| 137 | { | ||||||||||||||
| 138 | 1 | $_->cash_war_bonds($nation); | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | } | ||||||||||||||
| 141 | sub execute_stock_orders | ||||||||||||||
| 142 | { | ||||||||||||||
| 143 | 40 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
execute_stock_orders: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 144 | 40 | foreach my $player ($self->shuffle("shuffle players to execute stock orders", @{$self->players})) | |||||||||||||
| 145 | { | ||||||||||||||
| 146 | 5 | foreach my $order (@{$player->stock_orders}) | |||||||||||||
| 147 | { | ||||||||||||||
| 148 | 2 | $order =~ /^(.*)\s(\d)\s(.*)$/; | |||||||||||||
| 149 | 2 | my $command = $1; | |||||||||||||
| 150 | 2 | my $q = $2; | |||||||||||||
| 151 | 2 | my $nation = $3; | |||||||||||||
| 152 | 2 | $self->manage_stock($command, $player->name, $nation, $q); | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | 5 | $player->empty_stock_orders(); | |||||||||||||
| 155 | } | ||||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | sub print_stock_events | ||||||||||||||
| 159 | { | ||||||||||||||
| 160 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
print_stock_events: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 161 | 0 | my $player = shift; | |||||||||||||
| 162 | 0 | my $player_obj = $self->get_player($player); | |||||||||||||
| 163 | 0 | my $y = shift; | |||||||||||||
| 164 | 0 | return $player_obj->print_turn_events($y); | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 11 | 1; | |||||||||||||