blib/lib/Games/SGF/Tournament.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Games::SGF::Tournament;
2  
3 3 ▶use version; $VERSION = qv(1.0);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use CGI qw/ :html /;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 sub new {
11 my $class = shift;
12 my %params = @_;
13 $params{sgf_dir} ||= '.';
14 $params{base_url} ||= '';
15 my %scores;
16 my @games;
17 undef $/;
18  
19 unless (opendir(SGF, $params{sgf_dir})) {
20 carp "While opening directory \"$params{sgf_dir}\": $!";
21 return undef;
22 }
23 foreach (grep { /.*\.sgf$/ } readdir SGF) {
24 open IN, "$params{sgf_dir}/$_" or next;
25 my $sgf_content = <IN>;
26 close IN;
27 my %game_info = ( file => "$params{base_url}$_" );
28 foreach (qw/ RU SZ HA KM PW PB DT TM RE /) {
29 if ($sgf_content =~ /$_\[(.*?)\]/) {
30 $game_info{$_} = $1;
31 } else {
32 $game_info{$_} = '?';
33 }
34 }
35 push @games, \%game_info;
36  
37 $game_info{RE} =~ /^([BW])\+/o;
38 foreach (qw/ B W /) {
39 $scores{$game_info{"P$_"}} += $1 eq $_ ? 1:0;
40 }
41 }
42 bless { games => \@games, scores => \%scores }, $class;
43 }
44  
45 sub games {
46 my $self = shift;
47 my @rows = TR(
48 th('Game#'),
49 th('Black'),
50 th('White'),
51 th('Setup'),
52 th('Date'),
53 th('Result')
54 );
55 my $i;
56  
57 foreach (sort { $a->{DT} cmp $b->{DT} } @{ $self->{games} }) {
58 push @rows, TR(
59 td(a({ -href => $_->{file} }, ++$i)),
60 td($_->{PB}),
61 td($_->{PW}),
62 td("$_->{RU}/$_->{SZ}/$_->{HA}/$_->{KM}/$_->{TM}"),
63 td($_->{DT}),
64 td($_->{RE})
65 );
66 }
67  
68 return table({ -border => 1 },
69 caption('Table of played games'),
70 @rows
71 );
72 }
73  
74 sub scores {
75 my $self = shift;
76 my @rows = TR(
77 th('Pos#'),
78 th('Name'),
79 th('Score')
80 );
81 my $i;
82  
83 foreach (sort { $self->{scores}->{$b} <=> $self->{scores}->{$a} }
84 (keys %{ $self->{scores} })
85 ) {
86 push @rows, TR(
87 td(++$i),
88 td($_),
89 td($self->{scores}->{$_})
90 );
91 }
92  
93 return table({ -border => 1 },
94 caption('Scoreboard'),
95 @rows
96 );
97 }
98  
99 1;
100 __END__
↑ Summary