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