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.1_2');
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use CGI qw/ :html /;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Parse::RecDescent;
9  
10 sub new {
11 my $class = shift;
12 my %params = @_;
13 $params{sgf_dir} = '.' unless defined $params{sgf_dir};
14 $params{base_url} = '' unless defined $params{base_url};
15 my @games;
16 undef $/;
17  
18 unless (opendir(SGF, $params{sgf_dir})) {
19 carp "While opening directory \"$params{sgf_dir}\": $!";
20 return undef;
21 }
22
23 my $parser = Parse::RecDescent->new(q<
24 { my %info }
25 SGF: GameTree { $return = \%info }
26 GameTree: '(' Node(s) GameTree(s?) ')'
27 Node: ';' Property(s?)
28 Property: PropIdent PropValue(s) {
29 $info{$item{PropIdent}} =
30 join ';', @{$item{'PropValue(s)'}}
31 if grep $item{PropIdent} eq $_,
32 qw/ RU SZ HA KM PW PB DT TM RE /;
33 }
34 PropIdent: /[[:alnum:]]+/o {
35 ($return = $item[1]) =~ s/[^[:upper:]\d]//go;
36 }
37 PropValue: '[' Character(s?) ']' {
38 $return = join '', @{$item{'Character(s?)'}}
39 }
40 Character: /[^\\\\\]]+/so | Escaped
41 Escaped: '\\\\' | '\]' { $return = substr $item[1], 1 }
42 >);
43
44 foreach (grep { /.*\.sgf$/io } readdir SGF) {
45 open IN, "$params{sgf_dir}/$_" or next;
46 my %info = %{$parser->SGF(<IN>)};
47 foreach (qw/ RU SZ HA KM PW PB DT TM RE /) {
48 $info{$_} = '' unless defined $info{$_};
49 }
50 $info{file} = "$params{base_url}$_" ;
51 push @games, \%info;
52 }
53
54 bless { _games => \@games }, $class;
55 }
56  
57 sub games {
58 my $self = shift;
59 my %params = @_;
60 $params{cgi}->{border} = '1' unless defined $params{cgi}->{border};
61 $params{caption} = 'Played games' unless defined $params{caption};
62 my @rows = TR(
63 th('Game#'),
64 th('Black'),
65 th('White'),
66 th('Setup'),
67 th('Date'),
68 th('Result')
69 );
70
71 my $i;
72 foreach (sort { $a->{DT} cmp $b->{DT} } @{$self->{_games}}) {
73 push @rows, TR(
74 td(a({ -href => $_->{file} }, ++$i)),
75 td($_->{PB}),
76 td($_->{PW}),
77 td("$_->{RU}/$_->{SZ}/$_->{HA}/$_->{KM}/$_->{TM}"),
78 td($_->{DT}),
79 td($_->{RE})
80 );
81 }
82
83 table($params{cgi}, caption($params{caption}), @rows);
84 }
85  
86 sub scores {
87 my $self = shift;
88 my %params = @_;
89 $params{cgi}->{border} = '1' unless defined $params{cgi}->{border};
90 $params{caption} = 'Scoreboard' unless defined $params{caption};
91 my @rows = TR(
92 th('Pos#'),
93 th('Name'),
94 th('Score')
95 );
96 my %scores;
97  
98 foreach my $info (@{$self->{_games}}) {
99 $info->{RE} =~ /^([BW])\+/o;
100 foreach (qw/ B W /) {
101 $scores{$info->{"P$_"}} += $1 eq $_ ? 1:0;
102 }
103 }
104
105 my $i;
106 foreach (sort { $scores{$b} <=> $scores{$a} } keys %scores) {
107 push @rows, TR(
108 td(++$i),
109 td($_),
110 td($scores{$_})
111 );
112 }
113  
114 table($params{cgi}, caption($params{caption}), @rows);
115 }
116  
117 1;
118  
119 __END__
↑ Summary