8 my $dbh = DBI->connect("dbi:Pg:dbname=foosball;host=127.0.0.1", "foosball", "cleanrun", {AutoCommit => 0});
9 $dbh->{RaiseError} = 1;
13 sub find_single_rating {
14 my ($dbh, $username, $limit) = @_;
15 my ($age, $rating, $rd) = $dbh->selectrow_array('SELECT EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP-ratetime)), rating, rd FROM single_rating WHERE username=? '.$limit.' ORDER BY ratetime DESC LIMIT 1',
17 $rd = apply_aging($rd, $age / 86400.0);
19 if (!defined($rating)) {
24 return ($rating, $rd);
27 sub find_double_rating {
28 my ($dbh, $username, $limit) = @_;
29 my ($age, $rating, $rd) = $dbh->selectrow_array('SELECT EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP-ratetime)), rating, rd FROM double_rating WHERE username=? '.$limit.'ORDER BY ratetime DESC LIMIT 1',
31 $rd = apply_aging($rd, $age / 86400.0);
33 if (!defined($rating)) {
38 return ($rating, $rd);
42 my ($rating1, $rd1, $rating2, $rd2) = @_;
44 my $rating_team = 0.5 * ($rating1 + $rating2);
45 my $rd_team = sqrt($rd1 * $rd1 + $rd2 * $rd2) / sqrt(2.0);
46 return ($rating_team, $rd_team);
49 sub create_user_if_not_exists {
50 my ($dbh, $username) = @_;
51 my $count = $dbh->selectrow_array('SELECT count(*) FROM users WHERE username=?',
53 return if ($count > 0);
54 $dbh->do('INSERT INTO users (username) VALUES (?)',
56 $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
58 $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
63 # c=8 => RD=50 moves to RD=350 over approx. five years
68 $rd = sqrt($rd*$rd + $c * $c * ($age / 86400.0));
69 $rd = 350.0 if ($rd > 350.0);
74 my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_;
75 my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $score1 $score2`;
77 my ($newr1, $newrd1) = split / /, $result;
79 $newrd1 = 30.0 if ($newrd1 < 30.0);
81 return ($newr1, $newrd1);