blib/lib/Games/Go/AGATourn.pm

filter: Statementstmt 63.8% 276 / 432 Branchbran 42.3% 110 / 260 Conditioncond 33.3% 11 / 33 MC/DCmcdc 9.5% 2 / 21 Subroutinesub 70.7% 29 / 41 Podpod 100% 34 / 34 totaltotal 56.2% 462 / 821 CC 149 SCAR 34.0
CC
149
Cov
55%
ParseTdListLine
112.0
Handicap
110.0
Sigma
56.0
CRAP 29.9
↑ Summary
1 # $Id: AGATourn.pm,v 1.35 2005/01/24 04:32:17 reid Exp $
2  
3 # AGATourn
4 #
5 # Copyright (C) 1999, 2004, 2005 Reid Augustin reid@netchip.com
6 # 1000 San Mateo Dr.
7 # Menlo Park, CA 94025 USA
8 #
9 # This library is free software; you can redistribute it and/or modify it
10 # under the same terms as Perl itself, either Perl version 5.8.5 or, at your
11 # option, any later version of Perl 5 you may have available.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE.
16 #
17  
18 =head1 NAME
19  
20 AGATourn - Perl extensions to ease the pain of using AGA tournament data files.
21  
22 =head1 SYNOPSIS
23  
24 use Games::Go::AGATourn;
25  
26 my $agaTourn = B<Games::Go::AGATourn-E<gt>new> (options);
27  
28 =head1 DESCRIPTION
29  
30 An AGATourn object represents a round or several rounds of an American Go
31 Association tournament. There are methods for parsing several type of AGA
32 file format:
33  
34 =over 4
35  
36 =item tdlist
37  
38 The entire list of AGA members including playing strength, club affiliation,
39 and some other stuff.
40  
41 =item register.tde
42  
43 The starting point for a tournament. All players in a tournament must be
44 entered in the register.tde file.
45  
46 =item round results: 1.tde, 2.tde, etc.
47  
48 Game results for each round of the tournament.
49  
50 =back
51  
52 A note on IDs: in general, hashes in an AGATourn object are keyed by the AGA
53 ID. An AGA ID consists of a three letter country specifier (like USA or TMP
54 for temporary IDs) concatenated to an integer. Here we specify the three
55 letter country specifier as the 'country' and the integer part as the
56 'agaNum'. The country concatenated with the agaNum is the ID. My ID for
57 example is USA2122. IDs should be normalized (capitalize the country part and
58 remove preceding 0s from the agaNum part) with the B<NormalizeID> method
59 (below).
60  
61 Note also that some programs may accept limited integers in the agaNum part of
62 the ID. Accelerat, for example, seems to accept only up to 32K (someone used
63 a signed short somewhere?)
64  
65 =cut
66  
67 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 require 5.001;
69  
70 package Games::Go::AGATourn;
71 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 1 our @ISA = qw(Exporter);
75  
76 # Items to export into callers namespace by default. Note: do not export
77 # names by default without a very good reason. Use EXPORT_OK instead.
78 # Do not simply export all your public functions/methods/constants.
79  
80 # This allows declaration use PackageName ':all';
81 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
82 # will save memory.
83 1 our %EXPORT_TAGS = ( 'all' => [ qw(
84 ) ] );
85  
86 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
87  
88 1 our @EXPORT = qw(
89 );
90  
91 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 our $VERSION = sprintf "%d.%03d", '$Revision: 1.35 $' =~ /(\d+)/g;
93 }
94  
95 ######################################################
96 #
97 # Class Variables
98 #
99 #####################################################
100  
101 1 ▶use constant NOTARANK => -99.9; # illegal rank or rating
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 ######################################################
104 #
105 # Public methods
106 #
107 #####################################################
108  
109 =head1 METHODS
110  
111 =over 4
112  
113 =item my $agaTourn = B<Games::Go::AGATourn-E<gt>new> (options)
114  
115 A B<new> AGATourn by default reads the B<register_tde> file to get the name,
116 rank, and AGA numbers for all the players in the tournament. It then reads
117 all available game results (B<Round> files: 1.tde, 2.tde, etc.) and the game
118 data is incorporated into the AGATourn object.
119  
120 =head2 Options:
121  
122 =over 4
123  
124 =item B<Round>
125  
126 Round file number to read. If B<Round> is 0, no round files are read. If
127 B<Round> is 1 or greater, only the one round file will be read. If B<round>
128 is undef (or not specified), all existing round files are read. Round files
129 should be named I<1.tde>, I<2.tde>, etc.
130  
131 Default: undef
132  
133 =item B<register_tde>
134  
135 Name of register.tde file. Use undef to prevent reading the register.tde
136 file. Changing the name of this file is probably a bad idea.
137  
138 Default 'register.tde' (in the current directory)
139  
140 =item B<nameLength>
141  
142 Starting length of name field. While reading the register file (see
143 B<ReadRegisterFile> below), B<nameLength> grows to reflect the longest name
144 seen so far (see B<NameLength> method below).
145  
146 Default: 0
147  
148 =item B<defaultCountry>
149  
150 Default three-letter country name.
151  
152 The tdlist file does not include country information in the ID, so the
153 B<ParseTdListLine> method returns country => B<defaultCountry>.
154  
155 Default: 'USA'
156  
157 =back
158  
159 =cut
160  
161 2 ▶sub new {
Subroutine new: called CC=9 SCAR=26.0
Pod documented
162 2 my ($proto, %args) = @_;
163  
164 2 my $self = {};
165 2 ▶ bless($self, ref($proto) || $proto);
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
166 2 $self->{defaultCountry} = 'USA';
167 2 $self->Clear;
168 # transfer user args
169 2 foreach (keys(%args)) {
170 4 $self->{$_} = $args{$_};
171 }
172 2 ▶ if (defined($self->{register_tde})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"register_tde"}) 1 1 2
173 1 ▶ return(undef) unless($self->ReadRegisterFile($self->{register_tde}));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ReadRegisterFile($self->{"register_tde"}) 0 1 1
174 }
175 2 ▶ if (defined($self->{register_tde})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"register_tde"}) 1 1 2
176 1 ▶ if (defined($self->{Round})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Round'}) { } 1 0 1
177 1 ▶ if ($self->{Round} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Round"} > 0) 0 1 1
178 0 $self->ReadRoundFile("$self->{Round}.tde");
179 }
180 } else {
181 0 my $round = 1;
182 0 ▶ while (-f "$round.tde") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$round.tde") 0 0 0
183 0 $self->{Round} = $round;
184 0 $self->ReadRoundFile("$self->{Round}.tde");
185 0 $round++;
186 }
187 }
188 }
189 2 return($self);
190 }
191  
192 =item $agaTourn-E<gt>B<Clear>
193  
194 Clears AGATourn database.
195  
196 =cut
197  
198 2 ▶sub Clear {
Subroutine Clear: called CC=1 SCAR=0.0
Pod documented
199 2 my ($self) = @_;
200  
201 # set defaults
202 2 $self->{Round} = undef;
203 2 $self->{register_tde} = "register.tde"; # default
204 2 $self->{Directive}{ROUNDS}[0] = 1; # I hope there's at least one!
205 2 $self->{Directive}{TOURNEY}[0] = "Unknown tournament";
206 2 $self->{nameLength} = 0;
207 2 $self->{Name} = {}; # empty hash
208 2 $self->{Rating} = {};
209 2 $self->{Rank} = {};
210 2 $self->{Comment} = {};
211 2 $self->{Wins} = {};
212 2 $self->{Losses} = {};
213 2 $self->{NoResults} = {};
214 2 $self->{Played} = {};
215 2 $self->{gameAllList} = []; # empty array
216 2 $self->{error} = 0;
217 }
218  
219 =item my $hash = $agaTourn-E<gt>B<ParseTdListLine> ($line)
220  
221 Parses a single line from the TDLIST file (the latest TDLIST file
222 should be downloaded from the AGA at http://usgo.org shortly before
223 the tournament, and either the tab-delimited tdlista or the
224 space-delimited versions are accepted). The return value is a
225 reference to a hash of the following values:
226 agaNum => the number part if the ID
227 country => the country part of the ID (always the default
228 country)
229 name => complains if there is no a comma
230 memType => membership type or '' if none
231 agaRating => rating in decimal form, or '' if none
232 agaRank => undef unless rating is a D/K style rank
233 expire => date membership expires or '' if none
234 club => club affiliation or '' if none
235 state => state or '' if none
236  
237 If the line is not parsable, prints a warning and returns undef.
238  
239 =cut
240  
241 # sadly, we need to deal with two formats
242 # old tdlist input looks like this:
243 # name AGA# MmbrTyp Rank expires Club State
244 #Abe, Shozo 2443 L 8603 NJ
245 #Abe, Y. 2043 8312 GA
246 #Abell, John 3605 -1.4 9105 MHGA CO
247 #Abrahms, Judy 1253 L 8012 MGA MA
248 #Abrams, Michael 6779 L -27.4 9411 MIAM FL
249 #Abramson, Allan 101 3.5 9504 NOVA VA
250 # the new format is like this:
251 #Abe, Shozo 2443 Limit 03/28/1986 NJ
252 #Abe, Y. 2043 Full 12/28/1983 GA
253 #Abell, John 3605 Full -1.4 05/28/1991 MHGA CO
254 #Abrahms, Judy 1253 Limit 12/28/1980 MGA MA
255 #
256 # There's also a tab-delimited version
257  
258 1 ▶sub ParseTdListLine {
Subroutine ParseTdListLine: called CC=21 SCAR=47.2
Pod documented
259 1 my ($self, $string) = @_;
260  
261 1 $string =~ s/[\n\r]*$/\t/s; # remove crlf, and tack on an extra tab
262 1 my @fields = $string =~ m/(.*?)\t/g; # is it the tab-delimited version?
263 1 ▶ if (@fields == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields == 9) 0 1 1
264 return {
265 name => $fields[0], # return ref to hash
266 agaNum => $fields[1],
267 memType => $fields[2],
268 agaRating => $fields[3],
269 expire => $fields[4],
270 club => $fields[5],
271 state => $fields[6],
272 sigma => $fields[7],
273 ratingDate => $fields[8],
274 country => $self->{defaultCountry},
275 0 };
276 }
277 # else parse a space-delimited version:
278 1 my ($name, $agaNum, $agaRank, $misc);
279 1 my ($agaRating, $memType, $club, $state, $expire) = (-99, '', '', '', '');
280  
281 1 ▶ unless($string =~ m/^\s*(.*?)\s*(\d+) (.*)/) { # break into manageble groups
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /^\s*(.*?)\s*(\d+) (.*)/) 0 1 1
282 0 carp("Error: can't extract AGA number from \"$string\"\n");
283 0 return(undef);
284 }
285 1 $name = $1; # part before is name
286 1 $agaNum = $2; # middle part is the AGA number
287 1 $misc = $3; # part after match
288 1 ▶ if ($misc =~ m/([\w ]{6}?) ([-\d\. ]{5}) ([\d\/ ]{10}) ([\w ]{4}) (.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($misc =~ m[([\w ]{6}?) ([-\d\. ]{5}) ([\d/ ]{10}) ([\w ]{4}) (.*?)\s*$]) { } 0 1 1
289 # parse by character positions (blanks lined up in the right places)
290 0 $memType = _ws_clean($1);
291 0 $agaRating = _ws_clean($2);
292 0 $expire = _ws_clean($3);
293 0 $club = _ws_clean($4);
294 0 $state = _ws_clean($5);
295 0 ▶ if ($agaRating =~ m/(\d+)([dk])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agaRating =~ /(\d+)([dk])/i) 0 0 0
296 0 $agaRank = uc($agaRating);
297 0 $agaRating = $1 + 0.5;
298 0 ▶ $agaRating = -$agaRating if (uc($2) eq 'K');
Branch: 1 branch
BranchTrueFalseTotal
if uc $2 eq "K" 0 0 0
299 }
300 } else { # try to parse free-form style
301 1 ▶ if ($misc =~ s/^\s*([^\s\d-]+) //) { # membership type, if any
Branch: 2 branches
BranchTrueFalseTotal
if ($misc =~ s/^\s*([^\s\d-]+) //) { } 1 0 1
elsif (not $misc =~ s/^ //) { } 0 0 0
302 1 $memType = $1;
303 } elsif (not $misc =~ s/^ //) {
304 0 carp("Uh oh, no membership type space in: '$misc'");
305 }
306 1 ▶ if ($misc =~ s/^\s*(-?\d+\.\d) //) { # find rank, if any
Branch: 4 branches
BranchTrueFalseTotal
if ($misc =~ s/^\s*(-?\d+\.\d) //) { } 1 0 1
elsif ($misc =~ s/^\s*(\d+)([dkDK]) //) { } 0 0 0
elsif ($misc =~ s/^\s*(-?\d\d?) //) { } 0 0 0
elsif (not $misc =~ s/^ //) { } 0 0 0
307 1 $agaRating = $1;
308 } elsif ($misc =~ s/^\s*(\d+)([dkDK]) //) { # 4D or 15k type rank
309 0 $agaRank = uc("$1$2");
310 0 $agaRating = $1 + 0.5;
311 0 ▶ $agaRating = -$agaRating if (uc($2) eq 'K');
Branch: 1 branch
BranchTrueFalseTotal
if uc $2 eq "K" 0 0 0
312 } elsif ($misc =~ s/^\s*(-?\d\d?) //) { # one or two digit number, no decimal point?
313 0 $agaRating = $1; # it's another way of indicating rank
314 } elsif (not $misc =~ s/^ //) {
315 0 carp("Uh oh, no rating space in: '$misc'");
316 }
317 1 ▶ if ($misc =~ s/^\s*([\d\/]+) //) { # expiration date, if any
Branch: 2 branches
BranchTrueFalseTotal
if ($misc =~ s[^\s*([\d/]+) ][]) { } 1 0 1
elsif (not $misc =~ s/ //) { } 0 0 0
318 1 $expire = $1;
319 } elsif (not $misc =~ s/ //) {
320 0 carp("Uh oh, no expire space in: '$misc'");
321 }
322 1 ▶ unless(defined($expire) or defined($memType)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expire or defined $memType) 0 1 1
Condition33%1/3
defined $expire or defined $memType
l!l&&r!l&&!r
100
MC/DC0%0/2
defined $expire or defined $memType
defined $expire defined $memType
Truth table33%1/3
A or B
A= defined $expireB= defined $memType
ABresult
1X1
011
000
323 0 carp "Uh oh";
324 }
325 1 ▶ if ($misc =~ s/^(\w+)\s*//) { # club
Branch: 2 branches
BranchTrueFalseTotal
if ($misc =~ s/^(\w+)\s*//) { } 1 0 1
elsif (not $misc =~ s/ //) { } 0 0 0
326 1 $club = $1;
327 1 $club =~ s/\W//g; # remove all non-word chars
328 } elsif (not $misc =~ s/ //) {
329 0 carp("Uh oh, no expire space in: '$misc'");
330 }
331 1 ▶ if ($misc =~ s/^\s*(.*?)\s*$//) { # state
Branch: 1 branch
BranchTrueFalseTotal
if ($misc =~ s/^\s*(.*?)\s*$//) 1 0 1
332 1 $state = $1;
333 }
334 1 ▶ if ($misc ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($misc ne "") 0 1 1
335 0 carp("Error: \"$misc\" was left over after parsing \"$string\"\n",
336 "name=$name, id=$agaNum, mem=$memType, rating=$agaRating, ",
337 "expire=$expire, club=$club, state=$state\n");
338 }
339 }
340 return {
341 agaNum => $agaNum, # return ref to hash
342 country => $self->{defaultCountry},
343 1 name => $name,
344 memType => $memType,
345 agaRating => $agaRating,
346 agaRank => $agaRank,
347 expire => $expire,
348 club => $club,
349 state => $state,
350 };
351 }
352  
353 0 ▶sub _ws_clean {
Subroutine _ws_clean: not called CC=1 SCAR=6.9
354 0 my $str = shift @_;
355 0 $str =~ m/^\s*(.*?)\s*$/;
356 0 return $1;
357 }
358  
359 =item my $result = $agaTourn-E<gt>B<ReadRegisterFile> ($fileName)
360  
361 Reads a register.tde file and calls B<AddRegisterLine> on each line of the file.
362  
363 Returns 0 if $fileName couldn't be opened for reading, 1 otherwise.
364  
365 =cut
366  
367 1 ▶sub ReadRegisterFile {
Subroutine ReadRegisterFile: called CC=3 SCAR=11.1
Pod documented
368 1 my ($self, $fName) = @_;
369  
370 1 $self->{fileName} = $fName; # set global name
371 1 my $inFP = new IO::File("<$fName");
372 1 ▶ unless ($inFP) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inFP) 0 1 1
373 carp("Error: can't open $fName for reading\n"),
374 0 $self->{error} = 1,
375 return(0);
376 }
377 1 ▶ while(my $line = <$inFP>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $inFP)) 17 1 18
378 17 $self->AddRegisterLine($line);
379 }
380 1 $inFP->close();
381 1 return(1);
382 }
383  
384 =item $agaTourn-E<gt>B<AddRegisterLine> ($line)
385  
386 Calls B<ParseRegisterLine> on $line. Information extracted about players and
387 directives is added to the $agaTourn object. Comments and blank lines are
388 ignored.
389  
390 =cut
391  
392 17 ▶sub AddRegisterLine {
Subroutine AddRegisterLine: called CC=13 SCAR=26.2
Pod documented
393 17 my ($self, $line) = @_;
394  
395 17 ▶ my $fileMsg = (ref ($self) and exists ($self->{fileName})) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $self && exists $self->{'fileName'} ? : 17 0 17
Condition33%1/3
ref $self and exists $self->{"fileName"}
!ll&&!rl&&r
0017
MC/DC0%0/2
ref $self and exists $self->{"fileName"}
ref $self exists $self->{"fileName"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"fileName"}
ABresult
0X0
100
111
396 " at line $. in $self->{fileName} " :
397 '';
398 17 my $h = $self->ParseRegisterLine($line);
399 17 ▶ return unless(defined($h));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 1 16 17
400 16 ▶ if (exists($h->{directive})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h->{"directive"}) 9 7 16
401 9 foreach (qw(HANDICAPS ROUNDS RULES TOURNEY)) { # non-array directives
402 30 ▶ if ($h->{directive} eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->{"directive"} eq $_) 4 26 30
403 4 $self->{Directive}{$h->{directive}} = [$h->{value}]; # single value
404 4 return;
405 }
406 }
407 5 push(@{$self->{Directive}{$h->{directive}}}, $h->{value});
408 5 return;
409 }
410 7 ▶ return unless(exists($h->{agaNum})); # probably a comment
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h->{"agaNum"} 3 4 7
411 4 my $id = "$h->{country}$h->{agaNum}";
412 4 ▶ if (defined($self->{Name}{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Name"}{$id}) 0 4 4
413 0 carp("Error: Player ID $id is duplicated$fileMsg\n");
414 0 $self->{error} = 1;
415 }
416 4 $self->{Name}{$id} = $h->{name};
417 4 $self->{Rating}{$id} = $h->{agaRating};
418 4 $self->{Rank}{$id} = $h->{agaRank};
419 4 $self->{Comment}{$id} = $h->{comment};
420 4 $self->{Club}{$id} = $h->{club};
421 4 $self->{Flags}{$id} = $h->{flags};
422 4 ▶ $self->{Played}{$id} = [] unless exists($self->{Played}{$id});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Played"}{$id} 4 0 4
423 4 foreach (qw(Wins Losses NoResults)) {
424 12 ▶ $self->{$_}{$id} = 0 unless exists($self->{$_}{$id});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_}{$id} 12 0 12
425 }
426 4 my $len = length($h->{name});
427 4 ▶ $self->{nameLength} = $len if ($len > $self->{nameLength});
Branch: 1 branch
BranchTrueFalseTotal
if $len > $self->{"nameLength"} 1 3 4
428 }
429  
430 =item my $hash = $agaTourn-E<gt>B<ParseRegisterLine> ($line)
431  
432 Parses a single line from the register.tde file (name lines). Here are some
433 examples lines from register.tde file:
434  
435 # this line is a comment. the following line is a directive:
436 ## HANDICAPS MAX
437 # the following line is a name line:
438 USA02122 Augustin, Reid 5.0 CLUB=PALO # 12/31/2004 CA
439  
440 The return value is a reference to a hash of the following values:
441 agaNum => just the number part of the ID
442 country => just the country part of the ID (default ='USA')
443 name => complains if name doesn't contain a comma
444 agaRating => rating for the player
445 agaRank => undef if line contains a rating and not a rank
446 club => if there is a club association, '' if not
447 flags => anything left over (excluding comment)
448 comment => everything after the #, '' if none
449  
450 If the line is a directive, the return hash reference contains only:
451 directive => the directive name
452 value => the directive value ('' if none)
453  
454 If the line is a comment, leading and trailing whitespace is removed and the
455 hash contains only:
456 comment => comment contents (may be '')
457  
458 If the line is empty, returns undef.
459  
460 If the line is not parsable, prints a warning and returns undef.
461  
462 =cut
463  
464 18 ▶sub ParseRegisterLine {
Subroutine ParseRegisterLine: called CC=13 SCAR=28.0
Pod documented
465 18 my ($self, $line) = @_;
466  
467 18 $line =~ s/\s*$//s; # delete trailing spaces
468 18 ▶ return undef if ($line eq ''); # nothing left? return undef
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 1 17 18
469  
470 17 ▶ if ($line =~ s/^\s*##\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^\s*##\s*//) 9 8 17
471 9 $line =~ m/(\S+)\s*(.*?)\s*$/;
472 return {
473 9 directive => $1,
474 value => $2
475 };
476 }
477 8 my $comment = '';
478 8 ▶ if ($line =~ s/\s*#\s*(.*?)\s*$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\s*#\s*(.*?)\s*$//) 8 0 8
479 8 $comment = $1;
480 }
481 8 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 3 5 8
482 return {
483 3 comment => $comment,
484 };
485 }
486  
487 5 ▶ my $fileMsg = (ref ($self) and exists ($self->{fileName})) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $self && exists $self->{'fileName'} ? : 5 0 5
Condition33%1/3
ref $self and exists $self->{"fileName"}
!ll&&!rl&&r
005
MC/DC0%0/2
ref $self and exists $self->{"fileName"}
ref $self exists $self->{"fileName"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"fileName"}
ABresult
0X0
100
111
488 " at line $. in $self->{fileName} " :
489 '';
490 5 my $club = '';
491 5 ▶ if ($line =~ s/\s*CLUB=(\S*)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\s*CLUB=(\S*)\s*//) 5 0 5
492 5 $club = $1;
493 5 $club =~ s/\W//g; # remove all non-word chars
494 }
495 5 my ($agaRating, $agaRank);
496 5 ▶ if($line =~ s/^\s*(\S*)\s+(.*?)\s+(\d+[dDkK])\s*//) { # look for dan or kyu rank
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ s/^\s*(\S*)\s+(.*?)\s+(\d+[dDkK])\s*//) { } 2 3 5
elsif ($line =~ s/^\s*(\S*)\s+(.*?)\s+(-*\d+\.\d+)\s*//) { } 3 0 3
elsif ($line =~ s/^\s*(\S*)\s+(.*?)\s+(-*\d+)\s*//) { } 0 0 0
497 2 $agaRank = $3;
498 2 $agaRating = $self->RankToRating($3);
499 } elsif($line =~ s/^\s*(\S*)\s+(.*?)\s+(-*\d+\.\d+)\s*//) { # look for 5.4 or -13.6 type of rank
500 3 $agaRating = $3; # ok as is
501 } elsif($line =~ s/^\s*(\S*)\s+(.*?)\s+(-*\d+)\s*//) { # look for 5 or -13 type of rank
502 0 carp("Warning: rank is non-decimalized:\n$line\n");
503 0 $agaRating = "$3.0";
504 } else {
505 0 carp("Error: Can't parse name$fileMsg:\n$line\n");
506 0 $self->{error} = 1;
507 0 return;
508 }
509  
510 5 my $name = $2;
511 5 my $agaNum = $self->NormalizeID($1);
512 5 my $country = $self->{defaultCountry};
513 5 ▶ if ($agaNum =~ s/^(\D+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agaNum =~ s/^(\D+)//) 5 0 5
514 5 $country = uc($1);
515 }
516 5 ▶ unless ($name =~ m/,/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /,/) 0 5 5
517 0 carp("Warning: no comma in name \"$name\"$fileMsg\n");
518 }
519 return { # return ref to hash of:
520 5 agaNum => $agaNum,
521 name => $name,
522 agaRating => $agaRating,
523 agaRank => $agaRank,
524 club => $club,
525 country => $country,
526 flags => $line, # whatever's left over
527 comment => $comment,
528 };
529 }
530  
531 =item my $result = $agaTourn-E<gt>B<ReadRoundFile> ($fileName)
532  
533 Reads a round file and calls B<AddRoundLine> on each line of the file.
534 Complains if filename is not in the form I<1.tde>, I<2.tde>, etc.
535  
536 Sets the current B<Round> number to the digit part of fileName.
537  
538 Returns 0 if fileName couldn't be opened for reading, 1 otherwise.
539  
540 =cut
541  
542 1 ▶sub ReadRoundFile {
Subroutine ReadRoundFile: called CC=5 SCAR=17.8
Pod documented
543 1 my ($self, $fName) = @_;
544  
545 1 ▶ if ($fName =~ m/^\d+$/) { # no TDE extension?
Branch: 1 branch
BranchTrueFalseTotal
if ($fName =~ /^\d+$/) 0 1 1
546 0 $fName .= '.tde';
547 }
548 1 $self->{fileName} = $fName; # set global name
549 1 ▶ if ($fName =~ m/(\d+).tde/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fName =~ /(\d+).tde/) { } 1 0 1
550 1 $self->{Round} = $1;
551 } else {
552 0 carp "Round filename not in normal ('1.tde', '2.tde', etc) format\n";
553 }
554 1 my $inFP = new IO::File("<$fName");
555 1 ▶ unless ($inFP) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inFP) 0 1 1
556 0 carp("Error: can't open $fName for reading\n");
557 0 $self->{error} = 1;
558 0 return(0);
559 }
560 1 ▶ while (my $line = <$inFP>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $inFP)) 4 1 5
561 4 $self->AddRoundLine($line);
562 }
563 1 $inFP->close();
564 1 return(1);
565 }
566  
567 =item $agaTourn-E<gt>B<AddRoundLine> ($line)
568  
569 Parses $line (by calling B<ParseRoundLine>) and adds the information to the
570 B<GamesList>. Games without a result ('?') increment both players' NoResults
571 list scores, and games with a result ('b' or 'w') increment the two players'
572 Wins and Losses scores. If the game result is 'b' or 'w', the black player is
573 added to the white player's B<Played> list and vica-versa. Note that
574 B<Played> is not affected by games that are not complete.
575  
576 Complains if either player, or both, are not registered via
577 B<AddRegisterLine>.
578  
579 =cut
580  
581 4 ▶sub AddRoundLine {
Subroutine AddRoundLine: called CC=11 SCAR=25.9
Pod documented
582 4 my ($self, $line) = @_;
583  
584 4 my $g = $self->ParseRoundLine($line); # get game result
585 4 ▶ return unless(defined($g) and exists($g->{result}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $g and exists $g->{"result"} 2 2 4
Condition66%2/3
defined $g and exists $g->{"result"}
!ll&&!rl&&r
022
MC/DC50%1/2
defined $g and exists $g->{"result"}
defined $g exists $g->{"result"}
Truth table66%2/3
A and B
A= defined $gB= exists $g->{"result"}
ABresult
0X0
100
111
586 2 my $wId = $self->NormalizeID("$g->{wcountry}$g->{wagaNum}");
587 2 my $bId = $self->NormalizeID("$g->{bcountry}$g->{bagaNum}");
588 2 ▶ carp("Game $wId.vs.$bId, $wId is not registered\n") unless (exists($self->{Rating}{$wId}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Rating"}{$wId} 0 2 2
589 2 ▶ carp("Game $wId.vs.$bId, $bId is not registered\n") unless (exists($self->{Rating}{$bId}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Rating"}{$bId} 0 2 2
590 2 foreach (qw(Wins Losses NoResults)) {
591 6 ▶ $self->{$_}{$wId} = 0 unless exists($self->{$_}{$wId});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_}{$wId} 0 6 6
592 6 ▶ $self->{$_}{$bId} = 0 unless exists($self->{$_}{$bId});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_}{$bId} 0 6 6
593 }
594 2 ▶ if ($g->{result} eq 'w') {
Branch: 3 branches
BranchTrueFalseTotal
if ($g->{'result'} eq 'w') { } 0 2 2
elsif ($g->{'result'} eq 'b') { } 1 1 2
elsif ($g->{'result'} eq '?') { } 1 0 1
595 0 $self->{Wins}{$wId}++;
596 0 $self->{Losses}{$bId}++;
597 0 push(@{$self->{Played}{$bId}}, $wId);
598 0 push(@{$self->{Played}{$wId}}, $bId);
599 } elsif ($g->{result} eq 'b') {
600 1 $self->{Wins}{$bId}++;
601 1 $self->{Losses}{$wId}++;
602 1 push(@{$self->{Played}{$bId}}, $wId);
603 1 push(@{$self->{Played}{$wId}}, $bId);
604 } elsif ($g->{result} eq '?') {
605 1 $self->{NoResults}{$bId}++;
606 1 $self->{NoResults}{$wId}++;
607 } else {
608 0 carp("Unknown game result:$g->{result}"); # probably can't happen
609 }
610 2 my $game = "$wId,$bId,$g->{result},$g->{handi},$g->{komi},$self->{Round}";
611 2 push(@{$self->{gameAllList}}, $game);
612 2 push(@{$self->{gameIDList}{$wId}}, $game);
613 2 push(@{$self->{gameIDList}{$bId}}, $game);
614 }
615  
616 =item my $hash = $agaTourn-E<gt>B<ParseRoundLine> ($line)
617  
618 Parses a single line from a results file (I<1.tde>, I<2.tde>, etc). Here's an
619 example line from a results file:
620  
621 TMP18 TMP10 b 0 7 # Lee, Ken -28.5 : Yang, John -28.5
622 # wID bID result handi komi comment
623  
624 The return value is a reference to a hash of the following values:
625 wcountry => combine with wagaNum to get complete ID
626 wagaNum => the number part of white's AGA number
627 bcountry => combine with bagaNum to get complete ID
628 bagaNum => the number part of black's AGA number
629 result => winner: 'b', 'w' or '?'
630 handi => handicap game was played with
631 komi => komi game was played with
632 comment => everything after the #
633  
634 If $line is empty, returns undef.
635  
636 If $line is a comment, returns only:
637 comment => everything after the #
638  
639 If the line is not parsable, prints a warning and returns undef.
640  
641 =cut
642  
643 4 ▶sub ParseRoundLine {
Subroutine ParseRoundLine: called CC=8 SCAR=25.9
Pod documented
644 4 my ($self, $line) = @_;
645  
646 4 $line =~ s/\s*$//s; # delete trailing spaces
647 4 ▶ return undef if ($line eq ''); # nothing left? return undef
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 4 4
648  
649 4 ▶ if ($line =~ s/^\s*##\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^\s*##\s*//) 2 2 4
650 2 $line =~ m/(\S+)\s*(.*?)\s*/;
651 return {
652 2 directive => $1,
653 value => $2
654 };
655 }
656 2 my $comment = '';
657 2 ▶ if ($line =~ s/\s*#\s*(.*?)\s*$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\s*#\s*(.*?)\s*$//) 2 0 2
658 2 $comment = $1;
659 }
660 2 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 2 2
661 return {
662 0 comment => $comment,
663 };
664 }
665  
666 2 ▶ if ($line =~ m/^\s*(\w+)(\d+)\s+(\w+)(\d+)\s+([bwBW\?])\s+(\d+)\s+(-?\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\w+)(\d+)\s+(\w+)(\d+)\s+([bwBW\?])\s+(\d+)\s+(-?\d+)$/) 2 0 2
667 return {
668 2 wcountry => uc($1),
669 wagaNum => $2,
670 bcountry => uc($3),
671 bagaNum => $4,
672 result => lc($5),
673 handi => $6,
674 komi => $7,
675 comment => $comment,
676 };
677 }
678 0 ▶ my $fileMsg = (ref ($self) and exists ($self->{fileName})) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $self && exists $self->{'fileName'} ? : 0 0 0
Condition0%0/3
ref $self and exists $self->{"fileName"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and exists $self->{"fileName"}
ref $self exists $self->{"fileName"}
Truth table0%0/3
A and B
A= ref $selfB= exists $self->{"fileName"}
ABresult
0X0
100
111
679 " at line $. in $self->{fileName} " :
680 '';
681 0 carp("Can't parse round line $.$fileMsg:\n$line\n");
682 0 $self->{error} = 1;
683 0 return undef;
684 }
685  
686 =item my $tourney = $agaTourn-E<gt>B<Tourney>
687  
688 Returns the name of the tournament from a ##TOURNEY directive added via
689 B<AddRegisterLine>, or 'Unknown Tournament' if no TOURNEY directive has been
690 added.
691  
692 =cut
693  
694 2 ▶sub Tourney {
Subroutine Tourney: called CC=1 SCAR=0.0
Pod documented
695 2 my ($self) = @_;
696 2 return ($self->{Directive}{TOURNEY}[0]); # last TOURNEY directive
697 }
698  
699 =item my $directive = $agaTourn-E<gt>B<Directive> ($directive)
700  
701 Returns a list (or a reference to the list in scalar context) of directives
702 added via calls to B<AddRegisterLine>. Directive names are always turned into
703 upper case (but the case of the directive value, if any, is preserved).
704  
705 Since some directives (like BAND) may occur several times, all directives are
706 stored as a list in the order added (either from B<ReadRegisterFile> or
707 B<AddRegisterLine>). Certain directives (HANDICAPS ROUNDS RULES TOURNEY) keep
708 only the last directive added.
709  
710 Some directives have no associated value.
711  
712 B<Directive> returns undef if $directive has not been added, or a list
713 (possibly empty) if $directive has been added.
714  
715 If called with no arguments (or $directive is undef), returns a reference to a
716 hash of all the current directives.
717  
718 =cut
719  
720 2 ▶sub Directive {
Subroutine Directive: called CC=4 SCAR=22.9
Pod documented
721 2 my ($self, $directive) = @_;
722  
723 2 ▶ if (defined($directive)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directive) 0 2 2
724 0 $directive = uc($directive); # force to upper case
725 0 ▶ if (exists($self->{Directive}{$directive})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Directive"}{$directive}) 0 0 0
726 0 ▶ return wantarray ? @{$self->{Directive}{$directive}} : $self->{Directive}{$directive};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
727 }
728 0 return(undef);
729 }
730 2 return($self->{Directive}); # the whole shebang...
731 }
732  
733 =item my $rounds = $agaTourn-E<gt>B<Rounds>
734  
735 Returns the total number of rounds the $agaTourn object knows about. If there
736 has been a ##ROUNDS directive in a call to B<AddRegisterLine> file, this will
737 return that number. If not, it will return the number part of the last
738 I<round_number>.tde file read or undef.
739  
740 =cut
741  
742 2 ▶sub Rounds {
Subroutine Rounds: called CC=2 SCAR=8.1
Pod documented
743 2 my ($self) = @_;
744  
745 return $self->{Directive}{ROUNDS}[0] # fetch ROUNDS directive
746 2 ▶ if(defined($self->{Directive}{ROUNDS}[0]));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Directive"}{"ROUNDS"}[0] 2 0 2
747 0 return($self->{Round});
748 }
749  
750 =item my $round = $agaTourn-E<gt>B<Round>
751  
752 Returns the number of the current round (based on the last I<round_number>.tde
753 file read).
754  
755 =cut
756  
757 2 ▶sub Round {
Subroutine Round: called CC=1 SCAR=0.0
Pod documented
758 2 my ($self) = @_;
759 2 return($self->{Round});
760 }
761  
762 =item my $name = $agaTourn-E<gt>B<Name> ($id)
763  
764 Returns the the name for $id.
765  
766 If $id is undef, returns a reference to the entire B<Name> hash (keyed by ID).
767  
768 =cut
769  
770 2 ▶sub Name {
Subroutine Name: called CC=2 SCAR=7.1
Pod documented
771 2 my ($self, $id) = @_;
772  
773 2 ▶ return ($self->{Name}{$id}) if (defined($id));
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 2 2
774 2 return ($self->{Name});
775 }
776  
777 =item my $name_length = $agaTourn-E<gt>B<NameLength>
778  
779 Returns the length of the longest name.
780  
781 =cut
782  
783 2 ▶sub NameLength {
Subroutine NameLength: called CC=1 SCAR=0.0
Pod documented
784 2 my ($self) = @_;
785 2 return ($self->{nameLength});
786 }
787  
788 =item my $rating = $agaTourn-E<gt>B<Rating> ($id, $newRating)
789  
790 Sets (if $newRating is defined) or returns the rating for $id. If $id is not
791 defined, returns a reference to the entire B<Rating> hash (keyed by IDs).
792  
793 $id can also be a rank ('4d', or '5k'), or a rating (4.2 or -5.3, but not
794 between 1.0 and -1.0). This form is simply a converter - $newRating is not
795 accepted.
796  
797 If $id is defined but not registered (via B<AddRegisterLine>), complains and
798 returns undef.
799  
800 =cut
801  
802 2 ▶sub Rating {
Subroutine Rating: called CC=8 SCAR=36.9
Pod documented
803 2 my ($self, $id, $newRating) = @_;
804  
805 2 ▶ $self->{Rating}{$id} = $newRating if (defined($newRating));
Branch: 1 branch
BranchTrueFalseTotal
if defined $newRating 0 2 2
806 2 ▶ if (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 2 2
807 0 ▶ return ($self->{Rating}{$id}) if (exists($self->{Rating}{$id}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Rating"}{$id} 0 0 0
808 0 ▶ if ($id =~ m/^(-?\d+\.\d)\s*/) { # find rank
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^(-?\d+\.\d)\s*/) 0 0 0
809 0 return $1; # rating format
810 }
811 0 ▶ if ($id =~ m/^\s*(\d+)([dkDK])\b/) { # 4D or 15k type rank
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^\s*(\d+)([dkDK])\b/) 0 0 0
812 0 my $rating = $1;
813 0 ▶ $rating = -$rating if (lc($2) eq 'k');
Branch: 1 branch
BranchTrueFalseTotal
if lc $2 eq "k" 0 0 0
814 0 return $rating;
815 }
816 0 ▶ if ($id =~ m/^\s*(-?\d\d?)\b/) { # one or two digit number, no decimal point?
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^\s*(-?\d\d?)\b/) 0 0 0
817 0 return $1; # it's another way of indicating rank
818 }
819 0 carp ("Invalid Rating argument:$id\n");
820 0 return undef; # eh?
821 }
822 2 return ($self->{Rating});
823 }
824  
825 =item my $rank = $agaTourn-E<gt>B<Rank> ($id)
826  
827 Returns the rank for $id. This field is undef unless the B<AddRegisterLine>
828 contained a rank field of the form '4k' or '3d' as opposed to a rating of the
829 form '-4.5' or '3.4'.
830  
831 If $id is not defined, returns a reference to the entire B<Rank> hash (keyed
832 by IDs).
833  
834 =cut
835  
836 0 ▶sub Rank {
Subroutine Rank: not called CC=2 SCAR=17.9
Pod documented
837 0 my ($self, $id) = @_;
838  
839 0 ▶ return ($self->{Rank}{$id}) if(defined($id));
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
840 0 return ($self->{Rank});
841 }
842  
843 =item my $sigma = $agaTourn-E<gt>B<Sigma> ($id)
844  
845 Returns the sigma for $id. Sigma is determined by the rating/rank in the
846 B<AddRegisterLine>. If the line contains a rank field of the form '4k' or '3d',
847 sigma is 1.2 for 7k and stronger, and
848  
849 (k - 0.3) / 6
850  
851 for 8k and weaker. If the line contains a rating of the form '-4.5' or '3.4',
852 sigma is 0.6 for -8.0 and stronger, and
853  
854 (-rating - 4.4) / 6
855  
856 for weaker than -8.0.
857  
858 Complains and returns undef if $id is undefined or unregistered.
859  
860 =cut
861  
862 0 ▶sub Sigma {
Subroutine Sigma: not called CC=7 SCAR=40.3
Pod documented
863 0 my ($self, $id) = @_;
864  
865 0 ▶ if (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 0 0 0
866 0 ▶ if (defined($self->{Rank}{$id})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'Rank'}{$id}) { } 0 0 0
elsif (defined $self->{'Rating'}{$id}) { } 0 0 0
867 0 $self->{Rank}{$id} =~ m/^([\d]+)([kdKD])$/;
868 0 my $r = $1;
869 0 ▶ $r = -$r if (lc($2) eq 'k');
Branch: 1 branch
BranchTrueFalseTotal
if lc $2 eq "k" 0 0 0
870 0 my $sigma = (-$r - 0.3) / 6;
871 0 ▶ return ($sigma > 1.2) ? $sigma : 1.2;
Branch: 1 branch
BranchTrueFalseTotal
$sigma > 1.2 ? : 0 0 0
872 } elsif (defined($self->{Rating}{$id})) {
873 0 my $sigma = (-$self->{Rating}{$id} - 4.4) / 6;
874 0 ▶ return ($sigma > 0.6) ? $sigma : 0.6;
Branch: 1 branch
BranchTrueFalseTotal
$sigma > 0.6 ? : 0 0 0
875 } else {
876 0 carp("$id is not registered\n");
877 }
878 } else {
879 0 carp("called Sigma(\$id) without a valid ID\n");
880 }
881 0 return(undef);
882 }
883  
884 =item my $club = $agaTourn-E<gt>B<Club> ($id)
885  
886 Returns the club for $id or '' if no club is known. Returns undef if $id is
887 not registered (via B<AddRegisterLine>).
888  
889 If no $id parameter is passed, returns a reference to the entire B<Club> hash
890 (keyed by IDs).
891  
892 =cut
893  
894 0 ▶sub Club {
Subroutine Club: not called CC=2 SCAR=17.9
Pod documented
895 0 my ($self, $id) = @_;
896  
897 0 ▶ return ($self->{Club}{$id}) if (defined($id));
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
898 0 return($self->{Club});
899 }
900  
901 =item my $flags = $agaTourn-E<gt>B<Flags> ($id)
902  
903 Returns the flags for $id or '' if no flags are known. Flags are anything
904 left over (excluding the comment) after the ID, name, rating, and club have
905 been parsed by B<AddRegisterLine>. It might include (for example) BYE or
906 Drop. The case is preserved from the original line parsed.
907  
908 Returns undef if $id is not registered (via B<AddRegisterLine>). If no $id
909 parameter is passed, returns a reference to the entire B<Flags> hash (keyed by
910 IDs).
911  
912 =cut
913  
914 0 ▶sub Flags {
Subroutine Flags: not called CC=4 SCAR=30.0
Pod documented
915 0 my ($self, $id) = @_;
916  
917 0 ▶ if (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 0 0
918 0 ▶ return ($self->{Flags}{$id}) if (exists($self->{Flags}{$id}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Flags"}{$id} 0 0 0
919 0 ▶ return ('') if exists($self->{Rating}{$id});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Rating"}{$id} 0 0 0
920 return (undef)
921 0 }
922 0 return($self->{Flags});
923 }
924  
925 =item $comment = $agaTourn-E<gt>B<Comment> ($id)
926  
927 Returns the comment associated with $id line as added via B<AddRegisterLine>.
928  
929 If no $id argument is passed, returns a reference to the entire B<Comments>
930 hash (keyed by IDs).
931  
932 =cut
933  
934 2 ▶sub Comment {
Subroutine Comment: called CC=4 SCAR=21.7
Pod documented
935 2 my ($self, $id) = @_;
936  
937 2 ▶ if (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 2 2
938 0 ▶ return ($self->{Comment}{$id}) if (exists($self->{Comment}{$id}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Comment"}{$id} 0 0 0
939 0 ▶ return ('') if exists($self->{Rating}{$id});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Rating"}{$id} 0 0 0
940 return (undef)
941 0 }
942 2 return ($self->{Comment});
943 }
944  
945 =item my $error = $agaTourn-E<gt>B<Error>
946  
947 If called with an argument, sets the error flag to the new value.
948 Returns the current (or new) value of the error flag.
949  
950 =cut
951  
952 2 ▶sub Error {
Subroutine Error: called CC=2 SCAR=7.1
Pod documented
953 2 my ($self, $error) = @_;
954  
955 2 ▶ $self->{error} = $error if (defined($error));
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 2 2
956 2 return ($self->{error});
957 }
958  
959 =item my $gamesList = $agaTourn-E<gt>B<GamesList> ($id, ...)
960  
961 Returns a list (or a reference to the list in scalar context) of games played
962 by B<player>(s). If no B<player> argument is passed, returns the list of all
963 games.
964  
965 Games are added via the B<ReadRoundFile> or the B<AddRoundLine> methods.
966  
967 Entries in the returned list are comma separated strings. They can be parsed
968 with:
969  
970 my ($whiteID, $blackID, $result,
971 $handicap, $komi, $round) = split(',', $agaTourn->GamesList[$index]);
972  
973 =cut
974  
975 3 ▶sub GamesList {
Subroutine GamesList: called CC=4 SCAR=23.2
Pod documented
976 3 my ($self, @arg) = @_;
977  
978 3 ▶ return($self->{gameAllList}) unless (@arg);
Branch: 1 branch
BranchTrueFalseTotal
unless @arg 3 0 3
979 0 my @games;
980 0 foreach (@arg) {
981 0 push(@games, @{$self->{gameIDList}{$_}});
982 }
983 0 ▶ return(wantarray ? @games : \@games);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
984 }
985  
986 =item my $wins = $agaTourn-E<gt>B<Wins> ($id)
987  
988 Returns the number of winning games recorded for $id. Wins are recorded
989 via the B<AddRoundLine> method.
990  
991 If no $id argument is passed, returns a reference to the entire B<Wins> hash
992 (keyed by IDs).
993  
994 =cut
995  
996 0 ▶sub Wins {
Subroutine Wins: not called CC=2 SCAR=17.9
Pod documented
997 0 my ($self, $id) = @_;
998  
999 0 ▶ return($self->{Wins}{$id}) if (defined($id));
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
1000 0 return($self->{Wins});
1001 }
1002  
1003 =item my $losses = $agaTourn-E<gt>B<Losses> ($id)
1004  
1005 Returns the number of losing games recorded for $id. Losses are
1006 recorded via the B<AddRoundLine> method.
1007  
1008 If no $id argument is passed, returns a reference to the entire B<Losses> hash
1009 (keyed by IDs).
1010  
1011 =cut
1012  
1013 0 ▶sub Losses {
Subroutine Losses: not called CC=2 SCAR=17.9
Pod documented
1014 0 my ($self, $id) = @_;
1015  
1016 0 ▶ return($self->{Losses}{$id}) if (defined($id));
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
1017 0 return($self->{Losses});
1018 }
1019  
1020 =item my $no_results = $agaTourn-E<gt>B<NoResults> ($id)
1021  
1022 Returns the number of no-result games recorded for $id. No-results are
1023 recorded via the B<AddRoundLine> method.
1024  
1025 If no $id argument is passed, returns a reference to the entire B<NoResults>
1026 hash (keyed by IDs).
1027  
1028 =cut
1029  
1030 0 ▶sub NoResults {
Subroutine NoResults: not called CC=2 SCAR=17.9
Pod documented
1031 0 my ($self, $id) = @_;
1032  
1033 0 ▶ return($self->{NoResults}{$id}) if (defined($id));
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
1034 0 return($self->{NoResults});
1035 }
1036  
1037 =item my @played = $agaTourn-E<gt>B<Played> ($id)
1038  
1039 Returns a list (or a reference to the list in scalar context) of $id's
1040 opponents. The list is ordered as they were added by B<AddRoundLine> method.
1041  
1042 If no $id argument is passed, returns a reference to the entire B<Played> hash
1043 (keyed by IDs).
1044  
1045 =cut
1046  
1047 0 ▶sub Played {
Subroutine Played: not called CC=3 SCAR=24.8
Pod documented
1048 0 my ($self, $id) = @_;
1049  
1050 0 ▶ if (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 0 0
1051 0 ▶ return wantarray ? @{$self->{Played}{$id}} : $self->{Played}{$id};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1052 }
1053 0 return $self->{Played};
1054 }
1055  
1056 =item my $rating = $agaTourn-E<gt>B<RankToRating> ($rank | $rating)
1057  
1058 Returns a value guaranteed to be in a correct AGA Rating format. The format
1059 is a number with a tenths decimal, where the number represents the dan rank
1060 (if positive) or the kyu rank (if negative). A rating of 3.5 represents
1061 squarely in the middle of the 3 dan rank, and -1.9 represents a weak 1 kyu
1062 rank. The range from 1.0 to -1.0 is not used (see
1063 B<CollapseRating>/B<ExpandRating> below).
1064  
1065 =cut
1066  
1067 32 ▶sub RankToRating {
Subroutine RankToRating: called CC=7 SCAR=19.9
Pod documented
1068 32 my ($self, $rating) = @_;
1069  
1070 32 ▶ return (NOTARANK) if (not defined($rating) or ($rating eq ''));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rating or $rating eq "" 0 32 32
Condition33%1/3
not defined $rating or $rating eq ""
l!l&&r!l&&!r
0032
MC/DC0%0/2
not defined $rating or $rating eq ""
not defined $rating $rating eq ""
Truth table33%1/3
not(A) or B
A= not defined $ratingB= $rating eq ""
ABresult
1X1
011
000
1071 32 ▶ return "$rating.0" if ($rating =~ m/^-?\d+$/); # not in decimalized format?
Branch: 1 branch
BranchTrueFalseTotal
if $rating =~ /^-?\d+$/ 3 29 32
1072 29 ▶ unless ($rating =~ m/^-?\d+\.\d+$/) { # not in rating format?
Branch: 1 branch
BranchTrueFalseTotal
unless ($rating =~ /^-?\d+\.\d+$/) 24 5 29
1073 24 ▶ return(NOTARANK) unless($rating =~ m/^(\d+)([dDkK])$/); # not in rank format either?
Branch: 1 branch
BranchTrueFalseTotal
unless $rating =~ /^(\d+)([dDkK])$/ 0 24 24
1074 24 $rating = "$1.5"; # it's in rank format (like 5D or 2k), convert to rating
1075 24 ▶ $rating = -$rating if (uc($2) eq "K"); # kyus are negative
Branch: 1 branch
BranchTrueFalseTotal
if uc $2 eq "K" 12 12 24
1076 }
1077 29 return($rating);
1078 }
1079  
1080 =item my $band_idx = $agaTourn-E<gt>B<WhichBandIs> ($rank | $rating)
1081  
1082 Returns the band index for a B<rank> or B<rating>. Returns NOTARANK if
1083 rank/rating is not in any bands.
1084  
1085 See also B<BandName> below.
1086  
1087 =cut
1088  
1089 8 ▶sub WhichBandIs {
Subroutine WhichBandIs: called CC=5 SCAR=16.1
Pod documented
1090 8 my ($self, $r) = @_;
1091  
1092 8 ▶ unless (exists($self->{bandTop})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"bandTop"}) 2 6 8
1093 2 $self->_setBands();
1094 }
1095 8 $r = $self->RankToRating($r);
1096 8 my $ii;
1097 8 ▶ for ($ii = 0; $ii < @{$self->{bandTop}}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < @{$self->{"bandTop"};}) 15 6 21
1098 15 ▶ next if ($r > $self->{bandTop}[$ii]);
Branch: 1 branch
BranchTrueFalseTotal
if $r > $self->{"bandTop"}[$ii] 0 15 15
1099 15 ▶ if ($r >= $self->{bandBot}[$ii]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r >= $self->{"bandBot"}[$ii]) 2 13 15
1100 2 return($ii); # this is it
1101 }
1102 }
1103 6 return(NOTARANK);
1104 }
1105  
1106 =item my $band_name = $agaTourn-E<gt>B<BandName> ($bandIndex)
1107  
1108 Returns the name of a band specified by the B<bandIndex> or undef of not known.
1109  
1110 Scoring bands are specified via B<AddRegisterLine> with ##BAND directives.
1111  
1112 AGATourn complains if bands are specified with holes between them.
1113  
1114 The bands are sorted (by strength) and indexed. B<BandName> returns the
1115 original name (as specified in the ##BAND directive) from a band index.
1116  
1117 =cut
1118  
1119 4 ▶sub BandName {
Subroutine BandName: called CC=4 SCAR=13.9
Pod documented
1120 4 my ($self, $idx) = @_;
1121  
1122 4 my ($band, $top, $bot);
1123 4 foreach $band (@{$self->{Directive}{'BAND'}}) {
1124 6 ($top, $bot) = split(/\s+/, $band);
1125 6 $top = int($self->RankToRating($top));
1126 6 ▶ return undef unless defined($self->{bandTop}[$idx]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"bandTop"}[$idx] 1 5 6
1127 5 ▶ if ($top == int($self->{bandTop}[$idx])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top == int $self->{"bandTop"}[$idx]) 3 2 5
1128 3 return($band);
1129 }
1130 }
1131 0 return(undef);
1132 }
1133  
1134 =item my ($handicap, $komi) = $agaTourn-E<gt>B<Handicap> ($player1, $player2)
1135  
1136 Returns the appropriate handicap and komi for two players. Players can be in
1137 any form acceptable to B<Rating>.
1138  
1139 If player1 is stronger than player two, the handicap is a
1140 positive number. If player1 is weaker than player2, (players need to be
1141 swapped), the returned handicap is a negative number. If the handicap would
1142 normally be 0 and the players need to be swapped, the returned handicap is -1.
1143  
1144 A handicap of 1 is never returned. The returned handicap and komi are always
1145 integers (you may assume that komi needs a additional half-point if you like).
1146  
1147 If either player1 or player2 is invalid, B<Handicap> complains (during the
1148 call to B<Rating> for the player) and returns (-1, -1).
1149  
1150 B<Handicap> uses the following table (same as the AGA handicap practice):
1151  
1152 rating handi Ing AGA
1153 diff Komi Komi
1154 0.000-0.650 0 7 6 even, normal komi
1155 0.651-1.250 0 -1* 0 no komi (* black wins ties under Ing)
1156 1.251-2.200 0 -7 -6 reverse komi
1157 2.201-3.300 2 -2 0 two stones
1158 3.301-4.400 3 -3 0 three stones ...
1159  
1160 =cut
1161  
1162 0 ▶sub Handicap {
Subroutine Handicap: not called CC=10 SCAR=47.0
Pod documented
1163 0 my ($self, $p1, $p2) = @_;
1164  
1165 0 $p1 = $self->CollapseRating($self->Rating($p1));
1166 0 $p2 = $self->CollapseRating($self->Rating($p2));
1167 0 ▶ return (-1, -1) unless(defined($p1) and defined($p2));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p1 and defined $p2 0 0 0
Condition0%0/3
defined $p1 and defined $p2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p1 and defined $p2
defined $p1 defined $p2
Truth table0%0/3
A and B
A= defined $p1B= defined $p2
ABresult
0X0
100
111
1168 0 my $diff = $p1 - $p2;
1169 0 my $ing = $self->{Directive}{RULES}[0] eq 'ING';
1170 0 my $swap = 1;
1171 0 my ($handi, $komi) = (0, 0);
1172 0 ▶ if ($diff < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff < 0) 0 0 0
1173 0 $swap = $handi = -1;
1174 0 $diff = -$diff;
1175 }
1176 0 ▶ if ($diff <= .650) {
Branch: 3 branches
BranchTrueFalseTotal
if ($diff <= 0.65) { } 0 0 0
elsif ($diff <= 1.25) { } 0 0 0
elsif ($diff <= 2.2) { } 0 0 0
1177 0 ▶ $komi = $ing ? 7 : 6; # normal komi game
Branch: 1 branch
BranchTrueFalseTotal
$ing ? : 0 0 0
1178 } elsif ($diff <= 1.25) {
1179 0 ▶ $komi = $ing ? -1 : 0; # no komi game
Branch: 1 branch
BranchTrueFalseTotal
$ing ? : 0 0 0
1180 } elsif ($diff <= 2.2) {
1181 0 ▶ $komi = $ing ? -7 : -6; # reverse komi game
Branch: 1 branch
BranchTrueFalseTotal
$ing ? : 0 0 0
1182 } else {
1183 0 $handi = $swap * int($diff / 1.1);
1184 0 $komi = 0;
1185 }
1186 0 return (int($handi), int($komi));
1187 }
1188  
1189 =item my $collapsed_rating = $agaTourn-E<gt>B<CollapseRating> ($aga_rating)
1190  
1191 AGA ratings have a hole between 1.0 and -1.0. This method fills the hole by
1192 adding 1 to kyu ratings and subtracting 1 from dan ratings. If $aga_rating is
1193 between 1.0 and -1.0, complains and returns the original $rating.
1194  
1195 =cut
1196  
1197 0 ▶sub CollapseRating {
Subroutine CollapseRating: not called CC=3 SCAR=24.8
Pod documented
1198 0 my ($self, $rating) = @_;
1199  
1200 0 ▶ if ($rating >= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rating >= 1) { } 0 0 0
elsif ($rating <= -1) { } 0 0 0
1201 0 $rating -= 1; # pull dan ratings down to 0
1202 } elsif ($rating <= -1) {
1203 0 $rating += 1; # pull kyu ratings up to 0
1204 } else {
1205 0 carp "CollapseRating called on a rating between -1 and +1: $rating\n";
1206 }
1207 0 return $rating;
1208 }
1209  
1210 =item my $AGA_rating = $agaTourn-E<gt>B<ExpandRating> ($collapsed_rating)
1211  
1212 AGA ratings have a hole between 1.0 and -1.0. This method converts a
1213 continuous rating with no hole into a valid AGA rating by adding 1 to ratings
1214 greater than 0 and subtracting 1 from ratings less than 0.
1215  
1216 =cut
1217  
1218 0 ▶sub ExpandRating {
Subroutine ExpandRating: not called CC=2 SCAR=17.9
Pod documented
1219 0 my ($self, $rating) = @_;
1220  
1221 0 ▶ if ($rating >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rating >= 0) { } 0 0 0
1222 0 $rating += 1; # dan ratings are upwards from 1
1223 } else {
1224 0 $rating -= 1; # kyu ratings are downwards from -1
1225 }
1226 0 return $rating;
1227 }
1228  
1229 =item my $normalized_id = $agaTourn-E<gt>B<NormalizeID> ($id)
1230  
1231 Performs normalization of $id so the we can compare variations of $id without
1232 considering them as different. Normalization consists of turning the country
1233 part of $id to all upper-case and removing leading zeros from the number part.
1234  
1235 All $ids used as hash keys should be normalized.
1236  
1237 =cut
1238  
1239 9 ▶sub NormalizeID {
Subroutine NormalizeID: called CC=1 SCAR=0.0
Pod documented
1240 9 my ($self, $id) = @_;
1241  
1242 9 $id = uc ($id); # make all letters upper case
1243 9 $id =~ s/^([A-Z]*)0*([1-9].*)/$1$2/; # remove leading zeros from number part
1244 9 return($id);
1245 }
1246  
1247 ######################################################
1248 #
1249 # Private methods
1250 #
1251 #####################################################
1252  
1253 2 ▶sub _setBands {
Subroutine _setBands: called CC=14 SCAR=27.7
1254 2 my ($self) = @_;
1255  
1256 2 ▶ unless(exists($self->{Directive}{'BAND'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"Directive"}{"BAND"}) 1 1 2
1257 # carp("Note: no bands selected, assuming one band.\n");
1258 1 unshift(@{$self->{Directive}{'BAND'}}, '99D 99K');
1259 }
1260 2 $self->{bandTop} = []; # ref to empty array (to prevent infinite recursion)
1261 2 my ($band, $ovBand, $top, $bot);
1262 2 foreach $band (@{$self->{Directive}{'BAND'}}) {
1263 6 ($top, $bot) = split(/\s+/, $band);
1264 6 $top = int($self->RankToRating($top));
1265 6 ▶ $top += 0.99999 if ($top > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $top > 0 3 3 6
1266 6 $bot = int($self->RankToRating($bot));
1267 6 ▶ $bot -= 0.99999 if ($bot < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bot < 0 4 2 6
1268 6 ▶ if (($top > 9999) || ($bot < -9999) || ($bot >= $top)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top > 9999 or $bot < -9999 or $bot >= $top) 0 6 6
Condition33%2/6
$top > 9999 or $bot < -9999 or $bot >= $top
l!l&&r!l&&!r
006
$top > 9999 or $bot < -9999
l!l&&r!l&&!r
006
MC/DC0%0/3
$top > 9999 or $bot < -9999 or $bot >= $top
$top > 9999 $bot < -9999 $bot >= $top
Truth table25%1/4
A or B or C
A= $top > 9999B= $bot < -9999C= $bot >= $top
ABCresult
1XX1
01X1
0011
0000
1269 0 carp("Error: can't parse BAND directive at line $. in $self->{fileName}: $band\n");
1270 0 $self->{error} = 1;
1271 return
1272 0 }
1273 6 $ovBand = $self->WhichBandIs($top); # check for overlapped bands
1274 6 ▶ $ovBand = $self->WhichBandIs($bot) unless ($ovBand eq NOTARANK);
Branch: 1 branch
BranchTrueFalseTotal
unless $ovBand eq "-99.9" 0 6 6
1275 6 ▶ unless ($ovBand eq NOTARANK) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ovBand eq "-99.9") 0 6 6
1276 0 carp("Warning: band conflict: $band\n (overlaps $self->{Directive}{'BAND'}[$ovBand])\n");
1277 }
1278 6 push(@{$self->{bandTop}}, $top);
1279 6 push(@{$self->{bandBot}}, $bot);
1280 }
1281 2 my (@tops) = sort({ $b <=> $a; } @{$self->{bandTop}}); # now check for holes
1282 2 my (@bots) = sort({ $b <=> $a; } @{$self->{bandBot}});
1283 2 my $ii;
1284 2 ▶ for ($ii = 0; $ii < @tops - 1; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < @tops - 1) 4 2 6
1285 4 ▶ next if (($bots[$ii] == 1) && ($tops[$ii + 1] == -1)); # 1d to 1k is a legitimate hole
Branch: 1 branch
BranchTrueFalseTotal
if $bots[$ii] == 1 and $tops[$ii + 1] == -1 1 3 4
Condition66%2/3
$bots[$ii] == 1 and $tops[$ii + 1] == -1
!ll&&!rl&&r
301
MC/DC50%1/2
$bots[$ii] == 1 and $tops[$ii + 1] == -1
$bots[$ii] == 1 $tops[$ii + 1] == -1
Truth table66%2/3
A and B
A= $bots[$ii] == 1B= $tops[$ii + 1] == -1
ABresult
0X0
100
111
1286 3 ▶ if ($bots[$ii] - $tops[$ii + 1] > 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bots[$ii] - $tops[$ii + 1] > "0.001") 0 3 3
1287 0 carp( "Warning: hole between bands\n");
1288 }
1289 }
1290 2 $self->{bandTop} = \@tops; # use sorted bands
1291 2 $self->{bandBot} = \@bots;
1292 }
1293  
1294 1 1;
1295  
1296 __END__
↑ Summary