]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/St.pm
Centralize template cooking and introduce reloadable templates
[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 HTML::Template::Compiled;
11 use List::Util qw/sum/;
12 use POSIX qw/strftime/;
13 use Gruntmaster::Page::Common qw/cook_templates reload_templates/;
14 use Gruntmaster::Data qw/problems jobcard job_result_text job_result job_problem job_user/;
15
16 my %orig_templates = (
17 en => <<'HTML',
18 <table border>
19 <thead>
20 <tr><th>Username<tmpl_loop problems><th><tmpl_var _></tmpl_loop><th>Total
21 <tbody>
22 <tmpl_loop st><tr><td><tmpl_var user>
23 <tmpl_loop scores><td><tmpl_var _>
24 </tmpl_loop><td><tmpl_var score>
25 </tmpl_loop>
26 </table>
27 HTML
28 );
29
30 my %templates = cook_templates %orig_templates, st => 'Standings';
31
32 sub generate{
33 %templates = cook_templates %orig_templates, st => 'Standings' if reload_templates;
34 local $Gruntmaster::Data::contest = ($_[0] =~ m,^ct/([^/]+)/,)[0];
35 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$_[1]});
36
37 my @problems = sort problems;
38 my %scores;
39 for (1 .. jobcard) {
40 if (job_result_text =~ m/^(\d+)/) {
41 $scores{job_user()}{job_problem()} = $1;
42 } else {
43 $scores{job_user()}{job_problem()} = job_result() ? 0 : 100;
44 }
45 }
46
47 my @st = sort { $b->{score} <=> $a->{score} } map {
48 my $user = $_;
49 +{
50 user => $user,
51 score => sum (values $scores{$user}),
52 scores => [map { $scores{$user}{$_} // '-'} @problems],
53 }
54 } keys %scores;
55 $htc->param(problems => \@problems);
56 $htc->param(st => \@st);
57 $htc->output
58 }
59
60 1
This page took 0.043437 seconds and 5 git commands to generate.