]> mj.ucw.cz Git - eval.git/blob - bin/mo-score
ev-all can fail.
[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 foreach $u (keys %users) {
46         opendir (D, "testing/$u") or next;
47         foreach $t (readdir(D)) {
48                 $t =~ /^\./ && next;
49                 $t =~ /$need_tasks/ || next;
50                 $known_tasks{$t} = 1;
51                 $tt = "testing/$u/$t/points" . ($alt ? ".alt" : "");
52                 -f $tt || next;
53                 print STDERR "$u/$t ";
54                 open (X, $tt) || die "Unable to open $tt";
55                 while (<X>) {
56                         chomp;
57                         /^(\S+) (-?\d+)\s*(.*)/ || die "Parse error: $_";
58                         $ttest = $1;
59                         $tpts = $2;
60                         $trem = $3;
61                         $known_tests{$t}{$ttest} = 1;
62                         $results{$u}{$t}{$ttest} = $tpts;
63                         $remarks{$u}{$t}{$ttest} = $trem;
64                         $cmt = $tpts;
65                         if ($tpts == 0) {
66                                 if ($trem =~ /^Compile /) { $cmt = "CE"; }
67                                 elsif ($trem =~ /^Time limit exceeded/) { $cmt = "TO"; }
68                                 elsif ($trem =~ /^Exited with error /) { $cmt = "RE"; }
69                                 elsif ($trem =~ /^Caught fatal signal /) { $cmt = "SG"; }
70                                 elsif ($trem =~ /^([A-Za-z])\S*\s+([A-Za-z])/) {
71                                         ($cmt = "$1$2") =~ tr/a-z/A-Z/;
72                                 }
73                                 elsif ($trem =~ /^Wrong answer/) { $cmt = "WA"; }
74                         }
75                         $comment{$u}{$t}{$ttest} = $cmt;
76                         $total{$u}{$t} += $tpts;
77                 }
78                 close X;
79         }
80         closedir D;
81 }
82 print STDERR "OK\n";
83
84 print STDERR "Creating table template... ";
85 @header = ("Rank","User","Name");
86 @body = ('','$u','$users{$u}');
87 @bodysums = ();
88 @footer = ('"Total"','','');
89 if (keys %extra) {
90         push @header, "Extra";
91         push @body, '$extra{$u}';
92         $col = 0+@footer;
93         push @bodysums, $col;
94         push @footer, "sum($col)";
95 }
96 foreach $t (@ARGV) {
97         defined $known_tasks{$t} || die "Unknown task $t";
98         push @header, substr($t, 0, 4);
99         push @body, "(\$xx = \$total{\$u}{'$t'}) > 0 ? \$xx : 0";
100         $col = 0+@footer;
101         push @footer, "sum($col)";
102         push @bodysums, $col;
103         if ($detail) {
104                 foreach $s (sort { $a <=> $b } keys %{$known_tests{$t}}) {
105                         push @header, "$s";
106                         push @body, "\$comment{\$u}{'$t'}{'$s'}";
107                         $col = 0+@footer;
108                         push @footer, "sum($col)";
109                 }
110         }
111 }
112 push @header, "Total";
113 push @body, join('+', map { $_ = "\$$_" } @bodysums);
114 $col = 0+@footer;
115 push @footer, "sum($col)";
116 print STDERR "OK\n";
117
118 print STDERR "h: ", join(':',@header), "\n" if $debug;
119 print STDERR "b: ", join(':',@body), "\n" if $debug;
120 print STDERR "f: ", join(':',@footer), "\n" if $debug;
121
122 print STDERR "Filling in results... ";
123 @table = ();
124 foreach $u (keys %users) {
125         $row = [];
126         foreach my $c (@body) {
127                 $c =~ s/\$(\d+)/\$\$row[$1]/g;
128                 $x = eval $c;
129                 push @$row, (defined $x ? $x : '-');
130         }
131         print STDERR "row: ", join(':',@$row), "\n" if $debug;
132         push @table, $row;
133 }
134 print STDERR "OK\n";
135
136 print STDERR "Sorting... ";
137 $sortcol = @{$table[0]} - 1;
138 $namecol = 2;
139 @table = sort {
140         my $p, $an, $bn;
141         $p = $$b[$sortcol] <=> $$a[$sortcol];
142         ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
143         ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
144         $p ? $p : ($an cmp $bn);
145 } @table;
146 $i=0;
147 while ($i < @table) {
148         $j = $i;
149         while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
150                 $i++;
151         }
152         if ($i == $j+1) {
153                 ${table[$j]}[0] = "$i.";
154         } else {
155                 ${table[$j]}[0] = $j+1 . ".-" . $i . ".";
156                 $j++;
157                 while ($j < $i) { ${table[$j++]}[0] = ""; };
158         }
159 }
160 print STDERR "OK\n";
161
162 print STDERR "Attaching headers and footers... ";
163 sub sum { my $col=shift @_; my $t=0; foreach my $z (0..@table-1) { $t += ${$table[$z]}[$col]; } $t; }
164 map { $_ = eval $_; } @footer;
165 push @table, \@footer;
166 unshift @table, \@header;
167 print STDERR "OK\n";
168
169 if ($debug) {
170         foreach $r (@table) { print join(':',@$r), "\n"; }
171 } elsif ($html) {
172         print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd">', "\n";
173         print "<HTML><HEAD><TITLE>Rank list</TITLE></HEAD><BODY>\n";
174         print "<H1>Rank list</H1>\n";
175         print "<TABLE>\n";
176         $hdr = 1;
177         foreach $r (@table) {
178                 print "<TR>", join('',map {
179                         if ($hdr) { $_ = "<TH>$_"; }
180                         else { $_ = "<TD align=" . (/^[0-9A-Z-]+$/ ? "right" : "left") . (length($_) > 14 ? " width=150" : "") . ">$_"; }
181                 } @$r), "\n";
182                 $hdr = 0;
183         }
184         print "</TABLE>\n";
185         print "</BODY></HTML>\n";
186 } elsif ($tex) {
187         print "\\error{TeX output not supported yet!}\n";
188 } else {
189         foreach $r (@table) { print join("\t",@$r), "\n"; }
190 }