]> git.sesse.net Git - foosball/blob - recalc-single-result.pl
Don't show ratings without any information.
[foosball] / recalc-single-result.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 use DBI;
5 use CGI;
6 use CGI::Carp qw(fatalsToBrowser);
7 require 'foosball.pm';
8
9 my $dbh = foosball::db_connect();
10 $dbh->{AutoCommit} = 0;
11
12 $dbh->do('delete from single_rating');
13
14 my %ratings = ();
15 my $q = $dbh->prepare('select * from single_results order by gametime');
16 $q->execute;
17
18 while (my $ref = $q->fetchrow_hashref) {
19         for my $user (($ref->{'username1'}, $ref->{'username2'})) {
20                 if (!exists($ratings{$user})) {
21                         $ratings{$user} = [ 1500, 350 ];
22                 }
23         }
24         
25         my $rating1 = $ratings{$ref->{'username1'}}->[0];
26         my $rd1 = $ratings{$ref->{'username1'}}->[1];
27         my $rating2 = $ratings{$ref->{'username2'}}->[0];
28         my $rd2 = $ratings{$ref->{'username2'}}->[1];
29         my $score1 = $ref->{'score1'};
30         my $score2 = $ref->{'score2'};
31
32         my ($newr1, $newrd1) = foosball::calc_rating($rating1, $rd1, $rating2, $rd2, $score1, $score2);
33         my ($newr2, $newrd2) = foosball::calc_rating($rating2, $rd2, $rating1, $rd1, $score2, $score1);
34
35         printf("%-10s - %-10s: %u - %u, new ratings %u/%u %u/%u\n",
36                 $ref->{'username1'}, $ref->{'username2'}, $ref->{'score1'},
37                 $ref->{'score2'}, $newr1, $newrd1, $newr2, $newrd2);
38         $dbh->do('insert into single_rating values (?,?,?,?)', undef,
39                 $ref->{'username1'}, $ref->{'gametime'}, $newr1, $newrd1);
40         $dbh->do('insert into single_rating values (?,?,?,?)', undef,
41                 $ref->{'username2'}, $ref->{'gametime'}, $newr2, $newrd2);
42
43         $ratings{$ref->{'username1'}} = [ $newr1, $newrd1 ];
44         $ratings{$ref->{'username2'}} = [ $newr2, $newrd2 ];
45 }
46
47 $dbh->commit;
48 $dbh->disconnect;