]> git.sesse.net Git - foosball/blob - www/add-double-result.pl
Don't show ratings without any information.
[foosball] / www / add-double-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 $cgi = CGI->new;
10
11 my $team1_username1 = $cgi->param('team1_username1');
12 $team1_username1 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 1/1";
13 $team1_username1 = $1;
14
15 my $team1_username2 = $cgi->param('team1_username2');
16 $team1_username2 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 1/2";
17 $team1_username2 = $1;
18
19 my $team2_username1 = $cgi->param('team2_username1');
20 $team2_username1 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 2/1";
21 $team2_username1 = $1;
22
23 my $team2_username2 = $cgi->param('team2_username2');
24 $team2_username2 =~ /^([a-z][a-z0-9]*)$/ or die "Invalid user name 2/2";
25 $team2_username2 = $1;
26
27 my $score1 = $cgi->param('score1');
28 $score1 =~ /^([0-9]|10)$/ or die "Invalid score 1";
29 $score1 = $1;
30
31 my $score2 = $cgi->param('score2');
32 $score2 =~ /^([0-9]|10)$/ or die "Invalid score 2";
33 $score2 = $1;
34
35 my $dbh = foosball::db_connect();
36 $dbh->{AutoCommit} = 0;
37
38 foosball::create_user_if_not_exists($dbh, $team1_username1);
39 foosball::create_user_if_not_exists($dbh, $team1_username2);
40 foosball::create_user_if_not_exists($dbh, $team2_username1);
41 foosball::create_user_if_not_exists($dbh, $team2_username2);
42 $dbh->commit;
43
44 # fetch the previous double ratings
45 my ($rating1_1, $rd1_1) = foosball::find_double_rating($dbh, $team1_username1);
46 my ($rating1_2, $rd1_2) = foosball::find_double_rating($dbh, $team1_username2);
47 my ($rating2_1, $rd2_1) = foosball::find_double_rating($dbh, $team2_username1);
48 my ($rating2_2, $rd2_2) = foosball::find_double_rating($dbh, $team2_username2);
49
50 # make virtual "team players"
51 my $rating_team1 = 0.5 * ($rating1_1 + $rating1_2);
52 my $rd_team1 = sqrt($rd1_1 * $rd1_1 + $rd1_2 * $rd1_2) / sqrt(2.0);
53 my $rating_team2 = 0.5 * ($rating1_2 + $rating2_2);
54 my $rd_team2 = sqrt($rd2_1 * $rd2_1 + $rd2_2 * $rd2_2) / sqrt(2.0);
55
56 my $q = $foosball::q;
57
58 my ($new_t1r, undef) =  foosball::calc_rating($rating_team1, $rd_team1, $rating_team2, $rd_team2, $score1, $score2);
59 my ($new_t2r, undef) =  foosball::calc_rating($rating_team2, $rd_team2, $rating_team1, $rd_team1, $score2, $score1);
60 my $newr1_1 = $rating1_1 + ($new_t1r - $rating_team1);
61 my $newr1_2 = $rating1_2 + ($new_t1r - $rating_team1);
62 my $newr2_1 = $rating2_1 + ($new_t2r - $rating_team2);
63 my $newr2_2 = $rating2_2 + ($new_t2r - $rating_team2);
64
65 my (undef, $newrd1_1) = foosball::calc_rating($rating1_1, $rd1_1, $rating_team2, $rd_team2, $score1, $score2);
66 my (undef, $newrd1_2) = foosball::calc_rating($rating1_2, $rd1_2, $rating_team2, $rd_team2, $score1, $score2);
67 my (undef, $newrd2_1) = foosball::calc_rating($rating2_1, $rd2_1, $rating_team1, $rd_team1, $score2, $score1);
68 my (undef, $newrd2_2) = foosball::calc_rating($rating2_2, $rd2_2, $rating_team1, $rd_team1, $score2, $score1);
69
70 $dbh->do('INSERT INTO double_results (gametime,team1_username1,team1_username2,team2_username1,team2_username2,score1,score2) VALUES (CURRENT_TIMESTAMP,?,?,?,?,?,?)',
71         undef, $team1_username1, $team1_username2, $team2_username1, $team2_username2, $score1, $score2);
72 $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
73         undef, $team1_username1, $newr1_1, $newrd1_1);
74 $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
75         undef, $team1_username2, $newr1_2, $newrd1_2);
76 $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
77         undef, $team2_username1, $newr2_1, $newrd2_1);
78 $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
79         undef, $team2_username2, $newr2_2, $newrd2_2);
80
81 $dbh->commit;
82
83 print $cgi->header(-status=>'303 See Other',
84                    -location=>'http://foosball.sesse.net/');
85