]> mj.ucw.cz Git - moe.git/blob - mo-score-mop.sh
fc6c6a297783f767c2278a1f7c744bb008b0d51b
[moe.git] / 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
63                 my %tests = ();
64                 while (<X>) {
65                         chomp;
66                         /^(\S+) (-?\d+)/ || die "Parse error: $_";
67                         my ($t, $p) = ($1, $2);
68                         $t =~ s/[^0-9]//g;
69                         $tests{$t} = $p if not exists $tests{$t} or $tests{$t} > $p;
70                 }
71                 foreach my $p (values %tests) {
72                         $tasks{$u}{$t_num} += $p;
73                 }
74                 close X;
75         }
76         closedir D;
77 }
78 print STDERR "OK\n";
79
80 print STDERR "Creating table template... ";
81 @body = ('','$names{$u}','$forms{$u}','$addresses{$u}');
82 for ($a=0;$a<$theory+$praxis;$a++) {push @body,"\$tasks{\$u}{$a}";}
83 print STDERR "OK\n";
84
85 print STDERR "Filling in results... ";
86 @table = ();
87 foreach $u (keys %users) {
88         next unless defined $names{$u}; # don't show any user not defined in teorie.txt
89         $row = [];
90         $row_index=0;
91         $row_sum=0;
92         foreach my $c (@body) {
93                 $c =~ s/\$(\d+)/\$\$row[$1]/g;
94                 $x = eval $c;
95                 push @$row, (defined $x ? $x : '-');
96                 if ($row_index>3) {
97                     if ((defined $x) && ($x>0)) {$row_sum+=$x;}
98                 }
99                 $row_index++;
100         }
101         push @$row, $row_sum;
102         push @table, $row;
103 }
104 print STDERR "OK\n";
105
106 print STDERR "Sorting... ";
107 $sortcol = @{$table[0]} - 1;
108 $namecol = 1;
109 @table = sort {
110         my $p, $an, $bn;
111         $p = $$b[$sortcol] <=> $$a[$sortcol];
112         ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
113         ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
114         $p ? $p : ($an cmp $bn);
115 } @table;
116 $i=0;
117 while ($i < @table) {
118         $j = $i;
119         while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
120                 $i++;
121         }
122         if ($i == $j+1) {
123                 ${table[$j]}[0] = "$i.";
124         } else {
125                 ${table[$j]}[0] = $j+1 . '.' . $pos_delim . $i . ".";
126                 $j_old=$j;
127                 $j++;
128                 while ($j < $i) { ${table[$j++]}[0] = $j_old+1 . '.' . $pos_delim . $i . "."; };
129         }
130 }
131 print STDERR "OK\n";
132
133 if ($tex) {
134         open HDR,"mop/score/listina.hdr" or die "Cannot open file mop/score/listina.hdr with TeX template!";
135         while (<HDR>) {print; }
136         close HDR;
137         
138         foreach $r (@table) { print join('&',@$r), "\\cr\n";}
139
140         open FTR,"mop/score/listina.ftr" or die "Cannot open file mop/score/listina.ftr with TeX template!";
141         while (<FTR>) {print; }
142         close FTR;
143 } else {
144         foreach $r (@table) { print join("\t",@$r), "\n"; }
145 }