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