]> git.sesse.net Git - foosball/blob - recalc-single-result.pl
c67be84b36e84a68e3fba5bd596e57b2e4b460a1
[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 *,extract(epoch from gametime) as eptime 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, 0 ];
22                 }
23         }
24         
25         my $rating1 = $ratings{$ref->{'username1'}}->[0];
26         my $rd1 = $ratings{$ref->{'username1'}}->[1];
27         my $age1 = $ref->{'eptime'} - $ratings{$ref->{'username1'}}->[2];
28
29         my $rating2 = $ratings{$ref->{'username2'}}->[0];
30         my $rd2 = $ratings{$ref->{'username2'}}->[1];
31         my $age2 = $ref->{'eptime'} - $ratings{$ref->{'username2'}}->[2];
32
33         my $score1 = $ref->{'score1'};
34         my $score2 = $ref->{'score2'};
35
36         $rd1 = foosball::apply_aging($rd1, $age1 / 86400.0);
37         $rd2 = foosball::apply_aging($rd2, $age2 / 86400.0);
38
39         my ($newr1, $newrd1) = foosball::calc_rating($rating1, $rd1, $rating2, $rd2, $score1, $score2);
40         my ($newr2, $newrd2) = foosball::calc_rating($rating2, $rd2, $rating1, $rd1, $score2, $score1);
41
42         printf("%-10s - %-10s: %u - %u, new ratings %u/%u %u/%u\n",
43                 $ref->{'username1'}, $ref->{'username2'}, $ref->{'score1'},
44                 $ref->{'score2'}, $newr1, $newrd1, $newr2, $newrd2);
45         $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
46                 $ref->{'username1'}, $ref->{'gametime'}, $newr1, $newrd1, $newr1-$rating1);
47         $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
48                 $ref->{'username2'}, $ref->{'gametime'}, $newr2, $newrd2, $newr2-$rating2);
49
50         $ratings{$ref->{'username1'}} = [ $newr1, $newrd1, $ref->{'eptime'} ];
51         $ratings{$ref->{'username2'}} = [ $newr2, $newrd2, $ref->{'eptime'} ];
52 }
53
54 $dbh->commit;
55 $dbh->disconnect;