]> git.sesse.net Git - foosball/blobdiff - foosball.pm
Make the initial rating/RD constants instead of hard-coded values.
[foosball] / foosball.pm
index 4fd318c85b4f2c4d625fef094c9fe6f843aac81a..b40b9fda17b82ac92e7c97586ac65bb7ebc99d5b 100644 (file)
@@ -1,6 +1,7 @@
 use strict;
 use warnings;
 use DBI;
+use POSIX;
 
 package foosball;
 
@@ -10,19 +11,39 @@ sub db_connect {
        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;
+       }
+
        return ($rating, $rd);
 }
 
 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;
+       }
+
        return ($rating, $rd);
 }
 
@@ -40,21 +61,6 @@ sub create_user_if_not_exists {
        return $dbh;
 }
 
-# 10-9 is 0.60
-# 10-0 is 1.00
-sub find_score {
-       my ($score1, $score2) = @_;
-       if ($score1 == 10) {
-               # yay, a win
-               return 0.60 + 0.40 * (9.0-$score2)/9.0;
-       }
-       if ($score2 == 10) {
-               # a loss
-               return 0.40 - 0.40 * (9.0-$score1)/9.0;
-       }
-       die "Nobody won?";
-}
-
 # c=8 => RD=50 moves to RD=350 over approx. five years
 our $c = 8;
 
@@ -67,7 +73,18 @@ sub apply_aging {
 
 sub calc_rating {
        my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_;
-       my $result = `/srv/foosball.sesse.net/foorank $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;
 
@@ -76,4 +93,5 @@ sub calc_rating {
        return ($newr1, $newrd1);
 }
 
+
 1;