6 use CGI::Carp qw(fatalsToBrowser);
9 my $dbh = foosball::db_connect();
10 $dbh->{AutoCommit} = 0;
12 $dbh->do('delete from single_rating');
15 my $q = $dbh->prepare('select *,extract(epoch from gametime) as eptime from single_results order by gametime');
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 ];
25 my $rating1 = $ratings{$ref->{'username1'}}->[0];
26 my $rd1 = $ratings{$ref->{'username1'}}->[1];
27 my $age1 = $ref->{'eptime'} - $ratings{$ref->{'username1'}}->[2];
29 my $rating2 = $ratings{$ref->{'username2'}}->[0];
30 my $rd2 = $ratings{$ref->{'username2'}}->[1];
31 my $age2 = $ref->{'eptime'} - $ratings{$ref->{'username2'}}->[2];
33 my $score1 = $ref->{'score1'};
34 my $score2 = $ref->{'score2'};
36 $rd1 = foosball::apply_aging($rd1, $age1 / 86400.0);
37 $rd2 = foosball::apply_aging($rd2, $age2 / 86400.0);
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);
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);
47 $dbh->do('insert into single_rating values (?,?,?,?)', undef,
48 $ref->{'username2'}, $ref->{'gametime'}, $newr2, $newrd2);
50 $ratings{$ref->{'username1'}} = [ $newr1, $newrd1, $ref->{'eptime'} ];
51 $ratings{$ref->{'username2'}} = [ $newr2, $newrd2, $ref->{'eptime'} ];