DataObjects/Player.pm

filter: Statementstmt 45.0% 96 / 213 Branchbran 16.6% 13 / 78 Conditioncond 13.0% 3 / 23 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 54.7% 23 / 42 Podpod 77.2% 17 / 22 totaltotal 39.2% 153 / 390 CC 54 SCAR 30.1
CC
54
Cov
36%
delete_game
71.2
adj_rating
42.0
no_result
20.0
CRAP 20.3
« DataObjects/Round.pm ↑ Summary DataObjects/Register.pm »
1 #===============================================================================
2 #
3 # FILE: Games::Go::AGA::DataObjects::Player.pm
4 # PODNAME: Games::Go::AGA::DataObjects::Player
5 # ABSTRACT: model an AGA player
6 #
7 # AUTHOR: Reid Augustin (REID), <reid@lucidport.com>
8 # COMPANY: LucidPort Technology, Inc.
9 # CREATED: 11/19/2010 03:13:05 PM PST
10 #===============================================================================
11  
12  
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Games::Go::AGA::DataObjects::Player;
17  
18 4 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Carp qw{ cluck carp croak };
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Scalar::Util qw( refaddr looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Games::Go::AGA::Parse::Util qw( normalize_ID Rank_to_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Games::Go::AGA::DataObjects::Types qw( is_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 our $VERSION = '0.107'; # VERSION
25  
26 has id => (
27 is => 'rw',
28 isa => 'ID',
29 trigger => sub {
30 # TODO: make this a coercion?
31 29 ▶ my ($self, $new) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
32 29 $self->{id} = normalize_ID($new);
33 29 $self->changed;
34 },
35 4 );
36 has last_name => (
37 is => 'rw',
38 isa => 'Str',
39 25 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 4 );
41 has first_name => (
42 is => 'rw',
43 isa => 'Str',
44 25 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 4 );
46 has rank => (
47 is => 'rw',
48 isa => 'Rank_or_Rating',
49 trigger => sub {
50 26 ▶ my ($self, $new) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
51 26 ▶ if (is_Rating($new)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_Rating($new)) 16 10 26
52 16 $self->{rank} += 0;
53 }
54 26 shift->changed;
55 },
56 4 );
57 has date => (
58 is => 'rw',
59 isa => 'Str',
60 default => '',
61 0 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 4 );
63 has membership => (
64 is => 'rw',
65 isa => 'Str',
66 default => '',
67 0 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 4 );
69 has state => (
70 is => 'rw',
71 isa => 'Str',
72 default => '',
73 0 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 4 );
75 has club => (
76 is => 'rw',
77 isa => 'Str',
78 default => '',
79 trigger => sub {
80 18 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 18 $self->{club} = uc $self->{club};
82 18 $self->changed;
83 },
84 4 );
85 4 has flags => (
86 is => 'rw',
87 isa => 'ArrayRef',
88 );
89 has comment => (
90 is => 'rw',
91 isa => 'Str',
92 default => '',
93 21 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 4 );
95 has sigma => (
96 is => 'rw',
97 isa => 'Num',
98 0 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 4 );
100 has games => (
101 is => 'rw',
102 isa => 'ArrayRef',
103 25 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 # trigger => sub { shift->changed; },
105 4 );
106 has change_callback => (
107 is => 'rw',
108 isa => 'Maybe[CodeRef]',
109 25 ▶ default => sub { sub { } }
Subroutine __ANON__: called CC=1 SCAR=0.0
110 4 );
111  
112 sub BUILD {
113 25 ▶ my ($self) = @_;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod documented
114 25 ▶ $self->{flags} ||= []; # empty array
Condition100%2/2
$self->{"flags"} ||= []
l!l
232
MC/DC100%1/1
$self->{"flags"} ||= []
$self->{"flags"}
Truth table100%2/2
A
A= $self->{"flags"}
Aresult
11
00
115 }
116  
117 sub changed {
118 144 ▶ my ($self) = @_;
Subroutine changed: called CC=2 SCAR=7.1
Pod undocumented
119  
120 144 ▶ &{$self->change_callback}($self) if ($self->{change_callback});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"change_callback"} 144 0 144
121 }
122  
123 sub full_name {
124 0 ▶ my ($self) = @_;
Subroutine full_name: not called CC=2 SCAR=17.9
Pod undocumented
125  
126 0 ▶ if (my $name = $self->first_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $self->first_name) 0 0 0
127 0 return join(', ', $self->last_name, $name),
128 }
129 # some players don't have first name
130 0 return $self->last_name;
131 }
132  
133  
134 # sub comment {
135 # my $self = shift;
136 # if (@_) {
137 # if (defined $_[0]) {
138 # $self->{comment} = join '', @_;
139 # }
140 # else {
141 # delete $self->{comment};
142 # }
143 # }
144 # return $self->{comment};
145 # }
146  
147 sub add_game {
148 11 ▶ my ($self, $game, $idx) = @_;
Subroutine add_game: called CC=3 SCAR=11.3
Pod documented
149  
150 11 my $add_refaddr = refaddr $game; # ID of game to add
151 11 ▶ if(not grep { (refaddr $_) == $add_refaddr } @{$self->{games}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({builtin::refaddr($_) == $add_refaddr;} @{$$self{"games"};})) 7 4 11
152 #print STDERR "add_game $game to " . $self->full_name . "\n";
153 7 ▶ if (@_ < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) { } 7 0 7
154 7 push @{$self->{games}}, $game;
155 }
156 else {
157 0 splice @{$self->{games}}, $idx, 0, $game;
158 }
159 #$self->changed;
160 }
161 }
162  
163 sub delete_game {
164 1 ▶ my ($self, $idx, $id1) = @_;
Subroutine delete_game: called CC=15 SCAR=42.6
Pod documented
165  
166 1 my $games = $self->{games};
167 1 ▶ if (not looks_like_number($idx)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($idx)) 1 0 1
168 1 ▶ if (ref $idx) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $idx) { } 1 0 1
elsif ($id1) { } 0 0 0
169 # $idx is actually a Game object, find its idx in our games
170 # list
171 1 my $game = $idx;
172 1 my $refaddr = refaddr($game);
173 1 for my $ii (0 .. $#{$games}) {
174 2 ▶ if (refaddr($games->[$ii]) == $refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($games->[$ii]) == $refaddr) 1 1 2
175 1 $idx = $ii;
176 1 last;
177 }
178 }
179 }
180 elsif ($id1) {
181 0 my $id0 = $idx; # idx is actually ID_0
182 0 for my $ii (0 .. $#{$games}) {
183 0 ▶ if (($games->[$ii]->black->id eq $id0 and
Branch: 1 branch
BranchTrueFalseTotal
if ($games->[$ii]->black->id eq $id0 and $games->[$ii]->white->id eq $id1 or $games->[$ii]->black->id eq $id1 and $games->[$ii]->white->id eq $id0) 0 0 0
Condition0%0/9
$games->[$ii]->black->id eq $id0 and $games->[$ii]->white->id eq $id1 or $games->[$ii]->black->id eq $id1 and $games->[$ii]->white->id eq $id0
l!l&&r!l&&!r
000
$games->[$ii]->black->id eq $id0 and $games->[$ii]->white->id eq $id1
!ll&&!rl&&r
000
$games->[$ii]->black->id eq $id1 and $games->[$ii]->white->id eq $id0
!ll&&!rl&&r
000
MC/DC0%0/4
$games->[$ii]->black->id eq $id0 and $games->[$ii]->white->id eq $id1 or $games->[$ii]->black->id eq $id1 and $games->[$ii]->white->id eq $id0
$games->[$ii]->black->id eq $id0 $games->[$ii]->white->id eq $id1 $games->[$ii]->black->id eq $id1 $games->[$ii]->white->id eq $id0
Truth table0%0/7
A and B or C and D
A= $games->[$ii]->black->id eq $id0B= $games->[$ii]->white->id eq $id1C= $games->[$ii]->black->id eq $id1D= $games->[$ii]->white->id eq $id0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
184 $games->[$ii]->white->id eq $id1) or
185 ($games->[$ii]->black->id eq $id1 and
186 $games->[$ii]->white->id eq $id0)) {
187 0 $idx = $ii;
188 0 last;
189 }
190 }
191 }
192 }
193 1 ▶ if (not looks_like_number($idx)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($idx)) 0 1 1
194 0 ▶ if (ref $idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $idx) { } 0 0 0
195 0 croak(sprintf "Can't find game %s vs %s in games list for %s\n",
196 $idx->white->last_name,
197 $idx->black->last_name,
198 $self->id,
199 );
200 }
201 else {
202 0 ▶ $idx ||= '<>';
Condition0%0/2
$idx ||= "<>"
l!l
00
MC/DC0%0/1
$idx ||= "<>"
$idx
Truth table0%0/2
A
A= $idx
Aresult
11
00
203 0 ▶ $id1 ||= '<>';
Condition0%0/2
$id1 ||= "<>"
l!l
00
MC/DC0%0/1
$id1 ||= "<>"
$id1
Truth table0%0/2
A
A= $id1
Aresult
11
00
204 0 my $id = $self->id;
205 0 croak("Can't find game $idx vs $id1 in games list for $id\n");
206 }
207 }
208 1 splice(@{$games}, $idx, 1); # remove from list
209 #$self->changed; # not recorded in any file, so ignore
210 1 return $idx;
211 }
212  
213 sub adj_rating {
214 0 ▶ my ($self, $round_num, $new) = @_;
Subroutine adj_rating: not called CC=6 SCAR=37.4
Pod documented
215  
216 0 ▶ $self->{adj_rating} = [] if (not $self->{adj_rating});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"adj_rating"} 0 0 0
217 0 ▶ if (defined $new) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new) 0 0 0
218 0 ▶ croak "round number not defined" if (not defined $round_num);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $round_num 0 0 0
219 0 $self->{adj_rating}[$round_num] = $new;
220 }
221 0 ▶ if (defined $round_num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $round_num) 0 0 0
222 0 return $self->{adj_rating}[$round_num];
223 }
224 0 ▶ return $self->{adj_rating}[-1] || $self->rating; # rating if no adjusted rating has been set
Condition0%0/3
$self->{'adj_rating'}[-1] || $self->rating
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'adj_rating'}[-1] || $self->rating
$self->{'adj_rating'}[-1] $self->rating
Truth table0%0/3
A || B
A= $self->{'adj_rating'}[-1]B= $self->rating
ABresult
1X1
011
000
225 }
226  
227 sub handicap_rating {
228 0 ▶ my ($self, $new) = @_;
Subroutine handicap_rating: not called CC=3 SCAR=24.8
Pod documented
229  
230 0 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) 0 0 0
231 0 $self->{handicap_rating} = $new;
232 }
233 return defined $self->{handicap_rating}
234 ? $self->{handicap_rating}
235 0 ▶ : $self->rating;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'handicap_rating'} ? : 0 0 0
236 }
237  
238 sub opponents {
239 0 ▶ my ($self) = @_;
Subroutine opponents: not called CC=2 SCAR=17.9
Pod undocumented
240  
241 0 my @opps = map { $_->opponent($self) } @{$self->{games}};
242 return wantarray ? @opps
243 0 ▶ : scalar @opps;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
244 }
245  
246 sub defeated {
247 0 ▶ my ($self) = @_;
Subroutine defeated: not called CC=3 SCAR=24.8
Pod documented
248  
249 0 my $me = refaddr $self;
250 0 my @defeated = map { $_->loser }
251 0 ▶ grep { defined $_->winner and
Branch: 1 branch
BranchTrueFalseTotal
defined $_->winner and builtin::refaddr($_->winner) == $me 0 0 0
252 0 (refaddr($_->winner) == $me) } @{$self->{games}};
253 return wantarray ? @defeated
254 0 ▶ : scalar @defeated;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
255 }
256  
257 sub defeated_by {
258 0 ▶ my ($self) = @_;
Subroutine defeated_by: not called CC=3 SCAR=24.8
Pod documented
259  
260 0 my $me = refaddr $self;
261 0 my @defeated_by = map { $_->winner }
262 0 ▶ grep { defined $_->loser and
Branch: 1 branch
BranchTrueFalseTotal
defined $_->loser and builtin::refaddr($_->loser) == $me 0 0 0
263 0 (refaddr($_->loser) == $me) } @{$self->{games}};
264 return wantarray ? @defeated_by
265 0 ▶ : scalar @defeated_by;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
266 }
267  
268 # games with no result (usually means still playing)
269 sub no_result {
270 0 ▶ my ($self) = @_;
Subroutine no_result: not called CC=4 SCAR=30.0
Pod undocumented
271  
272 0 my $me = refaddr $self;
273 0 ▶ my @no_result = map { (refaddr($_->black) == $me) ? $_->white : $_->black }
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($_->black) == $me ? : 0 0 0
274 0 ▶ grep { not defined $_->loser and
Condition0%0/3
defined $_->loser || defined $_->winner
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $_->loser || defined $_->winner
defined $_->loser defined $_->winner
Truth table0%0/3
A || B
A= defined $_->loserB= defined $_->winner
ABresult
1X1
011
000
275 0 not defined $_->winner } @{$self->{games}};
276 return wantarray ? @no_result
277 0 ▶ : scalar @no_result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
278 }
279  
280 sub wins {
281 6 ▶ my ($self) = @_;
Subroutine wins: called CC=3 SCAR=11.2
Pod documented
282  
283 6 my $me = refaddr $self;
284 6 ▶ my @wins = grep { (refaddr($_->winner) || -1) == $me} @{$self->{games}};
Condition50%1/2
builtin::refaddr($_->winner) || -1
l!l
60
MC/DC0%0/1
builtin::refaddr($_->winner) || -1
builtin::refaddr($_->winner)
Truth table50%1/2
A
A= builtin::refaddr($_->winner)
Aresult
11
00
285 return wantarray ? @wins
286 6 ▶ : scalar @wins;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
287 }
288  
289 sub losses {
290 1 ▶ my ($self) = @_;
Subroutine losses: called CC=3 SCAR=12.7
Pod documented
291  
292 1 my $me = refaddr $self;
293 0 ▶ my @losses = grep {defined $_->winner and
Branch: 1 branch
BranchTrueFalseTotal
defined $_->winner and builtin::refaddr($_->winner) != $me 0 0 0
294 1 refaddr $_->winner != $me} @{$self->{games}};
295 return wantarray ? @losses
296 1 ▶ : scalar @losses;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
297 }
298  
299 sub completed_games {
300 0 ▶ my ($self) = @_;
Subroutine completed_games: not called CC=2 SCAR=17.9
Pod undocumented
301  
302 0 my @games = grep { $_->winner } @{$self->{games}};
303 return wantarray ? @games
304 0 ▶ : scalar @games;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
305 }
306  
307 sub drop {
308 0 ▶ my ($self, $round_num) = @_;
Subroutine drop: not called CC=4 SCAR=30.0
Pod documented
309  
310 0 ▶ return 1 if ($self->get_flag('drop'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_flag("drop") 0 0 0
311 0 ▶ if (defined $round_num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $round_num) 0 0 0
312 0 ▶ return 1 if ($self->get_flag("drop$round_num"));
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_flag("drop$round_num") 0 0 0
313 }
314 0 return 0;
315 }
316  
317 sub bye {
318 0 ▶ my ($self) = @_;
Subroutine bye: not called CC=1 SCAR=6.9
Pod documented
319  
320 0 return $self->get_flag('bye');
321 }
322  
323 sub get_flag {
324 0 ▶ my ($self, $key) = @_;
Subroutine get_flag: not called CC=3 SCAR=24.8
Pod documented
325  
326 0 my $flags = $self->{flags};
327 0 my $ii = 0;
328 0 for (@{$flags}) {
329 0 ▶ last if (uc $key eq uc $flags->[$ii]);
Branch: 1 branch
BranchTrueFalseTotal
if uc $key eq uc $flags->[$ii] 0 0 0
330 0 $ii++;
331 }
332 0 return $flags->[$ii];
333 }
334  
335 sub set_flag {
336 0 ▶ my ($self, $key) = @_;
Subroutine set_flag: not called CC=3 SCAR=24.8
Pod documented
337  
338 0 my $flags = $self->{flags};
339 0 my $ii = 0;
340 0 for (@{$flags}) {
341 0 ▶ last if (uc $key eq uc $flags->[$ii]);
Branch: 1 branch
BranchTrueFalseTotal
if uc $key eq uc $flags->[$ii] 0 0 0
342 0 $ii++;
343 }
344 0 $flags->[$ii] = $key; # add if not there, overwrite if it is
345 0 $self->changed;
346 0 return $flags->[$ii];
347 }
348  
349 sub clear_flag {
350 0 ▶ my ($self, $key) = @_;
Subroutine clear_flag: not called CC=4 SCAR=30.0
Pod documented
351  
352 0 my $flags = $self->{flags};
353 0 my $ii = 0;
354 0 for (@{$flags}) {
355 0 ▶ last if (uc $key eq uc $flags->[$ii]);
Branch: 1 branch
BranchTrueFalseTotal
if uc $key eq uc $flags->[$ii] 0 0 0
356 0 $ii++;
357 }
358 0 ▶ if ($ii < @{$flags}) { # if element was found
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < @{$flags;}) 0 0 0
359 0 splice @{$flags}, $ii, 1; # remove it
360 0 $self->changed;
361 }
362 }
363  
364 sub rating {
365 36 ▶ my ($self) = @_;
Subroutine rating: called CC=1 SCAR=0.0
Pod documented
366  
367 36 return Rank_to_Rating($self->rank);
368 }
369  
370 # in register.tde format
371 sub fprint_register {
372 0 ▶ my ($self, $fh) = @_;
Subroutine fprint_register: not called CC=4 SCAR=30.0
Pod documented
373  
374 0 my @flags = @{$self->{flags}};
375 0 ▶ push(@flags, "Club=" . $self->club) if ($self->club);
Branch: 1 branch
BranchTrueFalseTotal
if $self->club 0 0 0
376  
377 0 $fh->printf("%s %s, %s %s",
378 $self->id,
379 $self->last_name,
380 $self->first_name,
381 $self->rank,
382 );
383 0 ▶ $fh->printf(" %s", join(' ', @flags)) if (@flags);
Branch: 1 branch
BranchTrueFalseTotal
if @flags 0 0 0
384 0 my $comment = $self->comment;
385 0 ▶ if ($comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 0 0 0
386 0 $comment =~ s/\n/\\n/g; # prevent newlines which would mess things up
387 0 $comment =~ s/^\s*(.*?)\s*$/$1/; # remove preceding and trailing whitespace
388 0 $fh->print(" # $comment");
389 }
390 0 $fh->print("\n");
391 }
392  
393 # in tdlist format
394 sub fprint_tdlist {
395 0 ▶ my ($self, $fh) = @_;
Subroutine fprint_tdlist: not called CC=1 SCAR=6.9
Pod documented
396  
397 0 $fh->printf("%s, %s %s %s %s %s %s %s\n",
398 $self->last_name,
399 $self->first_name,
400 $self->id,
401 $self->membership,
402 $self->rank,
403 $self->date,
404 $self->club,
405 $self->state,
406 );
407 }
408  
409 4 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
410 4 __PACKAGE__->meta->make_immutable;
411  
412 4 1;
413  
414 __END__
« DataObjects/Round.pm ↑ Summary DataObjects/Register.pm »