blib/lib/Games/Tournament/Swiss.pm

filter: Statementstmt 83.8% 280 / 334 Branchbran 58.5% 48 / 82 Conditioncond 53.2% 49 / 92 MC/DCmcdc 22.0% 11 / 50 Subroutinesub 79.4% 31 / 39 Podpod 100% 17 / 17 totaltotal 71.0% 436 / 614 CC 98 SCAR 29.4
CC
98
Cov
74%
recreateCards
56.2
incompatibles
42.0
compatible
30.0
CRAP 18.8
« blib/lib/Games/Tournament/Swiss/Procedure/FIDE.pm ↑ Summary blib/lib/Games/Tournament/Contestant.pm »
1 package Games::Tournament::Swiss;
2 32 $Games::Tournament::Swiss::VERSION = '0.21';
3 # Last Edit: 2016 Jan 01, 13:44:35
4 # $Id: Swiss.pm 1360 2016-01-01 05:54:20Z drbean $
5  
6 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 ▶use Games::Tournament::Swiss::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 32 ▶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 3 32
13 @Games::Tournament::Swiss::Config::roles:
14 32 Games::Tournament::Swiss::Config->roles;
15 32 ▶use constant FIRSTROUND => $Games::Tournament::Swiss::Config::firstround;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 32 ▶use base qw/Games::Tournament/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # use Games::Tournament::Swiss::Bracket;
20 #use Games::Tournament::Contestant::Swiss -mixin =>
21 # qw/score scores rating title name pairingNumber oldId roles/;
22 32 ▶use Games::Tournament::Contestant::Swiss;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 32 ▶use Games::Tournament::Swiss::Procedure;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 32 ▶use Games::Tournament::Contestant::Swiss::Preference;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 32 ▶use List::Util qw/max min reduce sum first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 32 ▶use List::MoreUtils qw/any all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 Games::Tournament::Swiss - FIDE Swiss Same-Rank Contestant Pairing
32  
33 =cut
34  
35 =head1 SYNOPSIS
36  
37 @Games::Tournament::Swiss::roles = qw/Black White/;
38 $tourney = Games::Tournament::Swiss->new($rounds, \@entrants);
39 @rankedPlayers = $tourney->assignPairingNumbers;
40 $tourney->initializePreferences;
41  
42  
43 ...
44  
45 $tourney->collectCards(@games);
46 @groups = $tourney->formBrackets($round);
47 $round5 = $tourney->pairing( \@groups );
48 $matches = $round5->matchPlayers;
49 $round5->allocateColors;
50  
51 =head1 DESCRIPTION
52  
53 In a Swiss tournament, there is a pre-declared number of rounds, each contestant meets every other contestant zero or one times, and in each round contestants are paired with other players with the same, or similar, scores.
54  
55 =head1 METHODS
56  
57 =head2 assignPairingNumbers
58  
59 @rankings = $tourney->assignPairingNumbers;
60  
61 Sets the participants pairing numbers, sorting on rating, title and name, and substitutes this for the id they had before (The id was, but is no longer, saved as oldId. But don't change id to pairingNumber. It will change with late entries.) This function uses Games::Tournament::rank. Before the first round, all scores are usually 0. A2
62  
63 =cut
64  
65 82 ▶sub assignPairingNumbers {
Subroutine assignPairingNumbers: called CC=3 SCAR=11.0
Pod documented
66 82 my $self = shift;
67 82 my @players = @{ $self->entrants };
68 82 $self->log( 'Pairing numbers' );
69 82 ▶ my $numbers = sub { join ', ',
Subroutine __ANON__: called CC=1 SCAR=0.0
70 82 map { $_->id . ": " . $_->pairingNumber } @players;
71 82 };
72 82 ▶ if ( all { $_->pairingNumber } @players ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { $_->pairingNumber; } , @players)) 1 81 82
73 1 $self->log( &$numbers );
74 1 return;
75 }
76 81 my @rankings = $self->rank(@players);
77 81 foreach my $n ( 0 .. $#rankings ) {
78 1643 my $id = $rankings[$n]->id;
79 1643 my $player = $self->ided($id);
80 1643 $player->pairingNumber( $n+1 );
81 }
82 81 $self->log( &$numbers );
83 81 $self->entrants( \@players );
84 }
85  
86  
87 =head2 initializePreferences
88  
89 @rankings = $tourney->initializePreferences;
90  
91 Before the first round, the color (role) preference of the highest ranked player and the other odd-numbered players in the top half of the rankings is determined by lot. The preference of the even-numbered players in the top half is given to the other color. If there is only one player in the tournament, the preference is not initialized. The method assumes all entrants have a preference attribute. This accessor is given the player by the Games::Tournament::Contestant::Swiss constructor. We take care to put the players back in the same order that we got them from entrants method. Users may rely on the original order being maintained in web app cookies. E5
92  
93 =cut
94  
95 150 ▶sub initializePreferences {
Subroutine initializePreferences: called CC=5 SCAR=16.1
Pod documented
96 150 my $self = shift;
97 150 my @players = @{ $self->{entrants} };
98 150 my @rankings = $self->rank( @players );
99 150 my ( $evenRole, $oddRole ) = $self->randomRole;
100 150 my $p = int( @rankings / 2 );
101 150 ▶ if ( $p == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == 0) 1 149 150
102 1 $rankings[ 0 ]->preference->sign('');
103 1 $rankings[ 0 ]->preference->difference(0);
104 1 return $self->entrants( \@rankings );
105 }
106 149 ▶ for ( my $n=0; $n <= $p-1; $n+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $p - 1) 855 149 1004
107 855 $rankings[ $n ]->preference->sign($evenRole);
108 855 $rankings[ $n ]->preference->difference(0);
109 }
110 149 ▶ for ( my $n=1; $n <= $p-1; $n+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $p - 1) 767 149 916
111 767 $rankings[ $n ]->preference->sign($oddRole);
112 767 $rankings[ $n ]->preference->difference(0);
113 }
114 149 foreach my $n ( 0 .. $#rankings ) {
115 3320 my $id = $rankings[$n]->id;
116 3320 my $player = $self->ided($id);
117 3320 my $preference = $rankings[$n]->preference;
118 3320 $player->preference( $preference );
119 }
120 149 $self->entrants( \@players );
121 }
122  
123  
124 =head2 recreateCards
125  
126 $tourney->recreateCards( {
127 round => $round,
128 opponents => { 1 => 2, 2 => 1, 3 => 'Bye', 4 => '-' },
129 roles => { 1 => 'W', 2 => 'B', 3 => 'Bye', 4 => '-' },
130 floats => { 1 => 'U', 2=> 'D', 3 => 'Down', 4 => 'Not' }
131 } )
132  
133 From hashes of the opponents, roles and floats for each player in a round (as provided by a pairing table), draws up the original game cards for each of the matches of the round. Returned is a list of Games::Tournament::Card objects, with undefined result fields. Pairing numbers are not used. Ids are used. Pairing numbers change with late entries.
134  
135 =cut
136  
137 6 ▶sub recreateCards {
Subroutine recreateCards: called CC=24 SCAR=40.3
Pod documented
138 6 my $self = shift;
139 6 my $args = shift;
140 6 my $round = $args->{round};
141 6 my $opponents = $args->{opponents};
142 6 my $roles = $args->{roles};
143 6 my $floats = $args->{floats};
144 6 my $players = $self->entrants;
145 6 my @ids = map { $_->id } @$players;
146 6 my $absentees = $self->absentees;
147 0 my @absenteeids = map { $_->id } @$absentees;
148 6 ▶ my $test = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
149 6 my %count = ();
150 6 $count{$_}++ for @ids, keys %$opponents, keys %$roles, keys %$floats;
151 6 return grep { $count{$_} != 4 } keys %count;
152 6 };
153 6 carp "Game card not constructable for player $_ in round $round" for &$test;
154 6 my (%games, @games);
155 6 for my $id ( @ids )
156 {
157 120 ▶ next if $games{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $games{$id} 60 60 120
158 60 my $player = $self->ided($id);
159 60 ▶ next if $round < $player->firstround;
Branch: 1 branch
BranchTrueFalseTotal
if $round < $player->firstround 0 60 60
160 60 my $opponentId = $opponents->{$id};
161 60 ▶ croak "Round $round: opponent info for Player $id?" unless $opponentId;
Branch: 1 branch
BranchTrueFalseTotal
unless $opponentId 0 60 60
162 60 my $opponent = $self->ided($opponentId);
163 60 my $opponentsOpponent = $opponents->{$opponentId};
164 60 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $opponentId eq "Bye" or $opponentId eq "Unpaired" or $opponentsOpponent eq $id 0 60 60
Condition33%2/6
$opponentId eq "Bye" or $opponentId eq "Unpaired" or $opponentsOpponent eq $id
l!l&&r!l&&!r
0600
$opponentId eq "Bye" or $opponentId eq "Unpaired"
l!l&&r!l&&!r
0060
MC/DC0%0/3
$opponentId eq "Bye" or $opponentId eq "Unpaired" or $opponentsOpponent eq $id
$opponentId eq "Bye" $opponentId eq "Unpaired" $opponentsOpponent eq $id
Truth table25%1/4
A or B or C
A= $opponentId eq "Bye"B= $opponentId eq "Unpaired"C= $opponentsOpponent eq $id
ABCresult
1XX1
01X1
0011
0000
165 "Player ${id}'s opponent is $opponentId, but ${opponentId}'s opponent is $opponentsOpponent, not $id in round $round"
166 unless $opponentId eq 'Bye' or $opponentId eq 'Unpaired'
167 or $opponentsOpponent eq $id;
168 60 my $role = $roles->{$id};
169 60 my $opponentRole = $roles->{$opponentId};
170 60 ▶ if ( $opponentId eq 'Unpaired' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opponentId eq 'Unpaired') { } 0 60 60
elsif ($opponentId eq 'Bye') { } 0 60 60
171 0 ▶ croak "Player $id has $role, in round $round?"
Branch: 1 branch
BranchTrueFalseTotal
unless $player and $role eq "Unpaired" 0 0 0
Condition0%0/3
$player and $role eq "Unpaired"
!ll&&!rl&&r
000
MC/DC0%0/2
$player and $role eq "Unpaired"
$player $role eq "Unpaired"
Truth table0%0/3
A and B
A= $playerB= $role eq "Unpaired"
ABresult
0X0
100
111
172 unless $player and $role eq 'Unpaired';
173 0 next;
174 0 next;
175 }
176 elsif ( $opponentId eq 'Bye' ) {
177 0 ▶ croak "Player $id has $role role, in round $round?"
Branch: 1 branch
BranchTrueFalseTotal
unless $player and $role eq "Bye" 0 0 0
Condition0%0/3
$player and $role eq "Bye"
!ll&&!rl&&r
000
MC/DC0%0/2
$player and $role eq "Bye"
$player $role eq "Bye"
Truth table0%0/3
A and B
A= $playerB= $role eq "Bye"
ABresult
0X0
100
111
178 unless $player and $role eq 'Bye';
179 }
180 else {
181 60 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $player and $opponent and $role and $opponentRole 0 60 60
Condition33%3/9
$player and $opponent and $role and $opponentRole
!ll&&!rl&&r
0060
$player and $opponent and $role
!ll&&!rl&&r
0060
$player and $opponent
!ll&&!rl&&r
0060
MC/DC0%0/4
$player and $opponent and $role and $opponentRole
$player $opponent $role $opponentRole
Truth table20%1/5
A and B and C and D
A= $playerB= $opponentC= $roleD= $opponentRole
ABCDresult
0XXX0
10XX0
110X0
11100
11111
182 "Player $id is $role, and opponent $opponentId is $opponentRole, in round $round?"
183 unless $player
184 and $opponent
185 and $role
186 and $opponentRole;
187  
188 }
189 60 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $opponentId and defined $opponentRole and $role eq $opponentRole 0 60 60
Condition33%2/6
$opponentId and defined $opponentRole and $role eq $opponentRole
!ll&&!rl&&r
0600
$opponentId and defined $opponentRole
!ll&&!rl&&r
0060
MC/DC0%0/3
$opponentId and defined $opponentRole and $role eq $opponentRole
$opponentId defined $opponentRole $role eq $opponentRole
Truth table25%1/4
A and B and C
A= $opponentIdB= defined $opponentRoleC= $role eq $opponentRole
ABCresult
0XX0
10X0
1100
1111
190 "Player $id has same $role role as opponent $opponentId in round $round?"
191 if $opponentId and defined $opponentRole and $role eq $opponentRole;
192 60 my $contestants;
193 0 ▶ if ( $opponentId eq 'Bye' ) { $contestants = { Bye => $player } }
Branch: 1 branch
BranchTrueFalseTotal
if ($opponentId eq 'Bye') { } 0 60 60
194 60 else { $contestants = { $role => $player, $opponentRole => $opponent } }
195 60 my $game = Games::Tournament::Card->new(
196 round => $round,
197 contestants => $contestants,
198 result => undef
199 );
200 60 my $float = $floats->{$id};
201 60 $game->float( $player, $float );
202  
203 60 ▶ unless ( $opponentId eq 'Bye' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opponentId eq "Bye") 60 0 60
204 60 my $opponentFloat = $floats->{$opponentId};
205 60 $game->float( $opponent, $opponentFloat );
206 }
207 60 $games{$id} = $game;
208 60 $games{$opponentId} = $game;
209 60 push @games, $game;
210 }
211 6 return @games;
212 }
213  
214  
215 =head2 collectCards
216  
217 $play = $tourney->collectCards( @games );
218 next if $htable->{$player1->id}->{$player2->id};
219  
220 Records @games after they have been played. Stored as $tourney's play field, keyed on round and ids of players. Returns the new play field. Updates player scores, preferences, unless the player forfeited the game or had a Bye. TODO Die (or warn) if game has no results TODO This has non-Swiss subclass elements I could factor out into a method in Games::Tournament. TODO What if player is matched more than one time in the round, filling in for someone? XXX It looks like all the games have to be the same round, or you have to collect all cards in one round before collecting cards in following rounds. XXX I'm having problems with recording roles. I want to be lazy about it, and trust the card I get back before the next round. The problem with this is, I may be getting the role from the wrong place. It should come from the card, and is a role which was assigned in the previous round, and is only now being recorded, at this point between the previous round and the next round. Or is the problem copying by value rather than reference of the entrants? Now I also need to record floats. It would be good to do this at the same time as I record roles. The card is the appropriate place to get this info according to A4.
221  
222 =cut
223  
224 74 ▶sub collectCards {
Subroutine collectCards: called CC=13 SCAR=25.7
Pod documented
225 74 my $self = shift;
226 74 my @games = @_;
227 74 ▶ my $play = $self->play || {};
Condition100%2/2
$self->play || {}
l!l
5024
MC/DC100%1/1
$self->play || {}
$self->play
Truth table100%2/2
A
A= $self->play
Aresult
11
00
228 # my @entrants = @{ $self->entrants };
229 74 my %games;
230 74 for my $game ( @games )
231 {
232 345 my $round = $game->round;
233 345 ▶ carp "round $round is not a number." unless $round =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $round =~ /^\d+$/ 0 345 345
234 345 push @{ $games{$round} }, $game;
235 }
236 74 for my $round ( sort { $a <=> $b } keys %games )
237 {
238 75 my $games = $games{$round};
239 75 for my $game ( @$games ) {
240 345 my @players = $game->myPlayers;
241 345 for my $player ( @players ) {
242 660 my $id = $player->id;
243 660 my $entrant = $self->ided($id);
244 660 my $oldroles = $player->roles;
245 660 my $scores = $player->scores;
246 660 my ( $role, $float, $score );
247 660 $role = $game->myRole($player);
248 660 $float = $game->myFloat($player);
249 $scores->{$round} = ref $game->result eq 'HASH'?
250 660 ▶ $game->result->{$role}: undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $game->result eq 'HASH' ? : 302 358 660
251 660 $score = $scores->{$round};
252 #carp
253 # "No result on card for player $id as $role in round $round,"
254 # unless $score;
255 660 ▶ $game ||= "No game";
Condition50%1/2
$game ||= "No game"
l!l
6600
MC/DC0%0/1
$game ||= "No game"
$game
Truth table50%1/2
A
A= $game
Aresult
11
00
256 660 $play->{$round}->{$id} = $game;
257 660 $entrant->scores($scores);
258 carp "No record in round $round for player $id $player->{name},"
259 660 ▶ unless $play->{$round}->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $play->{$round}{$id} 0 660 660
260 660 $entrant->roles( $round, $role );
261 660 $entrant->floats( $round, $float );
262 660 $entrant->floating('');
263 660 ▶ $entrant->preference->update( $entrant->rolesPlayedList ) unless
Branch: 1 branch
BranchTrueFalseTotal
unless $score and $score eq "Bye" || $score eq "Forfeit" 636 24 660
Condition83%5/6
$score and $score eq "Bye" || $score eq "Forfeit"
!ll&&!rl&&r
35827824
$score eq "Bye" or $score eq "Forfeit"
l!l&&r!l&&!r
240278
MC/DC66%2/3
$score and $score eq "Bye" || $score eq "Forfeit"
$score $score eq "Bye" $score eq "Forfeit"
Truth table75%3/4
A and B or C
A= $scoreB= $score eq "Bye"C= $score eq "Forfeit"
ABCresult
0XX0
1000
11X1
1011
264 $score and ( $score eq 'Bye' or $score eq 'Forfeit' );
265 ;
266 }
267 }
268 }
269 74 $self->play($play);
270 }
271  
272  
273 =head2 orderPairings
274  
275 @schedule = $tourney->orderPairings( @games );
276  
277 Tables are ordered by scores of the player with the higher score at the table, then the total scores of the players (in other words, the scores of the other player), then the A2 ranking of the higher-ranked player, in that order. F1
278  
279 =cut
280  
281 22 ▶sub orderPairings {
Subroutine orderPairings: called CC=4 SCAR=13.9
Pod documented
282 22 my $self = shift;
283 22 my @games = @_;
284 22 my $entrants = $self->entrants;
285 22 my @rankedentrants = $self->rank(@$entrants);
286 22 my %ranking = map { $rankedentrants[$_]->id => $_ } 0 .. $#rankedentrants;
287 my @orderings = map {
288 22 my @players = $_->myPlayers;
289 74 ▶ my @scores = map { $_->score || 0 } @players;
Condition100%2/2
$_->score || 0
l!l
1399
MC/DC100%1/1
$_->score || 0
$_->score
Truth table100%2/2
A
A= $_->score
Aresult
11
00
290 74 my $higherscore = max @scores;
291 74 my $totalscore = sum @scores;
292 74 my @rankedplayers = $self->rank( @players );
293 { higherscore => $higherscore,
294 totalscore => $totalscore,
295 74 higherranking => $ranking{$rankedplayers[0]->id} };
296 } @games;
297 74 my @neworder = map { $games[$_] } sort {
298 22 $orderings[$b]->{higherscore} <=> $orderings[$a]->{higherscore} ||
299 $orderings[$b]->{totalscore} <=> $orderings[$a]->{totalscore} ||
300 $orderings[$a]->{higherranking} <=> $orderings[$b]->{higherranking}
301 76 ▶ } 0 .. $#orderings;
Condition83%5/6
$orderings[$b]{'higherscore'} <=> $orderings[$a]{'higherscore'} || $orderings[$b]{'totalscore'} <=> $orderings[$a]{'totalscore'} || $orderings[$a]{'higherranking'} <=> $orderings[$b]{'higherranking'}
l!l&&r!l&&!r
60160
$orderings[$b]{"higherscore"} <=> $orderings[$a]{"higherscore"} or $orderings[$b]{"totalscore"} <=> $orderings[$a]{"totalscore"}
l!l&&r!l&&!r
322816
MC/DC0%0/3
$orderings[$b]{'higherscore'} <=> $orderings[$a]{'higherscore'} || $orderings[$b]{'totalscore'} <=> $orderings[$a]{'totalscore'} || $orderings[$a]{'higherranking'} <=> $orderings[$b]{'higherranking'}
$orderings[$b]{"higherscore"} <=> $orderings[$a]{"higherscore"} $orderings[$b]{"totalscore"} <=> $orderings[$a]{"totalscore"} $orderings[$a]{'higherranking'} <=> $orderings[$b]{'higherranking'}
Truth table75%3/4
A or B || C
A= $orderings[$b]{"higherscore"} <=> $orderings[$a]{"higherscore"}B= $orderings[$b]{"totalscore"} <=> $orderings[$a]{"totalscore"}C= $orderings[$a]{'higherranking'} <=> $orderings[$b]{'higherranking'}
ABCresult
1XX1
01X1
0011
0000
302 22 return @neworder;
303 }
304  
305  
306 =head2 publishCards
307  
308 $schedule = $tourney->publishCards( @games );
309  
310 Stores @games, perhaps before they have been played, as $tourney's play field, keyed on round and ids of players. Returns the games in F1 ordering.
311  
312 =cut
313  
314 2 ▶sub publishCards {
Subroutine publishCards: called CC=6 SCAR=18.2
Pod documented
315 2 my $self = shift;
316 2 ▶ my $play = $self->play || {};
Condition50%1/2
$self->play || {}
l!l
02
MC/DC0%0/1
$self->play || {}
$self->play
Truth table50%1/2
A
A= $self->play
Aresult
11
00
317 2 my @entrants = @{ $self->entrants };
318 2 my @games = @_;
319 2 for my $game (@games) {
320 4 my $round = $game->round;
321 4 my $contestants = $game->contestants;
322 4 my @players = map { $contestants->{$_} } keys %$contestants;
323 4 for my $player (@players) {
324 8 my $id = $player->id;
325 8 my $entrant = $self->ided($id);
326 8 ▶ die "Player $id $entrant in round $round?"
Branch: 1 branch
BranchTrueFalseTotal
unless $entrant and $entrant->isa("Games::Tournament::Contestant::Swiss") 0 8 8
Condition33%1/3
$entrant and $entrant->isa("Games::Tournament::Contestant::Swiss")
!ll&&!rl&&r
008
MC/DC0%0/2
$entrant and $entrant->isa("Games::Tournament::Contestant::Swiss")
$entrant $entrant->isa("Games::Tournament::Contestant::Swiss")
Truth table33%1/3
A and B
A= $entrantB= $entrant->isa("Games::Tournament::Contestant::Swiss")
ABresult
0X0
100
111
327 unless $entrant
328 and $entrant->isa("Games::Tournament::Contestant::Swiss");
329 8 $play->{$round}->{$id} = $game;
330 }
331 }
332 2 $self->orderPairings( @games );
333 }
334  
335  
336 =head2 myCard
337  
338 $game = $tourney->myCard(round => 4, player => 13301616);
339  
340 Finds match from $tourney's play accessor, which is keyed on round and IDS of players. 'player' is id of player.
341  
342 =cut
343  
344 0 ▶sub myCard {
Subroutine myCard: not called CC=1 SCAR=6.9
Pod documented
345 0 my $self = shift;
346 0 my %args = @_;
347 0 my $round = $args{round};
348 0 my $id = $args{player};
349 0 my $roundmatches = $self->{play}->{$round};
350 0 return $roundmatches->{$id};
351 }
352  
353  
354 =head2 formBrackets
355  
356 @groups = $tourney->formBrackets
357  
358 Returns for the next round a hash of Games::Tournament::Swiss::Bracket objects grouping contestants with the same score, keyed on score. Late entrants without a score cause the program to die. Some groups may have odd numbers of players, etc, and players will have to be floated to other score groups. A number, from 1 to the total number of brackets, reflecting the order of pairing, is given to each bracket.
359  
360 =cut
361  
362 64 ▶sub formBrackets {
Subroutine formBrackets: called CC=6 SCAR=17.9
Pod documented
363 64 my $self = shift;
364 64 my $players = $self->entrants;
365 64 my $absentees = $self->absentees;
366 64 my %hashed;
367 my %brackets;
368 64 foreach my $player (@$players) {
369 472 my $id = $player->id;
370 0 ▶ next if any { $id eq $_->id } @$absentees;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $id eq $_->id; } , @$absentees) 0 472 472
371 472 ▶ my $score = defined $player->score ? $player->score : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $player->score ? : 409 63 472
372 # die "$player has no score. Give them a zero, perhaps?"
373 # if $score eq "None";
374 472 $hashed{$score}{ $player->pairingNumber } = $player;
375 }
376 64 my $number = 1;
377 64 foreach my $score ( reverse sort keys %hashed ) {
378 173 my @members;
379 173 foreach
380 505 my $pairingNumber ( sort { $a <=> $b } keys %{ $hashed{$score} } )
381 {
382 472 push @members, $hashed{$score}{$pairingNumber};
383 }
384 32 ▶ use Games::Tournament::Swiss::Bracket;
Subroutine BEGIN: called CC=1 SCAR=0.0
385 173 my $group = Games::Tournament::Swiss::Bracket->new(
386 score => $score,
387 members => \@members,
388 number => $number
389 );
390 173 $brackets{$score} = $group;
391 173 $number++;
392 }
393 64 return %brackets;
394 }
395  
396 =head2 pairing
397  
398 $pairing = $tourney->pairing( \@groups );
399  
400 Returns a Games::Tournament::Swiss::Procedure object. Groups are Games::Tournament::Swiss::Brackets objects of contestants with the same score and they are ordered by score, the group with the highest score first, and the group with the lowest score last. This is the point where round i becomes round i+1. But the program is expected to update the Games::Tournament::Swiss object itself. (Why?)
401  
402 =cut
403  
404 62 ▶sub pairing {
Subroutine pairing: called CC=1 SCAR=0.0
Pod documented
405 62 my $self = shift;
406 62 my $entrants = $self->entrants;
407 62 my $brackets = shift;
408 62 my $round = $self->round;
409 62 return Games::Tournament::Swiss::Procedure->new(
410 round => $round + 1,
411 brackets => $brackets,
412 whoPlayedWho => $self->whoPlayedWho,
413 colorClashes => $self->colorClashes,
414 byes => $self->byesGone,
415 );
416 }
417  
418  
419 =head2 compatible
420  
421 $games = $tourney->compatible
422 next if $games->{$alekhine->pairingNumber}->{$capablanca->pairingNumber}
423  
424 Returns an anonymous hash, keyed on the ids of @grandmasters, indicating whether or not the individual @grandmasters could play each other in the next round. But what is the next round? This method uses the whoPlayedWho and colorClashes methods to remove incompatible players.
425  
426 =cut
427  
428 0 ▶sub compatible {
Subroutine compatible: not called CC=5 SCAR=34.0
Pod documented
429 0 my $self = shift;
430 0 my $players = $self->entrants;
431 0 my @ids = map { $_->id } @$players;
432 0 my $play = $self->play;
433 0 my $dupes = $self->whoPlayedWho;
434 0 my $colorbar = $self->colorClashes;
435 0 my $compat;
436 0 for my $id1 (@ids) {
437  
438 0 for my $id2 ( grep { $_ != $id1 } @ids ) {
439 $compat->{$id1}->{$id2} = 1
440 unless exists $dupes->{$id1}->{$id2}
441 0 ▶ or exists $colorbar->{$id1}->{$id2};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dupes->{$id1}{$id2} or exists $colorbar->{$id1}{$id2} 0 0 0
Condition0%0/3
exists $dupes->{$id1}{$id2} or exists $colorbar->{$id1}{$id2}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $dupes->{$id1}{$id2} or exists $colorbar->{$id1}{$id2}
exists $dupes->{$id1}{$id2} exists $colorbar->{$id1}{$id2}
Truth table0%0/3
A or B
A= exists $dupes->{$id1}{$id2}B= exists $colorbar->{$id1}{$id2}
ABresult
1X1
011
000
442 }
443 }
444 0 return $compat;
445 }
446  
447  
448 =head2 whoPlayedWho
449  
450 $games = $tourney->whoPlayedWho
451 next if $games->{$alekhine->pairingNumber}->
452 {$capablanca->pairingNumber}
453  
454 Returns an anonymous hash, keyed on the ids of the tourney's entrants, of the round in which individual entrants met. Don't forget to collect scorecards in the appropriate games first! (No tracking of how many times players have met if they have met more than once!) Do you know what round it is? B1 XXX Unplayed pairings are not considered illegal in future rounds. F2 See also Games::Tournament::met.
455  
456 =cut
457  
458 70 ▶sub whoPlayedWho {
Subroutine whoPlayedWho: called CC=15 SCAR=28.8
Pod documented
459 70 my $self = shift;
460 70 my $players = $self->entrants;
461 70 my @ids = map { $_->id } @$players;
462 70 my $absentees = $self->absentees;
463 0 my @absenteeids = map { $_->id } @$absentees;
464 70 my $play = $self->play;
465 70 my $dupes;
466 70 my $lastround = $self->round;
467 70 for my $round ( FIRSTROUND .. $lastround ) {
468 140 for my $id (@ids) {
469 1312 my $player = $self->ided($id);
470 1312 ▶ die "No player with $id id in round $round game of @ids"
Branch: 1 branch
BranchTrueFalseTotal
unless $player 0 1312 1312
471 unless $player;
472 1312 my $game = $play->{$round}->{$id};
473 1312 ▶ if ( $game and $game->can("myRole") ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($game and $game->can('myRole')) { } 1308 4 1312
elsif ($player->firstround > $round or &any(sub { $id eq $_; } , @absenteeids)) { } 4 0 4
Condition66%2/3
$game and $game->can("myRole")
!ll&&!rl&&r
401308
MC/DC50%1/2
$game and $game->can("myRole")
$game $game->can("myRole")
Truth table66%2/3
A and B
A= $gameB= $game->can("myRole")
ABresult
0X0
100
111
474 1308 ▶ next if $game->result and $game->result eq 'Bye';
Branch: 1 branch
BranchTrueFalseTotal
if $game->result and $game->result eq "Bye" 0 1308 1308
Condition66%2/3
$game->result and $game->result eq "Bye"
!ll&&!rl&&r
4408680
MC/DC0%0/2
$game->result and $game->result eq "Bye"
$game->result $game->result eq "Bye"
Truth table66%2/3
A and B
A= $game->resultB= $game->result eq "Bye"
ABresult
0X0
100
111
475 1308 my $role = $game->myRole($player);
476 die
477 "Player $id, $player->{name}'s role is $role, in round $round?"
478 1308 ▶ unless any { $_ eq $role } ROLES, 'Bye';
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $role; } , ("White", "Black"), "Bye") 0 1308 1308
479 next if $game->result and exists $game->result->{$role} and
480 1308 ▶ $game->result->{$role} eq 'Forfeit';
Branch: 1 branch
BranchTrueFalseTotal
if $game->result and exists $game->result->{$role} and $game->result->{$role} eq "Forfeit" 0 1308 1308
Condition66%4/6
$game->result and exists $game->result->{$role} and $game->result->{$role} eq "Forfeit"
!ll&&!rl&&r
4408680
$game->result and exists $game->result->{$role}
!ll&&!rl&&r
4400868
MC/DC0%0/3
$game->result and exists $game->result->{$role} and $game->result->{$role} eq "Forfeit"
$game->result exists $game->result->{$role} $game->result->{$role} eq "Forfeit"
Truth table50%2/4
A and B and C
A= $game->resultB= exists $game->result->{$role}C= $game->result->{$role} eq "Forfeit"
ABCresult
0XX0
10X0
1100
1111
481 1308 ▶ if ( any { $role eq $_ } ROLES ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $role eq $_; } , ("White", "Black"))) 1270 38 1308
482 1270 ▶ my $otherRole = first { $role ne $_ } ROLES;
Subroutine __ANON__: called CC=1 SCAR=0.0
483 1270 my $opponent = $game->contestants->{$otherRole};
484 1270 $dupes->{$id}->{ $opponent->id } = $round;
485 }
486 }
487 ▶ elsif ( $player->firstround > $round or
Condition33%1/3
$player->firstround > $round or &any(sub { $id eq $_; } , @absenteeids)
l!l&&r!l&&!r
400
MC/DC0%0/2
$player->firstround > $round or &any(sub { $id eq $_; } , @absenteeids)
$player->firstround > $round &any(sub { $id eq $_; } , @absenteeids)
Truth table33%1/3
A or B
A= $player->firstround > $roundB= &any(sub { $id eq $_; } , @absenteeids)
ABresult
1X1
011
000
488 0 ▶ any { $id eq $_ } @absenteeids ) { next }
Subroutine __ANON__: not called CC=1 SCAR=1.2
489 0 else { warn "Player ${id} game in round $round?"; }
490 }
491 }
492 70 return $dupes;
493 }
494  
495  
496 =head2 colorClashes
497  
498 $nomatch = $tourney->colorClashes
499 next if $nomatch->{$alekhine->id}->{$capablanca->id}
500  
501 Returns an anonymous hash, keyed on the ids of the tourney's entrants, of a color (role) if 2 of the individual @grandmasters both have an absolute preference for it in the next round and so can't play each other (themselves). Don't forget to collect scorecards in the appropriate games first! B2
502  
503 =cut
504  
505 62 ▶sub colorClashes {
Subroutine colorClashes: called CC=8 SCAR=20.8
Pod documented
506 62 my $self = shift;
507 62 my $players = $self->entrants;
508 62 my @id = map { $_->id } @$players;
509 62 my $clashes;
510 62 for my $player ( 0 .. $#$players ) {
511 464 for ( 0 .. $#$players ) {
512 5190 ▶ $clashes->{ $id[$player] }->{ $id[$_] } =
Branch: 1 branch
BranchTrueFalseTotal
if $players->[$player]->preference->role and $players->[$_]->preference->role and $players->[$player]->preference->role eq $players->[$_]->preference->role and $players->[$player]->preference->strength eq "Absolute" and $players->[$player]->preference->strength eq $players->[$_]->preference->strength 241 4949 5190
Condition100%12/12
$players->[$player]->preference->role and $players->[$_]->preference->role and $players->[$player]->preference->role eq $players->[$_]->preference->role and $players->[$player]->preference->strength eq "Absolute" and $players->[$player]->preference->strength eq $players->[$_]->preference->strength
!ll&&!rl&&r
4735214241
$players->[$player]->preference->role and $players->[$_]->preference->role and $players->[$player]->preference->role eq $players->[$_]->preference->role and $players->[$player]->preference->strength eq "Absolute"
!ll&&!rl&&r
28271908455
$players->[$player]->preference->role and $players->[$_]->preference->role and $players->[$player]->preference->role eq $players->[$_]->preference->role
!ll&&!rl&&r
48523422363
$players->[$player]->preference->role and $players->[$_]->preference->role
!ll&&!rl&&r
3221634705
MC/DC100%5/5
$players->[$player]->preference->role and $players->[$_]->preference->role and $players->[$player]->preference->role eq $players->[$_]->preference->role and $players->[$player]->preference->strength eq "Absolute" and $players->[$player]->preference->strength eq $players->[$_]->preference->strength
$players->[$player]->preference->role $players->[$_]->preference->role $players->[$player]->preference->role eq $players->[$_]->preference->role $players->[$player]->preference->strength eq "Absolute" $players->[$player]->preference->strength eq $players->[$_]->preference->strength
Truth table100%6/6
A and B and C and D and E
A= $players->[$player]->preference->roleB= $players->[$_]->preference->roleC= $players->[$player]->preference->role eq $players->[$_]->preference->roleD= $players->[$player]->preference->strength eq "Absolute"E= $players->[$player]->preference->strength eq $players->[$_]->preference->strength
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
513 $players->[$player]->preference->role
514 if $players->[$player]->preference->role
515 and $players->[$_]->preference->role
516 and $players->[$player]->preference->role eq
517 $players->[$_]->preference->role
518 and $players->[$player]->preference->strength eq 'Absolute'
519 and $players->[$player]->preference->strength eq
520 $players->[$_]->preference->strength;
521 }
522 }
523 62 return $clashes;
524 }
525  
526 =head2 byesGone
527  
528 next if $tourney->byesGone($grandmasters)
529  
530 Returns an anonymous hash of either the round in which the tourney's entrants had a 'Bye' or the empty string, keyed on @$grandmasters' ids. If a grandmaster had more than one bye, the last one is returned. Don't forget to collect scorecards in the appropriate games first! B1
531  
532 =cut
533  
534  
535 62 ▶sub byesGone {
Subroutine byesGone: called CC=10 SCAR=24.3
Pod documented
536 62 my $self = shift;
537 62 my $players = $self->entrants;
538 62 my @ids = map { $_->id } @$players;
539 62 my $absentees = $self->absentees;
540 0 my @absenteeids = map { $_->id } @$absentees;
541 62 my $play = $self->play;
542 62 my $byes = {};
543 62 my $round = $self->round;
544 62 for my $round ( FIRSTROUND .. $round ) {
545 116 for my $id (@ids) {
546 1120 my $player = $self->ided($id);
547 1120 my $game = $play->{$round}->{$id};
548 1120 ▶ if ( $game and $game->can("myRole") ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($game and $game->can('myRole')) { } 1116 4 1120
elsif ($player->firstround > $round or &any(sub { $id eq $_; } , @absenteeids)) { } 4 0 4
Condition66%2/3
$game and $game->can("myRole")
!ll&&!rl&&r
401116
MC/DC50%1/2
$game and $game->can("myRole")
$game $game->can("myRole")
Truth table66%2/3
A and B
A= $gameB= $game->can("myRole")
ABresult
0X0
100
111
549 1116 eval { $game->myRole($player) };
550 1116 ▶ die "Role of player $id in round $round? $@"
Branch: 1 branch
BranchTrueFalseTotal
if not $player or $@ 0 1116 1116
Condition33%1/3
not $player or $@
l!l&&r!l&&!r
001116
MC/DC0%0/2
not $player or $@
not $player $@
Truth table33%1/3
not(A) or B
A= not $playerB= $@
ABresult
1X1
011
000
551 if not $player or $@;
552 1116 my $role = $game->myRole($player);
553 1116 ▶ if ( $role eq 'Bye' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq "Bye") 38 1078 1116
554 38 $byes->{$id} = $round;
555 }
556 }
557 ▶ elsif ( $player->firstround > $round or
Condition33%1/3
$player->firstround > $round or &any(sub { $id eq $_; } , @absenteeids)
l!l&&r!l&&!r
400
MC/DC0%0/2
$player->firstround > $round or &any(sub { $id eq $_; } , @absenteeids)
$player->firstround > $round &any(sub { $id eq $_; } , @absenteeids)
Truth table33%1/3
A or B
A= $player->firstround > $roundB= &any(sub { $id eq $_; } , @absenteeids)
ABresult
1X1
011
000
558 0 ▶ any { $id eq $_ } @absenteeids ) { next }
Subroutine __ANON__: not called CC=1 SCAR=1.2
559 0 else { warn "Player ${id} had Bye in round $round?"; }
560 }
561 }
562 62 return $byes;
563 }
564  
565 =head2 incompatibles
566  
567 $nomatch = $tourney->incompatibles(@grandmasters)
568 next if $nomatch->{$alekhine->id}->{$capablanca->id}
569  
570 Collates information from the whoPlayedWho and colorClashes methods to show who cannot be matched or given a bye in the next round, returning an anonymous hash keyed on the ids of @grandmasters. B1,2 C1,6
571  
572 =cut
573  
574 0 ▶sub incompatibles {
Subroutine incompatibles: not called CC=6 SCAR=37.4
Pod documented
575 0 my $self = shift;
576 0 my $oldOpponents = $self->whoPlayedWho;
577 0 my $colorIncompatible = $self->colorClashes;
578 0 my $players = $self->entrants;
579 0 my @id = map { $_->id } @$players;
580 0 my $unavailables;
581 0 for my $player ( 0 .. $#$players ) {
582 0 for ( 0 .. $#$players ) {
583 0 my $color = $colorIncompatible->{ $id[$player] }->{ $id[$_] };
584 0 my $round = $oldOpponents->{ $id[$player] }->{ $id[$_] };
585 0 ▶ $unavailables->{ $id[$player] }->{ $id[$_] } = $color if $color;
Branch: 1 branch
BranchTrueFalseTotal
if $color 0 0 0
586 0 ▶ $unavailables->{ $id[$player] }->{ $id[$_] } ||= $round if $round;
Branch: 1 branch
BranchTrueFalseTotal
if $round 0 0 0
Condition0%0/3
$$unavailables{$id[$player]}{$id[$_]} ||= $round
l!l&&r!l&&!r
000
MC/DC0%0/2
$$unavailables{$id[$player]}{$id[$_]} ||= $round
$$unavailables{$id[$player]}{$id[$_]} $round
Truth table0%0/3
A ||= B
A= $$unavailables{$id[$player]}{$id[$_]}B= $round
ABresult
1X1
011
000
587 }
588 }
589 0 return $unavailables;
590 }
591  
592  
593 =head2 medianScore
594  
595 $group = $tourney->medianScore($round)
596  
597 Returns the score equal to half the number of rounds that have been played. Half the contestants will have scores above or equal to this score and half will have ones equal to or below it, assuming everyone has played every round. What IS the number of rounds played, again?
598  
599 =cut
600  
601 4 ▶sub medianScore {
Subroutine medianScore: called CC=1 SCAR=0.0
Pod documented
602 4 my $self = shift;
603 4 my $round = shift;
604 4 return $round / 2;
605 }
606  
607 =head2 rounds
608  
609 $tourney->rounds
610  
611 Gets/sets the total number of rounds to be played in the competition
612  
613 =cut
614  
615 0 ▶sub rounds {
Subroutine rounds: not called CC=3 SCAR=24.8
Pod documented
616 0 my $self = shift;
617 0 my $rounds = shift;
618 0 ▶ if ( defined $rounds ) { $self->{rounds} = $rounds; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rounds) { } 0 0 0
elsif ($self->{'rounds'}) { } 0 0 0
619 0 elsif ( $self->{rounds} ) { return $self->{rounds}; }
620 }
621  
622  
623 =head2 size
624  
625 $size = 'Maxi' if $tourney->size > 2**$tourney->rounds
626  
627 Gets the number of entrants
628  
629 =cut
630  
631 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
632 0 my $self = shift;
633 0 return scalar @{ $self->entrants };
634 }
635  
636 =head1 AUTHOR
637  
638 Dr Bean, C<< <drbean, followed by the at mark (@), cpan, then a dot, and finally, org> >>
639  
640 =head1 BUGS
641  
642 Please report any bugs or feature requests to
643 C<bug-games-tournament-swiss at rt.cpan.org>, or through the web interface at
644 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Tournament-Swiss>.
645 I will be notified, and then you'll automatically be notified of progress on
646 your bug as I make changes.
647  
648 =head1 SUPPORT
649  
650 You can find documentation for this module with the perldoc command.
651  
652 perldoc Games::Tournament::Swiss
653  
654 You can also look for information at:
655  
656 =over 4
657  
658 =item * AnnoCPAN: Annotated CPAN documentation
659  
660 L<http://annocpan.org/dist/Games-Tournament-Swiss>
661  
662 =item * CPAN Ratings
663  
664 L<http://cpanratings.perl.org/d/Games-Tournament-Swiss>
665  
666 =item * RT: CPAN's request tracker
667  
668 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Tournament-Swiss>
669  
670 =item * Search CPAN
671  
672 L<http://search.cpan.org/dist/Games-Tournament-Swiss>
673  
674 =back
675  
676 =head1 ACKNOWLEDGEMENTS
677  
678 See L<http://www.fide.com/official/handbook.asp?level=C04> for the FIDE's Swiss rules.
679  
680 =head1 COPYRIGHT & LICENSE
681  
682 Copyright 2006 Dr Bean, all rights reserved.
683  
684 This program is free software; you can redistribute it and/or modify it
685 under the same terms as Perl itself.
686  
687 =cut
688  
689 32 1; # End of Games::Tournament::Swiss
690  
691 # vim: set ts=8 sts=4 sw=4 noet:
« blib/lib/Games/Tournament/Swiss/Procedure/FIDE.pm ↑ Summary blib/lib/Games/Tournament/Contestant.pm »