aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterWebInterface/Database/SQLite/Servers.pm
blob: 041c4d9100ccd8e0fea475e53bb8b6e162eb0dc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package MasterWebInterface::Database::SQLite::Servers;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT = qw| dbAddServer dbServerListGet |;

################################################################################
## check if an ip, port/hostport combination is recent in the serverlist.
## if not, add the address to the pending list
################################################################################
sub dbAddServer {
  my $self = shift;
  my %o = (updated => 3600, @_ );

  my %where = (
    $o{ip}       ? (      'ip = ?' => $o{ip})       : (),
    $o{port}     ? (    'port = ?' => $o{port})     : (),
    $o{updated}  ? ('updated > datetime(?, \'unixepoch\')' => (time-$o{updated})) : (),
  );

  # determine if it already exsits
  my $u = $self->dbAll("SELECT id FROM serverlist !W", \%where)->[0];
  return 0 if $u;

  # else, insert in pending (duplicates may exist -- see remove_pending)
  $self->dbExec("INSERT INTO pending (ip, heartbeat) VALUES (?, ?)", $o{ip}, $o{port});
  return 1;
}

################################################################################
## get the serverlist. default 2 hours time limit
################################################################################
sub dbServerListGet {
  my $s = shift;
  my %o = ( page => 1, results => 50, sort => '', updated => '7200', @_ );
  
  my %where = (
    defined $o{gamename} && $o{gamename} !~ /all/ 
      ? ('serverlist.gamename = ?' => $o{gamename}) : (),
    $o{firstchar} 
      ? ('upper(SUBSTR(hostname, 1, 1)) = ?' => $o{firstchar} ) : (),
    $o{search} 
      ? ('lower(hostname) LIKE lower(?)' => "%$o{search}%") : (),
    $o{updated}  
      ? ('serverlist.updated > datetime(?, \'unixepoch\')' => (time-$o{updated})) : (),
#    ('length(hostname) > ?' => 1),  # don't show empty hostnames
    ('hostport > ?' => 0),          # or games with empty hostport
  );
  
  my @select = ( qw| id ip hostport hostname serverlist.gamename country numplayers maxplayers maptitle mapname gametype added description |,
    "strftime('\%s', CURRENT_TIMESTAMP) - strftime('\%s', serverlist.updated) as diff",
    "strftime('\%s', serverlist.updated) as updated",
    "strftime('\%s', serverlist.added)   as added");

  my $order = sprintf {
    id          => 'id %s',
    ip          => 'ip %s',
    hostport    => 'hostport %s',
    hostname    => 'hostname %s',
    gamename    => 'serverlist.gamename %s',
    country     => 'country %s',
    diff        => 'diff %s',
    added       => 'serverlist.added %s',
    updated     => 'updated %s',
    gametype    => 'gametype %s',
    numplayers  => 'numplayers %s',
    maxplayers  => 'maxplayers %s',
    mapname     => 'mapname %s',
    description => 'description %s',
  }->{ $o{sort}||'hostname' }, $o{reverse} ? 'DESC' : 'ASC';

  my($r, $np) = $s->dbPage(\%o, q|
    SELECT !s FROM serverlist
      JOIN games ON serverlist.gamename = games.gamename
      JOIN extended_info ON serverlist.id = extended_info.server_id
      !W
      ORDER BY !s|,
    join(', ', @select), \%where, $order
  );

  my $p = $s->dbAll( q|
    SELECT COUNT(*) AS num
    FROM serverlist
    !W|, \%where,
  )->[0]{num};
  return wantarray ? ($r, $np, $p) : $r;
}

1;