lib/BalanceOfPower/Dice.pm

filter: Statementstmt 98.0% 101 / 103 Branchbran 95.8% 23 / 24 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 89.1% 140 / 157 CC 17 SCAR 13.0
CC
17
Cov
95%
shuffle_array
7.0
tricked
4.0
random_around_zero
3.1
CRAP 3.7
« lib/BalanceOfPower/Relations/Influence.pm ↑ Summary lib/BalanceOfPower/Relations/Friendship.pm »
1 package BalanceOfPower::Dice;
2 11 $BalanceOfPower::Dice::VERSION = '0.200000';
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 14 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
13 11 );
14 has trick_counters => (
15 is => 'rw',
16 14 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
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 701 ▶ my $self = shift;
Subroutine random: called CC=3 SCAR=11.0
Pod undocumented
30 701 my $min = shift;
31 701 my $max = shift;
32 701 ▶ my $message = shift || "NO MESSAGE [$min-$max]";
Condition50%1/2
shift() || "NO MESSAGE [$min-$max]"
l!l
7010
MC/DC0%0/1
shift() || "NO MESSAGE [$min-$max]"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
33 701 my $out = $self->tricked($message);
34 701 ▶ if(defined $out)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 82 619 701
35 {
36 82 $self->write_log($message, $out, 1);
37 82 return $out;
38 }
39 else
40 {
41 619 my $random_range = $max - $min + 1;
42 619 $out = int(rand($random_range)) + $min;
43 619 $self->write_log($message, $out, 0);
44 619 return $out;
45 }
46 }
47  
48 sub random10
49 {
50 848 ▶ my $self = shift;
Subroutine random10: called CC=3 SCAR=11.0
Pod undocumented
51 848 my $min = shift;
52 848 my $max = shift;
53 848 ▶ my $message = shift || "NO MESSAGE [$min-$max]";
Condition50%1/2
shift() || "NO MESSAGE [$min-$max]"
l!l
8480
MC/DC0%0/1
shift() || "NO MESSAGE [$min-$max]"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
54 848 my $out = $self->tricked($message);
55 848 ▶ if(defined $out)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 49 799 848
56 {
57 49 $self->write_log($message, $out, 1);
58 49 return $out;
59 }
60 else
61 {
62 799 my $random_range = (($max - $min) / 10) + 1;
63 799 $out = (int(rand($random_range)) * 10) + $min;
64 799 $self->write_log($message, $out, 0);
65 799 return $out;
66 }
67 }
68  
69 sub random_around_zero
70 {
71 159 ▶ my $self = shift;
Subroutine random_around_zero: called CC=3 SCAR=11.3
Pod undocumented
72 159 my $max = shift;
73 159 ▶ my $divider = shift || 1;
Condition50%1/2
shift() || 1
l!l
1590
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
74 159 my $message = shift;
75 159 my $out = $self->tricked($message);
76 159 ▶ if(defined $out)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 159 159
77 {
78 0 $self->write_log($message, $out, 1);
79 0 return $out;
80 }
81 else
82 {
83 159 my $random_max = $max * 2;
84 159 my $r = $self->random(0, $random_max, "Inside dice, from random_around_zero");
85 159 $r = $r - $max;
86 159 $r = $r / $divider;
87 159 $self->write_log($message, $r, 0);
88 159 return $r;
89 }
90 }
91 sub shuffle_array
92 {
93 600 ▶ my $self = shift;
Subroutine shuffle_array: called CC=7 SCAR=19.5
Pod undocumented
94 600 ▶ my $message = shift || "NO MESSAGE IN SHUFFLE";
Condition50%1/2
shift() || 'NO MESSAGE IN SHUFFLE'
l!l
6000
MC/DC0%0/1
shift() || 'NO MESSAGE IN SHUFFLE'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
95 600 my @array = @_;
96 600 ▶ if($message =~ /^Choosing advisor for (.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /^Choosing advisor for (.*)$/) 64 536 600
97 {
98 64 my $nation = $1;
99 64 my @array_back;
100 64 my $tricked = 0;
101 64 ▶ if($self->forced_advisor())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->forced_advisor) { } 29 35 64
102 {
103 29 @array_back = ( $self->forced_advisor() );
104 29 $tricked = 1;
105 }
106 else
107 {
108 35 @array_back = shuffle @array;
109 }
110 64 ▶ if($self->only_one_nation_acting)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->only_one_nation_acting) 5 59 64
111 {
112 5 ▶ if($self->only_one_nation_acting ne $nation)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->only_one_nation_acting ne $nation) 4 1 5
113 {
114 4 @array_back = ("Noone");
115 4 $tricked = 1;
116 }
117 }
118 64 $self->write_log($message, "<<array>>, first result: " . $array_back[0], $tricked);
119 return @array_back
120 64 }
121  
122 536 ▶ if(@array == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@array == 0) 356 180 536
123 {
124 356 $self->write_log($message, "<<array>>, Array empty");
125 356 return @array;
126 }
127 180 @array = shuffle @array;
128 180 $self->write_log($message, "<<array>>, first result: " . $array[0]);
129 180 return @array;
130 }
131 sub tricked
132 {
133 1708 ▶ my $self = shift;
Subroutine tricked: called CC=4 SCAR=13.9
Pod undocumented
134 1708 my $message = shift;
135 1708 ▶ if(exists $self->tricks->{$message})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->tricks->{$message}) { } 137 1571 1708
136 {
137 137 my $index;
138 137 ▶ if(exists $self->trick_counters->{$message})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->trick_counters->{$message}) { } 68 69 137
139 {
140 68 $index = $self->trick_counters->{$message};
141 }
142 else
143 {
144 69 $index = 0;
145 }
146 137 my $result;
147 137 ▶ if(exists $self->tricks->{$message}->[$index])
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->tricks->{$message}[$index]) { } 131 6 137
148 {
149 131 $result = $self->tricks->{$message}->[$index];
150 }
151 else
152 {
153 6 $result = undef;
154 }
155 137 $self->trick_counters->{$message} = $index + 1;
156 137 return $result;
157 }
158 else
159 {
160 1571 return undef;
161 }
162 }
163  
164 sub write_log
165 {
166 2308 ▶ my $self = shift;
Subroutine write_log: called CC=2 SCAR=6.9
Pod undocumented
167 2308 my $message = shift;
168 2308 my $result = shift;
169 2308 my $tricked = shift;
170 2308 ▶ if($tricked)
Branch: 1 branch
BranchTrueFalseTotal
if ($tricked) 160 2148 2308
171 {
172 160 $message .= " *TRICKED* ";
173 }
174 2308 $message = "[" . $message . "] $result";
175 2308 $self->log($message);
176 }
177  
178 11 1;
« lib/BalanceOfPower/Relations/Influence.pm ↑ Summary lib/BalanceOfPower/Relations/Friendship.pm »