From 34a2c7390ea9662d33258d384e72fff1912343ff Mon Sep 17 00:00:00 2001 From: Darkelarious Date: Sat, 13 May 2017 14:18:28 +0200 Subject: revised synchronization methods, config settings and bug fixes --- .../Database/SQLite/dbAppletActions.pm | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 lib/MasterServer/Database/SQLite/dbAppletActions.pm (limited to 'lib/MasterServer/Database/SQLite/dbAppletActions.pm') diff --git a/lib/MasterServer/Database/SQLite/dbAppletActions.pm b/lib/MasterServer/Database/SQLite/dbAppletActions.pm new file mode 100755 index 0000000..d2421fc --- /dev/null +++ b/lib/MasterServer/Database/SQLite/dbAppletActions.pm @@ -0,0 +1,92 @@ +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 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 = 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 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 > 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; -- cgit v1.2.3