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