aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Database/Pg/dbStats.pm
diff options
context:
space:
mode:
authorDarkelarious <darkelarious@333networks.com>2016-11-20 19:30:58 +0100
committerDarkelarious <darkelarious@333networks.com>2016-11-20 19:30:58 +0100
commit702d1898b012d6d992d66b996508610890bf2963 (patch)
tree9556d890c4afdb7062f5b63277aa6b3d5c5d9d03 /lib/MasterServer/Database/Pg/dbStats.pm
parentc3f8d65a4fb1f5674557ee67cf7f74369df86ad1 (diff)
downloadMasterServer-Perl-702d1898b012d6d992d66b996508610890bf2963.tar.gz
MasterServer-Perl-702d1898b012d6d992d66b996508610890bf2963.zip
Restore SQLite support
Diffstat (limited to 'lib/MasterServer/Database/Pg/dbStats.pm')
-rwxr-xr-xlib/MasterServer/Database/Pg/dbStats.pm117
1 files changed, 117 insertions, 0 deletions
diff --git a/lib/MasterServer/Database/Pg/dbStats.pm b/lib/MasterServer/Database/Pg/dbStats.pm
new file mode 100755
index 0000000..6e92ab6
--- /dev/null
+++ b/lib/MasterServer/Database/Pg/dbStats.pm
@@ -0,0 +1,117 @@
+package MasterServer::Database::Pg::dbStats;
+
+use strict;
+use warnings;
+use Exporter 'import';
+
+our @EXPORT = qw| get_gamelist_stats
+ write_direct_beacons
+ write_stat
+ write_kfstats |;
+
+################################################################################
+# calculate stats for all individual games
+################################################################################
+sub get_gamelist_stats {
+ my $self = shift;
+
+ return $self->{dbh}->selectall_arrayref(
+ "SELECT DISTINCT gamename AS gamename,
+ COUNT(NULLIF(b333ms AND updated > to_timestamp(?), FALSE)) AS numdirect,
+ COUNT(NULLIF(updated > to_timestamp(?), FALSE)) AS numtotal
+ FROM serverlist
+ GROUP BY gamename", undef, time-7200, time-7200);
+}
+
+################################################################################
+# Determine from the last beacon whether the server is still uplinking
+# directly to us, or whether it stopped uplinking and is now artificially
+# kept in the database.
+################################################################################
+sub write_direct_beacons {
+ my $self = shift;
+ my $u = $self->{dbh}->do(
+ "UPDATE serverlist
+ SET b333ms = CAST(0 AS BOOLEAN)
+ WHERE beacon < to_timestamp(?) AND b333ms",
+ undef, time-3600);
+
+ # notify
+ $self->log("unset", "Lost $u direct beacons.") if ($u > 0);
+}
+
+################################################################################
+# Write the stats to the games table
+# A stat can not exist without existing gamename. Was inserted by cipher loader.
+################################################################################
+sub write_stat {
+ my ($self, %opt) = @_;
+
+ # if it is already in the pending list, update it with a new challenge
+ my $u = $self->{dbh}->do(
+ "UPDATE games
+ SET num_uplink = ?,
+ num_total = ?
+ WHERE gamename = ?",
+ undef, $opt{num_uplink}, $opt{num_total}, lc $opt{gamename});
+
+ # notify
+ $self->log("update", "Updated stats for $opt{gamename}.") if ($u > 0);
+
+}
+
+################################################################################
+## Write the KFStats to the database
+################################################################################
+sub write_kfstats {
+ my ($self, $h) = @_;
+
+ # check if entry already excists.
+ my $u = $self->{dbh}->selectall_arrayref(
+ "SELECT * FROM kfstats WHERE UTkey = ? ", undef, $h->{UTkey});
+
+ if ( !defined $u->[0] ) {
+ $u = $self->{dbh}->do(
+ "INSERT INTO kfstats (UTkey, Username) VALUES (?,?)",
+ undef, $h->{UTkey}, $h->{Username});
+
+ # notify
+ $self->log("kfnew", "New KF Player $h->{Username} added");
+ }
+
+ # update existing information
+ $u = $self->{dbh}->do("UPDATE kfstats SET
+ Username = ?,
+ CurrentVeterancy = ?,
+ TotalKills = ?,
+ DecaptedKills = ?,
+ TotalMeleeDamage = ?,
+ MeleeKills = ?,
+ PowerWpnKills = ?,
+ BullpupDamage = ?,
+ StalkerKills = ?,
+ TotalWelded = ?,
+ TotalHealed = ?,
+ TotalPlaytime =?,
+ GamesWon = ?,
+ GamesLost = ?
+ WHERE UTkey = ?", undef,
+ $h->{Username},
+ $h->{CurrentVeterancy},
+ $h->{TotalKills},
+ $h->{DecaptedKills},
+ $h->{TotalMeleeDamage},
+ $h->{MeleeKills},
+ $h->{PowerWpnKills},
+ $h->{BullpupDamage},
+ $h->{StalkerKills},
+ $h->{TotalWelded},
+ $h->{TotalHealed},
+ $h->{TotalPlaytime},
+ $h->{GamesWon},
+ $h->{GamesLost},
+ $h->{UTkey}
+ );
+}
+
+1;