aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Database/Pg/dbAppletActions.pm
diff options
context:
space:
mode:
authorDarkelarious <darkelarious@333networks.com>2017-05-13 14:18:28 +0200
committerDarkelarious <darkelarious@333networks.com>2017-05-13 14:20:49 +0200
commit34a2c7390ea9662d33258d384e72fff1912343ff (patch)
treed96ea33c0107e4906a152aa1de4b5c75b81ba0a8 /lib/MasterServer/Database/Pg/dbAppletActions.pm
parent84af66aba26d2088d5d95c240d176f3edaf17b58 (diff)
downloadMasterServer-Perl-34a2c7390ea9662d33258d384e72fff1912343ff.tar.gz
MasterServer-Perl-34a2c7390ea9662d33258d384e72fff1912343ff.zip
revised synchronization methods, config settings and bug fixesv2.3.0
Diffstat (limited to 'lib/MasterServer/Database/Pg/dbAppletActions.pm')
-rwxr-xr-xlib/MasterServer/Database/Pg/dbAppletActions.pm92
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/MasterServer/Database/Pg/dbAppletActions.pm b/lib/MasterServer/Database/Pg/dbAppletActions.pm
new file mode 100755
index 0000000..dc7d941
--- /dev/null
+++ b/lib/MasterServer/Database/Pg/dbAppletActions.pm
@@ -0,0 +1,92 @@
+package MasterServer::Database::Pg::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 port = ?
+ AND gamename = ?",
+ undef, $o{ip}, $o{port}, lc $o{gamename});
+
+ # return if found
+ return if ($u > 0);
+
+ # insert applet data
+ return $self->{dbh}->do("INSERT INTO appletlist (ip, port, gamename)
+ SELECT ?, ?, ?", undef,
+ $o{ip}, $o{port}, lc $o{gamename});
+}
+
+################################################################################
+## reset added/updated time after restart
+################################################################################
+sub reset_master_applets {
+ my $self = shift;
+ return $self->{dbh}->do("UPDATE appletlist
+ SET added = to_timestamp(?),
+ updated = to_timestamp(?)",
+ undef, time, time);
+}
+
+################################################################################
+## update time on master applet
+################################################################################
+sub update_master_applet {
+ my ($self, %o) = @_;
+
+ return $self->{dbh}->do("UPDATE appletlist
+ SET updated = to_timestamp(?)
+ WHERE ip = ?
+ AND port = ?
+ AND gamename = ?",
+ undef, time, $o{ip}, $o{port}, 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 > to_timestamp(?)",
+ 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 < to_timestamp(?)", undef, time-604800);
+
+ # notify
+ $self->log("delete", "Removed $u entries from applet list.") if ($u > 0);
+}
+
+1;