blib/lib/Games/Tournament.pm

filter: Statementstmt 53.3% 167 / 313 Branchbran 44.1% 45 / 102 Conditioncond 22.2% 14 / 63 MC/DCmcdc 3.3% 1 / 30 Subroutinesub 63.6% 28 / 44 Podpod 100% 29 / 29 totaltotal 48.8% 284 / 581 CC 94 SCAR 35.8
CC
94
Cov
47%
unmarkedCards
156.0
updateScores
132.0
roleCheck
90.0
CRAP 36.0
↑ Summary blib/lib/Games/Tournament/Swiss/Procedure/FIDE.pm »
1 package Games::Tournament;
2 35 $Games::Tournament::VERSION = '0.21';
3 # Last Edit: 2016 Jan 01, 13:44:32
4 # $Id: $
5  
6 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 35 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use List::MoreUtils qw/any all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 35 ▶use Games::Tournament::Swiss::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶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 6 35
17 @Games::Tournament::Swiss::Config::roles:
18 35 Games::Tournament::Swiss::Config->roles;
19 35 ▶use constant FIRSTROUND => $Games::Tournament::Swiss::Config::firstround;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 Games::Tournament - Contestant Pairing
24  
25 =cut
26  
27 =head1 SYNOPSIS
28  
29 $tourney = Games::Tournament->new(\@entrants);
30 next if $capablanca->met($alekhine)
31  
32 $round = $tourney->meeting($member1, [$member2, $member3]);
33 ...
34  
35 =head1 DESCRIPTION
36  
37 In a tournament, there are contestants, and matches over rounds between the contestants, in which they are differentiated by role. TODO firstround and roles.
38  
39 =head1 METHODS
40  
41 =head2 new
42  
43 Games::Tournament->new( rounds => 2, entrants => [ $a, $b, $c ] )
44  
45 Creates a competition for entrants, over a number of rounds. entrants is a list of player objects. Enters (see enter method) each of the entrants in the tournament. (But why is the entrants arg being deleted?)
46  
47 =cut
48  
49 100 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
50 100 my $self = shift;
51 100 my %args = @_;
52 100 my $entrants = $args{entrants};
53 100 delete $args{entrants};
54 100 my $object = bless \%args, $self;
55 100 for my $entrant ( @$entrants ) { $object->enter( $entrant ); }
56 100 return $object;
57 }
58  
59  
60 =head2 enter
61  
62 $tourney->enter($player)
63  
64 Enters a Games::Tournament::Contestant player object with a rating, title id, and name in the entrants of the tournament. Die if no name or id. We are authoritarians. Warn if no rating defined. No check for duplicate ids. Set this round as their first round, unless they already entered in an earlier round (But did they play in that round?) Set their absent accessor if they are in absentees.
65  
66 =cut
67  
68 1778 ▶sub enter {
Subroutine enter: called CC=10 SCAR=26.2
Pod documented
69 1778 my $self = shift;
70 1778 my $player = shift;
71 1778 my $round = $self->round;
72 1778 ▶ die "Player " . $player->id . " entering in Round $round + 1?" unless
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($round) 0 1778 1778
73 looks_like_number($round);
74 1778 ▶ $player->firstround($round+1) unless $player->firstround;
Branch: 1 branch
BranchTrueFalseTotal
unless $player->firstround 1760 18 1778
75 1778 my $absent = $self->absentees;
76 1778 my @absentids;
77 0 ▶ @absentids = map { $_->id } @$absent if $absent and ref $absent eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $absent and ref $absent eq "ARRAY" 0 1778 1778
Condition33%1/3
$absent and ref $absent eq "ARRAY"
!ll&&!rl&&r
177800
MC/DC0%0/2
$absent and ref $absent eq "ARRAY"
$absent ref $absent eq "ARRAY"
Truth table33%1/3
A and B
A= $absentB= ref $absent eq "ARRAY"
ABresult
0X0
100
111
78 0 ▶ $player->absent(1) if any { $_ eq $player->id } @absentids;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $player->id; } , @absentids) 0 1778 1778
79 1778 my $entrants = $self->entrants;
80 1778 for my $required ( qw/id name/ ) {
81 3556 ▶ unless ( $player->$required ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($player->$required) 0 3556 3556
82 0 croak "No $required for player " . $player->id;
83 }
84 }
85 1778 for my $recommended ( qw/rating/ ) {
86 1778 ▶ unless ( defined $player->$recommended ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $player->$recommended) 0 1778 1778
87 0 carp "No $recommended for player " . $player->id;
88 0 $player->$recommended( 'None' );
89 }
90 }
91 1778 push @$entrants, $player;
92 1778 $self->entrants( $entrants );
93 }
94  
95 =head2 rank
96  
97 @rankings = $tourney->rank(@players)
98  
99 Ranks a list of Games::Tournament::Contestant player objects by score, rating, title and name if they all have a score, otherwise ranks them by rating, title and name. This is the same ordering that is used to determine pairing numbers in a swiss tournament.
100  
101 =cut
102  
103 5408 ▶sub rank {
Subroutine rank: called CC=7 SCAR=19.7
Pod documented
104 5408 my $self = shift;
105 5408 my @players = @_;
106 5408 ▶ if ( all { defined $_->score } @players ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { defined $_->score; } , @players)) { } 4774 634 5408
107 sort {
108 4774 ▶ $b->score <=> $a->score
Condition77%7/9
$b->score <=> $a->score || $b->rating <=> $a->rating || $a->title cmp $b->title || $a->name cmp $b->name
l!l&&r!l&&!r
172425140
$b->score <=> $a->score or $b->rating <=> $a->rating or $a->title cmp $b->title
l!l&&r!l&&!r
172420514
$b->score <=> $a->score or $b->rating <=> $a->rating
l!l&&r!l&&!r
125415988514
MC/DC0%0/4
$b->score <=> $a->score || $b->rating <=> $a->rating || $a->title cmp $b->title || $a->name cmp $b->name
$b->score <=> $a->score $b->rating <=> $a->rating $a->title cmp $b->title $a->name cmp $b->name
Truth table60%3/5
A or B or C || D
A= $b->score <=> $a->scoreB= $b->rating <=> $a->ratingC= $a->title cmp $b->titleD= $a->name cmp $b->name
ABCDresult
1XXX1
01XX1
001X1
00011
00000
109 || $b->rating <=> $a->rating
110 || $a->title cmp $b->title
111 || $a->name cmp $b->name
112 } @players;
113 }
114 else {
115 sort {
116 634 ▶ $b->rating <=> $a->rating
Condition66%4/6
$b->rating <=> $a->rating || $a->title cmp $b->title || $a->name cmp $b->name
l!l&&r!l&&!r
10313140
$b->rating <=> $a->rating or $a->title cmp $b->title
l!l&&r!l&&!r
10313014
MC/DC0%0/3
$b->rating <=> $a->rating || $a->title cmp $b->title || $a->name cmp $b->name
$b->rating <=> $a->rating $a->title cmp $b->title $a->name cmp $b->name
Truth table50%2/4
A or B || C
A= $b->rating <=> $a->ratingB= $a->title cmp $b->titleC= $a->name cmp $b->name
ABCresult
1XX1
01X1
0011
0000
117 || $a->title cmp $b->title
118 || $a->name cmp $b->name
119 } @players;
120 }
121 }
122  
123 =head2 reverseRank
124  
125 @reverseRankings = $tourney->reverseRank(@players)
126  
127 Ranks in reverse order a list of Games::Tournament::Contestant player objects by score, rating, title and name if they all have a score, otherwise reverseRanks them by rating, title and name.
128  
129 =cut
130  
131 0 ▶sub reverseRank {
Subroutine reverseRank: not called CC=1 SCAR=6.9
Pod documented
132 0 my $self = shift;
133 0 my @players = @_;
134 0 my @rankers = $self->rank(@players);
135 0 return reverse @rankers;
136 }
137  
138  
139 #=head2 firstRound
140 #
141 # $tourney->firstRound(7)
142 #
143 #Gets/sets the first round in the competition in which the swiss system is used to pair opponents, when this might not be the first round of the competition.
144 #
145 #=cut
146 #
147 #field 'firstRound' => 1;
148  
149  
150 =head2 named
151  
152 $tourney->named($name)
153  
154 Returns a contestant whose name is $name, the first entrant with a name with stringwise equality. So beware same-named contestants.
155  
156 =cut
157  
158 0 ▶sub named {
Subroutine named: not called CC=1 SCAR=6.9
Pod documented
159 0 my $self = shift;
160 0 my $name = shift;
161 0 my $contestants = $self->entrants;
162 0 ▶ return ( first { $_->name eq $name } @$contestants );
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 }
164  
165  
166 =head2 ided
167  
168 $tourney->ided($id)
169  
170 Returns the contestant whose id is $id. Ids are grepped for stringwise equality.
171  
172 =cut
173  
174 8415 ▶sub ided {
Subroutine ided: called CC=1 SCAR=0.0
Pod documented
175 8415 my $self = shift;
176 8415 my $id = shift;
177 8415 my @contestants = @{ $self->entrants };
178 8415 ▶ return first { $_->id eq $id } @contestants;
Subroutine __ANON__: called CC=1 SCAR=0.0
179 }
180  
181  
182 =head2 roleCheck
183  
184 roleCheck(@games)
185  
186 Returns the roles of the contestants in the individual $games in @games, eg qw/Black White/, qw/Home Away/, these being all the same (ie no typos), or dies.
187  
188 =cut
189  
190 0 ▶sub roleCheck {
Subroutine roleCheck: not called CC=9 SCAR=45.0
Pod documented
191 0 my $self = shift;
192 0 my @games = @_;
193 0 my @roles;
194 0 for my $game (@games) {
195 0 my $contestants = $game->contestants;
196 0 my $result = $game->result;
197 0 my @otherroles = sort keys %$contestants;
198 0 for my $key ( keys %$result ) {
199 die "$key: $result->{$key}, but no $key player in $game."
200 0 ▶ unless grep { $key eq $_ } @otherroles;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$key eq $_;} @otherroles 0 0 0
201 }
202 0 ▶ unless (@roles) {
Branch: 1 branch
BranchTrueFalseTotal
if (@roles) { } 0 0 0
203 0 @roles = @otherroles;
204 }
205 else {
206 0 my $test = 0;
207 0 ▶ $test++ unless @roles == @otherroles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles == @otherroles 0 0 0
208 0 for my $i ( 0 .. $#roles ) {
209 0 ▶ $test++ unless $roles[$i] eq $otherroles[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $roles[$i] eq $otherroles[$i] 0 0 0
210 }
211 0 ▶ die "@roles in game 1, but @otherroles in $game."
Branch: 1 branch
BranchTrueFalseTotal
if $test 0 0 0
212 if $test;
213 }
214 }
215 0 return @roles;
216 }
217  
218  
219 =head2 met
220  
221 @rounds = $tourney->met($deepblue, @grandmasters)
222 next if $tourney->met($deepblue, $capablanca)
223  
224 In list context, returns an array of the rounds in which $deepblue met the corresponding member of @grandmasters (and of the empty string '' if they haven't met.) In scalar context, returns the number of grandmasters met. Don't forget to collect scorecards in the appropriate games first! (Assumes players do not meet more than once!) This is NOT the same as Games::Tournament::Contestant::met! See also Games;:Tournament::Swiss::whoPlayedWho.
225  
226 =cut
227  
228 41 ▶sub met {
Subroutine met: called CC=8 SCAR=21.1
Pod documented
229 41 my $self = shift;
230 41 my $player = shift;
231 41 my @opponents = @_;
232 41 my @ids = map { $_->id } @opponents;
233 41 my $games = $self->play;
234 41 my $rounds = $self->round;
235 41 my %roundGames = map { $_ => $games->{$_} } FIRSTROUND .. $rounds;
236 41 ▶ carp "No games to round $rounds. Where are the cards?" unless %roundGames;
Branch: 1 branch
BranchTrueFalseTotal
unless %roundGames 0 41 41
237 41 my @meetings;
238 41 @meetings[ 0 .. $#opponents ] = ('') x @opponents;
239 41 my $n = 0;
240 41 for my $other (@opponents) {
241 233 for my $round ( FIRSTROUND .. $rounds ) {
242 699 my $game = $roundGames{$round}{ $other->id };
243 699 ▶ next unless $game and $game->can('contestants');
Branch: 1 branch
BranchTrueFalseTotal
unless $game and $game->can("contestants") 126 573 699
Condition66%2/3
$game and $game->can("contestants")
!ll&&!rl&&r
1260573
MC/DC50%1/2
$game and $game->can("contestants")
$game $game->can("contestants")
Truth table66%2/3
A and B
A= $gameB= $game->can("contestants")
ABresult
0X0
100
111
244 573 ▶ $meetings[$n] = $round if $other->myOpponent($game) == $player;
Branch: 1 branch
BranchTrueFalseTotal
if $other->myOpponent($game) == $player 68 505 573
245 }
246 }
247 233 continue { $n++; }
248 41 ▶ return @meetings if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 41 0 41
249 0 return scalar grep { $_ } @meetings;
250 }
251  
252  
253 =head2 unmarkedCards
254  
255 @unfinished = $tourney->unmarkedCards(@games)
256  
257 Returns an array of the games which have no or a wrong result. The result accessor should be an anonymous hash with roles, or 'Bye' as keys and either 'Win' & 'Loss', 'Loss' & 'Win' or 'Draw' & 'Draw', or 'Bye', as values.
258  
259 =cut
260  
261 0 ▶sub unmarkedCards {
Subroutine unmarkedCards: not called CC=12 SCAR=50.5
Pod documented
262 0 my $self = shift;
263 0 my @games = @_;
264 0 my @unfinished;
265 0 for my $game (@games) {
266 0 my $contestants = $game->contestants;
267 0 my $result = $game->result;
268 push @unfinished, $game
269 unless (
270 ( keys %$contestants == 1 and $result->{Bye} =~ m/Bye/i )
271 or $result->{ (ROLES)[0] } and $result->{ (ROLES)[1] }
272 and (
273 (
274 $result->{ (ROLES)[0] } eq 'Win'
275 and $result->{ (ROLES)[1] } eq 'Loss'
276 )
277 or ( $result->{ (ROLES)[0] } eq 'Loss'
278 and $result->{ (ROLES)[1] } eq 'Win' )
279 or ( $result->{ (ROLES)[0] } eq 'Draw'
280 0 ▶ and $result->{ (ROLES)[1] } eq 'Draw' )
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$contestants == 1 and $result->{"Bye"} =~ /Bye/i or $$result{("White", "Black")[0]} and $$result{("White", "Black")[1]} and $$result{("White", "Black")[0]} eq "Win" && $$result{("White", "Black")[1]} eq "Loss" || $$result{("White", "Black")[0]} eq "Loss" && $$result{("White", "Black")[1]} eq "Win" || $$result{("White", "Black")[0]} eq "Draw" && $$result{("White", "Black")[1]} eq "Draw" 0 0 0
Condition0%0/27
keys %$contestants == 1 and $result->{"Bye"} =~ /Bye/i or $$result{("White", "Black")[0]} and $$result{("White", "Black")[1]} and $$result{("White", "Black")[0]} eq "Win" && $$result{("White", "Black")[1]} eq "Loss" || $$result{("White", "Black")[0]} eq "Loss" && $$result{("White", "Black")[1]} eq "Win" || $$result{("White", "Black")[0]} eq "Draw" && $$result{("White", "Black")[1]} eq "Draw"
l!l&&r!l&&!r
000
keys %$contestants == 1 and $result->{"Bye"} =~ /Bye/i
!ll&&!rl&&r
000
$$result{("White", "Black")[0]} and $$result{("White", "Black")[1]} and $$result{("White", "Black")[0]} eq "Win" && $$result{("White", "Black")[1]} eq "Loss" || $$result{("White", "Black")[0]} eq "Loss" && $$result{("White", "Black")[1]} eq "Win" || $$result{("White", "Black")[0]} eq "Draw" && $$result{("White", "Black")[1]} eq "Draw"
!ll&&!rl&&r
000
$$result{("White", "Black")[0]} and $$result{("White", "Black")[1]}
!ll&&!rl&&r
000
$$result{("White", "Black")[0]} eq "Win" and $$result{("White", "Black")[1]} eq "Loss" or $$result{("White", "Black")[0]} eq "Loss" and $$result{("White", "Black")[1]} eq "Win" or $$result{("White", "Black")[0]} eq "Draw" and $$result{("White", "Black")[1]} eq "Draw"
l!l&&r!l&&!r
000
$$result{("White", "Black")[0]} eq "Win" and $$result{("White", "Black")[1]} eq "Loss" or $$result{("White", "Black")[0]} eq "Loss" and $$result{("White", "Black")[1]} eq "Win"
l!l&&r!l&&!r
000
$$result{("White", "Black")[0]} eq "Win" and $$result{("White", "Black")[1]} eq "Loss"
!ll&&!rl&&r
000
$$result{("White", "Black")[0]} eq "Loss" and $$result{("White", "Black")[1]} eq "Win"
!ll&&!rl&&r
000
$$result{("White", "Black")[0]} eq "Draw" and $$result{("White", "Black")[1]} eq "Draw"
!ll&&!rl&&r
000
MC/DC0%0/10
keys %$contestants == 1 and $result->{"Bye"} =~ /Bye/i or $$result{("White", "Black")[0]} and $$result{("White", "Black")[1]} and $$result{("White", "Black")[0]} eq "Win" && $$result{("White", "Black")[1]} eq "Loss" || $$result{("White", "Black")[0]} eq "Loss" && $$result{("White", "Black")[1]} eq "Win" || $$result{("White", "Black")[0]} eq "Draw" && $$result{("White", "Black")[1]} eq "Draw"
keys %$contestants == 1 $result->{"Bye"} =~ /Bye/i $$result{("White", "Black")[0]} $$result{("White", "Black")[1]} $$result{("White", "Black")[0]} eq "Win" $$result{("White", "Black")[1]} eq "Loss" $$result{("White", "Black")[0]} eq "Loss" $$result{("White", "Black")[1]} eq "Win" $$result{("White", "Black")[0]} eq "Draw" $$result{("White", "Black")[1]} eq "Draw"
Truth table0%0/35
A and B or C and D and E and F or G and H or I and J
A= keys %$contestants == 1B= $result->{"Bye"} =~ /Bye/iC= $$result{("White", "Black")[0]}D= $$result{("White", "Black")[1]}E= $$result{("White", "Black")[0]} eq "Win"F= $$result{("White", "Black")[1]} eq "Loss"G= $$result{("White", "Black")[0]} eq "Loss"H= $$result{("White", "Black")[1]} eq "Win"I= $$result{("White", "Black")[0]} eq "Draw"J= $$result{("White", "Black")[1]} eq "Draw"
ABCDEFGHIJresult
11XXXXXXXX1
0X1111XXXX1
0X110X11XX1
0X111011XX1
0X110X0X111
0X110X10111
0X11100X111
0X111010111
101111XXXX1
10110X11XX1
10111011XX1
10110X0X111
10110X10111
1011100X111
10111010111
0X0XXXXXXX0
0X10XXXXXX0
0X110X0X0X0
0X110X0X100
0X110X100X0
0X110X10100
0X11100X0X0
0X11100X100
0X1110100X0
0X111010100
100XXXXXXX0
1010XXXXXX0
10110X0X0X0
10110X0X100
10110X100X0
10110X10100
1011100X0X0
1011100X100
101110100X0
10111010100
281 )
282 );
283 }
284 0 return @unfinished;
285 }
286  
287  
288 =head2 dupes
289  
290 $games = $tourney->dupes(@grandmasters)
291  
292 Returns an anonymous array, of the games in which @grandmasters have met. Don't forget to collect scorecards in the appropriate games first! (Assumes players do not meet more than once!)
293  
294 =cut
295  
296 0 ▶sub dupes {
Subroutine dupes: not called CC=2 SCAR=17.9
Pod documented
297 0 my $self = shift;
298 0 my @players = @_;
299 0 my @ids = map { $_->id } @players;
300 0 my $games = $self->play;
301 0 my @dupes;
302 map {
303 0 my $id = $_;
304 0 ▶ map { push @dupes, $games->{$id}->{$_} if exists $games->{$id}->{$_}; }
Branch: 1 branch
BranchTrueFalseTotal
if exists $games->{$id}{$_} 0 0 0
305 @ids;
306 } @ids;
307 0 return \@dupes;
308 }
309  
310  
311 =head2 updateScores
312  
313 @scores = $tourney->updateScores;
314  
315 Updates entrants' scores for the present (previous) round, using $tourney's play (ie games played) field. Returns an array of the scores in order of the player ids (not at the moment, it doesn't), dying on those entrants who don't have a result for the round. Be careful. Garbage in, garbage out. What is the present round?
316  
317 =cut
318  
319 0 ▶sub updateScores {
Subroutine updateScores: not called CC=11 SCAR=48.8
Pod documented
320 0 my $self = shift;
321 0 my $players = $self->entrants;
322 0 my $round = $self->round;
323 0 my $games = $self->play;
324 0 my @scores;
325 0 for my $player (@$players) {
326 0 my $id = $player->id;
327 0 my $oldId = $player->oldId;
328 0 my $scores = $player->scores;
329 0 my $card = $games->{$round}->{$id};
330 0 ▶ die "Game in round $round for player $id? Is $round the right round?"
Branch: 1 branch
BranchTrueFalseTotal
unless $card and $card->isa("Games::Tournament::Card") 0 0 0
Condition0%0/3
$card and $card->isa("Games::Tournament::Card")
!ll&&!rl&&r
000
MC/DC0%0/2
$card and $card->isa("Games::Tournament::Card")
$card $card->isa("Games::Tournament::Card")
Truth table0%0/3
A and B
A= $cardB= $card->isa("Games::Tournament::Card")
ABresult
0X0
100
111
331 unless $card
332 and $card->isa('Games::Tournament::Card');
333 0 my $results = $card->{result};
334 0 die @{ [ keys %$results ] } . " roles in player ${id}'s game?"
335 0 ▶ unless grep { $_ eq (ROLES)[0] or $_ eq (ROLES)[1] or $_ eq 'Bye' }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq ("White", "Black")[0] or $_ eq ("White", "Black")[1] or $_ eq "Bye";} keys %$results 0 0 0
Condition0%0/6
$_ eq ('White', 'Black')[0] || $_ eq ('White', 'Black')[1] || $_ eq 'Bye'
l!l&&r!l&&!r
000
$_ eq ("White", "Black")[0] or $_ eq ("White", "Black")[1]
l!l&&r!l&&!r
000
MC/DC0%0/3
$_ eq ('White', 'Black')[0] || $_ eq ('White', 'Black')[1] || $_ eq 'Bye'
$_ eq ("White", "Black")[0] $_ eq ("White", "Black")[1] $_ eq 'Bye'
Truth table0%0/4
A or B || C
A= $_ eq ("White", "Black")[0]B= $_ eq ("White", "Black")[1]C= $_ eq 'Bye'
ABCresult
1XX1
01X1
0011
0000
336 keys %$results;
337 0 eval { $card->myResult($player) };
338 0 ▶ die "$@: Result in player ${id}'s $card game in round $round?"
Branch: 1 branch
BranchTrueFalseTotal
if not $card or $@ 0 0 0
Condition0%0/3
not $card or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
not $card or $@
not $card $@
Truth table0%0/3
not(A) or B
A= not $cardB= $@
ABresult
1X1
011
000
339 if not $card or $@;
340 0 my $result = $card->myResult($player);
341 0 ▶ die "$result result in $card game for player $id in round $round?"
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /^(?:Win|Loss|Draw|Bye|Forfeit)/i 0 0 0
342 unless $result =~ m/^(?:Win|Loss|Draw|Bye|Forfeit)/i;
343 0 $$scores{$round} = $result;
344 0 ▶ $player->scores($scores) if defined $scores;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scores 0 0 0
345 0 push @scores, $$scores{$round};
346 }
347 0 $self->entrants($players);
348 # return @scores;
349 }
350  
351  
352 =head2 randomRole
353  
354 ( $myrole, $yourrole ) = randomRole;
355  
356 This returns the 2 roles, @Games::Tournament::roles in a random order.
357  
358 =cut
359  
360 150 ▶sub randomRole {
Subroutine randomRole: called CC=3 SCAR=11.0
Pod documented
361 150 my $self = shift;
362 150 ▶ my $evenRole = int rand(2) ? (ROLES)[0] : (ROLES)[1];
Branch: 1 branch
BranchTrueFalseTotal
int rand 2 ? : 76 74 150
363 150 ▶ my $oddRole = $evenRole eq (ROLES)[0] ? (ROLES)[1] : (ROLES)[0];
Branch: 1 branch
BranchTrueFalseTotal
$evenRole eq ('White', 'Black')[0] ? : 76 74 150
364 150 return ( $evenRole, $oddRole );
365 }
366  
367  
368 =head2 play
369  
370 $tourney->play
371  
372 Gets the games played, keyed on round and id of player. Also sets, but you don't want to do that.
373  
374 =cut
375  
376 324 ▶sub play {
Subroutine play: called CC=3 SCAR=11.0
Pod documented
377 324 my $self = shift;
378 324 my $play = shift;
379 324 ▶ if ( defined $play ) { $self->{play} = $play; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $play) { } 74 250 324
elsif ($self->{'play'}) { } 192 58 250
380 192 elsif ( $self->{play} ) { return $self->{play}; }
381 }
382  
383 =head2 entrants
384  
385 $tourney->entrants
386  
387 Gets/sets the entrants as an anonymous array of player objects. Users may rely on the original order being maintained in web app cookies.
388  
389 =cut
390  
391 12813 ▶sub entrants {
Subroutine entrants: called CC=3 SCAR=11.0
Pod documented
392 12813 my $self = shift;
393 12813 my $entrants = shift;
394 12813 ▶ if ( defined $entrants ) { $self->{entrants} = $entrants; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $entrants) { } 2010 10803 12813
elsif ($self->{'entrants'}) { } 10703 100 10803
395 10703 elsif ( $self->{entrants} ) { return $self->{entrants}; }
396 }
397  
398  
399 =head2 absentees
400  
401 $tourney->absentees
402  
403 Gets/sets the absentees as an anonymous array of player objects. These players won't be included in the brackets of players who are to be paired.
404  
405 =cut
406  
407 1980 ▶sub absentees {
Subroutine absentees: called CC=3 SCAR=13.3
Pod documented
408 1980 my $self = shift;
409 1980 my $absentees = shift;
410 0 ▶ if ( defined $absentees ) { $self->{absentees} = $absentees; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $absentees) { } 0 1980 1980
elsif ($self->{'absentees'}) { } 0 1980 1980
411 0 elsif ( $self->{absentees} ) { return $self->{absentees}; }
412 }
413  
414  
415 =head2 round
416  
417 $tourney->round
418  
419 Gets/sets the round number of a round near you. The default round number is 0. That is, the 'round' before round 1. The question is when one round becomes the next round.
420  
421 =cut
422  
423 2153 ▶sub round {
Subroutine round: called CC=3 SCAR=11.0
Pod documented
424 2153 my $self = shift;
425 2153 my $round = shift;
426 2153 ▶ if ( defined $round ) { $self->{round} = $round; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $round) { } 140 2013 2153
elsif ($self->{'round'}) { } 195 1818 2013
427 195 elsif ( $self->{round} ) { return $self->{round}; }
428 1818 else { return 0 }
429 }
430  
431  
432 =head2 rounds
433  
434 $tourney->rounds
435  
436 Gets/sets the number of rounds in the tournament.
437  
438 =cut
439  
440 0 ▶sub rounds {
Subroutine rounds: not called CC=3 SCAR=24.8
Pod documented
441 0 my $self = shift;
442 0 my $rounds = shift;
443 0 ▶ if ( defined $rounds ) { $self->{rounds} = $rounds; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rounds) { } 0 0 0
elsif ($self->{'rounds'}) { } 0 0 0
444 0 elsif ( $self->{rounds} ) { return $self->{rounds}; }
445 }
446  
447  
448 =head2 size
449  
450 $size = 'Maxi' if $tourney->size > 2**$tourney->rounds
451  
452 Gets the number of entrants
453  
454 =cut
455  
456 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
457 0 my $self = shift;
458 0 return scalar @{ $self->entrants };
459 }
460  
461  
462 =head2 idNameCheck
463  
464 $tourney->idNameCheck # WARNING: 13301616 and 13300849 both, Petrosian, Tigran
465  
466 Dies if 2 entrants have the same id, warns if they have the same name.
467  
468 =cut
469  
470 2 ▶sub idNameCheck {
Subroutine idNameCheck: called CC=4 SCAR=14.5
Pod documented
471 2 my $self = shift;
472 2 my $lineup = $self->entrants;
473 2 my (%idcheck, %namecheck);
474 2 for my $player ( @$lineup ) {
475 8 my $id = $player->id;
476 8 my $name = $player->name;
477 8 ▶ if ( defined $idcheck{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idcheck{$id}) 0 8 8
478 0 croak $name . " and $idcheck{$id} have the same id: $id";
479 }
480 8 ▶ if ( defined $namecheck{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namecheck{$name}) 0 8 8
481 0 carp "WARNING: $id and $namecheck{$name} have the same name: " .
482 $name . ". Proceeding, but BEWARE there may be problems later,";
483 }
484 8 $idcheck{$id} = $name;
485 8 $namecheck{$name} = $id;
486 }
487 }
488  
489  
490 =head2 idCheck
491  
492 $tourney->idCheck # Petrosian, Tigran, and Tigran Petrosian both 13301616
493  
494 Dies if 2 entrants have the same id
495  
496 =cut
497  
498 0 ▶sub idCheck {
Subroutine idCheck: not called CC=3 SCAR=24.8
Pod documented
499 0 my $self = shift;
500 0 my $lineup = $self->entrants;
501 0 my %idcheck;
502 0 for my $player ( @$lineup ) {
503 0 my $id = $player->id;
504 0 my $name = $player->name;
505 0 ▶ if ( defined $idcheck{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idcheck{$id}) 0 0 0
506 0 croak $name . " and $idcheck{$id} have the same id: $id";
507 }
508 0 $idcheck{$id} = $name;
509 }
510 }
511  
512 =head2 nameCheck
513  
514 $tourney->idNameCheck # WARNING: 13301616 and 13300849 both, Petrosian, Tigran
515  
516 Warn if 2 entrants have the same name
517  
518 =cut
519  
520 0 ▶sub nameCheck {
Subroutine nameCheck: not called CC=3 SCAR=24.8
Pod documented
521 0 my $self = shift;
522 0 my $lineup = $self->entrants;
523 0 my %namecheck;
524 0 for my $player ( @$lineup ) {
525 0 my $id = $player->id;
526 0 my $name = $player->name;
527 0 ▶ if ( defined $namecheck{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namecheck{$name}) 0 0 0
528 0 carp "WARNING: $id and $namecheck{$name} have the same name: " .
529 $name . ". Proceeding, but BEWARE there may be problems later,";
530 }
531 0 $namecheck{$name} = $id;
532 }
533 }
534  
535 =head2 odd
536  
537 float($lowest) if $self->odd(@group)
538  
539 Tests whether the number of players in @group is odd or not.
540  
541 =cut
542  
543 0 ▶sub odd {
Subroutine odd: not called CC=1 SCAR=6.9
Pod documented
544 0 my $self = shift;
545 0 my @n = @_;
546 0 return @n % 2;
547 }
548  
549  
550 =head2 clearLog
551  
552 $pairing->clearLog(qw/C10 C11/)
553  
554 Discards the logged messages for the passed procedures.
555  
556 =cut
557  
558 0 ▶sub clearLog {
Subroutine clearLog: not called CC=2 SCAR=17.9
Pod documented
559 0 my $self = shift;
560 0 my @states = @_;
561 0 my $log = $self->{log};
562 0 delete $log->{$_} for @states;
563 0 return;
564 }
565  
566  
567 =head2 catLog
568  
569 $pairing->catLog(qw/C10 C11/)
570  
571 Returns the messages logged for the passed procedures, or all logged procedures if no procedures are passed, as a hash keyed on the procedures. If no messages were logged, because the procedures were not loggedProcedures, no messages will be returned.
572  
573 =cut
574  
575 0 ▶sub catLog {
Subroutine catLog: not called CC=5 SCAR=34.0
Pod documented
576 0 my $self = shift;
577 0 my @states = @_;
578 0 ▶ @states = $self->loggedProcedures unless @states;
Branch: 1 branch
BranchTrueFalseTotal
unless @states 0 0 0
579 0 my $log = $self->{log};
580 0 my %report;
581 0 for my $state ( @states ) {
582 0 my $strings = $log->{$state}->{strings};
583 0 ▶ unless ( $strings and ref $strings eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($strings and ref $strings eq "ARRAY") 0 0 0
Condition0%0/3
$strings and ref $strings eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$strings and ref $strings eq "ARRAY"
$strings ref $strings eq "ARRAY"
Truth table0%0/3
A and B
A= $stringsB= ref $strings eq "ARRAY"
ABresult
0X0
100
111
584 0 $report{$state} = undef;
585 0 next;
586 }
587 0 $report{$state} = join '', @$strings;
588 }
589 0 return %report;
590 }
591  
592  
593 =head2 tailLog
594  
595 $pairing->tailLog(qw/C10 C11/)
596  
597 Returns the new messages logged for the passed procedures since they were last tailed, as a hash keyed on the procedures. If no messages were logged, because the procedures were not loggedProcedures, no messages will be returned.
598  
599 =cut
600  
601 142 ▶sub tailLog {
Subroutine tailLog: called CC=7 SCAR=19.5
Pod documented
602 142 my $self = shift;
603 142 my @states = @_;
604 142 ▶ @states = $self->loggedProcedures unless @states;
Branch: 1 branch
BranchTrueFalseTotal
unless @states 0 142 142
605 142 my $log = $self->{log};
606 142 my %report = map { $_ => $log->{$_}->{strings} } @states;
607 142 my %tailpos = map { $_ => $log->{$_}->{tailpos} } @states;
608 142 my (%newpos, %lastpos, %tailedReport);
609 142 for my $state ( @states )
610 {
611 142 ▶ if ( defined $tailpos{$state} )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $tailpos{$state}) { } 109 33 142
elsif ($report{$state}) { } 33 0 33
612 {
613 109 $newpos{$state} = $tailpos{$state} + 1;
614 109 $lastpos{$state} = $#{ $report{$state} };
615 $tailedReport{$state} = join '',
616 109 @{$report{$state}}[ $newpos{$state}..$lastpos{$state} ];
617 109 $log->{$_}->{tailpos} = $lastpos{$_} for @states;
618 }
619 elsif ( $report{$state} ) {
620 33 $newpos{$state} = 0;
621 33 $lastpos{$state} = $#{ $report{$state} };
622 $tailedReport{$state} = join '',
623 33 @{$report{$state}}[ $newpos{$state}..$lastpos{$state} ];
624 33 $log->{$_}->{tailpos} = $lastpos{$_} for @states;
625 }
626 }
627 142 return %tailedReport;
628 }
629  
630  
631 =head2 log
632  
633 $pairing->log('x=p=1, no more x increases in Bracket 4 (2).')
634  
635 Saves the message in a log iff this procedure is logged.
636  
637 =cut
638  
639 6883 ▶sub log {
Subroutine log: called CC=3 SCAR=11.0
Pod documented
640 6883 my $self = shift;
641 6883 my $message = shift;
642 6883 ▶ return unless $message;
Branch: 1 branch
BranchTrueFalseTotal
unless $message 0 6883 6883
643 6883 (my $method = uc((caller 1)[3])) =~ s/^.*::(\w+)$/$1/;
644 6883 my @loggable = $self->loggedProcedures;
645 190 push @{ $self->{log}->{$method}->{strings} }, "\t$message\n" if
646 6883 ▶ any { $_ eq $method } @loggable;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $method; } , @loggable) 190 6693 6883
647 6883 return;
648 }
649  
650  
651 =head2 loggedProcedures
652  
653 $group->loggedProcedures(qw/C10 C11 C12/)
654 $group->loggedProcedures(qw/C5 C6PAIRS C7 C8/)
655  
656 Adds messages generated in the procedures named in the argument list to a reportable log. Without an argument returns the logged procedures as an array.
657  
658 =cut
659  
660 12506 ▶sub loggedProcedures {
Subroutine loggedProcedures: called CC=4 SCAR=20.1
Pod documented
661 12506 my $self = shift;
662 12506 my @states = @_;
663 12506 ▶ unless ( @states ) { return keys %{ $self->{logged} }; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@states) 12506 0 12506
664 0 my %logged;
665 0 @logged{qw/START NEXT PREV C1 C2 C3 C4 C5 C6PAIRS C6OTHERS C7 C8 C9 C10 C11 C12 C13 C14 BYE MATCHPLAYERS ASSIGNPAIRINGNUMBERS/} = (1) x 21;
666 0 for my $state (@states)
667 {
668 0 ▶ carp "$state is unloggable procedure" if not exists $logged{$state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $logged{$state} 0 0 0
669 0 $self->{logged}->{$state} = 1;
670 # push @{ $self->{log}->{$state}->{strings} }, $state . ",";
671 }
672 0 return;
673 }
674  
675  
676 =head2 loggingAll
677  
678 $group->loggingAll
679  
680 Adds messages generated in all the procedures to a reportable log
681  
682 =cut
683  
684 3 ▶sub loggingAll {
Subroutine loggingAll: called CC=2 SCAR=6.9
Pod documented
685 3 my $self = shift;
686 3 my %logged;
687 3 @logged{qw/START NEXT PREV C1 C2 C3 C4 C5 C6PAIRS C6OTHERS C7 C8 C9 C10 C11 C12 C13 C14 BYE MATCHPLAYERS ASSIGNPAIRINGNUMBERS/} = (1) x 21;
688 3 for my $state ( keys %logged )
689 {
690 # carp "$state is unloggable procedure" if not exists $logged{$state};
691 63 $self->{logged}->{$state} = 1;
692 }
693 3 return;
694 }
695  
696  
697 =head2 disloggedProcedures
698  
699 $group->disloggedProcedures
700 $group->disloggedProcedures(qw/C6PAIRS C7 C8/)
701  
702 Stops messages generated in the procedures named in the argument list being added to a reportable log. Without an argument stops logging of all procedures.
703  
704 =cut
705  
706 0 ▶sub disloggedProcedures {
Subroutine disloggedProcedures: not called CC=4 SCAR=30.0
Pod documented
707 0 my $self = shift;
708 0 my @states = @_;
709 0 ▶ unless ( @states )
Branch: 1 branch
BranchTrueFalseTotal
unless (@states) 0 0 0
710 {
711 0 my @methods = keys %{ $self->{logged} };
712 0 @{$self->{logged}}{@methods} = (0) x @methods;
713 }
714 0 my %logged;
715 0 @logged{qw/START NEXT PREV C1 C2 C3 C4 C5 C6PAIRS C6OTHERS C7 C8 C9 C10 C11 C12 C13 C14 BYE MATCHPLAYERS ASSIGNPAIRINGNUMBERS/} = (1) x 21;
716 0 for my $state (@states)
717 {
718 0 ▶ carp "$state is unloggable procedure" if not defined $logged{$state};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $logged{$state} 0 0 0
719 0 $self->{logged}->{$state} = 0;
720 }
721 0 return;
722 }
723  
724  
725 =head1 AUTHOR
726  
727 Dr Bean, C<< <drbean, followed by the at mark (@), cpan, then a dot, and finally, org> >>
728  
729 =head1 BUGS
730  
731 Please report any bugs or feature requests to
732 C<bug-games-tournament-swiss at rt.cpan.org>, or through the web interface at
733 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Tournament>.
734 I will be notified, and then you'll automatically be notified of progress on
735 your bug as I make changes.
736  
737 =head1 SUPPORT
738  
739 You can find documentation for this module with the perldoc command.
740  
741 perldoc Games::Tournament
742  
743 You can also look for information at:
744  
745 =over 4
746  
747 =item * AnnoCPAN: Annotated CPAN documentation
748  
749 L<http://annocpan.org/dist/Games-Tournament>
750  
751 =item * CPAN Ratings
752  
753 L<http://cpanratings.perl.org/d/Games-Tournament>
754  
755 =item * RT: CPAN's request tracker
756  
757 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Tournament>
758  
759 =item * Search CPAN
760  
761 L<http://search.cpan.org/dist/Games-Tournament>
762  
763 =back
764  
765 =head1 ACKNOWLEDGEMENTS
766  
767 =head1 COPYRIGHT & LICENSE
768  
769 Copyright 2006 Dr Bean, all rights reserved.
770  
771 This program is free software; you can redistribute it and/or modify it
772 under the same terms as Perl itself.
773  
774 =cut
775  
776 35 1; # End of Games::Tournament
777  
778 # vim: set ts=8 sts=4 sw=4 noet:
↑ Summary blib/lib/Games/Tournament/Swiss/Procedure/FIDE.pm »