]> git.sesse.net Git - foosball/blob - foosball.pm
Adjusted initial parameters for maximum prediction power; in particular,
[foosball] / foosball.pm
1 use strict;
2 use warnings;
3 use DBI;
4 use POSIX;
5
6 package foosball;
7
8 our $initial_rating = 1500.0;
9 our $initial_rd = 400.0;
10 our $c = 22.5;
11
12 sub db_connect {
13         my $dbh = DBI->connect("dbi:Pg:dbname=foosball;host=127.0.0.1", "foosball", "cleanrun", {AutoCommit => 0});
14         $dbh->{RaiseError} = 1;
15         return $dbh;
16 }
17
18 sub round {
19         my $x = shift;
20         return -round(-$x) if ($x < 0.0);
21         return POSIX::floor($x + 0.5);
22 }
23
24 sub find_single_rating {
25         my ($dbh, $username, $limit) = @_;
26         $limit = "" if (!defined($limit));
27         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',
28                 undef, $username);
29         $rd = apply_aging($rd, $age / 86400.0);
30
31         if (!defined($rating)) {
32                 $rating = $initial_rating;
33                 $rd = $initial_rd;
34         }
35
36         return ($rating, $rd);
37 }
38
39 sub find_double_rating {
40         my ($dbh, $username, $limit) = @_;
41         $limit = "" if (!defined($limit));
42         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',
43                 undef, $username);
44         $rd = apply_aging($rd, $age / 86400.0);
45         
46         if (!defined($rating)) {
47                 $rating = $initial_rating;
48                 $rd = $initial_rd;
49         }
50
51         return ($rating, $rd);
52 }
53
54 sub create_user_if_not_exists {
55         my ($dbh, $username) = @_;
56         my $count = $dbh->selectrow_array('SELECT count(*) FROM users WHERE username=?',
57                 undef, $username);
58         return if ($count > 0);
59         $dbh->do('INSERT INTO users (username) VALUES (?)',
60                 undef, $username);
61         $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
62                 undef, $username, $initial_rating, $initial_rd);
63         $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
64                 undef, $username, $initial_rating, $initial_rd);
65         return $dbh;
66 }
67
68 # $age is in days
69 sub apply_aging {
70         my ($rd, $age) = @_;
71         $rd = sqrt($rd*$rd + $c * $c * $age);
72         $rd = $initial_rd if ($rd > $initial_rd);
73         return $rd;
74 }
75
76 sub calc_rating {
77         my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_;
78         my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $score1 $score2`;
79         chomp $result;
80         my ($newr1, $newrd1, $likelihood) = split / /, $result;
81
82         $newrd1 = 30.0 if ($newrd1 < 30.0);
83
84         return ($newr1, $newrd1, $likelihood);
85 }
86
87 sub calc_rating_double {
88         my ($rating1, $rd1, $rating2, $rd2, $rating3, $rd3, $rating4, $rd4, $score1, $score2) = @_;
89         my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $rating3 $rd3 $rating4 $rd4 $score1 $score2`;
90         chomp $result;
91         my ($newr1, $newrd1) = split / /, $result;
92
93         $newrd1 = 30.0 if ($newrd1 < 30.0);
94
95         return ($newr1, $newrd1);
96 }
97
98
99 1;