From fe78f0c128c35e68e65850b02057997bad6717fe Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Wed, 27 Nov 2013 22:59:52 +0200 Subject: [PATCH] Add contest/multispace support --- lib/Gruntmaster/Page.pm | 21 ++++--- lib/Gruntmaster/Page/Ct.pm | 99 +++++++++++++++++++++++++++++++ lib/Gruntmaster/Page/Ct/Entry.pm | 51 ++++++++++++++++ lib/Gruntmaster/Page/Log.pm | 14 +++-- lib/Gruntmaster/Page/Log/Entry.pm | 9 +-- lib/Gruntmaster/Page/Pb.pm | 12 ++-- lib/Gruntmaster/Page/Pb/Entry.pm | 22 +++++-- 7 files changed, 201 insertions(+), 27 deletions(-) create mode 100644 lib/Gruntmaster/Page/Ct.pm create mode 100644 lib/Gruntmaster/Page/Ct/Entry.pm diff --git a/lib/Gruntmaster/Page.pm b/lib/Gruntmaster/Page.pm index 5c63f64..d014ac1 100644 --- a/lib/Gruntmaster/Page.pm +++ b/lib/Gruntmaster/Page.pm @@ -32,7 +32,7 @@ my %header_templates = (
iEval
TITLE_GOES_HERE
- + HTML ); @@ -65,12 +65,19 @@ sub declaregen{ push @generators, [$regex, $gensub]; } -declaregen Index => qr'^index$'; -declaregen Log => qr'^log/index$'; -declaregen 'Log::Entry' => qr'^log/.*/index$'; -declaregen Submit => qr'^submit$'; -declaregen Pb => qr'^pb/index$'; -declaregen 'Pb::Entry' => qr'^pb/.*/index$'; +{ + my $component = qr'[^/]+'; + my $contest = qr,(?:ct/$component/)?,; + declaregen Index => qr,^index$,; + declaregen Ct => qr,^ct/index$,; + declaregen 'Ct::Entry' => qr,^ct/$component/index$,; + #declaregen St => qr,^ct/$component/st/index$,; + declaregen Log => qr,^${contest}log/index$,; + declaregen 'Log::Entry' => qr,^${contest}log/$component/index$,; + declaregen Submit => qr,^${contest}submit$,; + declaregen Pb => qr,^${contest}pb/index$,; + declaregen 'Pb::Entry' => qr,^${contest}pb/$component/index$,; +} sub generate{ my ($path) = @_; diff --git a/lib/Gruntmaster/Page/Ct.pm b/lib/Gruntmaster/Page/Ct.pm new file mode 100644 index 0000000..c222e62 --- /dev/null +++ b/lib/Gruntmaster/Page/Ct.pm @@ -0,0 +1,99 @@ +package Gruntmaster::Page::Ct; + +use 5.014000; +use strict; +use warnings; +use parent qw/Exporter/; +our @EXPORT_OK = qw/generate/; +our $VERSION = '0.001'; + +use constant TITLE => 'Contests'; + +use Fcntl qw/:flock/; +use HTML::Template::Compiled; +use IO::File; +use POSIX qw/strftime/; +use YAML::Any qw/LoadFile/; +use Gruntmaster::Page qw/header footer/; + +my %templates = ( + en => <<'HTML', + +

Running contests

+ + + +
NameStart dateEnd dateOwner +
+ + + + +
+
+ + +

Pending contests

+ + + +
NameStart dateEnd dateOwner +
+ + + + +
+
+ + +

Finished contests

+ + + +
NameStart dateEnd dateOwner +
+ + + + +
+
+HTML +); + +$templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates; +$templates{$_} .= footer $_ for keys %templates; + +sub generate{ + my $template = $templates{$_[1]}; + my $htc = HTML::Template::Compiled->new(scalarref => \$template); + IO::File->new('>ct/meta.yml')->close unless -f 'ct/meta.yml'; + flock my $metafh = IO::File->new(') { + my $meta = LoadFile $_; + my $id = (m,^ct/(.*)/meta.yml$,)[0]; + my $ct = { id => $id, + name => $meta->{name}, + start => strftime ('%c', localtime $meta->{start}), + end => strftime ('%c', localtime $meta->{end}), + owner => $meta->{owner}}; + + my $time = time; + push @pending, $ct if time < $meta->{start}; + push @running, $ct if time >= $meta->{start} && time < $meta->{end}; + push @finished, $ct if time > $meta->{end}; + } + + $htc->param(runningn => scalar @running); + $htc->param(pendingn => scalar @pending); + $htc->param(finishedn => scalar @finished); + $htc->param(running => \@running); + $htc->param(pending => \@pending); + $htc->param(finished => \@finished); + $htc->output +} + +1 diff --git a/lib/Gruntmaster/Page/Ct/Entry.pm b/lib/Gruntmaster/Page/Ct/Entry.pm new file mode 100644 index 0000000..625020d --- /dev/null +++ b/lib/Gruntmaster/Page/Ct/Entry.pm @@ -0,0 +1,51 @@ +package Gruntmaster::Page::Ct::Entry; + +use 5.014000; +use strict; +use warnings; +use parent qw/Exporter/; +our @EXPORT_OK = qw/generate/; +our $VERSION = '0.001'; + +use Fcntl qw/:flock/; +use HTML::Template::Compiled; +use IO::File; +use POSIX qw/strftime/; +use YAML::Any qw/LoadFile/; +use File::Basename qw/fileparse/; +use File::Slurp qw/slurp/; +use Gruntmaster::Page qw/header footer/; + +use constant TITLE => ''; + +my %templates = ( + en => <<'HTML', +Contest start time:
+Contest end time:

+ +Problems
+Job log
+Standings
+HTML +); + +$templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates; +$templates{$_} .= footer $_ for keys %templates; + +sub generate{ + my ($path, $lang) = @_; + $path = ($path =~ m,ct/(.*)/index,)[0]; + my $template = $templates{$lang}; + my $htc = HTML::Template::Compiled->new(scalarref => \$template); + flock my $metafh = IO::File->new("param(id => $path); + $htc->param(name => $meta->{name}); + $htc->param(start => strftime '%c', localtime $meta->{start}); + $htc->param(end => strftime '%c', localtime $meta->{end}); + $htc->param(started => time >= $meta->{start}); + $htc->output +} + +1 diff --git a/lib/Gruntmaster/Page/Log.pm b/lib/Gruntmaster/Page/Log.pm index b12d779..d276f55 100644 --- a/lib/Gruntmaster/Page/Log.pm +++ b/lib/Gruntmaster/Page/Log.pm @@ -36,13 +36,15 @@ $templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates; $templates{$_} .= footer $_ for keys %templates; sub generate{ - my $template = $templates{$_[1]}; + my ($path, $lang) = @_; + $path =~ s,/index\.html$,,; + my $template = $templates{$lang}; my $htc = HTML::Template::Compiled->new(scalarref => \$template); - IO::File->new('>log/meta.yml')->close unless -f 'log/meta.yml'; - flock my $metafh = IO::File->new('new(">$path/meta.yml")->close unless -f "$path/meta.yml"; + flock my $metafh = IO::File->new("<$path/meta.yml"), LOCK_SH; + my $meta = LoadFile "$path/meta.yml"; my @log = sort { $b->{id} <=> $a->{id} } map { - my $meta = LoadFile "log/$_/meta.yml"; + my $meta = LoadFile "$path/$_/meta.yml"; +{ id => $_, date => strftime ('%c' => localtime $meta->{date}), user => $meta->{user}, @@ -51,7 +53,7 @@ sub generate{ problem => $meta->{problem}, filename => $meta->{files}{prog}{name}, (defined $meta->{private} ? (private => $meta->{private}) : ()), - size => sprintf ("%.2f KiB", (-s "log/$_/in/" . $meta->{files}{prog}{name}) / 1024), + size => sprintf ("%.2f KiB", (-s "$path/$_/in/" . $meta->{files}{prog}{name}) / 1024), result_text => $meta->{result_text}} } 1 .. $meta->{last}; $htc->param(log => \@log); $htc->output diff --git a/lib/Gruntmaster/Page/Log/Entry.pm b/lib/Gruntmaster/Page/Log/Entry.pm index 08230bf..4b7a70d 100644 --- a/lib/Gruntmaster/Page/Log/Entry.pm +++ b/lib/Gruntmaster/Page/Log/Entry.pm @@ -33,19 +33,20 @@ $templates{$_} .= footer $_ for keys %templates; sub generate{ my ($path, $lang) = @_; - $path = ($path =~ m,log/(.*)/index,)[0]; + $id = ($path =~ m,log/(.*)/index,)[0]; + $path =~ s,/index\.html,,; my $template = $templates{$lang}; my $htc = HTML::Template::Compiled->new(scalarref => \$template); - flock my $metafh = IO::File->new("new("<$path/meta.yml"), LOCK_SH; + my $meta = LoadFile "$path/meta.yml"; my @tests = map { $_->{time} = sprintf "%.4fs", $_->{time}; $_ } @{$meta->{results}}; - $htc->param(id => $path); + $htc->param(id => $id); $htc->param(tests => \@tests); $htc->output } diff --git a/lib/Gruntmaster/Page/Pb.pm b/lib/Gruntmaster/Page/Pb.pm index efe7d26..de9db87 100644 --- a/lib/Gruntmaster/Page/Pb.pm +++ b/lib/Gruntmaster/Page/Pb.pm @@ -28,14 +28,16 @@ $templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates; $templates{$_} .= footer $_ for keys %templates; sub generate{ - my $template = $templates{$_[1]}; + my ($path, $lang) = @_; + $path =~ s,/index\.html$,,; + my $template = $templates{$lang}; my $htc = HTML::Template::Compiled->new(scalarref => \$template); - IO::File->new('>pb/meta.yml')->close unless -f 'pb/meta.yml'; - flock my $metafh = IO::File->new('new(">$path/meta.yml")->close unless -f "$path/meta.yml"; + flock my $metafh = IO::File->new("<$path/meta.yml"), LOCK_SH; my @problems = sort { $b->{name} cmp $a->{name} } map { my $meta = LoadFile $_; - my $id = (m,^pb/(.*)/meta.yml$,)[0]; - +{ id => $id, name => $meta->{name} } } ; + my $id = (m,^$path/(.*)/meta.yml$,)[0]; + +{ id => $id, name => $meta->{name} } } <$path/*/meta.yml>; $htc->param(problems => \@problems); $htc->output } diff --git a/lib/Gruntmaster/Page/Pb/Entry.pm b/lib/Gruntmaster/Page/Pb/Entry.pm index 0f91c01..6252b99 100644 --- a/lib/Gruntmaster/Page/Pb/Entry.pm +++ b/lib/Gruntmaster/Page/Pb/Entry.pm @@ -23,9 +23,11 @@ my %templates = ( en => <<'HTML', +

Submit solution

+ + HTML ); @@ -41,16 +44,25 @@ $templates{$_} .= footer $_ for keys %templates; sub generate{ my ($path, $lang) = @_; - $path = ($path =~ m,pb/(.*)/index,)[0]; + my $contest; + $contest = $1 if $path =~ m,ct/([^/]*)/,; + my $id = ($path =~ m,pb/([^/]*)/index,)[0]; + $path =~ s,/index\.html$,,; my $template = $templates{$_[1]}; my $htc = HTML::Template::Compiled->new(scalarref => \$template); - flock my $metafh = IO::File->new("new("<$path/meta.yml"), LOCK_SH; + my $meta = LoadFile "$path/meta.yml"; + $htc->param(cansubmit => 1); + if (defined $contest) { + my $meta = LoadFile "ct/$contest/meta.yml"; + $htc->param(cansubmit => time >= $meta->{start} && time <= $meta->{end}); + $htc->param(contest => $contest); + } $htc->param(formats => FORMATS); - $htc->param(id => $path); + $htc->param(id => $id); $htc->param(name => $meta->{name}); - $htc->param(statement => scalar slurp "pb/$path/statement.$lang"); + $htc->param(statement => scalar slurp "$path/statement.$lang"); $htc->output } -- 2.39.2