]> git.sesse.net Git - foosball/blobdiff - foosball.pm
Adjusted initial parameters for maximum prediction power; in particular,
[foosball] / foosball.pm
index 58e468c6669ea3ac0c5dfbfbfcc1fd9a39578885..38722422caea742ec265373305a913c3e342b451 100644 (file)
@@ -5,6 +5,10 @@ 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;
@@ -25,8 +29,8 @@ sub find_single_rating {
        $rd = apply_aging($rd, $age / 86400.0);
 
        if (!defined($rating)) {
-               $rating = 1500;
-               $rd = 350;
+               $rating = $initial_rating;
+               $rd = $initial_rd;
        }
 
        return ($rating, $rd);
@@ -40,8 +44,8 @@ sub find_double_rating {
        $rd = apply_aging($rd, $age / 86400.0);
        
        if (!defined($rating)) {
-               $rating = 1500;
-               $rd = 350;
+               $rating = $initial_rating;
+               $rd = $initial_rd;
        }
 
        return ($rating, $rd);
@@ -54,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;
 }
 
@@ -75,11 +77,11 @@ 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) = split / /, $result;
+       my ($newr1, $newrd1, $likelihood) = split / /, $result;
 
        $newrd1 = 30.0 if ($newrd1 < 30.0);
 
-       return ($newr1, $newrd1);
+       return ($newr1, $newrd1, $likelihood);
 }
 
 sub calc_rating_double {