From c9889d45987bb56dc2f7b9a853386ad5d305cd47 Mon Sep 17 00:00:00 2001 From: Timo Smit Date: Sat, 14 Jan 2017 15:14:43 +0100 Subject: [PATCH] Load mutes from database and apply to online players (issue #55) --- luascripts/admin/admin.lua | 16 +++--- luascripts/admin/mutes.lua | 82 +++++++++++++++++++++++++++ luascripts/commands/admin/mute.lua | 3 +- luascripts/commands/admin/unmute.lua | 4 +- luascripts/commands/admin/vmute.lua | 8 +-- luascripts/commands/admin/vunmute.lua | 2 +- luascripts/db/sqlite3.lua | 9 +++ luascripts/players/players.lua | 6 +- 8 files changed, 112 insertions(+), 18 deletions(-) create mode 100644 luascripts/admin/mutes.lua diff --git a/luascripts/admin/admin.lua b/luascripts/admin/admin.lua index 1c6c572..5652371 100644 --- a/luascripts/admin/admin.lua +++ b/luascripts/admin/admin.lua @@ -32,14 +32,6 @@ function admin.putPlayer(clientId, teamId) et.trap_SendConsoleCommand(et.EXEC_APPEND, "forceteam "..clientId.." "..util.getTeamCode(teamId)..";") end -function admin.mutePlayer(victimId, invokerId, type, duration, reason) - players.setMuted(victimId, true, type, os.time(), duration) - db.addMute(victimId, invokerId, type, os.time(), duration, reason) - - et.trap_SendConsoleCommand(et.EXEC_APPEND, "ccp "..victimId.." \"^7You have been muted by "..players.getName(invokerId)..": ^7"..reason..".\";") - et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay -1 \"^dmute: ^7"..players.getName(victimId).." ^9has been muted.\";") -end - function admin.kickPlayer(victimId, invokerId, reason) et.trap_DropClient(victimId, "You have been kicked, Reason: "..(reason and reason or "kicked by admin"), 0) end @@ -76,6 +68,14 @@ function admin.onconnect(clientId, firstTime, isBot) -- clientbegin which is also triggered on warmup/maprestart/etc) --[[ stats.set(clientId, "namechangeStart", os.time()) stats.set(clientId, "namechangePts", 0) ]] + + local guid = et.Info_ValueForKey(et.trap_GetUserinfo(clientId), "cl_guid") + local playerId = db.getplayer(guid)["id"] + local mute = db.getMuteByPlayer(playerId) + + if mute then + players.setMuted(clientId, true, mute["type"], mute["issued"], mute["expires"]) + end end events.handle("onClientConnect", admin.onconnect) diff --git a/luascripts/admin/mutes.lua b/luascripts/admin/mutes.lua new file mode 100644 index 0000000..43e606b --- /dev/null +++ b/luascripts/admin/mutes.lua @@ -0,0 +1,82 @@ + +-- WolfAdmin module for Wolfenstein: Enemy Territory servers. +-- Copyright (C) 2015-2017 Timo 'Timothy' Smit + +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- at your option any later version. + +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. + +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +local db = require "luascripts.wolfadmin.db.db" + +local players = require "luascripts.wolfadmin.players.players" + +local events = require "luascripts.wolfadmin.util.events" +local timers = require "luascripts.wolfadmin.util.timers" + +local mutes = {} + +local muteTimer + +function mutes.get(muteId) + return db.getMute(muteId) +end + +function mutes.getCount() + return db.getMutesCount() +end + +function mutes.getList(start, limit) + return db.getMutes(start, limit) +end + +function mutes.add(victimId, invokerId, type, duration, reason) + local victimPlayerId = db.getplayer(players.getGUID(victimId))["id"] + local invokerPlayerId = db.getplayer(players.getGUID(invokerId))["id"] + + local reason = reason and reason or "muted by admin" + + players.setMuted(victimId, true, type, os.time(), os.time() + duration) + db.addMute(victimPlayerId, invokerPlayerId, type, os.time(), duration, reason) +end + +function mutes.remove(muteId) + db.removeMute(muteId) +end + +function mutes.removeByClient(clientId) + players.setMuted(clientId, false) + + local guid = et.Info_ValueForKey(et.trap_GetUserinfo(clientId), "cl_guid") + local playerId = db.getplayer(guid)["id"] + local mute = db.getMuteByPlayer(playerId) + + if mute then + return mutes.remove(mute["id"]) + end +end + +function mutes.checkUnmutes() + 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) + + et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^dunmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9has been automatically unmuted\";") + end + end +end + +function mutes.onInit() + muteTimer = timers.add(mutes.checkUnmutes, 1000, 0, false, false) +end +events.handle("onGameInit", mutes.onInit) + +return mutes diff --git a/luascripts/commands/admin/mute.lua b/luascripts/commands/admin/mute.lua index 100d214..01b0a31 100644 --- a/luascripts/commands/admin/mute.lua +++ b/luascripts/commands/admin/mute.lua @@ -19,6 +19,7 @@ local auth = require "luascripts.wolfadmin.auth.auth" local admin = require "luascripts.wolfadmin.admin.admin" local history = require "luascripts.wolfadmin.admin.history" +local mutes = require "luascripts.wolfadmin.admin.mutes" local commands = require "luascripts.wolfadmin.commands.commands" @@ -77,7 +78,7 @@ function commandMute(clientId, cmdArguments) return true end - admin.mutePlayer(cmdClient, clientId, players.MUTE_CHAT + players.MUTE_VOICE, duration, reason) + mutes.add(cmdClient, clientId, players.MUTE_CHAT + players.MUTE_VOICE, duration, reason) history.add(cmdClient, clientId, "mute", reason) et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^dmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9has been muted for "..duration.." seconds\";") diff --git a/luascripts/commands/admin/unmute.lua b/luascripts/commands/admin/unmute.lua index 1d6520a..917c87e 100644 --- a/luascripts/commands/admin/unmute.lua +++ b/luascripts/commands/admin/unmute.lua @@ -17,6 +17,8 @@ local auth = require "luascripts.wolfadmin.auth.auth" +local mutes = require "luascripts.wolfadmin.admin.mutes" + local commands = require "luascripts.wolfadmin.commands.commands" local players = require "luascripts.wolfadmin.players.players" @@ -52,7 +54,7 @@ function commandUnmute(clientId, cmdArguments) et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^dunmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9has been unmuted\";") - players.setMuted(cmdClient, false) + mutes.removeByClient(cmdClient) return true end diff --git a/luascripts/commands/admin/vmute.lua b/luascripts/commands/admin/vmute.lua index 6b23cc4..1a131db 100644 --- a/luascripts/commands/admin/vmute.lua +++ b/luascripts/commands/admin/vmute.lua @@ -17,8 +17,8 @@ local auth = require "luascripts.wolfadmin.auth.auth" -local admin = require "luascripts.wolfadmin.admin.admin" local history = require "luascripts.wolfadmin.admin.history" +local mutes = require "luascripts.wolfadmin.admin.mutes" local commands = require "luascripts.wolfadmin.commands.commands" @@ -76,10 +76,10 @@ function commandVoiceMute(clientId, cmdArguments) return true end - admin.mutePlayer(cmdClient, clientId, players.MUTE_VOICE, duration, reason) - history.add(cmdClient, clientId, "mute", reason) + mutes.add(cmdClient, clientId, players.MUTE_VOICE, duration, reason) + history.add(cmdClient, clientId, "vmute", reason) - et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^dvmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9has been voicemuted for "..vmuteTime.." seconds\";") + et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^dvmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9has been voicemuted for "..duration.." seconds\";") return true end diff --git a/luascripts/commands/admin/vunmute.lua b/luascripts/commands/admin/vunmute.lua index 0e5adc2..46f6765 100644 --- a/luascripts/commands/admin/vunmute.lua +++ b/luascripts/commands/admin/vunmute.lua @@ -50,7 +50,7 @@ function commandVoiceUnmute(clientId, cmdArguments) et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^dvunmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9has been unvoicemuted\";") - players.setMuted(cmdClient, false) + mutes.removeByClient(cmdClient) return true end diff --git a/luascripts/db/sqlite3.lua b/luascripts/db/sqlite3.lua index 5b4ed22..7355757 100644 --- a/luascripts/db/sqlite3.lua +++ b/luascripts/db/sqlite3.lua @@ -296,6 +296,15 @@ function sqlite3.getMute(muteId) return mute end +function sqlite3.getMuteByPlayer(playerId) + cur = assert(con:execute("SELECT * FROM `mute` WHERE `victim_id`="..tonumber(playerId).." AND `expires`>"..os.time())) + + local mute = cur:fetch({}, "a") + cur:close() + + return mute +end + -- bans function sqlite3.addBan(victimId, invokerId, issued, duration, reason) cur = assert(con:execute("INSERT INTO `ban` (`victim_id`, `invoker_id`, `issued`, `expires`, `duration`, `reason`) VALUES ("..tonumber(victimId)..", "..tonumber(invokerId)..", "..tonumber(issued)..", "..(tonumber(issued) + tonumber(duration))..", "..tonumber(duration)..", '"..util.escape(reason).."')")) diff --git a/luascripts/players/players.lua b/luascripts/players/players.lua index 3332931..5db8e7d 100644 --- a/luascripts/players/players.lua +++ b/luascripts/players/players.lua @@ -85,11 +85,11 @@ end function players.isMuted(clientId, type) if type == nil then - return data[clientId]["mute"] ~= nil + return data[clientId] ~= nil and data[clientId]["mute"] ~= nil elseif type == players.MUTE_CHAT then - return data[clientId]["mute"] ~= nil and bits.hasbit(data[clientId]["mute"]["type"], players.MUTE_CHAT) + return data[clientId] ~= nil and data[clientId]["mute"] ~= nil and bits.hasbit(data[clientId]["mute"]["type"], players.MUTE_CHAT) elseif type == players.MUTE_VOICE then - return data[clientId]["mute"] ~= nil and bits.hasbit(data[clientId]["mute"]["type"], players.MUTE_VOICE) + return data[clientId] ~= nil and data[clientId]["mute"] ~= nil and bits.hasbit(data[clientId]["mute"]["type"], players.MUTE_VOICE) end return false