| 1 | package BalanceOfPower::Dice; | ||||||||||||
| 2 | 11 | $BalanceOfPower::Dice::VERSION = '0.300010'; | |||||||||||
| 3 | 11 | ▶ | use v5.10; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 11 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 11 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 11 | ▶ | use List::Util qw(shuffle); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 11 | with 'BalanceOfPower::Role::Logger'; | |||||||||||
| 9 | |||||||||||||
| 10 | has tricks => ( | ||||||||||||
| 11 | is => 'rw', | ||||||||||||
| 12 | 0 | ▶ | default => sub { {} } | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 13 | 11 | ); | |||||||||||
| 14 | has trick_counters => ( | ||||||||||||
| 15 | is => 'rw', | ||||||||||||
| 16 | 0 | ▶ | default => sub { {} } | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 17 | 11 | ); | |||||||||||
| 18 | 11 | has forced_advisor => ( | |||||||||||
| 19 | is => 'rw', | ||||||||||||
| 20 | default => 0 | ||||||||||||
| 21 | ); | ||||||||||||
| 22 | 11 | has only_one_nation_acting => ( | |||||||||||
| 23 | is => 'rw', | ||||||||||||
| 24 | default => 0 | ||||||||||||
| 25 | ); | ||||||||||||
| 26 | |||||||||||||
| 27 | sub random | ||||||||||||
| 28 | { | ||||||||||||
| 29 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
random: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 30 | 0 | my $min = shift; | |||||||||||
| 31 | 0 | my $max = shift; | |||||||||||
| 32 | 0 | ▶ | my $message = shift || "NO MESSAGE [$min-$max]"; | ||||||||||
|
Condition0%0/2
shift() || "NO MESSAGE [$min-$max]"
MC/DC0%0/1
shift() || "NO MESSAGE [$min-$max]"
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 33 | 0 | my $out = $self->tricked($message); | |||||||||||
| 34 | 0 | ▶ | if(defined $out) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | { | ||||||||||||
| 36 | 0 | $self->write_log($message, $out, 1); | |||||||||||
| 37 | 0 | return $out; | |||||||||||
| 38 | } | ||||||||||||
| 39 | else | ||||||||||||
| 40 | { | ||||||||||||
| 41 | 0 | my $random_range = $max - $min + 1; | |||||||||||
| 42 | 0 | $out = int(rand($random_range)) + $min; | |||||||||||
| 43 | 0 | $self->write_log($message, $out, 0); | |||||||||||
| 44 | 0 | return $out; | |||||||||||
| 45 | } | ||||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | sub random10 | ||||||||||||
| 49 | { | ||||||||||||
| 50 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
random10: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 51 | 0 | my $min = shift; | |||||||||||
| 52 | 0 | my $max = shift; | |||||||||||
| 53 | 0 | ▶ | my $message = shift || "NO MESSAGE [$min-$max]"; | ||||||||||
|
Condition0%0/2
shift() || "NO MESSAGE [$min-$max]"
MC/DC0%0/1
shift() || "NO MESSAGE [$min-$max]"
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 54 | 0 | my $out = $self->tricked($message); | |||||||||||
| 55 | 0 | ▶ | if(defined $out) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | { | ||||||||||||
| 57 | 0 | $self->write_log($message, $out, 1); | |||||||||||
| 58 | 0 | return $out; | |||||||||||
| 59 | } | ||||||||||||
| 60 | else | ||||||||||||
| 61 | { | ||||||||||||
| 62 | 0 | my $random_range = (($max - $min) / 10) + 1; | |||||||||||
| 63 | 0 | $out = (int(rand($random_range)) * 10) + $min; | |||||||||||
| 64 | 0 | $self->write_log($message, $out, 0); | |||||||||||
| 65 | 0 | return $out; | |||||||||||
| 66 | } | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | sub random_around_zero | ||||||||||||
| 70 | { | ||||||||||||
| 71 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
random_around_zero: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 72 | 0 | my $max = shift; | |||||||||||
| 73 | 0 | ▶ | my $divider = shift || 1; | ||||||||||
|
Condition0%0/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 74 | 0 | my $message = shift; | |||||||||||
| 75 | 0 | my $out = $self->tricked($message); | |||||||||||
| 76 | 0 | ▶ | if(defined $out) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | { | ||||||||||||
| 78 | 0 | $self->write_log($message, $out, 1); | |||||||||||
| 79 | 0 | return $out; | |||||||||||
| 80 | } | ||||||||||||
| 81 | else | ||||||||||||
| 82 | { | ||||||||||||
| 83 | 0 | my $random_max = $max * 2; | |||||||||||
| 84 | 0 | my $r = $self->random(0, $random_max, "Inside dice, from random_around_zero"); | |||||||||||
| 85 | 0 | $r = $r - $max; | |||||||||||
| 86 | 0 | $r = $r / $divider; | |||||||||||
| 87 | 0 | $self->write_log($message, $r, 0); | |||||||||||
| 88 | 0 | return $r; | |||||||||||
| 89 | } | ||||||||||||
| 90 | } | ||||||||||||
| 91 | sub shuffle_array | ||||||||||||
| 92 | { | ||||||||||||
| 93 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
shuffle_array: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||
| 94 | 0 | ▶ | my $message = shift || "NO MESSAGE IN SHUFFLE"; | ||||||||||
|
Condition0%0/2
shift() || 'NO MESSAGE IN SHUFFLE'
MC/DC0%0/1
shift() || 'NO MESSAGE IN SHUFFLE'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 95 | 0 | my @array = @_; | |||||||||||
| 96 | 0 | ▶ | if($message =~ /^Choosing advisor for (.*)$/) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | { | ||||||||||||
| 98 | 0 | my $nation = $1; | |||||||||||
| 99 | 0 | my @array_back; | |||||||||||
| 100 | 0 | my $tricked = 0; | |||||||||||
| 101 | 0 | ▶ | if($self->forced_advisor()) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | { | ||||||||||||
| 103 | 0 | @array_back = ( $self->forced_advisor() ); | |||||||||||
| 104 | 0 | $tricked = 1; | |||||||||||
| 105 | } | ||||||||||||
| 106 | else | ||||||||||||
| 107 | { | ||||||||||||
| 108 | 0 | @array_back = shuffle @array; | |||||||||||
| 109 | } | ||||||||||||
| 110 | 0 | ▶ | if($self->only_one_nation_acting) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | { | ||||||||||||
| 112 | 0 | ▶ | if($self->only_one_nation_acting ne $nation) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | { | ||||||||||||
| 114 | 0 | @array_back = ("Noone"); | |||||||||||
| 115 | 0 | $tricked = 1; | |||||||||||
| 116 | } | ||||||||||||
| 117 | } | ||||||||||||
| 118 | 0 | $self->write_log($message, "<<array>>, first result: " . $array_back[0], $tricked); | |||||||||||
| 119 | return @array_back | ||||||||||||
| 120 | 0 | } | |||||||||||
| 121 | |||||||||||||
| 122 | 0 | ▶ | if(@array == 0) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | { | ||||||||||||
| 124 | 0 | $self->write_log($message, "<<array>>, Array empty"); | |||||||||||
| 125 | 0 | return @array; | |||||||||||
| 126 | } | ||||||||||||
| 127 | 0 | @array = shuffle @array; | |||||||||||
| 128 | 0 | $self->write_log($message, "<<array>>, first result: " . $array[0]); | |||||||||||
| 129 | 0 | return @array; | |||||||||||
| 130 | } | ||||||||||||
| 131 | sub tricked | ||||||||||||
| 132 | { | ||||||||||||
| 133 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
tricked: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 134 | 0 | my $message = shift; | |||||||||||
| 135 | 0 | ▶ | if(exists $self->tricks->{$message}) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | { | ||||||||||||
| 137 | 0 | my $index; | |||||||||||
| 138 | 0 | ▶ | if(exists $self->trick_counters->{$message}) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | { | ||||||||||||
| 140 | 0 | $index = $self->trick_counters->{$message}; | |||||||||||
| 141 | } | ||||||||||||
| 142 | else | ||||||||||||
| 143 | { | ||||||||||||
| 144 | 0 | $index = 0; | |||||||||||
| 145 | } | ||||||||||||
| 146 | 0 | my $result; | |||||||||||
| 147 | 0 | ▶ | if(exists $self->tricks->{$message}->[$index]) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | { | ||||||||||||
| 149 | 0 | $result = $self->tricks->{$message}->[$index]; | |||||||||||
| 150 | } | ||||||||||||
| 151 | else | ||||||||||||
| 152 | { | ||||||||||||
| 153 | 0 | $result = undef; | |||||||||||
| 154 | } | ||||||||||||
| 155 | 0 | $self->trick_counters->{$message} = $index + 1; | |||||||||||
| 156 | 0 | return $result; | |||||||||||
| 157 | } | ||||||||||||
| 158 | else | ||||||||||||
| 159 | { | ||||||||||||
| 160 | 0 | return undef; | |||||||||||
| 161 | } | ||||||||||||
| 162 | } | ||||||||||||
| 163 | |||||||||||||
| 164 | sub write_log | ||||||||||||
| 165 | { | ||||||||||||
| 166 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
write_log: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 167 | 0 | my $message = shift; | |||||||||||
| 168 | 0 | my $result = shift; | |||||||||||
| 169 | 0 | my $tricked = shift; | |||||||||||
| 170 | 0 | ▶ | if($tricked) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | { | ||||||||||||
| 172 | 0 | $message .= " *TRICKED* "; | |||||||||||
| 173 | } | ||||||||||||
| 174 | 0 | $message = "[" . $message . "] $result"; | |||||||||||
| 175 | 0 | $self->log($message); | |||||||||||
| 176 | } | ||||||||||||
| 177 | |||||||||||||
| 178 | 11 | 1; | |||||||||||