From 5ea4fc90e4cac7489cbed2e24157df3821207f0f Mon Sep 17 00:00:00 2001 From: Timo Smit Date: Tue, 6 Sep 2016 12:41:02 +0200 Subject: [PATCH] Seperated players and stats module --- luascripts/admin/admin.lua | 37 +++++--- luascripts/admin/warns.lua | 16 ++-- luascripts/auth/acl.lua | 4 +- luascripts/commands/admin/admintest.lua | 7 +- luascripts/commands/admin/finger.lua | 14 +-- luascripts/commands/admin/gib.lua | 7 +- luascripts/commands/admin/incognito.lua | 8 +- luascripts/commands/admin/listaliases.lua | 14 +-- luascripts/commands/admin/listlevels.lua | 16 ++-- luascripts/commands/admin/listplayers.lua | 9 +- luascripts/commands/admin/slap.lua | 7 +- luascripts/commands/client/ac.lua | 5 +- luascripts/commands/commands.lua | 16 ++-- luascripts/db/mysql.lua | 8 +- luascripts/db/sqlite3.lua | 8 +- luascripts/game/bots.lua | 13 ++- luascripts/game/game.lua | 2 +- luascripts/game/sprees.lua | 16 ++-- luascripts/players/greetings.lua | 10 +-- luascripts/players/players.lua | 102 ++++++++++++++++------ luascripts/players/stats.lua | 29 +++--- 21 files changed, 219 insertions(+), 129 deletions(-) diff --git a/luascripts/admin/admin.lua b/luascripts/admin/admin.lua index 71fa9ea..102472e 100644 --- a/luascripts/admin/admin.lua +++ b/luascripts/admin/admin.lua @@ -33,6 +33,18 @@ local teamLocks = { [constants.TEAM_SPECTATORS] = false, } +function admin.isPlayerMuted(clientId) + -- return players.isPlayerMuted(clientId) +end + +function admin.mutePlayer(clientId, length) + -- +end + +function admin.unmutePlayer(clientId) + -- +end + function admin.isVoiceMuted(clientId) if stats.get(clientId, "voiceMute") then if stats.get(clientId, "voiceMute") - os.time() > 0 then @@ -45,14 +57,6 @@ function admin.isVoiceMuted(clientId) return false end -function admin.isPlayerLocked(clientId) - if stats.get(clientId, "teamLock") then - return true - end - - return false -end - function admin.muteVoice(clientId, length) stats.set(clientId, "voiceMute", length) end @@ -73,6 +77,14 @@ function admin.unlockTeam(teamId) teamLocks[teamId] = false end +function admin.isPlayerLocked(clientId) + if stats.get(clientId, "teamLock") then + return true + end + + return false +end + function admin.lockPlayer(clientId) stats.set(clientId, "teamLock", true) end @@ -82,8 +94,8 @@ function admin.unlockPlayer(clientId) end function admin.setPlayerLevel(clientId, level, adminId) - local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"] - local adminid = db.getplayer(stats.get(adminId, "playerGUID"))["id"] + local playerid = db.getplayer(players.getGUID(clientId))["id"] + local adminid = db.getplayer(players.getGUID(clientId))["id"] db.updateplayerlevel(playerid, level) db.addsetlevel(playerid, level, adminid, os.time()) @@ -112,7 +124,8 @@ events.handle("onClientConnect", admin.onconnect) function players.oninfochange(clientId) local clientInfo = et.trap_GetUserinfo(clientId) - local old = stats.get(clientId, "playerName") + + local old = players.getCachedName(clientId) local new = et.Info_ValueForKey(clientInfo, "name") if new ~= old then @@ -127,8 +140,6 @@ function players.oninfochange(clientId) et.trap_SendServerCommand(clientId, "cp \"Too many name changes in 1 minute.\";") else - stats.set(clientId, "playerName", new) - if (os.time() - stats.get(clientId, "namechangeStart")) > settings.get("g_renameInterval") then stats.set(clientId, "namechangeStart", os.time()) stats.get(clientId, "namechangePts", 0) diff --git a/luascripts/admin/warns.lua b/luascripts/admin/warns.lua index 4c91856..c634ce7 100644 --- a/luascripts/admin/warns.lua +++ b/luascripts/admin/warns.lua @@ -16,9 +16,11 @@ -- 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 settings = require "luascripts.wolfadmin.util.settings" -local stats = require "luascripts.wolfadmin.players.stats" local warns = {} @@ -28,27 +30,27 @@ function warns.get(clientId, warnId) if warnId then return db.getwarn(warnId) else - local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"] + local playerid = db.getplayer(players.getGUID(clientId))["id"] return db.getwarns(playerid) end end function warns.getcount(clientId) - local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"] + local playerid = db.getplayer(players.getGUID(clientId))["id"] return db.getwarnscount(playerid) end function warns.getlimit(clientId, start, limit) - local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"] + local playerid = db.getplayer(players.getGUID(clientId))["id"] return db.getwarns(playerid, start, limit) end function warns.add(clientId, reason, adminId, datetime) - local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"] - local adminid = db.getplayer(stats.get(adminId, "playerGUID"))["id"] + local playerid = db.getplayer(players.getGUID(clientId))["id"] + local adminid = db.getplayer(players.getGUID(clientId))["id"] db.addwarn(playerid, reason, adminid, datetime) end @@ -61,4 +63,4 @@ function warns.remove(clientId, warnId) db.removewarn(warnId) end -return warns \ No newline at end of file +return warns diff --git a/luascripts/auth/acl.lua b/luascripts/auth/acl.lua index 6cc5193..054cdd7 100644 --- a/luascripts/auth/acl.lua +++ b/luascripts/auth/acl.lua @@ -19,7 +19,7 @@ local auth = require "luascripts.wolfadmin.auth.auth" local db = require "luascripts.wolfadmin.db.db" -local stats = require "luascripts.wolfadmin.players.stats" +local players = require "luascripts.wolfadmin.players.players" local events = require "luascripts.wolfadmin.util.events" local files = require "luascripts.wolfadmin.util.files" @@ -43,7 +43,7 @@ function acl.isallowed(clientId, permission) end function acl.getlevel(clientId) - local player = db.getplayer(stats.get(clientId, "playerGUID")) + local player = db.getplayer(players.getGUID(clientId)) return player["level_id"] end diff --git a/luascripts/commands/admin/admintest.lua b/luascripts/commands/admin/admintest.lua index dba07f0..be2652c 100644 --- a/luascripts/commands/admin/admintest.lua +++ b/luascripts/commands/admin/admintest.lua @@ -16,15 +16,18 @@ -- along with this program. If not, see . local auth = require "luascripts.wolfadmin.auth.auth" + local commands = require "luascripts.wolfadmin.commands.commands" -local stats = require "luascripts.wolfadmin.players.stats" + +local players = require "luascripts.wolfadmin.players.players" + local settings = require "luascripts.wolfadmin.util.settings" function commandAdminTest(clientId, cmdArguments) local level = auth.getlevel(clientId) local levelName = auth.getlevelname(level) - et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dadmintest: ^7"..stats.get(clientId, "playerName").." ^9is a level "..level.." user (^7"..levelName.."^9).\";") + et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dadmintest: ^7"..players.getName(clientId).." ^9is a level "..level.." user (^7"..levelName.."^9).\";") return true end diff --git a/luascripts/commands/admin/finger.lua b/luascripts/commands/admin/finger.lua index 4d19aeb..b8cb5a3 100644 --- a/luascripts/commands/admin/finger.lua +++ b/luascripts/commands/admin/finger.lua @@ -16,8 +16,11 @@ -- along with this program. If not, see . local auth = require "luascripts.wolfadmin.auth.auth" + local commands = require "luascripts.wolfadmin.commands.commands" -local stats = require "luascripts.wolfadmin.players.stats" + +local players = require "luascripts.wolfadmin.players.players" + local settings = require "luascripts.wolfadmin.util.settings" local util = require "luascripts.wolfadmin.util.util" @@ -43,12 +46,11 @@ function commandFinger(clientId, cmdArguments) end local stats = { - ["name"] = et.gentity_get(cmdClient, "pers.netname"), - ["cleanname"] = et.gentity_get(cmdClient, "pers.netname"):gsub("%^[^^]", ""), - ["codedsname"] = et.gentity_get(cmdClient, "pers.netname"):gsub("%^([^^])", "^^2%1"), - ["guid"] = et.gentity_get(cmdClient, "pers.netname"):gsub("%^([^^])", "^^2%1"), + ["name"] = players.getName(clientId), + ["cleanname"] = players.getName(clientId):gsub("%^[^^]", ""), + ["codedsname"] = players.getName(clientId):gsub("%^([^^])", "^^2%1"), ["slot"] = cmdClient, - ["guid"] = stats.get(cmdClient, "playerGUID"), + ["guid"] = players.getGUID(clientId), } et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dInformation about ^7"..stats["name"].."^d:\";") diff --git a/luascripts/commands/admin/gib.lua b/luascripts/commands/admin/gib.lua index 249db3a..0514d5e 100644 --- a/luascripts/commands/admin/gib.lua +++ b/luascripts/commands/admin/gib.lua @@ -16,8 +16,11 @@ -- along with this program. If not, see . local auth = require "luascripts.wolfadmin.auth.auth" + local commands = require "luascripts.wolfadmin.commands.commands" -local stats = require "luascripts.wolfadmin.players.stats" + +local players = require "luascripts.wolfadmin.players.players" + local settings = require "luascripts.wolfadmin.util.settings" function commandGib(clientId, cmdArguments) @@ -56,7 +59,7 @@ function commandGib(clientId, cmdArguments) -- ENTITYNUM_WORLD MAX_GENTITIES - 2 18 et.G_Damage(cmdClient, 18, 18, 500, 0, 0) -- MOD_UNKNOWN = 0 - et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dgib: ^7"..stats.get(cmdClient, "playerName").." ^9was gibbed.\";") + et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dgib: ^7"..players.getName(cmdClient).." ^9was gibbed.\";") return true end diff --git a/luascripts/commands/admin/incognito.lua b/luascripts/commands/admin/incognito.lua index d74cf22..e206f76 100644 --- a/luascripts/commands/admin/incognito.lua +++ b/luascripts/commands/admin/incognito.lua @@ -15,9 +15,11 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . -local commands = require "luascripts.wolfadmin.commands.commands" local auth = require "luascripts.wolfadmin.auth.auth" -local stats = require "luascripts.wolfadmin.players.stats" + +local commands = require "luascripts.wolfadmin.commands.commands" + +local players = require "luascripts.wolfadmin.players.players" function commandIncognito(clientId, cmdArguments) local fileName = et.trap_Cvar_Get("g_shrubbot") @@ -37,7 +39,7 @@ function commandIncognito(clientId, cmdArguments) for entry, adminName, adminGUID, adminLevel, adminFlags in string.gmatch(fileString, "(%[admin%]\nname%s+=%s+([%a%d%p]+)\nguid%s+=%s+([%u%d]+)\nlevel%s+=%s+([%d]+)\nflags%s+=%s+([%a%d%p]*)\n\n)") do -- et.G_Print(string.format("%s %s %d %s\n", adminName, adminGUID, adminLevel, adminFlags)) - if stats.get(clientId, "playerGUID") == adminGUID then + if players.getGUID(clientId) == adminGUID then if auth.isallowed(clientId, "@") ~= 1 then adminFlags = adminFlags.."+@" diff --git a/luascripts/commands/admin/listaliases.lua b/luascripts/commands/admin/listaliases.lua index ef61c53..9f02a21 100644 --- a/luascripts/commands/admin/listaliases.lua +++ b/luascripts/commands/admin/listaliases.lua @@ -15,13 +15,17 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . -local util = require "luascripts.wolfadmin.util.util" -local settings = require "luascripts.wolfadmin.util.settings" -local pagination = require "luascripts.wolfadmin.util.pagination" local auth = require "luascripts.wolfadmin.auth.auth" + local db = require "luascripts.wolfadmin.db.db" + local commands = require "luascripts.wolfadmin.commands.commands" -local stats = require "luascripts.wolfadmin.players.stats" + +local players = require "luascripts.wolfadmin.players.players" + +local pagination = require "luascripts.wolfadmin.util.pagination" +local settings = require "luascripts.wolfadmin.util.settings" +local util = require "luascripts.wolfadmin.util.util" function commandListAliases(clientId, cmdArguments) if not db.isconnected() then @@ -58,7 +62,7 @@ function commandListAliases(clientId, cmdArguments) return true end - local player = db.getplayer(stats.get(cmdClient, "playerGUID"))["id"] + local player = db.getplayer(players.getGUID(cmdClient))["id"] local count = db.getaliasescount(player) local limit, offset = pagination.calculate(count, 30, tonumber(cmdArguments[2])) diff --git a/luascripts/commands/admin/listlevels.lua b/luascripts/commands/admin/listlevels.lua index 9018ce4..2d4cb28 100644 --- a/luascripts/commands/admin/listlevels.lua +++ b/luascripts/commands/admin/listlevels.lua @@ -16,12 +16,16 @@ -- along with this program. If not, see . local auth = require "luascripts.wolfadmin.auth.auth" -local util = require "luascripts.wolfadmin.util.util" -local settings = require "luascripts.wolfadmin.util.settings" -local pagination = require "luascripts.wolfadmin.util.pagination" -local db = require "luascripts.wolfadmin.db.db" + local commands = require "luascripts.wolfadmin.commands.commands" -local stats = require "luascripts.wolfadmin.players.stats" + +local db = require "luascripts.wolfadmin.db.db" + +local players = require "luascripts.wolfadmin.players.players" + +local pagination = require "luascripts.wolfadmin.util.pagination" +local settings = require "luascripts.wolfadmin.util.settings" +local util = require "luascripts.wolfadmin.util.util" function commandListLevels(clientId, cmdArguments) if cmdArguments[1] == nil then @@ -74,7 +78,7 @@ function commandListLevels(clientId, cmdArguments) return true end - local player = db.getplayer(stats.get(cmdClient, "playerGUID"))["id"] + local player = db.getplayer(players.getGUID(cmdClient))["id"] local count = db.getlevelscount(player) local limit, offset = pagination.calculate(count, 30, tonumber(cmdArguments[2])) diff --git a/luascripts/commands/admin/listplayers.lua b/luascripts/commands/admin/listplayers.lua index 984d5ae..dddd858 100644 --- a/luascripts/commands/admin/listplayers.lua +++ b/luascripts/commands/admin/listplayers.lua @@ -22,6 +22,7 @@ local commands = require "luascripts.wolfadmin.commands.commands" local game = require "luascripts.wolfadmin.game.game" local fireteams = require "luascripts.wolfadmin.game.fireteams" +local players = require "luascripts.wolfadmin.players.players" local stats = require "luascripts.wolfadmin.players.stats" local constants = require "luascripts.wolfadmin.util.constants" @@ -32,7 +33,7 @@ function commandListPlayers(clientId, cmdArguments) local players = {} for playerId = 0, et.trap_Cvar_Get("sv_maxclients") - 1 do - if wolfa_isPlayer(playerId) then + if players.isConnected(playerId) then table.insert(players, playerId) end end @@ -41,10 +42,10 @@ function commandListPlayers(clientId, cmdArguments) for _, player in pairs(players) do local guidStub - if stats.get(player, "isBot") then + if players.isBot(player) then guidStub = "OMNIBOT-" else - guidStub = stats.get(player, "playerGUID"):sub(-8) + guidStub = players.getGUID(player):sub(-8) end local level = auth.getlevel(player) @@ -74,7 +75,7 @@ function commandListPlayers(clientId, cmdArguments) guidStub, -- guid stub (stats.get(player, "playerMuted") and "M" or ""), -- muted fireteamName, -- fireteam - stats.get(player, "playerName"), -- name + players.getName(player), -- name "", -- alias open "", -- alias "" -- alias close diff --git a/luascripts/commands/admin/slap.lua b/luascripts/commands/admin/slap.lua index 0477366..14de664 100644 --- a/luascripts/commands/admin/slap.lua +++ b/luascripts/commands/admin/slap.lua @@ -16,8 +16,11 @@ -- along with this program. If not, see . local auth = require "luascripts.wolfadmin.auth.auth" + local commands = require "luascripts.wolfadmin.commands.commands" -local stats = require "luascripts.wolfadmin.players.stats" + +local players = require "luascripts.wolfadmin.players.players" + local settings = require "luascripts.wolfadmin.util.settings" function commandSlap(clientId, cmdArguments) @@ -59,7 +62,7 @@ function commandSlap(clientId, cmdArguments) et.gentity_set(cmdClient, "health", newHealth) - et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dslap: ^7"..stats.get(cmdClient, "playerName").." ^9was slapped.\";") + et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dslap: ^7"..players.getName(cmdClient).." ^9was slapped.\";") return true end diff --git a/luascripts/commands/client/ac.lua b/luascripts/commands/client/ac.lua index 15b1fed..1b5da42 100644 --- a/luascripts/commands/client/ac.lua +++ b/luascripts/commands/client/ac.lua @@ -16,8 +16,11 @@ -- along with this program. If not, see . local auth = require "luascripts.wolfadmin.auth.auth" + local commands = require "luascripts.wolfadmin.commands.commands" +local players = require "luascripts.wolfadmin.players.players" + function commandAdminChat(clientId, cmdArguments) if #cmdArguments == 0 then et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^9usage: "..commands.getclient("adminchat")["syntax"].."\";") @@ -30,7 +33,7 @@ function commandAdminChat(clientId, cmdArguments) end for playerId = 0, et.trap_Cvar_Get("sv_maxclients") - 1 do - if wolfa_isPlayer(playerId) and auth.isallowed(playerId, "~") == 1 then + if players.isConnected(playerId) and auth.isallowed(playerId, "~") == 1 then table.insert(recipients, playerId) end end diff --git a/luascripts/commands/commands.lua b/luascripts/commands/commands.lua index 5d5dc80..b4d1705 100644 --- a/luascripts/commands/commands.lua +++ b/luascripts/commands/commands.lua @@ -17,12 +17,16 @@ require "luascripts.wolfadmin.util.debug" +local admin = require "luascripts.wolfadmin.admin.admin" + +local auth = require "luascripts.wolfadmin.auth.auth" + +local players = require "luascripts.wolfadmin.players.players" +local stats = require "luascripts.wolfadmin.players.stats" + local util = require "luascripts.wolfadmin.util.util" local events = require "luascripts.wolfadmin.util.events" local files = require "luascripts.wolfadmin.util.files" -local admin = require "luascripts.wolfadmin.admin.admin" -local auth = require "luascripts.wolfadmin.auth.auth" -local stats = require "luascripts.wolfadmin.players.stats" local commands = {} @@ -116,8 +120,8 @@ function commands.log(clientId, command, cmdArguments) local logLine local levelTime = wolfa_getLevelTime() / 1000 - local clientGUID = clientId and stats.get(clientId, "playerGUID") or "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - local clientName = clientId and stats.get(clientId, "playerName") or "console" + local clientGUID = clientId and players.getGUID(clientId) or "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + local clientName = clientId and players.getName(clientId) or "console" local clientFlags = "" local victimId @@ -140,7 +144,7 @@ function commands.log(clientId, command, cmdArguments) end if victimId then - local victimName = stats.get(victimId, "playerName") + local victimName = players.getName(victimId) logLine = string.format("%3i:%02i: %i: %s: %s: %s: %s: %s: %s: \"%s\"\n", math.floor(levelTime / 60), (levelTime % 60), clientId, clientGUID, clientName, clientFlags, command, victimId, victimName, table.concat(cmdArguments, " ", 2)) else logLine = string.format("%3i:%02i: %i: %s: %s: %s: %s: \"%s\"\n", math.floor(levelTime / 60), (levelTime % 60), clientId, clientGUID, clientName, clientFlags, command, table.concat(cmdArguments, " ")) diff --git a/luascripts/db/mysql.lua b/luascripts/db/mysql.lua index 6bd45a7..de7f837 100644 --- a/luascripts/db/mysql.lua +++ b/luascripts/db/mysql.lua @@ -15,13 +15,13 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +local players = require "luascripts.wolfadmin.players.players" + local constants = require "luascripts.wolfadmin.util.constants" local util = require "luascripts.wolfadmin.util.util" local settings = require "luascripts.wolfadmin.util.settings" local tables = require "luascripts.wolfadmin.util.tables" -local stats = require "luascripts.wolfadmin.players.stats" - local luasql = require "luasql.mysql" local mysql = {} @@ -43,8 +43,8 @@ function mysql.updateplayerlevel(id, level) cur = assert(con:execute("UPDATE `player` SET `level_id`='"..tonumber(level).."' WHERE `id`='"..tonumber(id).."'")) end -function mysql.getplayerid(clientid) - return mysql.getplayer(stats.get(clientid, "playerGUID"))["id"] +function mysql.getplayerid(clientId) + return mysql.getplayer(players.getGUID(clientId))["id"] end function mysql.getplayer(guid) diff --git a/luascripts/db/sqlite3.lua b/luascripts/db/sqlite3.lua index 2be688c..cf6c017 100644 --- a/luascripts/db/sqlite3.lua +++ b/luascripts/db/sqlite3.lua @@ -15,13 +15,13 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +local players = require "luascripts.wolfadmin.players.players" + local constants = require "luascripts.wolfadmin.util.constants" local util = require "luascripts.wolfadmin.util.util" local settings = require "luascripts.wolfadmin.util.settings" local tables = require "luascripts.wolfadmin.util.tables" -local stats = require "luascripts.wolfadmin.players.stats" - local luasql = require "luasql.sqlite3" local sqlite3 = {} @@ -43,8 +43,8 @@ function sqlite3.updateplayerlevel(id, level) cur = assert(con:execute("UPDATE `player` SET `level_id`='"..tonumber(level).."' WHERE `id`='"..tonumber(id).."'")) end -function sqlite3.getplayerid(clientid) - return sqlite3.getplayer(stats.get(clientid, "playerGUID"))["id"] +function sqlite3.getplayerid(clientId) + return sqlite3.getplayer(players.getGUID(clientId))["id"] end function sqlite3.getplayer(guid) diff --git a/luascripts/game/bots.lua b/luascripts/game/bots.lua index e05a098..6b4eeb6 100644 --- a/luascripts/game/bots.lua +++ b/luascripts/game/bots.lua @@ -15,22 +15,19 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . -local util = require "luascripts.wolfadmin.util.util" +local players = require "luascripts.wolfadmin.players.players" + local events = require "luascripts.wolfadmin.util.events" local settings = require "luascripts.wolfadmin.util.settings" -local stats = require "luascripts.wolfadmin.players.stats" +local util = require "luascripts.wolfadmin.util.util" local bots = {} -function bots.is(clientId) - return stats.get(clientId, "isBot") -end - function bots.put(team) local team = util.getTeamCode(team) for playerId = 0, et.trap_Cvar_Get("sv_maxclients") - 1 do - if wolfa_isPlayer(playerId) and bots.is(playerId) then + if players.isConnected(playerId) and players.isBot(playerId) then et.trap_SendConsoleCommand(et.EXEC_APPEND, "!put "..playerId.." "..team..";") end end @@ -51,4 +48,4 @@ function bots.oninit(levelTime, randomSeed, restartMap) end events.handle("onGameInit", bots.oninit) -return bots \ No newline at end of file +return bots diff --git a/luascripts/game/game.lua b/luascripts/game/game.lua index 8018183..091dbfc 100644 --- a/luascripts/game/game.lua +++ b/luascripts/game/game.lua @@ -96,7 +96,7 @@ events.handle("onPlayerDeath", game.ondeath) function game.onrevive(clientMedic, clientVictim) if settings.get("g_announceRevives") ~= 0 then for playerId = 0, et.trap_Cvar_Get("sv_maxclients") - 1 do - if wolfa_isPlayer(playerId) and tonumber(et.gentity_get(playerId, "sess.sessionTeam")) == tonumber(et.gentity_get(clientMedic, "sess.sessionTeam")) then + if players.isConnected(playerId) and tonumber(et.gentity_get(playerId, "sess.sessionTeam")) == tonumber(et.gentity_get(clientMedic, "sess.sessionTeam")) then et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat "..playerId.." \"^drevive: ^7"..et.gentity_get(clientMedic, "pers.netname").." ^9revived ^7"..et.gentity_get(clientVictim, "pers.netname").."^9.\";") end end diff --git a/luascripts/game/sprees.lua b/luascripts/game/sprees.lua index 69ba5af..22509f0 100644 --- a/luascripts/game/sprees.lua +++ b/luascripts/game/sprees.lua @@ -155,7 +155,7 @@ function sprees.ondeath(victimId, killerId, mod) elseif victimId == killerId then -- suicides -- happens when a bot disconnects, it selfkills before leaving, thus emptying the -- player data table, resulting in errors. I'm sorry for your spree records, bots. - if not wolfa_isPlayer(victimId) then return end + if not players.isConnected(victimId) then return end stats.set(victimId, "currentKillSpree", 0) stats.add(victimId, "currentDeathSpree", 1) @@ -163,7 +163,7 @@ function sprees.ondeath(victimId, killerId, mod) stats.set(victimId, "longestDeathSpree", stats.get(victimId, "currentDeathSpree") > stats.get(victimId, "longestDeathSpree") and stats.get(victimId, "currentDeathSpree") or stats.get(victimId, "longestDeathSpree")) - if (settings.get("g_botRecords") == 1 or not stats.get(victimId, "isBot")) and (not currentRecords["dsrecord"] or stats.get(victimId, "longestDeathSpree") > currentRecords["dsrecord"]) then + if (settings.get("g_botRecords") == 1 or not players.isBot(victimId)) and (not currentRecords["dsrecord"] or stats.get(victimId, "longestDeathSpree") > currentRecords["dsrecord"]) then currentRecords["dsplayer"] = db.getplayerid(victimId) currentRecords["dsrecord"] = stats.get(victimId, "longestDeathSpree") end @@ -176,14 +176,14 @@ function sprees.ondeath(victimId, killerId, mod) stats.set(killerId, "longestKillSpree", stats.get(killerId, "currentKillSpree") > stats.get(killerId, "longestKillSpree") and stats.get(killerId, "currentKillSpree") or stats.get(killerId, "longestKillSpree")) - if (settings.get("g_botRecords") == 1 or not stats.get(killerId, "isBot")) and (not currentRecords["ksrecord"] or stats.get(killerId, "longestKillSpree") > currentRecords["ksrecord"]) then + if (settings.get("g_botRecords") == 1 or not players.isBot(killerId)) and (not currentRecords["ksrecord"] or stats.get(killerId, "longestKillSpree") > currentRecords["ksrecord"]) then currentRecords["ksplayer"] = db.getplayerid(killerId) currentRecords["ksrecord"] = stats.get(killerId, "longestKillSpree") end -- happens when a bot disconnects, it selfkills before leaving, thus emptying the -- player data table, resulting in errors. I'm sorry for your spree records, bots. - if not wolfa_isPlayer(victimId) then return end + if not players.isConnected(victimId) then return end stats.set(victimId, "currentKillSpree", 0) stats.add(victimId, "currentDeathSpree", 1) @@ -191,7 +191,7 @@ function sprees.ondeath(victimId, killerId, mod) stats.set(victimId, "longestDeathSpree", stats.get(victimId, "currentDeathSpree") > stats.get(victimId, "longestDeathSpree") and stats.get(victimId, "currentDeathSpree") or stats.get(victimId, "longestDeathSpree")) - if (settings.get("g_botRecords") == 1 or not stats.get(victimId, "isBot")) and (not currentRecords["dsrecord"] or stats.get(victimId, "longestDeathSpree") > currentRecords["dsrecord"]) then + if (settings.get("g_botRecords") == 1 or not players.isBot(victimId)) and (not currentRecords["dsrecord"] or stats.get(victimId, "longestDeathSpree") > currentRecords["dsrecord"]) then currentRecords["dsplayer"] = db.getplayerid(victimId) currentRecords["dsrecord"] = stats.get(victimId, "longestDeathSpree") end @@ -204,13 +204,13 @@ function sprees.onrevive(clientMedic, clientVictim) stats.set(clientMedic, "longestReviveSpree", stats.get(clientMedic, "currentReviveSpree") > stats.get(clientMedic, "longestReviveSpree") and stats.get(clientMedic, "currentReviveSpree") or stats.get(clientMedic, "longestReviveSpree")) if revivespreeMessages[stats.get(clientMedic, "currentReviveSpree")] then - et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^1REVIVE SPREE! ^*"..stats.get(clientMedic, "playerName").." ^*"..revivespreeMessages[stats.get(clientMedic, "currentReviveSpree")]["msg"].." ^d(^3"..stats.get(clientMedic, "currentReviveSpree").." ^drevives in a row!)\";") + et.trap_SendConsoleCommand(et.EXEC_APPEND, "chat \"^1REVIVE SPREE! ^*"..players.getName(clientMedic).." ^*"..revivespreeMessages[stats.get(clientMedic, "currentReviveSpree")]["msg"].." ^d(^3"..stats.get(clientMedic, "currentReviveSpree").." ^drevives in a row!)\";") end - if (settings.get("g_botRecords") == 1 or not stats.get(clientMedic, "isBot")) and (not currentRecords["rsrecord"] or stats.get(clientMedic, "longestReviveSpree") > currentRecords["rsrecord"]) then + if (settings.get("g_botRecords") == 1 or not players.isBot(clientMedic)) and (not currentRecords["rsrecord"] or stats.get(clientMedic, "longestReviveSpree") > currentRecords["rsrecord"]) then currentRecords["rsplayer"] = db.getplayerid(clientMedic) currentRecords["rsrecord"] = stats.get(clientMedic, "longestReviveSpree") end end -return sprees \ No newline at end of file +return sprees diff --git a/luascripts/players/greetings.lua b/luascripts/players/greetings.lua index 333a00e..29f7a93 100644 --- a/luascripts/players/greetings.lua +++ b/luascripts/players/greetings.lua @@ -17,14 +17,14 @@ local auth = require "luascripts.wolfadmin.auth.auth" +local players = require "luascripts.wolfadmin.players.players" + local constants = require "luascripts.wolfadmin.util.constants" local util = require "luascripts.wolfadmin.util.util" local events = require "luascripts.wolfadmin.util.events" local settings = require "luascripts.wolfadmin.util.settings" local files = require "luascripts.wolfadmin.util.files" -local stats = require "luascripts.wolfadmin.players.stats" - local greetings = {} local userGreetings = {} @@ -34,8 +34,8 @@ function greetings.get(clientId) local lvl = auth.getlevel(clientId) if auth.isallowed(clientId, auth.PERM_INCOGNITO) ~= 1 then - if userGreetings[stats.get(clientId, "playerGUID")] ~= nil then - return userGreetings[stats.get(clientId, "playerGUID")] + if userGreetings[players.getGUID(clientId)] ~= nil then + return userGreetings[players.getGUID(clientId)] elseif levelGreetings[lvl] ~= nil then return levelGreetings[lvl] end @@ -113,7 +113,7 @@ end events.handle("onGameInit", greetings.oninit) function greetings.onready(clientId, firstTime) - if firstTime and (not stats.get(clientId, "isBot") or settings.get("g_botGreetings") == 1) then + if firstTime and (not players.isBot(clientId) or settings.get("g_botGreetings") == 1) then greetings.show(clientId) end end diff --git a/luascripts/players/players.lua b/luascripts/players/players.lua index ed266f2..0a10ab4 100644 --- a/luascripts/players/players.lua +++ b/luascripts/players/players.lua @@ -17,34 +17,87 @@ local db = require "luascripts.wolfadmin.db.db" -local stats = require "luascripts.wolfadmin.players.stats" - local events = require "luascripts.wolfadmin.util.events" local players = {} +local data = {} + +function players.isConnected(clientId) + return (data[clientId] ~= nil) +end + +function players.getCachedName(clientId) + return data[clientId]["name"] +end + +function players.getName(clientId) + return et.gentity_get(clientId, "pers.netname") +end + +function players.getGUID(clientId) + return data[clientId]["guid"] +end + +function players.getIP(clientId) + return data[clientId]["ip"] +end + +function players.isBot(clientId) + return data[clientId]["bot"] +end + +function players.setPlayerMuted(clientId, state, type, duration) + data[clientId]["mute"] = state + + if state == true then + data[clientId]["mutetype"] = type + data[clientId]["muteduration"] = duration + end +end + +function players.isPlayerMuted(clientId) + return data[clientId]["mute"] +end + +function players.getPlayerMuteType(clientId, state, type, duration) + return data[clientId]["mutetype"] +end + +function players.getPlayerMuteDuration(clientId, state, type, duration) + return data[clientId]["muteduration"] +end + +function players.setPlayerTeamLocked(clientId, state) + data[clientId]["teamlock"] = state +end + +function players.isPlayerTeamLocked(clientId) + return data[clientId]["teamlock"] +end + function players.onconnect(clientId, firstTime, isBot) local clientInfo = et.trap_GetUserinfo(clientId) -- name is NOT yet set in pers.netname, so get all info out of infostring - stats.set(clientId, "playerName", et.Info_ValueForKey(clientInfo, "name")) - stats.set(clientId, "playerGUID", et.Info_ValueForKey(clientInfo, "cl_guid")) - stats.set(clientId, "playerIP", string.gsub(et.Info_ValueForKey(clientInfo, "ip"), ":%d*", "")) - stats.set(clientId, "playerTeam", tonumber(et.gentity_get(clientId, "sess.sessionTeam"))) - stats.set(clientId, "isBot", isBot) + data[clientId] = {} + + -- data[clientId]["name"] is cached version for detecting namechanges, do not + -- use it to retrieve a player's name + data[clientId]["name"] = et.Info_ValueForKey(clientInfo, "name") + data[clientId]["guid"] = et.Info_ValueForKey(clientInfo, "cl_guid") + data[clientId]["ip"] = string.gsub(et.Info_ValueForKey(clientInfo, "ip"), ":%d*", "") + data[clientId]["bot"] = isBot + data[clientId]["team"] = tonumber(et.gentity_get(clientId, "sess.sessionTeam")) if firstTime then - stats.set(clientId, "newConnection", true) + data[clientId]["new"] = true if db.isconnected() then - local player = db.getplayer(stats.get(clientId, "playerGUID")) - local name = stats.get(clientId, "playerName") + local player = db.getplayer(data[clientId]["guid"]) if player then - local guid = stats.get(clientId, "playerGUID") - local ip = stats.get(clientId, "playerIP") - - db.updateplayerip(guid, ip) + db.updateplayerip(data[clientId]["guid"], data[clientId]["ip"]) local alias = db.getaliasbyname(player["id"], name) @@ -54,12 +107,9 @@ function players.onconnect(clientId, firstTime, isBot) db.addalias(playerid, name, os.time()) end else - local guid = stats.get(clientId, "playerGUID") - local ip = stats.get(clientId, "playerIP") + db.addplayer(data[clientId]["guid"], data[clientId]["ip"]) - db.addplayer(guid, ip) - - local player = db.getplayer(stats.get(clientId, "playerGUID")) + local player = db.getplayer(data[clientId]["guid"]) db.addalias(player["id"], name, os.time()) end end @@ -74,9 +124,9 @@ function players.onbegin(clientId) -- less coupling between main.lua and stats.lua) -- ensures that all data is loaded from this moment on - events.trigger("onPlayerReady", clientId, stats.get(clientId, "newConnection")) + events.trigger("onPlayerReady", clientId, data[clientId]["new"]) - stats.set(clientId, "newConnection", false) + data[clientId]["new"] = false end events.handle("onClientBegin", players.onbegin) @@ -90,9 +140,11 @@ function players.onnamechange(clientId, old, new) -- known: old NQ versions, Legacy et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay -1 \""..old.." ^7is now known as "..new.."\";") + data[clientId]["name"] = new + if db.isconnected() then - local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"] - local name = stats.get(clientId, "playerName") + local playerid = db.getplayer(players.getGUID(clientId))["id"] + local name = players.getName(clientId) local alias = db.getaliasbyname(playerid, name) if alias then @@ -106,11 +158,11 @@ events.handle("onClientNameChange", players.onnamechange) function players.oninfochange(clientId) local clientInfo = et.trap_GetUserinfo(clientId) - local old = stats.get(clientId, "playerTeam") + local old = data[clientId]["team"] local new = tonumber(et.gentity_get(clientId, "sess.sessionTeam")) if new ~= old then - stats.set(clientId, "playerTeam", new) + data[clientId]["team"] = new events.trigger("onClientTeamChange", clientId, old, new) end diff --git a/luascripts/players/stats.lua b/luascripts/players/stats.lua index 110f046..6f75a49 100644 --- a/luascripts/players/stats.lua +++ b/luascripts/players/stats.lua @@ -15,21 +15,16 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +local players = require "luascripts.wolfadmin.players.players" + local stats = {} -local data = {[-1337] = {["playerGUID"] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}} - --- TODO: need to check this in stat functions, apparently goes wrong -function wolfa_isPlayer(clientId) - if data[clientId] then - return true - end - - return false -end +local data = {} function stats.get(clientId, statKey) - -- if not wolfa_isPlayer(clientId) then return false end + if not players.isConnected(clientId) then + error("client "..clientId.." is not connected") + end if statKey and type(statKey) == "string" and data[clientId] then return data[clientId][statKey] @@ -39,7 +34,9 @@ function stats.get(clientId, statKey) end function stats.set(clientId, statKey, statValue) - -- if not wolfa_isPlayer(clientId) then return false end + if not players.isConnected(clientId) then + error("client "..clientId.." is not connected") + end if not data[clientId] then data[clientId] = {} end @@ -52,20 +49,22 @@ function stats.set(clientId, statKey, statValue) return false end -function stats.add(clientId, statKey, statAdd) -- alias +function stats.add(clientId, statKey, statAdd) statAdd = statAdd and statAdd or 1 return stats.set(clientId, statKey, stats.get(clientId, statKey) + statAdd) end -function stats.take(clientId, statKey, statTake) -- alias +function stats.take(clientId, statKey, statTake) statTake = statTake and statTake or 1 return stats.set(clientId, statKey, stats.get(clientId, statKey) - statTake) end function stats.remove(clientId) - -- if not wolfa_isPlayer(clientId) then return false end + if not players.isConnected(clientId) then + error("client "..clientId.." is not connected") + end data[clientId] = nil