]> mj.ucw.cz Git - eval.git/blob - mop/score/mo-score-mop.sh
Handle exit code translation failures gracefully.
[eval.git] / mop / score / mo-score-mop.sh
1 #!/usr/bin/perl
2
3 $tex = 0;
4 $usage = "Usage: mo-score-mop [--tex] theoretical_tasks_nr praxis_tasks_nr task1 task2 ...";
5 while (($arg = $ARGV[0]) =~ /^--([a-z]+)$/) {
6         shift @ARGV;
7         $var = "\$$1";
8         if (!eval "defined $var") { die $usage; }
9         eval "$var = 1;";
10 }
11 @ARGV >=2 || die $usage;
12 $theory=shift @ARGV;
13 $praxis=shift @ARGV;
14 @ARGV >= $praxis || die $usage;
15 $pos_delim=$tex ? '--' : '-';
16
17 print STDERR "Scanning contestants... ";
18 open (CT, "bin/mo-get-users --full |") || die "Cannot get list of contestants";
19 while (<CT>) {
20         chomp;
21         ($u,$f) = split /\t/;
22         ($u eq "somebody") && next;
23         $users{$u}=$f;
24 }
25 close CT;
26 print STDERR 0+keys %users, "\n";
27
28 print STDERR "Scanning teoretical results... ";
29 if (open (EX, "../mop/score/teorie.txt")) {
30         while (<EX>) {
31                 chomp;
32                 (/^$/ || /^#/) && next;
33                 @a = split /\ *\t\ */;
34                 $u = shift @a;
35                 defined $users{$u} || die "Unknown user $u";
36                 $names{$u} = shift @a;
37                 $forms{$u} = shift @a;
38                 $addresses{$u} = "{". (shift @a) ."}";
39                 $i=0;
40                 while (@a) { $tasks{$u}{$i} = shift @a;$i++; }
41         }
42         close EX;
43         print STDERR "OK\n";
44 } else {die "none, cannot find file teorie.txt!\n";}
45
46 print STDERR "Scanning task results... ";
47 $need_tasks = join("|", @ARGV);
48 foreach $u (keys %users) {
49         opendir (D, "testing/$u") or next;
50         foreach $t (readdir(D)) {
51                 $t =~ /^\./ && next;
52                 $t =~ /$need_tasks/ || next;
53
54                 $t_num=$praxis;
55                 for (my $t_num2=0;$t_num2<@ARGV;$t_num2++) {if ($t eq $ARGV[$t_num2]) {$t_num=$t_num2;}}
56                 $t_num+=$theory;
57
58                 $tt = "testing/$u/$t/points";
59                 -f $tt || next;
60                 print STDERR "$u/$t ";
61                 open (X, $tt) || die "Unable to open $tt";
62                 while (<X>) {
63                         chomp;
64                         /^\S+ (-?\d+)/ || die "Parse error: $_";
65                         $tasks{$u}{$t_num} += $1;
66                 }
67                 close X;
68         }
69         closedir D;
70 }
71 print STDERR "OK\n";
72
73 print STDERR "Creating table template... ";
74 @body = ('','$names{$u}','$forms{$u}','$addresses{$u}');
75 for ($a=0;$a<$theory+$praxis;$a++) {push @body,"\$tasks{\$u}{$a}";}
76 print STDERR "OK\n";
77
78 print STDERR "Filling in results... ";
79 @table = ();
80 foreach $u (keys %users) {
81         next unless defined $names{$u}; # don't show any user not defined in teorie.txt
82         $row = [];
83         $row_index=0;
84         $row_sum=0;
85         foreach my $c (@body) {
86                 $c =~ s/\$(\d+)/\$\$row[$1]/g;
87                 $x = eval $c;
88                 push @$row, (defined $x ? $x : '-');
89                 if ($row_index>3) {
90                     if ((defined $x) && ($x>0)) {$row_sum+=$x;}
91                 }
92                 $row_index++;
93         }
94         push @$row, $row_sum;
95         push @table, $row;
96 }
97 print STDERR "OK\n";
98
99 print STDERR "Sorting... ";
100 $sortcol = @{$table[0]} - 1;
101 $namecol = 1;
102 @table = sort {
103         my $p, $an, $bn;
104         $p = $$b[$sortcol] <=> $$a[$sortcol];
105         ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
106         ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
107         $p ? $p : ($an cmp $bn);
108 } @table;
109 $i=0;
110 while ($i < @table) {
111         $j = $i;
112         while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
113                 $i++;
114         }
115         if ($i == $j+1) {
116                 ${table[$j]}[0] = "$i.";
117         } else {
118                 ${table[$j]}[0] = $j+1 . '.' . $pos_delim . $i . ".";
119                 $j_old=$j;
120                 $j++;
121                 while ($j < $i) { ${table[$j++]}[0] = $j_old+1 . '.' . $pos_delim . $i . "."; };
122         }
123 }
124 print STDERR "OK\n";
125
126 if ($tex) {
127         open HDR,"../mop/score/listina.hdr" or die "Cannot open file ../mop/score/listina.hdr with TeX template!";
128         while (<HDR>) {print; }
129         close HDR;
130         
131         foreach $r (@table) { print join('&',@$r), "\\cr\n";}
132
133         open FTR,"../mop/score/listina.ftr" or die "Cannot open file ../mop/score/listina.ftr with TeX template!";
134         while (<FTR>) {print; }
135         close FTR;
136 } else {
137         foreach $r (@table) { print join("\t",@$r), "\n"; }
138 }