]> git.sesse.net Git - foosball/blob - foosball.pm
Consistency is king.
[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
19         if (!defined($rating)) {
20                 $rating = 1500;
21                 $rd = 350;
22         }
23
24         return ($rating, $rd);
25 }
26
27 sub find_double_rating {
28         my ($dbh, $username, $limit) = @_;
29         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',
30                 undef, $username);
31         $rd = apply_aging($rd, $age / 86400.0);
32         
33         if (!defined($rating)) {
34                 $rating = 1500;
35                 $rd = 350;
36         }
37
38         return ($rating, $rd);
39 }
40
41 sub combine_ratings {
42         my ($rating1, $rd1, $rating2, $rd2) = @_;
43
44         my $rating_team = 0.5 * ($rating1 + $rating2);
45         my $rd_team = sqrt($rd1 * $rd1 + $rd2 * $rd2) / sqrt(2.0);
46         return ($rating_team, $rd_team);
47 }
48
49 sub create_user_if_not_exists {
50         my ($dbh, $username) = @_;
51         my $count = $dbh->selectrow_array('SELECT count(*) FROM users WHERE username=?',
52                 undef, $username);
53         return if ($count > 0);
54         $dbh->do('INSERT INTO users (username) VALUES (?)',
55                 undef, $username);
56         $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
57                 undef, $username);
58         $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
59                 undef, $username);
60         return $dbh;
61 }
62
63 # c=8 => RD=50 moves to RD=350 over approx. five years
64 our $c = 8;
65
66 sub apply_aging {
67         my ($rd, $age) = @_;
68         $rd = sqrt($rd*$rd + $c * $c * ($age / 86400.0));
69         $rd = 350.0 if ($rd > 350.0);
70         return $rd;
71 }
72
73 sub calc_rating {
74         my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_;
75         my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $score1 $score2`;
76         chomp $result;
77         my ($newr1, $newrd1) = split / /, $result;
78
79         $newrd1 = 30.0 if ($newrd1 < 30.0);
80
81         return ($newr1, $newrd1);
82 }
83
84 sub calc_rating_double {
85         my ($rating1, $rd1, $rating2, $rd2, $rating3, $rd3, $rating4, $rd4, $score1, $score2) = @_;
86         my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $rating3 $rd3 $rating4 $rd4 $score1 $score2`;
87         chomp $result;
88         my ($newr1, $newrd1) = split / /, $result;
89
90         $newrd1 = 30.0 if ($newrd1 < 30.0);
91
92         return ($newr1, $newrd1);
93 }
94
95
96 1;