Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Decoupled player handling from admin and stats module (issue #63)
  • Loading branch information
Timo Smit committed Sep 4, 2016
1 parent 06d4413 commit 441e4cf
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 133 deletions.
94 changes: 13 additions & 81 deletions luascripts/admin/admin.lua
Expand Up @@ -20,6 +20,8 @@ local events = require "luascripts.wolfadmin.util.events"
local settings = require "luascripts.wolfadmin.util.settings"
local files = require "luascripts.wolfadmin.util.files"
local db = require "luascripts.wolfadmin.db.db"

local players = require "luascripts.wolfadmin.players.players"
local stats = require "luascripts.wolfadmin.players.stats"

local admin = {}
Expand Down Expand Up @@ -74,100 +76,30 @@ function admin.unlockPlayer(clientId)
stats.set(clientId, "teamLock", false)
end

function admin.updatePlayer(clientId)
local player = db.getplayer(stats.get(clientId, "playerGUID"))

if player then
local guid = stats.get(clientId, "playerGUID")
local ip = stats.get(clientId, "playerIP")

db.updateplayer(guid, ip)
else
local guid = stats.get(clientId, "playerGUID")
local ip = stats.get(clientId, "playerIP")

db.addplayer(guid, ip)
admin.setPlayerLevel(clientId, et.G_shrubbot_level(clientId), 1)
end
end

function admin.updateAlias(clientId)
local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"]
local name = stats.get(clientId, "playerName")
local alias = db.getaliasbyname(playerid, name)

if alias then
db.updatealias(alias["id"], os.time())
if alias["cleanalias"] == "" then
db.updatecleanalias(alias["id"], name)
end
else
db.addalias(playerid, name, os.time())
end
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"]

db.addsetlevel(playerid, level, adminid, os.time())
end

function admin.onconnect(clientId, firstTime, isBot)
-- only increase the counter on first connection (fixes counter increase on
-- clientbegin which is also triggered on warmup/maprestart/etc)
stats.set(clientId, "namechangeStart", os.time())
stats.set(clientId, "namechangePts", 0)

function admin.onconnectattempt(clientId, firstTime, isBot)
if firstTime then
if stats.get(clientId, "playerGUID") == "NO_GUID" or stats.get(clientId, "playerGUID") == "unknown" then
return "\n\nIt appears you do not have a ^7GUID^9/^7etkey^9. In order to play on this server, enable ^7PunkBuster ^9(use ^7\\pb_cl_enable^9) ^9and/or create an ^7etkey^9.\n\nMore info: ^7www.etkey.org"
end

if db.isconnected() then
admin.updatePlayer(clientId)
admin.updateAlias(clientId)
end
end

events.trigger("onClientConnect", clientId, firstTime, isBot)
end
events.handle("onClientConnect", admin.onconnect)
events.handle("onClientConnectAttempt", admin.onconnectattempt)

function stats.oninfochange(clientId)
local clientInfo = et.trap_GetUserinfo(clientId)
local old = stats.get(clientId, "playerName")
local new = et.Info_ValueForKey(clientInfo, "name")

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 stats.get(clientId, "namechangeForce") then
stats.set(clientId, "namechangeForce", true)

clientInfo = et.Info_SetValueForKey(clientInfo, "name", old)
et.trap_SetUserinfo(clientId, clientInfo)
et.ClientUserinfoChanged(clientId)

stats.set(clientId, "namechangeForce", false)

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)
end

stats.add(clientId, "namechangePts", 1)

et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay -1 \""..old.." ^7is now known as "..new.."\";")

if db.isconnected() then
admin.updateAlias(clientId)
end

events.trigger("onClientNameChange", clientId, old, new)
end
end
function admin.onconnect(clientId, firstTime, isBot)
-- only increase the counter on first connection (fixes counter increase on
-- clientbegin which is also triggered on warmup/maprestart/etc)
stats.set(clientId, "namechangeStart", os.time())
stats.set(clientId, "namechangePts", 0)
end
events.handle("onClientInfoChange", stats.oninfochange)
events.handle("onClientConnect", admin.onconnect)

return admin
return admin
6 changes: 3 additions & 3 deletions luascripts/game/game.lua
Expand Up @@ -104,11 +104,11 @@ function game.onrevive(clientMedic, clientVictim)
end
events.handle("onPlayerRevive", game.onrevive)

function game.onbegin(clientId, firstTime)
function game.onready(clientId, firstTime)
if firstTime and settings.get("g_welcomeMessage") ~= "" then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat "..clientId.." \""..settings.get("g_welcomeMessage").."\";")
end
end
events.handle("onClientBegin", game.onbegin)
events.handle("onPlayerReady", game.onready)

return game
return game
13 changes: 4 additions & 9 deletions luascripts/main.lua
Expand Up @@ -38,8 +38,9 @@ local sprees = require "luascripts.wolfadmin.game.sprees"
local teams = require "luascripts.wolfadmin.game.teams"
local voting = require "luascripts.wolfadmin.game.voting"

local stats = require "luascripts.wolfadmin.players.stats"
local greetings = require "luascripts.wolfadmin.players.greetings"
local players = require "luascripts.wolfadmin.players.players"
local stats = require "luascripts.wolfadmin.players.stats"

local version = "1.1.0-beta"
local release = "26 August 2016"
Expand Down Expand Up @@ -93,17 +94,11 @@ function et_ConsoleCommand(cmdText)
end

function et_ClientConnect(clientId, firstTime, isBot)
if firstTime == 1 then
stats.set(clientId, "newConnection", true)
end

return events.trigger("onClientConnect", clientId, (firstTime == 1), (isBot == 1))
return events.trigger("onClientConnectAttempt", clientId, (firstTime == 1), (isBot == 1))
end

function et_ClientBegin(clientId)
events.trigger("onClientBegin", clientId, stats.get(clientId, "newConnection"))

stats.set(clientId, "newConnection", false)
events.trigger("onClientBegin", clientId)
end

function et_ClientDisconnect(clientId)
Expand Down
4 changes: 2 additions & 2 deletions luascripts/players/greetings.lua
Expand Up @@ -107,12 +107,12 @@ function greetings.oninit(levelTime, randomSeed, restartMap)
if settings.get("g_fileGreetings") ~= "" then
greetings.load()

events.handle("onClientBegin", greetings.onbegin)
events.handle("onPlayerReady", greetings.onready)
end
end
events.handle("onGameInit", greetings.oninit)

function greetings.onbegin(clientId, firstTime)
function greetings.onready(clientId, firstTime)
if firstTime and (not stats.get(clientId, "isBot") or settings.get("g_botGreetings") == 1) then
greetings.show(clientId)
end
Expand Down
147 changes: 147 additions & 0 deletions luascripts/players/players.lua
@@ -0,0 +1,147 @@

-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2016 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 <http://www.gnu.org/licenses/>.

local stats = require "luascripts.wolfadmin.players.stats"

local events = require "luascripts.wolfadmin.util.events"

local players = {}

function players.updatePlayer(clientId)
local player = db.getplayer(stats.get(clientId, "playerGUID"))

if player then
local guid = stats.get(clientId, "playerGUID")
local ip = stats.get(clientId, "playerIP")

db.updateplayer(guid, ip)
else
local guid = stats.get(clientId, "playerGUID")
local ip = stats.get(clientId, "playerIP")

db.addplayer(guid, ip)
-- admin.setPlayerLevel(clientId, et.G_shrubbot_level(clientId), 1)
end
end

function players.updateAlias(clientId)
local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"]
local name = stats.get(clientId, "playerName")
local alias = db.getaliasbyname(playerid, name)

if alias then
db.updatealias(alias["id"], os.time())
if alias["cleanalias"] == "" then
db.updatecleanalias(alias["id"], name)
end
else
db.addalias(playerid, name, os.time())
end
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)

if firstTime == 1 then
stats.set(clientId, "newConnection", true)

if db.isconnected() then
players.updatePlayer(clientId)
players.updateAlias(clientId)
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.trigger("onPlayerReady", clientId, stats.get(clientId, "newConnection"))

stats.set(clientId, "newConnection", false)
end
events.handle("onClientBegin", players.onbegin)

function players.ondisconnect(clientId)
stats.remove(clientId)
end
events.handle("onClientDisconnect", players.ondisconnect)

-- TODO: split into admin-side and player-side event?
function players.oninfochange(clientId)
local clientInfo = et.trap_GetUserinfo(clientId)
local old = stats.get(clientId, "playerName")
local new = et.Info_ValueForKey(clientInfo, "name")

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 stats.get(clientId, "namechangeForce") then
stats.set(clientId, "namechangeForce", true)

clientInfo = et.Info_SetValueForKey(clientInfo, "name", old)
et.trap_SetUserinfo(clientId, clientInfo)
et.ClientUserinfoChanged(clientId)

stats.set(clientId, "namechangeForce", false)

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)
end

stats.add(clientId, "namechangePts", 1)

et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay -1 \""..old.." ^7is now known as "..new.."\";")

if db.isconnected() then
players.updateAlias(clientId)
end

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

function players.onteamchange(clientId)
local clientInfo = et.trap_GetUserinfo(clientId)
local old = stats.get(clientId, "playerTeam")
local new = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))

if new ~= old then
stats.set(clientId, "playerTeam", new)

events.trigger("onClientTeamChange", clientId, old, new)
end
end
events.handle("onClientInfoChange", players.onteamchange)

return players
38 changes: 1 addition & 37 deletions luascripts/players/stats.lua
Expand Up @@ -15,8 +15,6 @@
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.

local events = require "luascripts.wolfadmin.util.events"

local stats = {}

local data = {[-1337] = {["playerGUID"] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}}
Expand Down Expand Up @@ -74,38 +72,4 @@ function stats.remove(clientId)
return true
end

function stats.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)

if firstTime then
stats.set(clientId, "voiceMute", false)
end
end
events.handle("onClientConnect", stats.onconnect)

function stats.ondisconnect(clientId)
stats.remove(clientId)
end
events.handle("onClientDisconnect", stats.ondisconnect)

function stats.onteamchange(clientId)
local clientInfo = et.trap_GetUserinfo(clientId)
local old = stats.get(clientId, "playerTeam")
local new = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))

if new ~= old then
stats.set(clientId, "playerTeam", new)

events.trigger("onClientTeamChange", clientId, old, new)
end
end
events.handle("onClientInfoChange", stats.onteamchange)

return stats
return stats

0 comments on commit 441e4cf

Please sign in to comment.