Added tracking of teams in teams module

This commit is contained in:
Timo Smit 2016-02-24 11:46:58 +01:00
parent 9ab81bd4ac
commit a1910edb28
5 changed files with 128 additions and 0 deletions

72
luascripts/game/teams.lua Normal file
View file

@ -0,0 +1,72 @@
-- 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 constants = require "luascripts.wolfadmin.util.constants"
local tables = require "luascripts.wolfadmin.util.tables"
local events = require "luascripts.wolfadmin.util.events"
local teams = {}
local data = {
[constants.TEAM_AXIS] = {},
[constants.TEAM_ALLIES] = {},
[constants.TEAM_SPECTATORS] = {}
}
function teams.get()
return data
end
function teams.count(team)
return #data[team]
end
function teams.difference()
return math.abs(teams.count(constants.TEAM_AXIS) - teams.count(constants.TEAM_ALLIES))
end
function teams.onconnect(clientId, firstTime, isBot)
local team = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))
if not tables.contains(data[team], clientId) then
table.insert(data[team], clientId)
end
end
events.handle("onClientConnect", teams.onconnect)
function teams.ondisconnect(clientId)
local team = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))
local idx = tables.find(data[team], clientId)
if idx then
table.remove(data[team], idx)
end
end
events.handle("onClientDisconnect", teams.ondisconnect)
function teams.onclientteamchange(clientId, old, new)
local idx = tables.find(data[old], clientId)
if idx then
table.remove(data[old], idx)
end
table.insert(data[new], clientId)
end
events.handle("onClientTeamChange", teams.onclientteamchange)
return teams

View file

@ -35,6 +35,7 @@ local commands = require "luascripts.wolfadmin.commands.commands"
local game = require "luascripts.wolfadmin.game.game"
local bots = require "luascripts.wolfadmin.game.bots"
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"

View file

@ -81,6 +81,7 @@ function stats.onconnect(clientId, firstTime, isBot)
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
@ -94,4 +95,17 @@ function stats.ondisconnect(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

View file

@ -110,6 +110,7 @@ events.add("onClientBegin")
events.add("onClientCommand")
events.add("onClientInfoChange")
events.add("onClientNameChange")
events.add("onClientTeamChange")
events.add("onGameInit")
events.add("onGameStateChange")

View file

@ -0,0 +1,40 @@
-- 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 tables = {}
function tables.contains(table, needle)
for key, value in pairs(table) do
if value == needle then
return true
end
end
return false
end
function tables.find(table, needle)
for key, value in pairs(table) do
if value == needle then
return key
end
end
return nil
end
return tables