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