From d8ef69a6da8a7bd2e15ae4ce67182dcab0f652a2 Mon Sep 17 00:00:00 2001 From: Timo Smit Date: Mon, 4 Feb 2019 13:00:46 +0100 Subject: [PATCH] Added timers to remove expired mutes and bans from database --- luascripts/wolfadmin/admin/bans.lua | 14 ++++++++++++++ luascripts/wolfadmin/admin/mutes.lua | 12 +++++++++--- luascripts/wolfadmin/db/mysql.lua | 8 ++++++++ luascripts/wolfadmin/db/sqlite3.lua | 8 ++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/luascripts/wolfadmin/admin/bans.lua b/luascripts/wolfadmin/admin/bans.lua index 0f2df01..e96df10 100644 --- a/luascripts/wolfadmin/admin/bans.lua +++ b/luascripts/wolfadmin/admin/bans.lua @@ -19,8 +19,13 @@ local db = wolfa_requireModule("db.db") local players = wolfa_requireModule("players.players") +local events = wolfa_requireModule("util.events") +local timers = wolfa_requireModule("util.timers") + local bans = {} +local storedBanTimer + function bans.get(banId) return db.getBan(banId) end @@ -48,4 +53,13 @@ function bans.remove(banId) db.removeBan(banId) end +function bans.checkStoredBans() + db.removeExpiredBans() +end + +function bans.onInit() + storedBanTimer = timers.add(bans.checkStoredBans, 60000, 0, false, false) +end +events.handle("onGameInit", bans.onInit) + return bans diff --git a/luascripts/wolfadmin/admin/mutes.lua b/luascripts/wolfadmin/admin/mutes.lua index cd54f52..f2a97cd 100644 --- a/luascripts/wolfadmin/admin/mutes.lua +++ b/luascripts/wolfadmin/admin/mutes.lua @@ -24,7 +24,8 @@ local timers = wolfa_requireModule("util.timers") local mutes = {} -local muteTimer +local storedMuteTimer +local liveMuteTimer function mutes.get(muteId) return db.getMute(muteId) @@ -64,7 +65,11 @@ function mutes.removeByClient(clientId) end end -function mutes.checkUnmutes() +function mutes.checkStoredMutes() + db.removeExpiredMutes() +end + +function mutes.checkLiveMutes() for clientId = 0, et.trap_Cvar_Get("sv_maxclients") - 1 do if players.isMuted(clientId) and players.getMuteExpiresAt(clientId) < os.time() then mutes.removeByClient(clientId) @@ -75,7 +80,8 @@ function mutes.checkUnmutes() end function mutes.onInit() - muteTimer = timers.add(mutes.checkUnmutes, 1000, 0, false, false) + storedMuteTimer = timers.add(mutes.checkStoredMutes, 60000, 0, false, false) + liveMuteTimer = timers.add(mutes.checkLiveMutes, 1000, 0, false, false) end events.handle("onGameInit", mutes.onInit) diff --git a/luascripts/wolfadmin/db/mysql.lua b/luascripts/wolfadmin/db/mysql.lua index f117f75..0d70d96 100644 --- a/luascripts/wolfadmin/db/mysql.lua +++ b/luascripts/wolfadmin/db/mysql.lua @@ -336,6 +336,10 @@ function mysql.removeMute(muteId) cur = assert(con:execute("DELETE FROM `mute` WHERE `id`="..tonumber(muteId).."")) end +function mysql.removeExpiredMutes() + cur = assert(con:execute("DELETE FROM `mute` WHERE `expires`<="..os.time())) +end + function mysql.getMutesCount() cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `mute`")) @@ -391,6 +395,10 @@ function mysql.removeBan(banId) cur = assert(con:execute("DELETE FROM `ban` WHERE `id`="..tonumber(banId).."")) end +function mysql.removeExpiredBans() + cur = assert(con:execute("DELETE FROM `ban` WHERE `expires`<="..os.time())) +end + function mysql.getBansCount() cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `ban`")) diff --git a/luascripts/wolfadmin/db/sqlite3.lua b/luascripts/wolfadmin/db/sqlite3.lua index 2aa9715..5a2dca6 100644 --- a/luascripts/wolfadmin/db/sqlite3.lua +++ b/luascripts/wolfadmin/db/sqlite3.lua @@ -336,6 +336,10 @@ function sqlite3.removeMute(muteId) cur = assert(con:execute("DELETE FROM `mute` WHERE `id`="..tonumber(muteId).."")) end +function sqlite3.removeExpiredMutes() + cur = assert(con:execute("DELETE FROM `mute` WHERE `expires`<="..os.time())) +end + function sqlite3.getMutesCount() cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `mute`")) @@ -391,6 +395,10 @@ function sqlite3.removeBan(banId) cur = assert(con:execute("DELETE FROM `ban` WHERE `id`="..tonumber(banId).."")) end +function sqlite3.removeExpiredBans() + cur = assert(con:execute("DELETE FROM `ban` WHERE `expires`<="..os.time())) +end + function sqlite3.getBansCount() cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `ban`"))