aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Database/SQLite/dbAppletActions.pm
blob: 1d9c9751ee68985364ca0bedd459a06233f3e556 (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
90
91
92
93
94
95
package MasterServer::Database::SQLite::dbAppletActions;

use strict;
use warnings;
use Exporter 'import';
our @EXPORT = qw|  add_master_applet 
                update_master_applet 
                 reset_master_applets 
             get_masterserver_applets 
          remove_unresponsive_applets |;

################################################################################
## Add a remote master server applet 
################################################################################
sub add_master_applet {
  my $self = shift;
  my %o = @_;

  my $u = $self->{dbh}->do(
    "SELECT * FROM appletlist 
     WHERE     ip = ? 
      AND hostport = ?
      AND gamename = ?",
    undef, $o{ip}, $o{hostport}, lc $o{gamename});
  
  # return if found
  return if ($u > 0);

  # insert applet data 
  return $self->{dbh}->do(
  "INSERT INTO appletlist (ip, hostport, gamename) 
   SELECT ?, ?, ?", 
  undef, $o{ip}, $o{hostport}, lc $o{gamename});
}

################################################################################
## reset added/updated time after restart
################################################################################
sub reset_master_applets {
  my $self = shift;
  return $self->{dbh}->do(
   "UPDATE appletlist 
     SET added = datetime(?, \'unixepoch\'),
       updated = datetime(?, \'unixepoch\')",
   undef, time, time);
}

################################################################################
## update time on master applet
################################################################################
sub update_master_applet {
  my ($self, %o) = @_;

  return $self->{dbh}->do(
    "UPDATE appletlist 
     SET updated = datetime(?, \'unixepoch\') 
     WHERE       ip = ?
      AND hostport = ?
      AND gamename = ?", 
    undef, time, $o{ip}, $o{hostport}, lc $o{gamename});
}

################################################################################
## get a list of master server applets that were online in the past week
##
################################################################################
sub get_masterserver_applets {
  my $self = shift;

  return $self->db_all(
    "SELECT * 
     FROM appletlist
     WHERE updated > datetime(?, \'unixepoch\')",
    time-604800);
}

################################################################################
## Clear out applet entries that have been unresponsive or without servers for 
## more than a week. Servers with multiple entries only have the entry of this
## specific gamename removed.
################################################################################
sub remove_unresponsive_applets {
  my $self = shift;

  # remove entries
  my $u = $self->{dbh}->do(
  "DELETE FROM appletlist 
   WHERE updated < datetime(?, \'unixepoch\')", 
  undef, time-604800);
  
  # notify 
  $self->log("delete", "Removed $u entries from applet list") if ($u > 0);
}

1;