DataObjects/Register.pm

filter: Statementstmt 66.4% 119 / 179 Branchbran 51.9% 27 / 52 Conditioncond 37.5% 6 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 76.6% 23 / 30 Podpod 73.3% 11 / 15 totaltotal 61.9% 187 / 302 CC 40 SCAR 27.9
CC
40
Cov
62%
_find_player_idx
90.0
fprint
12.0
insert_player_at_idx
10.0
CRAP 16.2
« DataObjects/Player.pm ↑ Summary DataObjects/Tournament.pm »
1 #===============================================================================
2 #
3 # FILE: Games::Go::AGA::DataObjects::Register.pm
4 #
5 # USAGE: use Games::Go::AGA::DataObjects::Register;
6 #
7 # PODNAME: Games::Go::AGA::DataObjects::Register
8 # ABSTRACT: models AGA register.tde file information
9 #
10 # AUTHOR: Reid Augustin (REID), <reid@lucidport.com>
11 # COMPANY: LucidPort Technology, Inc.
12 # CREATED: 11/19/2010 03:13:05 PM PST
13 #===============================================================================
14  
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 package Games::Go::AGA::DataObjects::Register;
20 1 ▶use parent 'Games::Go::AGA::DataObjects::Directives';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Scalar::Util qw( refaddr looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Games::Go::AGA::Parse::Util qw( normalize_ID );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Games::Go::AGA::Parse::Util qw( Rank_to_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Games::Go::AGA::DataObjects::Player;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Games::Go::AGA::DataObjects::Directives;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 our $VERSION = '0.107'; # VERSION
33  
34 # has 'directives' => (
35 # isa => 'Games::Go::AGA::DataObjects::Directives',
36 # is => 'rw',
37 # default => sub {
38 # Games::Go::AGA::DataObjects::Directives->new();
39 # },
40 # );
41 # has 'comments' => (
42 # isa => 'ArrayRef',
43 # is => 'ro',
44 # default => sub { [] }
45 # );
46 # has 'players' => (
47 # isa => 'ArrayRef',
48 # is => 'ro',
49 # default => sub { [] }
50 # );
51 has 'change_callback' => (
52 isa => 'Maybe[CodeRef]',
53 is => 'rw',
54 1 ▶ default => sub { sub { } }
Subroutine __ANON__: called CC=1 SCAR=0.0
55 1 );
56  
57 sub BUILD {
58 1 ▶ my ($self) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
59 1 $self->{comments} = [];
60 1 $self->{players} = [];
61 }
62  
63 sub changed {
64 23 ▶ my ($self) = @_;
Subroutine changed: called CC=2 SCAR=7.1
Pod undocumented
65  
66 23 ▶ &{$self->change_callback}($self) if ($self->{change_callback});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"change_callback"} 23 0 23
67 }
68  
69 sub comments {
70 0 ▶ my ($self) = @_;
Subroutine comments: not called CC=2 SCAR=17.9
Pod documented
71  
72 0 ▶ return wantarray ? @{$self->{comments}} : $self->{comments};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
73 }
74  
75 sub add_comment {
76 0 ▶ my ($self, $comment) = @_;
Subroutine add_comment: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 0 push @{$self->{comments}}, $comment;
79 }
80  
81 sub players {
82 2 ▶ my ($self) = @_;
Subroutine players: called CC=2 SCAR=6.9
Pod documented
83  
84 2 ▶ return wantarray ? @{$self->{players}} : $self->{players};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
85 }
86  
87 sub id_is_duplicate {
88 21 ▶ my ($self, $id, $player) = @_;
Subroutine id_is_duplicate: called CC=4 SCAR=14.0
Pod documented
89  
90 # normalize the ID first. IDs in Players are already normalised.
91 21 $id = normalize_ID($id);
92 21 my @matched = grep { $id eq $_->id } @{$self->{players}};
93 21 ▶ my $my_refaddr = refaddr $player || 0;
Condition50%1/2
builtin::refaddr($player) || 0
l!l
021
MC/DC0%0/1
builtin::refaddr($player) || 0
builtin::refaddr($player)
Truth table50%1/2
A
A= builtin::refaddr($player)
Aresult
11
00
94 21 foreach my $p (@matched) {
95 2 ▶ if (refaddr $p != $my_refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($p) != $my_refaddr) 2 0 2
96 2 return 1
97 }
98 }
99 19 return 0;
100 }
101  
102 sub insert_player_at_idx {
103 19 ▶ my ($self, $idx, $player) = @_;
Subroutine insert_player_at_idx: called CC=6 SCAR=23.0
Pod documented
104  
105  
106 19 my $id = $player->id;
107 19 ▶ if (not $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 19 19
108 0 $id = 1;
109 0 ▶ $id++ while ($self->id_is_duplicate("TMP$id", undef));
Branch: 1 branch
BranchTrueFalseTotal
$self->id_is_duplicate("TMP$id", undef) and do { ++$id } 0 0 0
110 0 $id = "TMP$id";
111 0 $player->id($id);
112 }
113 19 ▶ if ($self->id_is_duplicate($id, undef)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id_is_duplicate($id, undef)) 1 18 19
114 1 croak "duplicate ID: $id\n";
115 }
116 #print "insert_player ", $player->id, " at $idx\n";
117 18 ▶ if ($idx < 0 or
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0 or $idx > $#{$$self{'players'};}) { } 18 0 18
Condition33%1/3
$idx < 0 or $idx > $#{$$self{"players"};}
l!l&&r!l&&!r
1800
MC/DC0%0/2
$idx < 0 or $idx > $#{$$self{"players"};}
$idx < 0 $idx > $#{$$self{"players"};}
Truth table33%1/3
A or B
A= $idx < 0B= $idx > $#{$$self{"players"};}
ABresult
1X1
011
000
118 0 $idx > $#{$self->{players}}) {
119 18 push (@{$self->{players}}, $player);
120 }
121 else {
122 0 splice (@{$self->{players}}, $idx, 0, $player);
123 }
124 18 $self->changed;
125 }
126  
127 sub get_player_idx {
128 0 ▶ my ($self, $idx) = @_;
Subroutine get_player_idx: not called CC=1 SCAR=6.9
Pod documented
129  
130 0 return $self->_find_player_idx($idx);
131 }
132  
133 sub get_player {
134 0 ▶ my ($self, $idx) = @_;
Subroutine get_player: not called CC=1 SCAR=6.9
Pod documented
135  
136 0 $idx = $self->_find_player_idx($idx);
137 0 return $self->{players}[$idx];
138 }
139  
140 sub delete_player {
141 0 ▶ my ($self, $idx) = @_;
Subroutine delete_player: not called CC=2 SCAR=17.9
Pod documented
142  
143 0 $idx = $self->_find_player_idx($idx);
144  
145 0 my $players = $self->{players};
146 0 ▶ if (@{$players->[$idx]->games}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$players->[$idx]->games;}) 0 0 0
147 0 my $id = $players->[$idx]->id;
148 0 croak "Games recorded for $id, can't delete\n";
149 }
150 0 my $player = splice(@{$players}, $idx, 1); # delete and return it
151 #print "delete ", $player->id, "\n";
152 0 $self->changed;
153 0 return $player;
154 }
155  
156 sub _find_player_idx {
157 0 ▶ my ($self, $idx) = @_;
Subroutine _find_player_idx: not called CC=9 SCAR=45.0
158  
159 0 my $players = $self->{players};
160 0 ▶ if (looks_like_number($idx)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&looks_like_number($idx)) { } 0 0 0
elsif (ref $idx) { } 0 0 0
161 # already what we need
162 }
163 elsif (ref $idx) { # must be a Player dataobject
164 # find Player object with matching refaddr
165 FIND_REFADDR : {
166 0 my $player = $idx;
167 0 my $my_refaddr = refaddr($player);
168 0 for my $ii (0 .. $#{$players}) {
169 0 ▶ if (refaddr($players->[$ii]) == $my_refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($players->[$ii]) == $my_refaddr) 0 0 0
170 0 $idx = $ii;
171 0 last FIND_REFADDR;
172 }
173 }
174 0 my $id = $player->id;
175 0 croak "can't find player at refaddr = $my_refaddr (ID=$id)\n";
176 }
177 }
178 else {
179 # find Player with matching ID
180 FIND_ID : {
181 0 my $id = $idx;
182 0 for my $ii (0 .. $#{$players}) {
183 0 ▶ if ($players->[$ii]->id eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($players->[$ii]->id eq $id) 0 0 0
184 0 $idx = $ii;
185 0 last FIND_ID;
186 }
187 }
188 0 croak "can't find player matching ID $id\n";
189 }
190 }
191 0 ▶ if ($idx < 0 or
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0 or $idx > $#{$players;}) 0 0 0
Condition0%0/3
$idx < 0 or $idx > $#{$players;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx < 0 or $idx > $#{$players;}
$idx < 0 $idx > $#{$players;}
Truth table0%0/3
A or B
A= $idx < 0B= $idx > $#{$players;}
ABresult
1X1
011
000
192 0 $idx > $#{$players}) {
193 0 croak "index=$idx is out of bounds\n";
194 }
195 0 return $idx;
196 }
197  
198 # override some Directives methods so we can intercept
199 # BAND_BREAKS directives (which requires player info)
200 sub get_directive_value {
201 20 ▶ my $self = shift;
Subroutine get_directive_value: called CC=2 SCAR=6.9
Pod documented
202 20 my ($key) = $_[0];
203  
204 20 ▶ if (uc $key eq 'BAND_BREAKS') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $key eq "BAND_BREAKS") 19 1 20
205 19 return $self->break_bands;
206 }
207 1 return $self->SUPER::get_directive_value(@_);
208 }
209  
210 # break players into number of bands in BANDS directives
211 sub break_bands {
212 19 ▶ my ($self) = @_;
Subroutine break_bands: called CC=9 SCAR=22.2
Pod undocumented
213  
214 19 my $band_breaks = $self->SUPER::get_directive_value('BAND_BREAKS');
215 19 ▶ return $band_breaks if ($band_breaks);
Branch: 1 branch
BranchTrueFalseTotal
if $band_breaks 18 1 19
216  
217 1 my $num_bands = $self->get_directive_value('BANDS');
218 1 ▶ return if (not $num_bands # no BANDS directive and not BAND_BREAKS
Branch: 1 branch
BranchTrueFalseTotal
if not $num_bands or $num_bands == 1 0 1 1
Condition33%1/3
not $num_bands or $num_bands == 1
l!l&&r!l&&!r
001
MC/DC0%0/2
not $num_bands or $num_bands == 1
not $num_bands $num_bands == 1
Truth table33%1/3
not(A) or B
A= not $num_bandsB= $num_bands == 1
ABresult
1X1
011
000
219 or $num_bands == 1); # everyone is in same band
220  
221 1 my @players = $self->players;
222 1 my %entrants_per_rating;
223 1 for my $player (@players) {
224 18 $entrants_per_rating{int $player->rating}++; # count entrants of each rating
225 }
226 1 my @sorted_ranks = sort {$b <=> $a} keys %entrants_per_rating;
227 1 my $running_total = 0;
228 1 my $ii = 0;
229 1 my @band_breaks;
230 1 for my $band (1 .. $num_bands - 1) {
231 2 my $next_break = $band * (@players / $num_bands);
232 2 ▶ while ($running_total + $entrants_per_rating{$sorted_ranks[$ii]} < $next_break) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running_total + $entrants_per_rating{$sorted_ranks[$ii]} < $next_break) 3 2 5
233 3 $running_total += $entrants_per_rating{$sorted_ranks[$ii++]};
234 }
235 2 ▶ if (($running_total + ($entrants_per_rating{$sorted_ranks[$ii]} / 2) < $next_break)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running_total + $entrants_per_rating{$sorted_ranks[$ii]} / 2 < $next_break) 2 0 2
236 2 $running_total += $entrants_per_rating{$sorted_ranks[$ii++]};
237 }
238 #print "$running_total to band $band=$sorted_ranks[$ii - 1]\n";
239 2 push(@band_breaks, $sorted_ranks[$ii - 1]);
240 2 ▶ last if ($ii >= @sorted_ranks);
Branch: 1 branch
BranchTrueFalseTotal
if $ii >= @sorted_ranks 0 2 2
241 }
242 1 $band_breaks = join ' ', @band_breaks;
243 1 $self->set_directive_value('BAND_BREAKS', $band_breaks);
244 1 return $band_breaks;
245 }
246  
247 sub bands {
248 18 ▶ my ($self) = @_;
Subroutine bands: called CC=3 SCAR=11.7
Pod documented
249  
250 18 ▶ my @breaks = split /[^\d\.\-]+/, ($self->get_directive_value('BAND_BREAKS') || '');
Condition50%1/2
$self->get_directive_value('BAND_BREAKS') || ''
l!l
180
MC/DC0%0/1
$self->get_directive_value('BAND_BREAKS') || ''
$self->get_directive_value('BAND_BREAKS')
Truth table50%1/2
A
A= $self->get_directive_value('BAND_BREAKS')
Aresult
11
00
251 return wantarray
252 ? @breaks
253 18 ▶ : \@breaks;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 18 18
254 }
255  
256 sub which_band_is {
257 18 ▶ my ($self, $rating) = @_;
Subroutine which_band_is: called CC=7 SCAR=19.5
Pod documented
258  
259 18 my $bands = $self->bands;
260 18 ▶ return 0 if (@{$bands} < 1); # one or no bands defined, all players are in band 0
Branch: 1 branch
BranchTrueFalseTotal
if @{$bands;} < 1 0 18 18
261  
262 18 $rating = Rank_to_Rating($rating); # make sure it's numeric
263 18 my $idx;
264 18 ▶ for ($idx = 0; $idx < @{$bands}; $idx++) { # go past the end
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @{$bands;}) 30 5 35
265 30 my $limit = $bands->[$idx];
266 30 ▶ if ($limit < 0 and
Branch: 1 branch
BranchTrueFalseTotal
if ($limit < 0 and $limit == int $limit) { } 12 18 30
Condition66%2/3
$limit < 0 and $limit == int $limit
!ll&&!rl&&r
18012
MC/DC50%1/2
$limit < 0 and $limit == int $limit
$limit < 0 $limit == int $limit
Truth table66%2/3
A and B
A= $limit < 0B= $limit == int $limit
ABresult
0X0
100
111
267 $limit == int $limit) { # -2.0 means entire 2K range down to -2.9999,
268 12 $limit = $limit - 1; # but -2.5 really means -2.5.
269 12 ▶ last if ($rating > $limit); # dan: -3.0 is a 3 kyu
Branch: 1 branch
BranchTrueFalseTotal
if $rating > $limit 7 5 12
270 }
271 else {
272 18 ▶ last if ($rating >= $limit); # dan: 3.0 is a 3 dan
Branch: 1 branch
BranchTrueFalseTotal
if $rating >= $limit 6 12 18
273 }
274 }
275 18 return $idx;
276 }
277  
278 sub fprint {
279 0 ▶ my ($self, $fh) = @_;
Subroutine fprint: not called CC=3 SCAR=24.8
Pod undocumented
280  
281 0 $self->SUPER::fprint($fh); # print the directives
282 0 foreach my $comment ($self->comments) {
283 0 $fh->print("#$comment\n");
284 }
285 0 foreach my $player ($self->players) {
286 0 $player->fprint_register($fh);
287 }
288 }
289  
290 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
291 1 __PACKAGE__->meta->make_immutable;
292  
293 1 1;
294  
295 __END__
« DataObjects/Player.pm ↑ Summary DataObjects/Tournament.pm »