blib/lib/Games/Tournament/Contestant.pm

filter: Statementstmt 49.1% 83 / 169 Branchbran 48.2% 28 / 58 Conditioncond 40.0% 10 / 25 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 68.0% 17 / 25 Podpod 100% 16 / 16 totaltotal 51.6% 159 / 308 CC 49 SCAR 26.1
CC
49
Cov
48%
clone
56.0
writeCard
30.0
met
20.0
CRAP 13.6
« blib/lib/Games/Tournament/Swiss.pm ↑ Summary blib/lib/Games/Tournament/Swiss/Bracket.pm »
1 package Games::Tournament::Contestant;
2 33 $Games::Tournament::Contestant::VERSION = '0.21';
3 # Last Edit: 2016 Jan 01, 13:44:39
4 # $Id: $
5  
6 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶use base qw/Games::Tournament/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 33 ▶use List::MoreUtils qw/all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 ▶use constant ROLES => @Games::Tournament::Swiss::Config::roles?
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@Games::Tournament::Swiss::Config::roles ? : 29 4 33
14 @Games::Tournament::Swiss::Config::roles:
15 33 Games::Tournament::Swiss::Config->roles;
16 33 ▶use constant SCORES => %Games::Tournament::Swiss::Config::scores?
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
%Games::Tournament::Swiss::Config::scores ? : 24 9 33
17 %Games::Tournament::Swiss::Config::scores:
18 33 Games::Tournament::Swiss::Config->scores;
19  
20 # use overload qw/0+/ => 'id', qw/""/ => 'name', fallback => 1;
21  
22 =head1 NAME
23  
24 Games::Tournament::Contestant A competitor matched with others over a series of rounds
25  
26 =cut
27  
28 =head1 SYNOPSIS
29  
30 my $foo = Games::Tournament::Contestant->new( rating => '15', name => 'Your New Knicks' );
31 ...
32  
33 =head1 DESCRIPTION
34  
35 A generic tournament/series player/team contestant object.
36  
37 =head1 METHODS
38  
39 =head2 new
40  
41 $team = Games::Tournament::Contestant->new( id => '15', name => 'Lala Lakers', rating => 0, score => 1000, )
42 $grandmaster = Games::Tournament::Contestant->new( name => 'Jose Raul Capablanca', rating => 1000 )
43  
44 Make sure the ids of all your contestants are unique.
45  
46 =cut
47  
48 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
49 0 my $self = shift;
50 0 my %args = @_;
51 0 return bless \%args, $self;
52 }
53  
54  
55 =head2 clone
56  
57 $monster = $alekhine->clone( score => 1000, reputation => 'bad' )
58  
59 Creates a similar object to $alekhine, with the same id, name, score, title, and rating fields but with any other changes or additions you want to make.
60  
61 =cut
62  
63 0 ▶sub clone {
Subroutine clone: not called CC=7 SCAR=40.3
Pod documented
64 0 my $self = shift;
65 0 my %args = @_;
66 0 ▶ my $clone = Games::Tournament::Contestant->new(
Condition0%0/10
$self->id || undef
l!l
00
$self->name || undef
l!l
00
$self->score || undef
l!l
00
$self->title || undef
l!l
00
$self->rating || undef
l!l
00
MC/DC0%0/1
$self->id || undef
$self->id
MC/DC0%0/1
$self->name || undef
$self->name
MC/DC0%0/1
$self->score || undef
$self->score
MC/DC0%0/1
$self->title || undef
$self->title
MC/DC0%0/1
$self->rating || undef
$self->rating
Truth table0%0/2
A
A= $self->id
Aresult
11
00
Truth table0%0/2
A
A= $self->name
Aresult
11
00
Truth table0%0/2
A
A= $self->score
Aresult
11
00
Truth table0%0/2
A
A= $self->title
Aresult
11
00
Truth table0%0/2
A
A= $self->rating
Aresult
11
00
67 id => $self->id || undef,
68 name => $self->name || undef,
69 score => $self->score || undef,
70 title => $self->title || undef,
71 rating => $self->rating || undef,
72 );
73 0 foreach my $key ( keys %args ) {
74 0 $clone->{$key} = $args{$key};
75 }
76 0 return $clone;
77 }
78  
79  
80 =head2 findCard
81  
82 @venues = $player->findCard(@games);
83  
84 Returns a/the first game in @games in which $player is a contestant. 'findCard' expects the game objects to have 'contestants' accessors and be 'canonize'able. The players are grepped for stringwise id equality.
85  
86 =cut
87  
88 0 ▶sub findCard {
Subroutine findCard: not called CC=3 SCAR=24.8
Pod documented
89 0 my $self = shift;
90 0 my $id = $self->id;
91 0 my @games = @_;
92 0 my @cards;
93 0 foreach my $game (@games) {
94 0 $game->canonize;
95 0 my $players = $game->contestants;
96 push @cards, $game
97 0 ▶ if grep { $players->{$_}->id eq $id } keys %$players;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$players->{$_}->id eq $id;} keys %$players 0 0 0
98 }
99 0 return $cards[0];
100 }
101  
102  
103 =head2 myOpponent
104  
105 $opponent = $player->myOpponent($game);
106  
107 Returns a/the opponent in $game of $player. 'myOpponent' expects the game object to have 'contestants' accessors. The players are grepped for stringwise id equality.
108  
109 =cut
110  
111 588 ▶sub myOpponent {
Subroutine myOpponent: called CC=6 SCAR=18.3
Pod documented
112 588 my $self = shift;
113 588 my $id = $self->id;
114 588 my $game = shift;
115 588 ▶ croak "Looking for opponent, but no contestants in $game game" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $game and $game->can("contestants") 0 588 588
Condition33%1/3
$game and $game->can("contestants")
!ll&&!rl&&r
00588
MC/DC0%0/2
$game and $game->can("contestants")
$game $game->can("contestants")
Truth table33%1/3
A and B
A= $gameB= $game->can("contestants")
ABresult
0X0
100
111
116 $game and $game->can('contestants');
117 588 my $contestants = $game->contestants;
118 588 my @contestants = values %$contestants;
119 588 my @ids = map { $_->id } @contestants;
120 588 ▶ die "Player $id not in match of @ids" unless grep m/$_/, @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$_/, @ids 0 588 588
121 588 my @opponents;
122  
123 588 for my $contestant (@contestants) {
124 1176 ▶ push @opponents, $contestant if $contestant->id ne $id;
Branch: 1 branch
BranchTrueFalseTotal
if $contestant->id ne $id 588 588 1176
125 }
126 588 return $opponents[0];
127 }
128  
129  
130 =head2 copyCard
131  
132 @result = $player->copyCard(@games);
133  
134 Stores a ref to the @games in which $player has participated and copied the cards for. @games may or may not be a complete list of result for all rounds, and may include games in which $player wasn't a participant. Pushed to an anonymous array stored as the 'play' field. 'copyCard' expects the game objects to have 'round' and 'contestants' accessors and be 'canonize'able.
135  
136 =cut
137  
138 0 ▶sub copyCard {
Subroutine copyCard: not called CC=3 SCAR=24.8
Pod documented
139 0 my $self = shift;
140 0 my $id = $self->id;
141 0 my $play = $self->play;
142 0 my @games = @_;
143 0 my %result;
144 0 foreach my $game (@games) {
145 0 $game->canonize;
146 0 my $round = $game->round;
147 0 my $players = $game->contestants;
148 0 my %roles = map { $players->{$_}->id => $_ } keys %$players;
149 0 ▶ next unless exists $roles{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $roles{$id} 0 0 0
150 0 push @$play, $game;
151 }
152 0 $self->play($play);
153 }
154  
155 =head2 writeCard (deprecated)
156  
157 @result = $player->writeCard(@games);
158  
159 Updates the contestant's result in the matches played, using no intelligence if records only have only opponents' scores. @games may or may not be a complete list of result for all rounds, and may include games in which $player wasn't a participant. Stored as a 'play' field and keyed on the round, the resultant records have 'opponent' and 'result' subfields. 'writeCard' expects the game objects to have 'round', 'contestants' and 'result' accessors. Returns the new play field.
160 TODO The 'opponent' subfield will be an anonymous array of player objects if it is a multi-player game.
161  
162 =cut
163  
164 0 ▶sub writeCard {
Subroutine writeCard: not called CC=5 SCAR=34.0
Pod documented
165 0 my $self = shift;
166 0 my $id = $self->id;
167 0 my @games = @_;
168 0 my %result;
169 0 foreach my $game (@games) {
170 0 $game->canonize;
171 0 my $round = $game->round;
172 0 my $players = $game->contestants;
173 0 my %roles = map { $players->{$_}->id => $_ } keys %$players;
174 0 ▶ next unless exists $roles{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $roles{$id} 0 0 0
175 0 my $role = $roles{$id};
176 0 my $opponent;
177 0 foreach my $player ( values %$players ) {
178 0 ▶ $opponent = $player unless $player->id == $self->id;
Branch: 1 branch
BranchTrueFalseTotal
unless $player->id == $self->id 0 0 0
179 }
180 0 $result{$round} = { opponent => $opponent };
181 0 $result{$round}{result} = $game->{result}->{$role};
182 }
183 0 $self->play( \%result );
184 }
185  
186  
187 =head2 score
188  
189 $rounds = $deepblue->score
190 next if $deepblue->score
191  
192 Gets/sets the total score over the rounds in which $deepblue has a score. Don't forget to tally $deepblue's scorecard with the appropriate games first! We don't check any cards. Internally, this method accumulates the results of all the rounds into a total score, unless no results exist. If they don't exist, a hash key $self->{score} is consulted. You can set the score this way too, but don't do that. It bypasses the elegant code to do it from individual game results stored by the Games::Tournament::Contestant object. It's a hack to allow importing a pairing table. Finally, if none of the above apply, undef is returned, despite FIDE Rule A2. This means that Bracket and FIDE methods using the score method need to handle undef scores.
193  
194 =cut
195  
196 128216 ▶sub score {
Subroutine score: called CC=8 SCAR=20.9
Pod documented
197 128216 my $self = shift;
198 128216 my %converter = SCORES;
199 128216 my $score = shift;
200 128216 ▶ if ( defined $score ) { $self->{score} = $score; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $score) 160 128056 128216
201 128216 my $scores = $self->scores;
202 return $self->{score} unless defined $scores and
203 128216 ▶ all { defined $_ } values %$scores;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scores and &all(sub { defined $_; } , values %$scores) 104722 23494 128216
Condition100%3/3
defined $scores and &all(sub { defined $_; } , values %$scores)
!ll&&!rl&&r
173438737923494
MC/DC100%2/2
defined $scores and &all(sub { defined $_; } , values %$scores)
defined $scores &all(sub { defined $_; } , values %$scores)
Truth table100%3/3
A and B
A= defined $scoresB= &all(sub { defined $_; } , values %$scores)
ABresult
0X0
100
111
204 23494 my %lcconverter = map { lc($_) => $converter{$_} } keys %converter;
205 23494 my %scores = map { $_ => lc $scores->{$_} } keys %$scores;
206 23494 for my $round ( keys %scores ) {
207 die
208 "Round $round $scores->{$round}, $scores{$round} score unconvertible to $lcconverter{$scores{$round}} for player $self->{id}"
209 58671 ▶ unless defined( $scores{$round} and $lcconverter{ $scores{$round} } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($scores{$round} && $lcconverter{$scores{$round}}) 0 58671 58671
Condition66%2/3
$scores{$round} && $lcconverter{$scores{$round}}
!ll&&!rl&&r
0464454027
MC/DC50%1/2
$scores{$round} && $lcconverter{$scores{$round}}
$scores{$round} $lcconverter{$scores{$round}}
Truth table66%2/3
A && B
A= $scores{$round}B= $lcconverter{$scores{$round}}
ABresult
0X0
100
111
210 }
211 23494 my @values = map { $lcconverter{$_} } values %scores;
212 23494 my $sum = sum(@values);
213 23494 ▶ return $sum if defined $sum;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sum 23494 0 23494
214 0 return undef;
215 }
216  
217  
218 =head2 met
219  
220 $rounds = $deepblue->met(@grandmasters)
221 next if $deepblue->met($capablanca)
222  
223 Returns an anonymous hash, keyed on @grandmasters' ids, either of the gamecards in which $deepblue remembers meeting the members of @grandmasters or of the empty string '' if there is no record of such a meeting. Don't forget to tally $deepblue's scorecard with the appropriate games first (using $deepblue->play?)! We don't check $deepblue's partners' cards. (Assumes players do not meet more than once!) Don't confuse this with Games::Tournament::met!
224  
225 =cut
226  
227 0 ▶sub met {
Subroutine met: not called CC=4 SCAR=30.0
Pod documented
228 0 my $self = shift;
229 0 my @opponents = @_;
230 0 my $games = $self->play;
231 0 my @rounds = keys %$games;
232 0 my @ids = map { $_->id } @opponents;
233 0 my %gameAgainst;
234 0 @gameAgainst{@ids} = ('') x @ids;
235 0 for my $round ( @rounds )
236 {
237 0 my $gameInRound = $games->{$round};
238 0 ▶ next unless UNIVERSAL::isa $gameInRound, 'Games::Tournament::Card';
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($gameInRound, "Games::Tournament::Card") 0 0 0
239 0 my $opponent = $self->myOpponent($gameInRound);
240 0 my $opponentId = $opponent->id;
241 0 $gameAgainst{$opponentId} = $gameInRound;
242 }
243 0 ▶ carp $self->id . " played @ids? Where are the cards?" unless %gameAgainst;
Branch: 1 branch
BranchTrueFalseTotal
unless %gameAgainst 0 0 0
244 0 return \%gameAgainst;
245 }
246  
247  
248 =head2 name
249  
250 $member->name('Alexander Alekhine');
251 $member->name
252  
253 Sets or gets the name of the contesting individual or team, a string that may or may not be unique to the tournament member.
254  
255 =cut
256  
257 2930 ▶sub name {
Subroutine name: called CC=3 SCAR=12.0
Pod documented
258 2930 my $self = shift;
259 2930 my $name = shift;
260 0 ▶ if ( defined $name ) { $self->{name} = $name; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $name) { } 0 2930 2930
elsif (exists $self->{'name'}) { } 2930 0 2930
261 2930 elsif ( exists $self->{name} ) { return $self->{name}; }
262 }
263  
264  
265 =head2 title
266  
267 $member->title('Grandmaster')
268  
269 Sets/gets the title of the contestant, a courtesy given to the contestant.
270  
271 =cut
272  
273 1060 ▶sub title {
Subroutine title: called CC=3 SCAR=11.3
Pod documented
274 1060 my $self = shift;
275 1060 my $title = shift;
276 0 ▶ if ( defined $title ) { $self->{title} = $title; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $title) { } 0 1060 1060
elsif (exists $self->{'title'}) { } 1012 48 1060
277 1012 elsif ( exists $self->{title} ) { return $self->{title}; }
278 }
279  
280  
281 =head2 scores
282  
283 $member->scores
284  
285 Sets/gets the scores (actually results, eg 'Draw', 'Win') of the contestant in the different matches of the tournament, an ongoing record of their standing in the competition. These scores may or may not include the current score. To calculate the total score, use 'score', because internally the scores are not stored as number scores.
286  
287 =cut
288  
289 129536 ▶sub scores {
Subroutine scores: called CC=2 SCAR=6.9
Pod documented
290 129536 my $self = shift;
291 129536 ▶ my $scores = shift() || $self->{scores};
Condition100%3/3
shift() || $self->{'scores'}
l!l&&r!l&&!r
66011133817538
MC/DC100%2/2
shift() || $self->{'scores'}
shift() $self->{'scores'}
Truth table100%3/3
A || B
A= shift()B= $self->{'scores'}
ABresult
1X1
011
000
292 129536 $self->{scores} = $scores;
293 129536 return $scores;
294 }
295  
296  
297 =head2 rating
298  
299 $member->rating
300  
301 Sets/gets the rating of the contestant, an estimate of their strength. The constructor assumes if no rating or a non-numeric rating is given, that they don't have a rating, and it is set to 0.
302  
303 =cut
304  
305 55456 ▶sub rating {
Subroutine rating: called CC=4 SCAR=15.0
Pod documented
306 55456 my $self = shift;
307 55456 my $rating = shift;
308 0 ▶ if ( defined $rating and $rating =~ m/^\d$/ ) { $self->{rating} = $rating; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rating and $rating =~ /^\d$/) { } 0 55456 55456
elsif (exists $self->{'rating'}) { } 53575 1881 55456
Condition33%1/3
defined $rating and $rating =~ /^\d$/
!ll&&!rl&&r
5545600
MC/DC0%0/2
defined $rating and $rating =~ /^\d$/
defined $rating $rating =~ /^\d$/
Truth table33%1/3
A and B
A= defined $ratingB= $rating =~ /^\d$/
ABresult
0X0
100
111
309 53575 elsif ( exists $self->{rating} ) { return $self->{rating}; }
310 1881 else { return 0; }
311 }
312  
313  
314 =head2 play
315  
316 $games = $member->play;
317 $games = $member->play( { $lastround => $game } );
318  
319 Sets/gets a hash reference to the result of the pairings in each of the rounds played so far. Don't use this to record a player's match result. Use $tourney->collectCards. Implementation: The keys of the hash are the round numbers and the values are the gamecard of the player in that round. Very similar to the play accessor for tournaments, which is what collectCards uses.
320  
321 =cut
322  
323 0 ▶sub play {
Subroutine play: not called CC=4 SCAR=30.0
Pod documented
324 0 my $self = shift;
325 0 my $play = shift;
326 0 ▶ if ( defined $play ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $play) { } 0 0 0
elsif ($self->{'play'}) { } 0 0 0
327 0 my @rounds = keys %$play;
328 0 for my $round ( @rounds ) {
329 0 $self->{play}->{$round} = $play->{$round};
330 }
331 }
332 0 elsif ( $self->{play} ) { return $self->{play}; }
333 }
334  
335  
336 =head2 id
337  
338 $member->id
339  
340 Returns/sets the id of the contestant, a number unique to the member. Users must make sure no two players have the same id. Pairing numbers may change with late entries, so the id is important.
341  
342 =cut
343  
344 201000 ▶sub id {
Subroutine id: called CC=3 SCAR=12.0
Pod documented
345 201000 my $self = shift;
346 201000 my $id = shift;
347 0 ▶ if ( defined $id ) { $self->{id} = $id; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $id) { } 0 201000 201000
elsif (exists $self->{'id'}) { } 201000 0 201000
348 201000 elsif ( exists $self->{id} ) { return $self->{id}; }
349 }
350  
351 =head2 firstround
352  
353 $member->firstround
354  
355 Returns/sets the firstround of the contestant, the round in which they first played or will play. Necessary for handling late entrants.
356  
357 =cut
358  
359 4246 ▶sub firstround {
Subroutine firstround: called CC=3 SCAR=11.0
Pod documented
360 4246 my $self = shift;
361 4246 my $firstround = shift;
362 4246 ▶ if ( defined $firstround ) { $self->{firstround} = $firstround; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $firstround) { } 1760 2486 4246
elsif (exists $self->{'firstround'}) { } 726 1760 2486
363 726 elsif ( exists $self->{firstround} ) { return $self->{firstround}; }
364 }
365  
366  
367 =head2 absent
368  
369 $member->absent(1)
370 puah @absent if $member->absent
371  
372 A flag of convenience telling you whether this player is absent and not to be paired in the tournament. This is not the same as a forfeit. The Games::Tournament::Swiss constructor uses this.
373  
374 =cut
375  
376 0 ▶sub absent {
Subroutine absent: not called CC=3 SCAR=24.8
Pod documented
377 0 my $self = shift;
378 0 my $absent = shift;
379 0 ▶ if ( $absent ) { $self->{absent} = 1; return }
Branch: 2 branches
BranchTrueFalseTotal
if ($absent) { } 0 0 0
elsif (defined $self->{'absent'}) { } 0 0 0
380 0 elsif ( defined $self->{absent} ) { return $self->{absent}; }
381 0 else { return; }
382 }
383  
384  
385 =head1 AUTHOR
386  
387 Dr Bean, C<< <drbean, followed by the at mark (@), cpan, then a dot, and finally, org> >>
388  
389 =head1 BUGS
390  
391 Please report any bugs or feature requests to
392 C<bug-games-tournament-contestant at rt.cpan.org>, or through the web interface at
393 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Tournament-Swiss>.
394 I will be notified, and then you'll automatically be notified of progress on
395 your bug as I make changes.
396  
397 =head1 SUPPORT
398  
399 You can find documentation for this module with the perldoc command.
400  
401 perldoc Games::Tournament::Contestant
402  
403 You can also look for information at:
404  
405 =over 4
406  
407 =item * AnnoCPAN: Annotated CPAN documentation
408  
409 L<http://annocpan.org/dist/Games-Tournament-Swiss>
410  
411 =item * CPAN Ratings
412  
413 L<http://cpanratings.perl.org/d/Games-Tournament-Swiss>
414  
415 =item * RT: CPAN's request tracker
416  
417 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Tournament-Swiss>
418  
419 =item * Search CPAN
420  
421 L<http://search.cpan.org/dist/Games-Tournament-Swiss>
422  
423 =back
424  
425 =head1 ACKNOWLEDGEMENTS
426  
427 =head1 COPYRIGHT & LICENSE
428  
429 Copyright 2006 Dr Bean, all rights reserved.
430  
431 This program is free software; you can redistribute it and/or modify it
432 under the same terms as Perl itself.
433  
434 =cut
435  
436 33 1; # End of Games::Tournament::Contestant
437  
438 # vim: set ts=8 sts=4 sw=4 noet:
« blib/lib/Games/Tournament/Swiss.pm ↑ Summary blib/lib/Games/Tournament/Swiss/Bracket.pm »