DataObjects/Tournament.pm

filter: Statementstmt 49.7% 97 / 195 Branchbran 14.5% 9 / 62 Conditioncond 16.0% 4 / 25 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 66.6% 24 / 36 Podpod 21.0% 4 / 19 totaltotal 39.3% 139 / 353 CC 50 SCAR 47.6
CC
50
Cov
39%
swap_players_in_round_num
380.0
send_to_AGA
182.0
games
30.0
CRAP 116.7
« DataObjects/Round.pm ↑ Summary DataObjects/Player.pm »
1 #===============================================================================
2 #
3 # FILE: Games::Go::AGA::DataObjects::Tournament.pm
4 #
5 # USAGE: use Games::Go::AGA::DataObjects::Tournament;
6 #
7 # PODNAME: Games::Go::AGA::DataObjects::Tournament
8 # ABSTRACT: models AGA register.tde file information
9 #
10 # AUTHOR: Reid Augustin (REID), <reid@hellosix.com>
11 # CREATED: 11/19/2010 03:13:05 PM PST
12 #===============================================================================
13  
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 package Games::Go::AGA::DataObjects::Tournament;
19 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use parent 'Games::Go::AGA::DataObjects::Register';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Games::Go::AGA::DataObjects::Round;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Games::Go::AGA::DataObjects::Types qw( isa_CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Games::Go::AGA::Parse::Util qw( normalize_ID );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 our $VERSION = '0.152'; # VERSION
33  
34 # public attributes
35 # has 'rounds' => (
36 # isa => 'ArrayRef[Games::Go::AGA::DataObjects::Round]',
37 # is => 'ro',
38 # default => sub { [] }
39 # );
40  
41 # Note: the change callback shouldn't really be necessary since
42 # Directives are changed by the Directives object and player data is
43 # changed by the Player object. But it might be convenient...
44 has change_callback => (
45 isa => \&isa_CodeRef,
46 is => 'rw',
47 lazy => 1,
48 1 ▶ default => sub { sub { } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 );
50 has suppress_changes => ( # don't call change_callback if set
51 is => 'rw',
52 lazy => 1,
53 1 ▶ default => sub { 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1 );
55 has fprint_pending => ( # set when changed called, cleared after fprint done
56 is => 'rw',
57 lazy => 1,
58 0 ▶ default => sub { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 1 );
60  
61 1 ▶sub clear_rounds { $_[0]->{rounds} = [ undef ]; } # rounds start at 1
Subroutine clear_rounds: called CC=1 SCAR=0.0
Pod undocumented
62  
63 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
64 1 my ($self) = @_;
65 1 $self->clear_rounds;
66 }
67  
68 11 ▶sub changed {
Subroutine changed: called CC=2 SCAR=7.0
Pod undocumented
69 11 my ($self) = @_;
70  
71 11 $self->fprint_pending(1);
72 11 ▶ &{$self->change_callback}(@_) if (not $self->suppress_changes);
Branch: 1 branch
BranchTrueFalseTotal
if not $self->suppress_changes 11 0 11
73 }
74  
75 1 ▶sub add_round {
Subroutine add_round: called CC=6 SCAR=21.2
Pod documented
76 1 my ($self, $round_num, $round, $replace) = @_;
77  
78 1 ▶ if ($round_num <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($round_num <= 0) { } 0 1 1
elsif ($round_num > 1 and not defined $self->{'rounds'}[$round_num - 1]) { } 0 2 2
79 0 croak "Round number must be >= 1\n";
80 }
81 ▶ elsif ($round_num > 1 and # allow round 1
Condition33%1/3
$round_num > 1 and not defined $self->{"rounds"}[$round_num - 1]
!ll&&!rl&&r
100
MC/DC0%0/2
$round_num > 1 and not defined $self->{"rounds"}[$round_num - 1]
$round_num > 1 not defined $self->{"rounds"}[$round_num - 1]
Truth table33%1/3
A and not(B)
A= $round_num > 1B= not defined $self->{"rounds"}[$round_num - 1]
ABresult
0X0
100
111
82 not defined $self->{rounds}[$round_num - 1]) {
83 0 croak "Can't add round $round_num, previous round doesn't exist yet\n";
84 }
85 1 my $rm_round = $self->{rounds}[$round_num];
86 1 ▶ if ( defined $rm_round
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rm_round and not $replace) 0 1 1
Condition33%1/3
defined $rm_round and not $replace
!ll&&!rl&&r
100
MC/DC0%0/2
defined $rm_round and not $replace
defined $rm_round not $replace
Truth table33%1/3
A and not(B)
A= defined $rm_roundB= not $replace
ABresult
0X0
100
111
87 and not $replace) {
88 0 croak "$round_num already exists\n";
89 }
90 1 my $prev_callback = $round->change_callback;
91 $round->change_callback(
92 7 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 7 delete $self->{player_stats}; # force re-count
94 7 $prev_callback->(@_);
95 7 $self->changed;
96 }
97 1 );
98 1 $self->{rounds}[$round_num] = $round;
99 # $self->changed; # rounds are recorded in N.tde files, not in register.tde
100 }
101  
102 # NOTE: returns games for ALL ROUNDS up to and including $round_num
103 0 ▶sub games {
Subroutine games: not called CC=5 SCAR=34.0
Pod undocumented
104 0 my ($self, $round_num) = @_;
105  
106 0 ▶ $round_num = $#{$self->{rounds}} if (not defined $round_num);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $round_num 0 0 0
107 0 my @games;
108 0 foreach my $r_num (1 .. $round_num) {
109 0 my $round = $self->{rounds}[$r_num];
110 0 ▶ next if (not $round);
Branch: 1 branch
BranchTrueFalseTotal
unless $round 0 0 0
111 0 push @games, $round->games;
112 }
113 return wantarray
114 ? @games
115 0 ▶ : \@games;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
116 }
117  
118 3 ▶sub rounds {
Subroutine rounds: called CC=1 SCAR=0.0
Pod documented
119 3 my ($self) = @_;
120  
121 3 return $#{$self->{rounds}}; # we don't count 0
122 }
123  
124 8 ▶sub round {
Subroutine round: called CC=2 SCAR=7.6
Pod documented
125 8 my ($self, $round_num) = @_;
126  
127 8 ▶ if (not defined $self->{rounds}[$round_num]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"rounds"}[$round_num]) 0 8 8
128 0 croak "Round $round_num doesn't exist\n";
129 }
130 8 return $self->{rounds}[$round_num];
131 }
132  
133 #return unpaired players for a round, sorted by rank
134 0 ▶sub unpaired_in_round_num {
Subroutine unpaired_in_round_num: not called CC=3 SCAR=24.8
Pod undocumented
135 0 my ($self, $round_num) = @_;
136  
137 0 my %un_paired = map { $_->id => $_ } @{$self->players}; # players by ID
138 0 my $round = $self->round($round_num);
139 0 for my $game (@{$round->games}) {
140 0 delete $un_paired{$game->white->id};
141 0 delete $un_paired{$game->black->id};
142 }
143 0 my @unpaired = sort { $b->rating <=> $a->rating } values %un_paired;
144 return wantarray
145 ? @unpaired
146 0 ▶ : \@unpaired;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
147 }
148  
149 0 ▶sub swap_players_in_round_num {
Subroutine swap_players_in_round_num: not called CC=19 SCAR=59.4
Pod undocumented
150 0 my ($self, $p0, $p1, $round_num) = @_;
151  
152 0 $p0 = $self->get_player($p0); # ensure both players are valid
153 0 $p1 = $self->get_player($p1);
154 0 ▶ my $round = $self->round($round_num)
Condition0%0/2
$self->round($round_num) || die("No round number $round_num in this tournament")
l!l
00
MC/DC0%0/1
$self->round($round_num) || die("No round number $round_num in this tournament")
$self->round($round_num)
Truth table0%0/2
A
A= $self->round($round_num)
Aresult
11
00
155 || die("No round number $round_num in this tournament");
156 0 my $id0 = $p0->id;
157 0 my $id1 = $p1->id;
158 0 my @games;
159 0 for my $game (@{$round->games}) {
160 0 ▶ push @games, $game if ($game->white->id eq $id0 or $game->black->id eq $id0);
Branch: 1 branch
BranchTrueFalseTotal
if $game->white->id eq $id0 or $game->black->id eq $id0 0 0 0
Condition0%0/3
$game->white->id eq $id0 or $game->black->id eq $id0
l!l&&r!l&&!r
000
MC/DC0%0/2
$game->white->id eq $id0 or $game->black->id eq $id0
$game->white->id eq $id0 $game->black->id eq $id0
Truth table0%0/3
A or B
A= $game->white->id eq $id0B= $game->black->id eq $id0
ABresult
1X1
011
000
161 0 ▶ push @games, $game if ($game->white->id eq $id1 or $game->black->id eq $id1);
Branch: 1 branch
BranchTrueFalseTotal
if $game->white->id eq $id1 or $game->black->id eq $id1 0 0 0
Condition0%0/3
$game->white->id eq $id1 or $game->black->id eq $id1
l!l&&r!l&&!r
000
MC/DC0%0/2
$game->white->id eq $id1 or $game->black->id eq $id1
$game->white->id eq $id1 $game->black->id eq $id1
Truth table0%0/3
A or B
A= $game->white->id eq $id1B= $game->black->id eq $id1
ABresult
1X1
011
000
162 }
163 0 ▶ return if not @games; # both in unpaired list?
Branch: 1 branch
BranchTrueFalseTotal
unless @games 0 0 0
164 0 ▶ if (@games > 2) { # if a player is in more than one game
Branch: 1 branch
BranchTrueFalseTotal
if (@games > 2) 0 0 0
165 0 die "Too many games. Please un-pair and re-pair games instead";
166 }
167 # two games if both are playing, one if there is a bye
168 0 ▶ if ($games[0]->white->id eq $id0) { $games[0]->white($p1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($games[0]->white->id eq $id0) { } 0 0 0
elsif ($games[0]->white->id eq $id1) { } 0 0 0
169 0 elsif ($games[0]->white->id eq $id1) { $games[0]->white($p0) }
170 0 ▶ if ($games[0]->black->id eq $id0) { $games[0]->black($p1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($games[0]->black->id eq $id0) { } 0 0 0
elsif ($games[0]->black->id eq $id1) { } 0 0 0
171 0 elsif ($games[0]->black->id eq $id1) { $games[0]->black($p0) }
172 0 $games[0]->handicap;
173 0 ▶ if (@games == 2 and refaddr($games[0]) != refaddr($games[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@games == 2 and builtin::refaddr($games[0]) != builtin::refaddr($games[1])) 0 0 0
Condition0%0/3
@games == 2 and builtin::refaddr($games[0]) != builtin::refaddr($games[1])
!ll&&!rl&&r
000
MC/DC0%0/2
@games == 2 and builtin::refaddr($games[0]) != builtin::refaddr($games[1])
@games == 2 builtin::refaddr($games[0]) != builtin::refaddr($games[1])
Truth table0%0/3
A and B
A= @games == 2B= builtin::refaddr($games[0]) != builtin::refaddr($games[1])
ABresult
0X0
100
111
174 0 ▶ if ($games[1]->white->id eq $id0) { $games[1]->white($p1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($games[1]->white->id eq $id0) { } 0 0 0
elsif ($games[1]->white->id eq $id1) { } 0 0 0
175 0 elsif ($games[1]->white->id eq $id1) { $games[1]->white($p0) }
176 0 ▶ if ($games[1]->black->id eq $id0) { $games[1]->black($p1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($games[1]->black->id eq $id0) { } 0 0 0
elsif ($games[1]->black->id eq $id1) { } 0 0 0
177 0 elsif ($games[1]->black->id eq $id1) { $games[1]->black($p0) }
178 0 $games[1]->handicap;
179 }
180 }
181  
182 0 ▶sub clear_stats {
Subroutine clear_stats: not called CC=1 SCAR=6.9
Pod undocumented
183 0 my ($self) = @_;
184  
185 0 delete $self->{player_stats};
186 }
187  
188 8 ▶sub player_stats {
Subroutine player_stats: called CC=7 SCAR=19.9
Pod undocumented
189 8 my ($self, $id, $stat) = @_;
190  
191 8 ▶ if (not defined $self->{player_stats}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"player_stats"}) 2 6 8
192 2 my %players;
193 2 for my $round_num (1 .. $self->rounds) {
194 2 my $round = $self->round($round_num);
195 2 for my $game (@{$round->games}) {
196 4 my $white = $game->white;
197 4 my $black = $game->black;
198 4 my $wid = $white->id;
199 4 my $bid = $black->id;
200 4 push @{$players{$wid}{games}}, $game;
201 4 push @{$players{$bid}{games}}, $game;
202 4 ▶ if (not defined $game->winner) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $game->winner) 0 4 4
203 0 push @{$players{$wid}{no_result}}, $black;
204 0 push @{$players{$bid}{no_result}}, $white;
205 0 next;
206 }
207 4 my $win_id = $game->winner->id;
208 4 my $los_id = $game->loser->id;
209 4 push @{$players{$win_id}{wins}}, $game;
210 4 push @{$players{$win_id}{defeated}}, $game->loser;
211 4 push @{$players{$los_id}{losses}}, $game;
212 4 push @{$players{$los_id}{defeated_by}}, $game->winner;
213 }
214 }
215 2 $self->{player_stats} = \%players;
216 }
217 8 $id = normalize_ID($id);
218 8 ▶ $self->{player_stats}{$id}{$stat} ||= [];
Condition100%2/2
$self->{"player_stats"}{$id}{$stat} ||= []
l!l
44
MC/DC100%1/1
$self->{"player_stats"}{$id}{$stat} ||= []
$self->{"player_stats"}{$id}{$stat}
Truth table100%2/2
A
A= $self->{"player_stats"}{$id}{$stat}
Aresult
11
00
219 return wantarray
220 0 ? @{$self->{player_stats}{$id}{$stat}}
221 8 ▶ : $self->{player_stats}{$id}{$stat};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 8 8
222 }
223  
224 0 ▶sub player_games { shift->player_stats(@_, 'games'); }
Subroutine player_games: not called CC=1 SCAR=6.9
Pod undocumented
225 8 ▶sub player_wins { shift->player_stats(@_, 'wins'); }
Subroutine player_wins: called CC=1 SCAR=0.0
Pod undocumented
226 0 ▶sub player_losses { shift->player_stats(@_, 'losses'); }
Subroutine player_losses: not called CC=1 SCAR=6.9
Pod undocumented
227 0 ▶sub player_no_result { shift->player_stats(@_, 'no_result'); }
Subroutine player_no_result: not called CC=1 SCAR=6.9
Pod undocumented
228 0 ▶sub player_defeated { shift->player_stats(@_, 'defeated'); }
Subroutine player_defeated: not called CC=1 SCAR=6.9
Pod undocumented
229 0 ▶sub player_defeated_by { shift->player_stats(@_, 'defeated_by'); }
Subroutine player_defeated_by: not called CC=1 SCAR=6.9
Pod undocumented
230  
231 0 ▶sub send_to_AGA {
Subroutine send_to_AGA: not called CC=13 SCAR=52.0
Pod documented
232 0 my ($self, $fd) = @_;
233  
234 0 ▶ if (not $fd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fd) 0 0 0
235 0 ▶ $fd = IO::String->new() or die "Failed to create IO::String\n";
Branch: 1 branch
BranchTrueFalseTotal
$fd = "IO::String"->new or die "Failed to create IO::String\n" 0 0 0
236 }
237  
238 0 $fd->printf("TOURNEY %s\n",
239 $self->get_directive_value('TOURNEY'));
240  
241 0 my $date = $self->get_directive_value('DATE');
242 0 my ($start, $finish) = $date =~ m/^(\S+)[\-\s]+(\S+)$/;
243 0 ▶ $start ||= $date;
Condition0%0/3
$start ||= $date
l!l&&r!l&&!r
000
MC/DC0%0/2
$start ||= $date
$start $date
Truth table0%0/3
A ||= B
A= $startB= $date
ABresult
1X1
011
000
244 0 ▶ $finish ||= $start;
Condition0%0/3
$finish ||= $start
l!l&&r!l&&!r
000
MC/DC0%0/2
$finish ||= $start
$finish $start
Truth table0%0/3
A ||= B
A= $finishB= $start
ABresult
1X1
011
000
245 0 $start =~ s/\D/\//g; # use slash date notation
246 0 $finish =~ s/\D/\//g;
247 0 $fd->print(" start=$start\n"),
248 $fd->print(" finish=$finish\n"),
249  
250 $fd->printf(" rules=%s\n",
251 $self->get_directive_value('RULES'));
252 0 $fd->print("\nPLAYERS\n");
253  
254 # print player info
255 0 my $name_width = 5;
256 0 for my $player ($self->players) {
257 0 ▶ $name_width = length($player->full_name)
Branch: 1 branch
BranchTrueFalseTotal
if length $player->full_name > $name_width 0 0 0
258 if (length($player->full_name) > $name_width);
259 }
260  
261 0 for my $player ($self->players) {
262 0 $fd->printf("%9.9s %*.*s %s\n",
263 $player->id,
264 $name_width,
265 $name_width,
266 $player->full_name,
267 $player->rating,
268 );
269 }
270  
271 # print games with results
272 0 $fd->print("\nGAMES\n");
273 0 for my $round (@{$self->{rounds}}) {
274 0 ▶ next if (not defined $round);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $round 0 0 0
275 0 for my $game ($round->games) {
276 0 ▶ if ($game->winner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->winner) 0 0 0
277 0 ▶ my $result = ($game->winner->id eq $game->white->id) ? 'W' : 'B';
Branch: 1 branch
BranchTrueFalseTotal
$game->winner->id eq $game->white->id ? : 0 0 0
278 0 $fd->printf("%9.9s %9.9s $result %s %s\n",
279 $game->white->id,
280 $game->black->id,
281 $game->handi,
282 $game->komi,
283 );
284 }
285 }
286 }
287 0 $fd->print("\n");
288  
289 0 return $fd
290 }
291  
292 # this really shouldn't be necessary. Register and Directives will
293 # fprint the register.tde files, and Round will fprint the N.tde files.
294 0 ▶sub fprint {
Subroutine fprint: not called CC=1 SCAR=6.9
Pod undocumented
295 0 my ($self, $fh) = @_;
296  
297 0 $self->SUPER::fprint($fh); # print the register.tde file
298 0 $self->fprint_pending(0);
299 }
300  
301 1 1;
302  
303 __END__
« DataObjects/Round.pm ↑ Summary DataObjects/Player.pm »