]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/St.pm
Add standings
[gruntmaster-page.git] / lib / Gruntmaster / Page / St.pm
1 package Gruntmaster::Page::St;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 use parent qw/Exporter/;
7 our @EXPORT_OK = qw/generate/;
8 our $VERSION = '0.001';
9
10 use constant TITLE => 'Standings';
11
12 use Fcntl qw/:flock/;
13 use HTML::Template::Compiled;
14 use IO::File;
15 use List::Util qw/sum/;
16 use POSIX qw/strftime/;
17 use YAML::Any qw/LoadFile/;
18 use Gruntmaster::Page::Common qw/header footer/;
19
20 my %templates = (
21 en => <<'HTML',
22 <table border>
23 <thead>
24 <tr><th>Username<tmpl_loop problems><th><tmpl_var _></tmpl_loop><th>Total
25 <tbody>
26 <tmpl_loop st><tr><td><tmpl_var user>
27 <tmpl_loop scores><td><tmpl_var _>
28 </tmpl_loop><td><tmpl_var score>
29 </tmpl_loop>
30 </table>
31 HTML
32 );
33
34 $templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates;
35 $templates{$_} .= footer $_ for keys %templates;
36
37 sub generate{
38 my ($path, $lang) = @_;
39 $path =~ s,/st\.html$,,;
40 my $template = $templates{$lang};
41 my $htc = HTML::Template::Compiled->new(scalarref => \$template);
42 IO::File->new(">$path/meta.yml")->close unless -f "$path/meta.yml";
43 flock my $metafh = IO::File->new("<$path/meta.yml"), LOCK_SH;
44 my $meta = LoadFile "$path/meta.yml";
45 my @problems = sort grep { /^\w+$/ } map { s,.*/,,r } <$path/../pb/*>;
46 my %scores;
47 for (1 .. $meta->{last}) {
48 my $meta = LoadFile "$path/$_/meta.yml";
49 if ($meta->{result_text} =~ m/^(\d+)/) {
50 $scores{$meta->{user}}{$meta->{problem}} = $1;
51 } else {
52 $scores{$meta->{user}}{$meta->{problem}} = $meta->{result} ? 0 : 100;
53 }
54 }
55
56 my @st = sort { $b->{score} <=> $a->{score} } map {
57 my $user = $_;
58 +{
59 user => $user,
60 score => sum (values $scores{$user}),
61 scores => [map { $scores{$user}{$_} // '-'} @problems],
62 }
63 } keys %scores;
64 $htc->param(problems => \@problems);
65 $htc->param(st => \@st);
66 $htc->output
67 }
68
69 1
This page took 0.054544 seconds and 5 git commands to generate.