2 # A generator of score sheets. More ugly than it deserves.
12 $usage = "Usage: mo-score [--detail] [--alt] [--extras] [--html] [--tex] [--table] [--merged] [<directory>/]<task> ...";
13 while (($arg = $ARGV[0]) =~ /^--([a-z]+)$/) {
16 if (!eval "defined $var") { die $usage; }
23 open STDOUT, "|column -t -s'\t'" or die;
27 print STDERR "Scanning contestants... ";
28 open (CT, "bin/mo-get-users --full |") || die "Cannot get list of contestants";
32 ($u eq "somebody") && next;
36 print STDERR 0+keys %users, "\n";
38 print STDERR "Scanning exceptions... ";
39 if ($extras && open (EX, "exceptions")) {
42 (/^$/ || /^#/) && next;
45 defined $users{$u} || die "Unknown user $u";
46 while (@a) { $extra{$u} += shift @a; }
50 } else { print STDERR "none\n"; }
52 print STDERR "Scanning task results... ";
55 foreach $u (keys %users) {
56 foreach $task (@ARGV) {
57 my ($dir, $t) = ("testing", $task);
58 if ($task =~ m@^(.*)/([^/]*)$@) {
63 $tt = "$dir/$u/$t/points" . ($alt ? ".alt" : "");
65 print STDERR "$u/$t ";
66 open (X, $tt) || die "Unable to open $tt";
69 /^(\S+) (-?\d+)\s*(.*)/ || die "Parse error: $_";
74 ($ttest_merged = $ttest) =~ s/[^0-9]//g;
75 $ttest = $ttest_merged if $merged;
76 $known_tests{$t}{$ttest} = 1;
78 if ($tpts == 0 && $trem ne "OK") {
79 if ($trem =~ /^Compile /) { $cmt = "CE"; }
80 elsif ($trem =~ /^Time limit exceeded/) { $cmt = "TO"; }
81 elsif ($trem =~ /^Exited with error /) { $cmt = "RE"; }
82 elsif ($trem =~ /^Caught fatal signal /) { $cmt = "SG"; }
83 elsif ($trem =~ /^([A-Za-z])\S*\s+([A-Za-z])/) {
84 ($cmt = "$1$2") =~ tr/a-z/A-Z/;
85 } elsif ($trem =~ /^([A-Za-z]{2})/) {
86 ($cmt = $1) =~ tr/a-z/A-Z/;
88 if (!defined $messages{$trem}) {
89 $messages{$trem} = $cmt;
90 if (!defined $error_codes{$cmt}) {
91 $error_codes{$cmt} = $trem;
93 $error_codes{$cmt} .= ", $trem";
97 if (!defined($results{$u}{$t}{$ttest}) || $results{$u}{$t}{$ttest} > $tpts) {
98 $results{$u}{$t}{$ttest} = $tpts;
99 $comment{$u}{$t}{$ttest} = $cmt;
101 if (!defined($results_merged{$u}{$t}{$ttest_merged}) || $results_merged{$u}{$t}{$ttest_merged} > $tpts) {
102 $results_merged{$u}{$t}{$ttest_merged} = $tpts;
107 foreach my $t (keys %known_tasks) {
109 foreach my $pts (values %{$results_merged{$u}{$t}}) { $total{$u}{$t} += $pts; }
114 print STDERR "Creating table template... ";
115 @header = ("Rank","User","Name");
116 @body = ('','$u','$users{$u}');
118 @footer = ('"Total"','','');
120 push @header, "Extra";
121 push @body, '$extra{$u}';
123 push @bodysums, $col;
124 push @footer, "sum($col)";
127 foreach $task (@ARGV) {
128 my $t = ($task =~ m@/([^/]*)$@) ? $1 : $task;
129 defined $known_tasks{$t} || die "Unknown task $t";
131 push @header, substr($t, 0, 4);
132 push @body, "(\$xx = \$total{\$u}{'$t'}) > 0 ? \$xx : 0";
134 push @footer, "sum($col)";
135 push @bodysums, $col;
137 foreach $s (sort { $a <=> $b } keys %{$known_tests{$t}}) {
139 push @body, "\$comment{\$u}{'$t'}{'$s'}";
141 push @footer, "sum($col)";
145 push @header, "Total";
146 push @body, join('+', map { $_ = "\$$_" } @bodysums);
148 push @footer, "sum($col)";
151 print STDERR "h: ", join(':',@header), "\n" if $debug;
152 print STDERR "b: ", join(':',@body), "\n" if $debug;
153 print STDERR "f: ", join(':',@footer), "\n" if $debug;
155 print STDERR "Filling in results... ";
157 foreach $u (keys %users) {
159 foreach my $c (@body) {
160 $c =~ s/\$(\d+)/\$\$row[$1]/g;
162 push @$row, (defined $x ? $x : '-');
164 print STDERR "row: ", join(':',@$row), "\n" if $debug;
169 print STDERR "Sorting... ";
170 $sortcol = @{$table[0]} - 1;
174 $p = $$b[$sortcol] <=> $$a[$sortcol];
175 ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
176 ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
177 $p ? $p : ($an cmp $bn);
180 while ($i < @table) {
182 while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
186 ${table[$j]}[0] = "$i.";
188 ${table[$j]}[0] = $j+1 . ".-" . $i . ".";
190 while ($j < $i) { ${table[$j++]}[0] = " "; };
195 print STDERR "Attaching headers and footers... ";
196 sub sum { my $col=shift @_; my $t=0; foreach my $z (0..@table-1) { $t += ${$table[$z]}[$col]; } $t; }
197 map { $_ = eval $_; } @footer;
198 push @table, \@footer;
199 unshift @table, \@header;
203 foreach $r (@table) { print join(':',@$r), "\n"; }
205 print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd">', "\n";
206 print "<HTML><HEAD><TITLE>Rank list</TITLE></HEAD><BODY>\n";
207 print "<H1>Rank list</H1>\n";
210 &printHtmlHeader(\@perm);
213 foreach $r (@table[1..($#table - 1)]) {
214 &printHtmlRow(@{$r}[@perm]);
218 &printHtmlRow(@{$table[$#table]}[@perm]);
222 print "<H2>Error codes</H2><UL>\n";
223 foreach $r (sort keys %error_codes) { print "<LI>$r: $error_codes{$r}\n"; }
226 print "</BODY></HTML>\n";
228 print "\\error{TeX output not supported yet!}\n";
230 foreach $r (@table) { print join("\t",@$r), "\n"; }
233 foreach $r (sort keys %error_codes) { print "$r: $error_codes{$r}\n"; }
238 print "<TR>", join('',map {
239 if ($hdr) { $_ = "<TH>$_"; }
240 else { $_ = "<TD align=" . (/^[0-9A-Z-]+$/ ? "right" : "left") . (length($_) > 14 ? " width=150" : "") . ">$_"; }
244 sub printHtmlHeader {
248 my $colspec = "<colgroup span=3>";
256 $hdr1 = "<th rowspan=2>Rank<th rowspan=2>User<th rowspan=2>Name";
257 $extras and $p++ and push @$perm, 3 and $hdr1.="<th rowspan=2>Extra" and $colspec.="<colgroup span=1>"; ##Extra hack
258 for my $task (@tasks) {
260 my $nSub = scalar(keys %{$known_tests{$task}});
263 map { push @$perm, $p++ } (1..$nSub);
264 push @$perm, $p - $nSub - 1;
266 $colspec .= "<colgroup span='" . $nSub . "'>\n";
267 $colspec .= "<colgroup span='1'>\n";
268 $hdr1 .= "<th colspan='" . ($nSub + 1) . "' style='border-bottom:1px solid black;'>$task";
269 $hdr2 .= join("", map { "<th>$_" } sort {$a <=> $b} keys %{$known_tests{$task}});
270 $hdr2 .= "<th>Total";
273 $hdr1 .= "<th rowspan='2'>Total";
275 } else { ## no detail
277 $hdr1 = "<th>Rank<th>User<th>Name";
278 $extras and $p++ and push @$perm, 3 and $hdr1.="<th>Extra" and $colspec.="<colgroup span=1>"; ##Extra hack
280 for my $task (@tasks) {
282 $hdr1 .= "<th>$task";
284 $hdr1 .= "<th>Total";
285 $colspec .= "<colgroup span='" . scalar (@tasks) . "'>";
290 print "<TABLE rules=groups frame=all border='1' cellpadding='2'>\n";
291 print "$colspec<colgroup span='1'>\n";
292 print "<tr>$hdr1</tr>\n";
293 print "<tr>$hdr2</tr>\n" if $detail;