#! /usr/bin/perl
use strict;
use warnings;
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
require 'foosball.pm';

my $dbh = foosball::db_connect();
$dbh->{AutoCommit} = 0;

$dbh->do('delete from double_rating');

my %ratings = ();
my $q = $dbh->prepare('select *,extract(epoch from gametime) as eptime from double_results order by gametime');
$q->execute;

while (my $ref = $q->fetchrow_hashref) {
	for my $user (($ref->{'team1_username1'}, $ref->{'team1_username2'},
	               $ref->{'team2_username1'}, $ref->{'team2_username2'})) {
		if (!exists($ratings{$user})) {
			$ratings{$user} = [ 1500, 350, 0 ];
		}
	}
	
	my $rating1_1 = $ratings{$ref->{'team1_username1'}}->[0];
	my $rd1_1 = $ratings{$ref->{'team1_username1'}}->[1];
	my $age1_1 = $ref->{'eptime'} - $ratings{$ref->{'team1_username1'}}->[2];

	my $rating1_2 = $ratings{$ref->{'team1_username2'}}->[0];
	my $rd1_2 = $ratings{$ref->{'team1_username2'}}->[1];
	my $age1_2 = $ref->{'eptime'} - $ratings{$ref->{'team1_username2'}}->[2];

	my $rating2_1 = $ratings{$ref->{'team2_username1'}}->[0];
	my $rd2_1 = $ratings{$ref->{'team2_username1'}}->[1];
	my $age2_1 = $ref->{'eptime'} - $ratings{$ref->{'team2_username1'}}->[2];

	my $rating2_2 = $ratings{$ref->{'team2_username2'}}->[0];
	my $rd2_2 = $ratings{$ref->{'team2_username2'}}->[1];
	my $age2_2 = $ref->{'eptime'} - $ratings{$ref->{'team2_username2'}}->[2];
	
	$rd1_1 = foosball::apply_aging($rd1_1, $age1_1 / 86400.0);
	$rd1_2 = foosball::apply_aging($rd1_2, $age1_2 / 86400.0);
	$rd2_1 = foosball::apply_aging($rd2_1, $age2_1 / 86400.0);
	$rd2_2 = foosball::apply_aging($rd2_2, $age2_2 / 86400.0);

	my $score1 = $ref->{'score1'};
	my $score2 = $ref->{'score2'};

	# make virtual "team players"
	my ($rating_team1, $rd_team1) = foosball::combine_ratings($rating1_1, $rd1_1, $rating1_2, $rd1_2);
	my ($rating_team2, $rd_team2) = foosball::combine_ratings($rating2_1, $rd2_1, $rating2_2, $rd2_2);

	my ($new_t1r, undef) =  foosball::calc_rating($rating_team1, $rd_team1, $rating_team2, $rd_team2, $score1, $score2);
	my ($new_t2r, undef) =  foosball::calc_rating($rating_team2, $rd_team2, $rating_team1, $rd_team1, $score2, $score1);
	my $newr1_1 = $rating1_1 + ($new_t1r - $rating_team1);
	my $newr1_2 = $rating1_2 + ($new_t1r - $rating_team1);
	my $newr2_1 = $rating2_1 + ($new_t2r - $rating_team2);
	my $newr2_2 = $rating2_2 + ($new_t2r - $rating_team2);

	my (undef, $newrd1_1) = foosball::calc_rating($rating1_1, $rd1_1, $rating_team2, $rd_team2, $score1, $score2);
	my (undef, $newrd1_2) = foosball::calc_rating($rating1_2, $rd1_2, $rating_team2, $rd_team2, $score1, $score2);
	my (undef, $newrd2_1) = foosball::calc_rating($rating2_1, $rd2_1, $rating_team1, $rd_team1, $score2, $score1);
	my (undef, $newrd2_2) = foosball::calc_rating($rating2_2, $rd2_2, $rating_team1, $rd_team1, $score2, $score1);

	printf("%-10s/%-10s - %-10s/%-10s: %u - %u, new ratings %u/%u %u/%u   %u/%u %u/%u\n",
		$ref->{'team1_username1'}, $ref->{'team1_username2'},
		$ref->{'team2_username1'}, $ref->{'team2_username2'},
		$ref->{'score1'}, $ref->{'score2'},
		$newr1_1, $newrd1_1, $newr1_2, $newrd1_2,
		$newr2_1, $newrd2_1, $newr2_2, $newrd2_2);
	$dbh->do('insert into double_rating values (?,?,?,?)', undef,
		$ref->{'team1_username1'}, $ref->{'gametime'}, $newr1_1, $newrd1_1);
	$dbh->do('insert into double_rating values (?,?,?,?)', undef,
		$ref->{'team1_username2'}, $ref->{'gametime'}, $newr1_2, $newrd1_2);
	$dbh->do('insert into double_rating values (?,?,?,?)', undef,
		$ref->{'team2_username1'}, $ref->{'gametime'}, $newr2_1, $newrd2_1);
	$dbh->do('insert into double_rating values (?,?,?,?)', undef,
		$ref->{'team2_username2'}, $ref->{'gametime'}, $newr2_2, $newrd2_2);

	$ratings{$ref->{'team1_username1'}} = [ $newr1_1, $newrd1_1, $ref->{'eptime'} ];
	$ratings{$ref->{'team1_username2'}} = [ $newr1_2, $newrd1_2, $ref->{'eptime'} ];
	$ratings{$ref->{'team2_username1'}} = [ $newr2_1, $newrd2_1, $ref->{'eptime'} ];
	$ratings{$ref->{'team2_username2'}} = [ $newr2_2, $newrd2_2, $ref->{'eptime'} ];
}

$dbh->commit;
$dbh->disconnect;
