]> iEval git - gruntmaster-page.git/blame - lib/Gruntmaster/Page/St.pm
Add standings
[gruntmaster-page.git] / lib / Gruntmaster / Page / St.pm
CommitLineData
5bbf0128
MG
1package Gruntmaster::Page::St;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7our @EXPORT_OK = qw/generate/;
8our $VERSION = '0.001';
9
10use constant TITLE => 'Standings';
11
12use Fcntl qw/:flock/;
13use HTML::Template::Compiled;
14use IO::File;
15use List::Util qw/sum/;
16use POSIX qw/strftime/;
17use YAML::Any qw/LoadFile/;
18use Gruntmaster::Page::Common qw/header footer/;
19
20my %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>
31HTML
32);
33
34$templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates;
35$templates{$_} .= footer $_ for keys %templates;
36
37sub 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
691
This page took 0.025148 seconds and 4 git commands to generate.