]> mj.ucw.cz Git - eval.git/blob - bin/mo-score
e04c47b0737866c62f463caf377b633b42ebaba4
[eval.git] / bin / mo-score
1 #!/usr/bin/perl
2
3 $debug = 0;
4 $detail = 0;
5 $html = 0;
6 $tex = 0;
7 $extras = 0;
8 $alt = 0;
9 $usage = "Usage: mo-score [--detail] [--alt] [--extras] [--html] [--tex] <task1> <task2> ...";
10 while (($arg = $ARGV[0]) =~ /^--([a-z]+)$/) {
11         shift @ARGV;
12         $var = "\$$1";
13         if (!eval "defined $var") { die $usage; }
14         eval "$var = 1;";
15 }
16 @ARGV || die $usage;
17
18 print STDERR "Scanning contestants... ";
19 open (CT, "bin/mo-get-users --full |") || die "Cannot get list of contestants";
20 while (<CT>) {
21         chomp;
22         ($u,$f) = split /\t/;
23         ($u eq "somebody") && next;
24         $users{$u}=$f;
25 }
26 close CT;
27 print STDERR 0+keys %users, "\n";
28
29 print STDERR "Scanning exceptions... ";
30 if ($extras && open (EX, "exceptions")) {
31         while (<EX>) {
32                 chomp;
33                 (/^$/ || /^#/) && next;
34                 @a = split /\s+/;
35                 $u = shift @a;
36                 defined $users{$u} || die "Unknown user $u";
37                 while (@a) { $extra{$u} += shift @a; }
38         }
39         close EX;
40         print STDERR "OK\n";
41 } else { print STDERR "none\n"; }
42
43 print STDERR "Scanning task results... ";
44 $need_tasks = join("|", @ARGV);
45 %messages = ();
46 %error_codes = ();
47 foreach $u (keys %users) {
48         opendir (D, "testing/$u") or next;
49         foreach $t (readdir(D)) {
50                 $t =~ /^\./ && next;
51                 $t =~ /$need_tasks/ || next;
52                 $known_tasks{$t} = 1;
53                 $tt = "testing/$u/$t/points" . ($alt ? ".alt" : "");
54                 -f $tt || next;
55                 print STDERR "$u/$t ";
56                 open (X, $tt) || die "Unable to open $tt";
57                 while (<X>) {
58                         chomp;
59                         /^(\S+) (-?\d+)\s*(.*)/ || die "Parse error: $_";
60                         $ttest = $1;
61                         $tpts = $2;
62                         $trem = $3;
63                         $known_tests{$t}{$ttest} = 1;
64                         $results{$u}{$t}{$ttest} = $tpts;
65                         $remarks{$u}{$t}{$ttest} = $trem;
66                         $cmt = $tpts;
67                         if ($tpts == 0) {
68                                 if ($trem =~ /^Compile /) { $cmt = "CE"; }
69                                 elsif ($trem =~ /^Time limit exceeded/) { $cmt = "TO"; }
70                                 elsif ($trem =~ /^Exited with error /) { $cmt = "RE"; }
71                                 elsif ($trem =~ /^Caught fatal signal /) { $cmt = "SG"; }
72                                 elsif ($trem =~ /^([A-Za-z])\S*\s+([A-Za-z])/) {
73                                         ($cmt = "$1$2") =~ tr/a-z/A-Z/;
74                                 }
75                                 elsif ($trem =~ /^Wrong answer/) { $cmt = "WA"; }
76                                 if (!defined $messages{$trem}) {
77                                         $messages{$trem} = $cmt;
78                                         if (!defined $error_codes{$cmt}) {
79                                                 $error_codes{$cmt} = $trem;
80                                         } else {
81                                                 $error_codes{$cmt} .= ", $trem";
82                                         }
83                                 }
84                         }
85                         $comment{$u}{$t}{$ttest} = $cmt;
86                         $total{$u}{$t} += $tpts;
87                 }
88                 close X;
89         }
90         closedir D;
91 }
92 print STDERR "OK\n";
93
94 print STDERR "Creating table template... ";
95 @header = ("Rank","User","Name");
96 @body = ('','$u','$users{$u}');
97 @bodysums = ();
98 @footer = ('"Total"','','');
99 if (keys %extra) {
100         push @header, "Extra";
101         push @body, '$extra{$u}';
102         $col = 0+@footer;
103         push @bodysums, $col;
104         push @footer, "sum($col)";
105 }
106 foreach $t (@ARGV) {
107         defined $known_tasks{$t} || die "Unknown task $t";
108         push @header, substr($t, 0, 4);
109         push @body, "(\$xx = \$total{\$u}{'$t'}) > 0 ? \$xx : 0";
110         $col = 0+@footer;
111         push @footer, "sum($col)";
112         push @bodysums, $col;
113         if ($detail) {
114                 foreach $s (sort { $a <=> $b } keys %{$known_tests{$t}}) {
115                         push @header, "$s";
116                         push @body, "\$comment{\$u}{'$t'}{'$s'}";
117                         $col = 0+@footer;
118                         push @footer, "sum($col)";
119                 }
120         }
121 }
122 push @header, "Total";
123 push @body, join('+', map { $_ = "\$$_" } @bodysums);
124 $col = 0+@footer;
125 push @footer, "sum($col)";
126 print STDERR "OK\n";
127
128 print STDERR "h: ", join(':',@header), "\n" if $debug;
129 print STDERR "b: ", join(':',@body), "\n" if $debug;
130 print STDERR "f: ", join(':',@footer), "\n" if $debug;
131
132 print STDERR "Filling in results... ";
133 @table = ();
134 foreach $u (keys %users) {
135         $row = [];
136         foreach my $c (@body) {
137                 $c =~ s/\$(\d+)/\$\$row[$1]/g;
138                 $x = eval $c;
139                 push @$row, (defined $x ? $x : '-');
140         }
141         print STDERR "row: ", join(':',@$row), "\n" if $debug;
142         push @table, $row;
143 }
144 print STDERR "OK\n";
145
146 print STDERR "Sorting... ";
147 $sortcol = @{$table[0]} - 1;
148 $namecol = 2;
149 @table = sort {
150         my $p, $an, $bn;
151         $p = $$b[$sortcol] <=> $$a[$sortcol];
152         ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
153         ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
154         $p ? $p : ($an cmp $bn);
155 } @table;
156 $i=0;
157 while ($i < @table) {
158         $j = $i;
159         while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
160                 $i++;
161         }
162         if ($i == $j+1) {
163                 ${table[$j]}[0] = "$i.";
164         } else {
165                 ${table[$j]}[0] = $j+1 . ".-" . $i . ".";
166                 $j++;
167                 while ($j < $i) { ${table[$j++]}[0] = ""; };
168         }
169 }
170 print STDERR "OK\n";
171
172 print STDERR "Attaching headers and footers... ";
173 sub sum { my $col=shift @_; my $t=0; foreach my $z (0..@table-1) { $t += ${$table[$z]}[$col]; } $t; }
174 map { $_ = eval $_; } @footer;
175 push @table, \@footer;
176 unshift @table, \@header;
177 print STDERR "OK\n";
178
179 if ($debug) {
180         foreach $r (@table) { print join(':',@$r), "\n"; }
181 } elsif ($html) {
182         print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd">', "\n";
183         print "<HTML><HEAD><TITLE>Rank list</TITLE></HEAD><BODY>\n";
184         print "<H1>Rank list</H1>\n";
185         print "<TABLE>\n";
186         $hdr = 1;
187         foreach $r (@table) {
188                 print "<TR>", join('',map {
189                         if ($hdr) { $_ = "<TH>$_"; }
190                         else { $_ = "<TD align=" . (/^[0-9A-Z-]+$/ ? "right" : "left") . (length($_) > 14 ? " width=150" : "") . ">$_"; }
191                 } @$r), "\n";
192                 $hdr = 0;
193         }
194         print "</TABLE>\n";
195         print "<H2>Error codes</H2><UL>\n";
196         foreach $r (sort keys %error_codes) { print "<LI>$r: $error_codes{$r}\n"; }
197         print "</UL>\n";
198         print "</BODY></HTML>\n";
199 } elsif ($tex) {
200         print "\\error{TeX output not supported yet!}\n";
201 } else {
202         foreach $r (@table) { print join("\t",@$r), "\n"; }
203         print "\n";
204         foreach $r (sort keys %error_codes) { print "$r: $error_codes{$r}\n"; }
205 }