package Gruntmaster::Page::St;

use 5.014000;
use strict;
use warnings;
use Gruntmaster::Page::Base st => 'Standings';
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';

use constant => TEMPLATES => {
	en => <<'HTML',
<table border>
<thead>
<tr><th>Username<tmpl_loop problems><th><tmpl_var _></tmpl_loop><th>Total
<tbody>
<tmpl_loop st><tr><td><tmpl_var user>
<tmpl_loop scores><td><tmpl_var _>
</tmpl_loop><td><tmpl_var score>
</tmpl_loop>
</table>
HTML
};

sub _generate{
	my ($self, $htc, $path, $lang) = @_;

	local $Gruntmaster::Data::contest = ($path =~ m,^ct/([^/]+)/,)[0];

	my @problems = sort problems;
	my %scores;
	for (1 .. jobcard) {
		if (job_result_text =~ m/^(\d+)/) {
			$scores{job_user()}{job_problem()} = $1;
		} else {
			$scores{job_user()}{job_problem()} = job_result() ? 0 : 100;
		}
	}

	my @st = sort { $b->{score} <=> $a->{score} } map {
		my $user = $_;
		+{
			user => $user,
			score => sum (values $scores{$user}),
			scores => [map { $scores{$user}{$_} // '-'} @problems],
		}
	} keys %scores;
	$htc->param(problems => \@problems);
	$htc->param(st => \@st);
}

1
