Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Timo Smit committed Feb 8, 2017
1 parent d7d5565 commit 4a80ab7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 80 deletions.
66 changes: 37 additions & 29 deletions luamods/wolfadmin/admin/admin.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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)
local old = players.getCachedName(clientId)
local new = et.Info_ValueForKey(clientInfo, "name")
-- 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)
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()
-- 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
if renamesPerMinute > settings.get("g_renameLimit") then
admin.kickPlayer(clientId, -1337, "Too many name changes.")
end
end
end
clientInfo = et.Info_SetValueForKey(clientInfo, "name", old)
et.trap_SetUserinfo(clientId, clientInfo)
et.ClientUserinfoChanged(clientId)
-- 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
players.setNameForced(clientId, false)
-- update database
if db.isconnected() then
local playerId = db.getplayer(players.getGUID(clientId))["id"]
local alias = db.getaliasbyname(playerId, newName)
et.trap_SendServerCommand(clientId, "cp \"Too many name changes in 1 minute.\";")
if alias then
db.updatealias(alias["id"], os.time())
else
if (os.time() - stats.get(clientId, "namechangeStart")) > settings.get("g_renameInterval") then
stats.set(clientId, "namechangeStart", os.time())
stats.get(clientId, "namechangePts", 0)
end
stats.add(clientId, "namechangePts", 1)
events.trigger("onClientNameChange", clientId, old, new)
db.addalias(playerId, newName, os.time())
end
end ]]
end
end
events.handle("onClientInfoChange", players.oninfochange)
events.handle("onClientNameChange", admin.onClientNameChange)
return admin
67 changes: 18 additions & 49 deletions luamods/wolfadmin/players/players.lua
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

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.."\";")
events.handle("onClientDisconnect", players.onClientDisconnect)

data[clientId]["name"] = new
function players.onClientInfoChange(clientId)
local oldTeam = data[clientId]["team"]
local newTeam = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))

if db.isconnected() then
local playerid = db.getplayer(players.getGUID(clientId))["id"]
local name = players.getName(clientId)
local alias = db.getaliasbyname(playerid, name)
if newTeam ~= oldTeam then
data[clientId]["team"] = newTeam

if alias then
db.updatealias(alias["id"], os.time())
else
db.addalias(playerid, name, os.time())
end
events.trigger("onClientTeamChange", clientId, oldTeam, newTeam)
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"))
local oldName = data[clientId]["name"]
local newName = et.gentity_get(clientId, "pers.netname")

if new ~= old then
data[clientId]["team"] = new
if newName ~= oldName then
data[clientId]["name"] = newName

events.trigger("onClientTeamChange", clientId, old, new)
events.trigger("onClientNameChange", clientId, oldName, newName)
end
end
events.handle("onClientInfoChange", players.oninfochange)
events.handle("onClientInfoChange", players.onClientInfoChange)

return players
3 changes: 1 addition & 2 deletions luamods/wolfadmin/util/settings.lua
Expand Up @@ -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,
Expand Down

0 comments on commit 4a80ab7

Please sign in to comment.