X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=foosball.pm;h=38722422caea742ec265373305a913c3e342b451;hb=HEAD;hp=dc5ff223d4c70b82493dbd2a067b3f62d8b930e7;hpb=6b71fbcb1949c150dcc8f50bd9f138f56454964c;p=foosball diff --git a/foosball.pm b/foosball.pm index dc5ff22..3872242 100644 --- a/foosball.pm +++ b/foosball.pm @@ -1,24 +1,36 @@ use strict; use warnings; use DBI; +use POSIX; package foosball; +our $initial_rating = 1500.0; +our $initial_rd = 400.0; +our $c = 22.5; + sub db_connect { my $dbh = DBI->connect("dbi:Pg:dbname=foosball;host=127.0.0.1", "foosball", "cleanrun", {AutoCommit => 0}); $dbh->{RaiseError} = 1; return $dbh; } +sub round { + my $x = shift; + return -round(-$x) if ($x < 0.0); + return POSIX::floor($x + 0.5); +} + sub find_single_rating { my ($dbh, $username, $limit) = @_; + $limit = "" if (!defined($limit)); 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', undef, $username); $rd = apply_aging($rd, $age / 86400.0); if (!defined($rating)) { - $rating = 1500; - $rd = 350; + $rating = $initial_rating; + $rd = $initial_rd; } return ($rating, $rd); @@ -26,26 +38,19 @@ sub find_single_rating { sub find_double_rating { my ($dbh, $username, $limit) = @_; + $limit = "" if (!defined($limit)); 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', undef, $username); $rd = apply_aging($rd, $age / 86400.0); if (!defined($rating)) { - $rating = 1500; - $rd = 350; + $rating = $initial_rating; + $rd = $initial_rd; } return ($rating, $rd); } -sub combine_ratings { - my ($rating1, $rd1, $rating2, $rd2) = @_; - - my $rating_team = 0.5 * ($rating1 + $rating2); - my $rd_team = sqrt($rd1 * $rd1 + $rd2 * $rd2) / sqrt(2.0); - return ($rating_team, $rd_team); -} - sub create_user_if_not_exists { my ($dbh, $username) = @_; my $count = $dbh->selectrow_array('SELECT count(*) FROM users WHERE username=?', @@ -53,20 +58,18 @@ sub create_user_if_not_exists { return if ($count > 0); $dbh->do('INSERT INTO users (username) VALUES (?)', undef, $username); - $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)', - undef, $username); - $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)', - undef, $username); + $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)', + undef, $username, $initial_rating, $initial_rd); + $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)', + undef, $username, $initial_rating, $initial_rd); return $dbh; } -# c=8 => RD=50 moves to RD=350 over approx. five years -our $c = 8; - +# $age is in days sub apply_aging { my ($rd, $age) = @_; - $rd = sqrt($rd*$rd + $c * $c * ($age / 86400.0)); - $rd = 350.0 if ($rd > 350.0); + $rd = sqrt($rd*$rd + $c * $c * $age); + $rd = $initial_rd if ($rd > $initial_rd); return $rd; } @@ -74,6 +77,17 @@ sub calc_rating { my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_; my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $score1 $score2`; chomp $result; + my ($newr1, $newrd1, $likelihood) = split / /, $result; + + $newrd1 = 30.0 if ($newrd1 < 30.0); + + return ($newr1, $newrd1, $likelihood); +} + +sub calc_rating_double { + my ($rating1, $rd1, $rating2, $rd2, $rating3, $rd3, $rating4, $rd4, $score1, $score2) = @_; + my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $rating3 $rd3 $rating4 $rd4 $score1 $score2`; + chomp $result; my ($newr1, $newrd1) = split / /, $result; $newrd1 = 30.0 if ($newrd1 < 30.0); @@ -81,4 +95,5 @@ sub calc_rating { return ($newr1, $newrd1); } + 1;