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