]> git.sesse.net Git - foosball/blob - foosball.pm
fd46aae029677a34122ddd1f9033c522a6fbe48d
[foosball] / foosball.pm
1 use strict;
2 use warnings;
3 use DBI;
4
5 package foosball;
6
7 sub db_connect {
8         my $dbh = DBI->connect("dbi:Pg:dbname=foosball;host=127.0.0.1", "foosball", "cleanrun", {AutoCommit => 0});
9         $dbh->{RaiseError} = 1;
10         return $dbh;
11 }
12
13 sub find_single_rating {
14         my ($dbh, $username, $limit) = @_;
15         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',
16                 undef, $username);
17         $rd = apply_aging($rd, $age / 86400.0);
18         return ($rating, $rd);
19 }
20
21 sub find_double_rating {
22         my ($dbh, $username, $limit) = @_;
23         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',
24                 undef, $username);
25         $rd = apply_aging($rd, $age / 86400.0);
26         return ($rating, $rd);
27 }
28
29 sub create_user_if_not_exists {
30         my ($dbh, $username) = @_;
31         my $count = $dbh->selectrow_array('SELECT count(*) FROM users WHERE username=?',
32                 undef, $username);
33         return if ($count > 0);
34         $dbh->do('INSERT INTO users (username) VALUES (?)',
35                 undef, $username);
36         $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
37                 undef, $username);
38         $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
39                 undef, $username);
40         return $dbh;
41 }
42
43 # 10-9 is 0.60
44 # 10-0 is 1.00
45 sub find_score {
46         my ($score1, $score2) = @_;
47         if ($score1 == 10) {
48                 # yay, a win
49                 return 0.60 + 0.40 * (9.0-$score2)/9.0;
50         }
51         if ($score2 == 10) {
52                 # a loss
53                 return 0.40 - 0.40 * (9.0-$score1)/9.0;
54         }
55         die "Nobody won?";
56 }
57
58 # c=8 => RD=50 moves to RD=350 over approx. five years
59 our $c = 8;
60
61 sub apply_aging {
62         my ($rd, $age) = @_;
63         $rd = sqrt($rd*$rd + $c * $c * ($age / 86400.0));
64         $rd = 350.0 if ($rd > 350.0);
65         return $rd;
66 }
67
68 our $q = log(10)/400.0;
69 our $pi = 3.1415926535897932384626433832795;
70
71 # computes a^b
72 sub pow {
73         my ($a, $b) = @_;
74
75         # a^b = exp(log(a^b)) = exp(b log a)
76         return exp($b * log($a));
77 }
78
79 sub g {
80         my $rd = shift;
81         return 1.0 / sqrt(1.0 + 3.0 * $q * $q * $rd * $rd / ($pi*$pi));
82 }
83
84 sub E {
85         my ($r, $rating, $rd) = @_;
86         return 1.0 / (1.0 + pow(10.0, -g($rd) * ($r - $rating) / 400.0));
87 }
88
89 sub dsq {
90         my ($r, $rating, $rd) = @_;
91         return 1.0 / ($q*$q * g($rd) * g($rd) * E($r, $rating, $rd) * (1 - E($r, $rating, $rd)));
92 }
93
94 sub calc_rating {
95         my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_;
96         my $s1 = find_score($score1, $score2);
97         my $d1sq = dsq($rating1, $rating2, $rd2);
98         my $newr1 = $rating1 + ($q / (1.0/($rd1*$rd1) + 1.0 / $d1sq)) * g($rd2) * ($s1 - E($rating1, $rating2, $rd2));
99         my $newrd1 = sqrt(1.0 / (1.0 / ($rd1*$rd1) + 1.0 / $d1sq));
100
101         $newrd1 = 30.0 if ($newrd1 < 30.0);
102
103         return ($newr1, $newrd1);
104 }
105
106 1;