From e05962752502c0c72eb311af9622bbc5c9261738 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 9 Dec 2007 14:33:30 +0100 Subject: [PATCH] Parametrize initial rating/RD instead of hardcoding it. --- foosball.pm | 21 ++++++++++++--------- recalc-double-result.pl | 2 +- recalc-single-result.pl | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/foosball.pm b/foosball.pm index b40b9fd..b5750b2 100644 --- a/foosball.pm +++ b/foosball.pm @@ -5,6 +5,9 @@ use POSIX; package foosball; +our $initial_rating = 1500.0; +our $initial_rd = 350.0; + 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 +28,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 +43,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,10 +57,10 @@ 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; } @@ -67,7 +70,7 @@ our $c = 8; sub apply_aging { my ($rd, $age) = @_; $rd = sqrt($rd*$rd + $c * $c * ($age / 86400.0)); - $rd = 350.0 if ($rd > 350.0); + $rd = $initial_rd if ($rd > $initial_rd); return $rd; } diff --git a/recalc-double-result.pl b/recalc-double-result.pl index b606b5a..4e20c15 100755 --- a/recalc-double-result.pl +++ b/recalc-double-result.pl @@ -19,7 +19,7 @@ while (my $ref = $q->fetchrow_hashref) { for my $user (($ref->{'team1_username1'}, $ref->{'team1_username2'}, $ref->{'team2_username1'}, $ref->{'team2_username2'})) { if (!exists($ratings{$user})) { - $ratings{$user} = [ 1500, 350, 0 ]; + $ratings{$user} = [ $foosball::initial_rating, $foosball::initial_rd, 0 ]; } } diff --git a/recalc-single-result.pl b/recalc-single-result.pl index 6794787..8c6923b 100755 --- a/recalc-single-result.pl +++ b/recalc-single-result.pl @@ -21,7 +21,7 @@ my $cll = 0.0; while (my $ref = $q->fetchrow_hashref) { for my $user (($ref->{'username1'}, $ref->{'username2'})) { if (!exists($ratings{$user})) { - $ratings{$user} = [ 1500, 350, 0 ]; + $ratings{$user} = [ $foosball::initial_rating, $foosball::initial_rd, 0 ]; } } -- 2.39.2