]> mj.ucw.cz Git - moe.git/blob - bin/mo-score2005
...
[moe.git] / bin / mo-score2005
1 #!/usr/bin/perl
2
3 $tex = 0;
4 $usage = "Usage: mo-score2004 [--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, "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\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         $row = [];
82         $row_index=0;
83         $row_sum=0;
84         foreach my $c (@body) {
85                 $c =~ s/\$(\d+)/\$\$row[$1]/g;
86                 $x = eval $c;
87                 push @$row, (defined $x ? $x : '-');
88                 if ($row_index>3) {
89                     if ((defined $x) && ($x>0)) {$row_sum+=$x;}
90                 }
91                 $row_index++;
92         }
93         push @$row, $row_sum;
94         push @table, $row;
95 }
96 print STDERR "OK\n";
97
98 print STDERR "Sorting... ";
99 $sortcol = @{$table[0]} - 1;
100 $namecol = 1;
101 @table = sort {
102         my $p, $an, $bn;
103         $p = $$b[$sortcol] <=> $$a[$sortcol];
104         ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
105         ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
106         $p ? $p : ($an cmp $bn);
107 } @table;
108 $i=0;
109 while ($i < @table) {
110         $j = $i;
111         while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
112                 $i++;
113         }
114         if ($i == $j+1) {
115                 ${table[$j]}[0] = "$i.";
116         } else {
117                 ${table[$j]}[0] = $j+1 . '.' . $pos_delim . $i . ".";
118                 $j_old=$j;
119                 $j++;
120                 while ($j < $i) { ${table[$j++]}[0] = $j_old+1 . '.' . $pos_delim . $i . "."; };
121         }
122 }
123 print STDERR "OK\n";
124
125 if ($tex) {
126         open HDR,"teorie.hdr" or die "Cannot open file teorie.hdr with TeX template!";
127         while (<HDR>) {print; }
128         close HDR;
129         
130         foreach $r (@table) { print join('&',@$r), "\\cr\n";}
131
132 print '}}}
133
134 \bye
135 ';
136 } else {
137         foreach $r (@table) { print join("\t",@$r), "\n"; }
138 }