]> git.sesse.net Git - wloh/blob - www/index.pl
Add some disclaimers.
[wloh] / www / index.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 no warnings qw(once);
5 use CGI;
6 use CGI::Carp qw( fatalsToBrowser );
7 use DBI;
8 use POSIX;
9 use Devel::Peek;
10 use HTML::Entities;
11 use Encode;
12 use locale;
13 require '../config.pm';
14
15 my $cgi = CGI->new;
16
17 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
18         or die "connect: " . $DBI::errstr;
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
21
22 my $trials = 25_000;
23
24 binmode STDOUT, ':utf8';
25
26 my %players = ();
27 my %ratings = ();
28 my @matches = ();
29 my %parms = ();
30 my $match_stddev;
31
32 sub sanitize {
33         return HTML::Entities::encode_entities(shift);
34 }
35
36 sub color {
37         my $x = shift;
38         return int(255.0 * ($x ** (1.80)));
39 }
40
41 sub make_table {
42         my ($lowest_division, $used_ratings) = @_;
43
44         print <<"EOF";
45
46     <table>
47       <tr>
48         <th></th>
49 EOF
50
51         POSIX::setlocale(&POSIX::LC_ALL, 'C');
52
53         my $tmpnam = POSIX::tmpnam();
54         open MCCALC, ">", $tmpnam
55                 or die "$tmpnam: $!";
56
57         printf MCCALC "%f\n", $match_stddev;
58         printf MCCALC "%d\n", scalar keys %players;
59
60         for my $id (keys %players) {
61                 if (exists($used_ratings->{$id})) {
62                         printf MCCALC "%s %f\n", $id, $used_ratings->{$id};
63                 } else {
64                         printf MCCALC "%s %f\n", $id, 1500.0;
65                 }
66         }
67
68         for my $match (@matches) {
69                 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
70         }
71         close MCCALC;
72
73         POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
74
75         my %prob = ();
76
77         open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
78                 or die "mccalc: $!";
79         while (<MCCALC>) {
80                 chomp;
81                 my @x = split /\s+/;
82                 my $id = $x[0];
83                 my $player = $players{$id};
84                 $prob{$player} = [ @x[1..$#x] ];
85         }
86         close MCCALC;
87         #unlink $tmpnam;
88
89         my $num_games = scalar keys %prob;
90         for my $i (1..$num_games) {
91                 print "        <th>$i.</th>\n";
92         }
93         print "        <th>NEDRYKK</th>\n" unless ($lowest_division);
94         print "      </tr>\n";
95
96         for my $player (sort { $a cmp $b } keys %prob) {
97                 print "      <tr>\n";
98                 print "        <th>$player</th>\n";
99
100                 for my $i (1..$num_games) {
101                         my $pn = $prob{$player}->[$i - 1] / $trials;
102
103                         my $r = color(1.0 - $pn / 3);
104                         my $g = color(1.0 - $pn / 3);
105                         my $b = color(1.0);
106
107                         if ($i == 1) {
108                                 ($g, $b) = ($b, $g);
109                         } elsif ($i >= $num_games - 1 && !$lowest_division) {
110                                 ($r, $b) = ($b, $r);
111                         }
112
113                         printf "        <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
114                 }
115
116                 unless ($lowest_division) {
117                         my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
118
119                         my $r = color(1.0);
120                         my $g = color(1.0 - $pn / 3);
121                         my $b = color(1.0 - $pn / 3);
122                         printf "        <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
123                 }
124                 print "      </tr>\n";
125         }
126
127         print << "EOF";
128     </table>
129 EOF
130 }
131
132 # Get auxillary parameters
133 my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
134 $q->execute;
135
136 while (my $ref = $q->fetchrow_hashref) {
137         $parms{$ref->{'id'}} = $ref->{'rating'};
138 }
139 $match_stddev = $parms{-2} * sqrt(2.0);
140
141 my $season;
142 my $division = $cgi->param('divisjon') // -1;
143 my $subdivision = $cgi->param('avdeling') // -1;
144 my $last_division = 0;
145
146 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
147
148 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
149 printf <<"EOF", $match_stddev;
150 <html>
151   <head>
152     <title>WLoH-plasseringsannsynlighetsberegning</title>
153     <style type="text/css">
154 body {
155         color: black;
156         background: white;
157         font-family: sans-serif;
158 }
159 table {
160         border-collapse: collapse;
161         border: 1px solid black;
162 }
163 td, th {
164         border: 1px solid black;
165         padding: 5px;
166 }
167 td {
168         text-align: right;
169 }
170     </style>
171   </head>
172   <body>
173     <h1>WLoH-plasseringsannsynlighetsberegning</h1>
174
175     <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
176       <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
177
178     <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
179       den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
180       %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
181       Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
182
183     <p>Spillerne er sortert etter nick.</p>
184
185     <form method="get" action="/">
186 EOF
187
188 $q = $dbh->prepare('SELECT MAX(sesong) AS max_sesong FROM fotballserier');
189 $q->execute;
190 my $season;
191 while (my $ref = $q->fetchrow_hashref) {
192         $season = $ref->{'max_sesong'};
193 }
194
195 print <<"EOF";
196      <p>Divisjon:
197         <select name="divisjon" onchange="form.submit();">
198 EOF
199
200 $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
201 $q->execute($season);
202
203 my $found_division = 0;
204 my $max_division;
205
206 while (my $ref = $q->fetchrow_hashref) {
207         my $d = $ref->{'divisjon'};
208         if ($d == $division) {
209                 print "        <option value=\"$d\" selected=\"selected\">$d</option>\n";
210                 $found_division = 1;
211         } else {
212                 print "        <option value=\"$d\">$d</option>\n";
213         }
214         $max_division = $d;
215 }
216
217 $division = 1 if (!$found_division);
218
219 print <<"EOF";
220         </select>
221         Avdeling:
222         <select name="avdeling" onchange="form.submit();">
223 EOF
224
225 $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
226 $q->execute($season, $division);
227
228 my $found_subdivision = 0;
229
230 while (my $ref = $q->fetchrow_hashref) {
231         my $sd = $ref->{'avdeling'};
232         if ($sd == $subdivision) {
233                 print "        <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
234                 $found_subdivision = 1;
235         } else {
236                 print "        <option value=\"$sd\">$sd</option>\n";
237         }
238 }
239
240 $subdivision = 1 if (!$found_subdivision);
241
242 print <<"EOF";
243         </select>
244         <input type="submit" value="Vis" />
245       </p>
246     </form>
247 EOF
248
249 # Get players and ratings
250 $q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr JOIN ratings ON fotballdeltagere.id=ratings.id AND sesong=? AND divisjon=? AND avdeling=?');
251 $q->execute($season, $division, $subdivision);
252
253 while (my $ref = $q->fetchrow_hashref) {
254         my $id = $ref->{'id'};
255         $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
256         $ratings{$id} = $ref->{'rating'};
257 }
258 $q->finish;
259
260 $q = $dbh->prepare('
261 SELECT
262   d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
263 FROM fotballresultater r
264   JOIN fotballserier s ON r.serie=s.nr
265   JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
266   JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
267 WHERE
268   sesong=? AND divisjon=? AND avdeling=?
269   AND lagrecno > motstander
270 ');
271 $q->execute($season, $division, $subdivision);
272
273 while (my $ref = $q->fetchrow_hashref) {
274         push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
275 }
276 $q->finish;
277
278 my $lowest_division = ($division == $max_division);
279 make_table($lowest_division, {});
280
281 print <<"EOF";
282     <p>Under er en variant som tar relativ spillestyrke med i beregningen;
283       se <a href="/rating">ratingsiden</a>.</p>
284 EOF
285
286 make_table($lowest_division, \%ratings);
287
288 print << "EOF";
289     </table>
290   </body>
291 </html>
292 EOF