From 4a80ab729b7cf1fce895229713fb7ca4531538bd Mon Sep 17 00:00:00 2001 From: Timo Smit Date: Wed, 8 Feb 2017 18:18:51 +0100 Subject: [PATCH] Fixed rename limit (refs #46) * moved name change detection to players module * rename limit now kicks instead of name forcing * rename limit now actually calculates over past minute (approximately) * removed g_renameInterval, obsolete --- luamods/wolfadmin/admin/admin.lua | 68 ++++++++++++++----------- luamods/wolfadmin/players/players.lua | 73 ++++++++------------------- luamods/wolfadmin/util/settings.lua | 3 +- 3 files changed, 60 insertions(+), 84 deletions(-) diff --git a/luamods/wolfadmin/admin/admin.lua b/luamods/wolfadmin/admin/admin.lua index 09fdbf5..f50b09c 100644 --- a/luamods/wolfadmin/admin/admin.lua +++ b/luamods/wolfadmin/admin/admin.lua @@ -18,16 +18,15 @@ local db = require (wolfa_getLuaPath()..".db.db") local players = require (wolfa_getLuaPath()..".players.players") --- local stats = require (wolfa_getLuaPath()..".players.stats") -local constants = require (wolfa_getLuaPath()..".util.constants") local events = require (wolfa_getLuaPath()..".util.events") -local files = require (wolfa_getLuaPath()..".util.files") local settings = require (wolfa_getLuaPath()..".util.settings") local util = require (wolfa_getLuaPath()..".util.util") local admin = {} +local playerRenames = {} + function admin.putPlayer(clientId, teamId) et.trap_SendConsoleCommand(et.EXEC_APPEND, "forceteam "..clientId.." "..util.getTeamCode(teamId)..";") end @@ -86,37 +85,46 @@ function admin.onconnect(clientId, firstTime, isBot) end events.handle("onClientConnect", admin.onconnect) -function players.oninfochange(clientId) - local clientInfo = et.trap_GetUserinfo(clientId) +function admin.onClientNameChange(clientId, oldName, newName) + -- rename filter + if not playerRenames[clientId] or playerRenames[clientId]["last"] < os.time() - 60 then + playerRenames[clientId] = { + ["first"] = os.time(), + ["last"] = os.time(), + ["count"] = 1 + } + else + playerRenames[clientId]["count"] = playerRenames[clientId]["count"] + 1 + playerRenames[clientId]["last"] = os.time() - local old = players.getCachedName(clientId) - local new = et.Info_ValueForKey(clientInfo, "name") + -- give them some time + if (playerRenames[clientId]["last"] - playerRenames[clientId]["first"]) > 3 then + local renamesPerMinute = playerRenames[clientId]["count"] / (playerRenames[clientId]["last"] - playerRenames[clientId]["first"]) * 60 - -- TODO fix for Legacy - -- prints messages by itself, also when rename is rejected - not desirable - --[[ if new ~= old then - if (os.time() - stats.get(clientId, "namechangeStart")) < settings.get("g_renameInterval") and stats.get(clientId, "namechangePts") >= settings.get("g_renameLimit") and not players.isNameForced(clientId) then - players.setNameForced(clientId, true) - - clientInfo = et.Info_SetValueForKey(clientInfo, "name", old) - et.trap_SetUserinfo(clientId, clientInfo) - et.ClientUserinfoChanged(clientId) - - players.setNameForced(clientId, false) - - et.trap_SendServerCommand(clientId, "cp \"Too many name changes in 1 minute.\";") - else - if (os.time() - stats.get(clientId, "namechangeStart")) > settings.get("g_renameInterval") then - stats.set(clientId, "namechangeStart", os.time()) - stats.get(clientId, "namechangePts", 0) + if renamesPerMinute > settings.get("g_renameLimit") then + admin.kickPlayer(clientId, -1337, "Too many name changes.") end - - stats.add(clientId, "namechangePts", 1) - - events.trigger("onClientNameChange", clientId, old, new) end - end ]] + end + + -- on some mods, this message is already printed + -- known: old NQ versions, Legacy + if et.trap_Cvar_Get("fs_game") ~= "legacy" then + et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay -1 \""..oldName.." ^7is now known as "..newName.."\";") + end + + -- update database + if db.isconnected() then + local playerId = db.getplayer(players.getGUID(clientId))["id"] + local alias = db.getaliasbyname(playerId, newName) + + if alias then + db.updatealias(alias["id"], os.time()) + else + db.addalias(playerId, newName, os.time()) + end + end end -events.handle("onClientInfoChange", players.oninfochange) +events.handle("onClientNameChange", admin.onClientNameChange) return admin diff --git a/luamods/wolfadmin/players/players.lua b/luamods/wolfadmin/players/players.lua index 5299a08..109d986 100644 --- a/luamods/wolfadmin/players/players.lua +++ b/luamods/wolfadmin/players/players.lua @@ -31,10 +31,6 @@ function players.isConnected(clientId) return (data[clientId] ~= nil) end -function players.getCachedName(clientId) - return data[clientId]["name"] -end - function players.getName(clientId) if clientId == -1337 then return "console" @@ -67,14 +63,6 @@ function players.getLastPMSender(clientId) return data[clientId]["lastpmsender"] end -function players.setNameForced(clientId, state) - data[clientId]["nameforced"] = state -end - -function players.isNameForced(clientId) - return data[clientId]["nameforced"] -end - function players.setMuted(clientId, state, type, issued, expires) data[clientId]["mute"] = nil @@ -119,7 +107,7 @@ function players.isTeamLocked(clientId) return data[clientId]["teamlock"] end -function players.onconnect(clientId, firstTime, isBot) +function players.onClientConnect(clientId, firstTime, isBot) local clientInfo = et.trap_GetUserinfo(clientId) -- name is NOT yet set in pers.netname, so get all info out of infostring @@ -159,58 +147,39 @@ function players.onconnect(clientId, firstTime, isBot) end end end -events.handle("onClientConnect", players.onconnect) - -function players.onbegin(clientId) - -- TODO: - -- new approach: load necessary data in onClientConnect event handlers, - -- load rest in onClientBegin handlers (avoids useless loading of stats, - -- less coupling between main.lua and stats.lua) - -- ensures that all data is loaded from this moment on +events.handle("onClientConnect", players.onClientConnect) +function players.onClientBegin(clientId) events.trigger("onPlayerReady", clientId, data[clientId]["new"]) data[clientId]["new"] = false end -events.handle("onClientBegin", players.onbegin) +events.handle("onClientBegin", players.onClientBegin) -function players.ondisconnect(clientId) +function players.onClientDisconnect(clientId) data[clientId] = nil end -events.handle("onClientDisconnect", players.ondisconnect) +events.handle("onClientDisconnect", players.onClientDisconnect) -function players.onnamechange(clientId, old, new) - -- TODO: on some mods, this message is already printed - -- known: old NQ versions, Legacy - et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay -1 \""..old.." ^7is now known as "..new.."\";") +function players.onClientInfoChange(clientId) + local oldTeam = data[clientId]["team"] + local newTeam = tonumber(et.gentity_get(clientId, "sess.sessionTeam")) - data[clientId]["name"] = new + if newTeam ~= oldTeam then + data[clientId]["team"] = newTeam - if db.isconnected() then - local playerid = db.getplayer(players.getGUID(clientId))["id"] - local name = players.getName(clientId) - local alias = db.getaliasbyname(playerid, name) + events.trigger("onClientTeamChange", clientId, oldTeam, newTeam) + end - if alias then - db.updatealias(alias["id"], os.time()) - else - db.addalias(playerid, name, os.time()) - end + local oldName = data[clientId]["name"] + local newName = et.gentity_get(clientId, "pers.netname") + + if newName ~= oldName then + data[clientId]["name"] = newName + + events.trigger("onClientNameChange", clientId, oldName, newName) end end -events.handle("onClientNameChange", players.onnamechange) - -function players.oninfochange(clientId) - local clientInfo = et.trap_GetUserinfo(clientId) - local old = data[clientId]["team"] - local new = tonumber(et.gentity_get(clientId, "sess.sessionTeam")) - - if new ~= old then - data[clientId]["team"] = new - - events.trigger("onClientTeamChange", clientId, old, new) - end -end -events.handle("onClientInfoChange", players.oninfochange) +events.handle("onClientInfoChange", players.onClientInfoChange) return players diff --git a/luamods/wolfadmin/util/settings.lua b/luamods/wolfadmin/util/settings.lua index 7987957..1c67fd3 100644 --- a/luamods/wolfadmin/util/settings.lua +++ b/luamods/wolfadmin/util/settings.lua @@ -40,8 +40,7 @@ local data = { ["g_evenerInterval"] = 30, ["g_voteNextMapTimeout"] = 0, ["g_restrictedVotes"] = "", - ["g_renameLimit"] = 3, - ["g_renameInterval"] = 60, + ["g_renameLimit"] = 80, ["g_standalone"] = 1, ["g_debugWolfAdmin"] = 0, ["omnibot_maxbots"] = 10,