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

my $cgi = CGI->new;

my $team1_username1 = $cgi->param('team1_username1');
$team1_username1 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 1/1";
$team1_username1 = $1;

my $team1_username2 = $cgi->param('team1_username2');
$team1_username2 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 1/2";
$team1_username2 = $1;

my $team2_username1 = $cgi->param('team2_username1');
$team2_username1 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 2/1";
$team2_username1 = $1;

my $team2_username2 = $cgi->param('team2_username2');
$team2_username2 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 2/2";
$team2_username2 = $1;

my $score1 = $cgi->param('score1');
$score1 =~ /^([0-9]|10)$/ or die "Invalid score 1";
$score1 = $1;

my $score2 = $cgi->param('score2');
$score2 =~ /^([0-9]|10)$/ or die "Invalid score 2";
$score2 = $1;

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

foosball::create_user_if_not_exists($dbh, $team1_username1);
foosball::create_user_if_not_exists($dbh, $team1_username2);
foosball::create_user_if_not_exists($dbh, $team2_username1);
foosball::create_user_if_not_exists($dbh, $team2_username2);
$dbh->commit;

# fetch the previous double ratings
my ($rating1_1, $rd1_1) = foosball::find_double_rating($dbh, $team1_username1);
my ($rating1_2, $rd1_2) = foosball::find_double_rating($dbh, $team1_username2);
my ($rating2_1, $rd2_1) = foosball::find_double_rating($dbh, $team2_username1);
my ($rating2_2, $rd2_2) = foosball::find_double_rating($dbh, $team2_username2);

# 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 $q = $foosball::q;

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);

$dbh->do('INSERT INTO double_results (gametime,team1_username1,team1_username2,team2_username1,team2_username2,score1,score2) VALUES (CURRENT_TIMESTAMP,?,?,?,?,?,?)',
	undef, $team1_username1, $team1_username2, $team2_username1, $team2_username2, $score1, $score2);
$dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd,rating_diff) VALUES (?,CURRENT_TIMESTAMP,?,?,?,?)',
	undef, $team1_username1, $newr1_1, $newrd1_1, $newr1_1-$rating1_1);
$dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd,rating_diff) VALUES (?,CURRENT_TIMESTAMP,?,?,?,?)',
	undef, $team1_username2, $newr1_2, $newrd1_2, $newr1_2-$rating1_2);
$dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd,rating_diff) VALUES (?,CURRENT_TIMESTAMP,?,?,?,?)',
	undef, $team2_username1, $newr2_1, $newrd2_1, $newr2_1-$rating2_1);
$dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd,rating_diff) VALUES (?,CURRENT_TIMESTAMP,?,?,?,?)',
	undef, $team2_username2, $newr2_2, $newrd2_2, $newr2_2-$rating2_2);

$dbh->commit;

print $cgi->header(-status=>'303 See Other',
		   -location=>'http://foosball.sesse.net/');

