aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Database/SQLite/dbAddServers.pm
blob: 88b1bc807f701df8b230dc79e1509c1de11775e7 (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
package MasterServer::Database::SQLite::dbAddServers;

use strict;
use warnings;
use MasterServer::Core::Util 'sqlprint';
use Exporter 'import';
our @EXPORT = qw| insert_server
                  update_server 
                  insert_pending |;

################################################################################
## Insert minimalistic game data into serverlist
## params: ip, port, hostport
################################################################################
sub insert_server {
  my $self = shift;
  my %o = (@_);

  # if not found at all, add to pending
  return $self->{dbh}->do(
    "INSERT INTO serverlist (ip, port, hostport, country) VALUES (?, ?, ?, ?)", 
    undef, $o{ip}, $o{port}, $o{hostport}, $self->ip2country($o{ip}) );
}

################################################################################
## Update the server info in the serverlist
## required: id or ip + port/hostport
################################################################################
sub update_server {
  my $self = shift;
  my %o = (updated  => time, @_);

  # either id, ip+port or ip+hostport are provided.
  my %W = (
    $o{id}       ? (      'id = ?' => $o{id})       : (),
    $o{ip}       ? (      'ip = ?' => $o{ip})       : (),
    $o{port}     ? (    'port = ?' => $o{port})     : (),
    $o{hostport} ? ('hostport = ?' => $o{hostport}) : (),
  );

  # update where possible
  my %H = (
    $o{gamename} ? ('gamename = ?' => lc $o{gamename}) : (),
    $o{gamever}  ? ( 'gamever = ?' => $o{gamever})     : (),
    $o{hostname} ? ('hostname = ?' => $o{hostname})    : (),
    $o{hostport} ? ('hostport = ?' => $o{hostport})    : (),
    $o{direct}   ? (  'b333ms = CAST(? AS BOOLEAN)' => $o{direct})  : (),
    $o{direct}   ? (  'beacon = datetime(?, \'unixepoch\')'    => $o{updated}) : (),
    $o{updated}  ? ( 'updated = datetime(?, \'unixepoch\')'    => $o{updated}) : (),
  );

  my($q, @p) = sqlprint("UPDATE serverlist !H !W", \%H, \%W);
  return $self->{dbh}->do($q, undef, @p);
}

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

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

  # determine if it already exsits
  my($q, @p) = sqlprint("SELECT id FROM serverlist !W", \%W);
  my $u = $self->{dbh}->do($q, undef, @p);
  return 0 if int($u);

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

1;