]>
Commit | Line | Data |
---|---|---|
fe78f0c1 MG |
1 | package Gruntmaster::Page::Ct; |
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 => 'Contests'; | |
11 | ||
12 | use Fcntl qw/:flock/; | |
13 | use HTML::Template::Compiled; | |
14 | use IO::File; | |
15 | use POSIX qw/strftime/; | |
16 | use YAML::Any qw/LoadFile/; | |
17 | use Gruntmaster::Page qw/header footer/; | |
18 | ||
19 | my %templates = ( | |
20 | en => <<'HTML', | |
21 | <tmpl_if runningn> | |
22 | <h1>Running contests</h1> | |
23 | <table border> | |
24 | <thead> | |
25 | <tr><th>Name<th>Start date<th>End date<th>Owner | |
26 | <tbody> | |
27 | <tmpl_loop running><tr><td><a href="<tmpl_var id>"><tmpl_var name></a> | |
28 | <td><tmpl_var start> | |
29 | <td><tmpl_var end> | |
30 | <td><tmpl_var owner> | |
31 | </tmpl_loop> | |
32 | </table> | |
33 | </tmpl_if> | |
34 | ||
35 | <tmpl_if pendingn> | |
36 | <h1>Pending contests</h1> | |
37 | <table border> | |
38 | <thead> | |
39 | <tr><th>Name<th>Start date<th>End date<th>Owner | |
40 | <tbody> | |
41 | <tmpl_loop pending><tr><td><a href="<tmpl_var id>"><tmpl_var name></a> | |
42 | <td><tmpl_var start> | |
43 | <td><tmpl_var end> | |
44 | <td><tmpl_var owner> | |
45 | </tmpl_loop> | |
46 | </table> | |
47 | </tmpl_if> | |
48 | ||
49 | <tmpl_if finishedn> | |
50 | <h1>Finished contests</h1> | |
51 | <table border> | |
52 | <thead> | |
53 | <tr><th>Name<th>Start date<th>End date<th>Owner | |
54 | <tbody> | |
55 | <tmpl_loop finished><tr><td><a href="<tmpl_var id>"><tmpl_var name></a> | |
56 | <td><tmpl_var start> | |
57 | <td><tmpl_var end> | |
58 | <td><tmpl_var owner> | |
59 | </tmpl_loop> | |
60 | </table> | |
61 | </tmpl_if> | |
62 | HTML | |
63 | ); | |
64 | ||
65 | $templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates; | |
66 | $templates{$_} .= footer $_ for keys %templates; | |
67 | ||
68 | sub generate{ | |
69 | my $template = $templates{$_[1]}; | |
70 | my $htc = HTML::Template::Compiled->new(scalarref => \$template); | |
71 | IO::File->new('>ct/meta.yml')->close unless -f 'ct/meta.yml'; | |
72 | flock my $metafh = IO::File->new('<ct/meta.yml'), LOCK_SH; | |
73 | ||
74 | my (@running, @pending, @finished); | |
75 | for (<ct/*/meta.yml>) { | |
76 | my $meta = LoadFile $_; | |
77 | my $id = (m,^ct/(.*)/meta.yml$,)[0]; | |
78 | my $ct = { id => $id, | |
79 | name => $meta->{name}, | |
80 | start => strftime ('%c', localtime $meta->{start}), | |
81 | end => strftime ('%c', localtime $meta->{end}), | |
82 | owner => $meta->{owner}}; | |
83 | ||
84 | my $time = time; | |
85 | push @pending, $ct if time < $meta->{start}; | |
86 | push @running, $ct if time >= $meta->{start} && time < $meta->{end}; | |
87 | push @finished, $ct if time > $meta->{end}; | |
88 | } | |
89 | ||
90 | $htc->param(runningn => scalar @running); | |
91 | $htc->param(pendingn => scalar @pending); | |
92 | $htc->param(finishedn => scalar @finished); | |
93 | $htc->param(running => \@running); | |
94 | $htc->param(pending => \@pending); | |
95 | $htc->param(finished => \@finished); | |
96 | $htc->output | |
97 | } | |
98 | ||
99 | 1 |