local event = require('event')
local this = {}
this.clients = {}
event.extend(this)
this.CHAT = 'chat'
this.CPM = 'cpm'
this.PRINT = 'print'
this.CP = 'cp'
this.SC = 'sc'
this.B_CHAT = 8
this.B_POPUP = 16
this.B_CP = 32
this.B_CONSOLE = 64
this.B_TOP = 128
function this.find(term, allowNum)
if type(term) == 'string' and string.len(term) == 0 then
return {}
end
if allowNum == true and (type(term) == 'number' or string.len(term) < 3) then
local num = tonumber(term)
if num ~= nil and num >= 0 and num <= 63 then
if this.clients[num] ~= nil then
local result = {[num] = this.clients[num]}
table.setn(result, 1)
return result
end
return {}
end
end
term = string.escape_pattern(string.lower(et.Q_CleanStr(term)))
local result = {}
local n = 0
for i, client in pairs(this.clients) do
if string.find(string.lower(client.name_clean), term) ~= nil then
result[i] = client
n = n + 1
end
end
table.setn(result, n)
return result
end
function this.find_one(term)
if type(term) == 'number' or string.len(term) < 3 then
term = tonumber(term)
if term == nil or term < 0 or term > 63 or this.clients[term] == nil then
return nil, 0
end
return this.clients[term], 1
end
local clients = this.find(term)
if table.getn(clients) ~= 1 then
return nil, table.getn(clients)
end
for _, client in pairs(clients) do
return client, 1
end
end
function this.command(command)
et.trap_SendServerCommand(-1, command)
end
function this.print(where, message)
this.command(where .. ' ' .. '"' .. message .. '"')
end
function this.banner(position, message)
this.command('b ' .. position .. ' ' .. '"' .. message .. '"')
end
function this.h_connect(num, firstTime)
local client = {
num = num,
ent = {},
}
setmetatable(client.ent, {
__index = function(_, k)
return client.entity_get(k)
end,
__newindex = function(_, k, v)
return client.entity_set(k, v)
end,
})
function client.entity_get(key, index)
return et.gentity_get(client.num, key, index)
end
function client.entity_set(key, index, value)
et.gentity_set(client.num, key, index, value)
end
function client.command(command)
et.trap_SendServerCommand(client.num, command)
end
function client.print(where, message)
client.command(where .. ' ' .. '"' .. message .. '"')
end
function client.banner(position, message)
client.command('b ' .. position .. ' ' .. '"' .. message .. '"')
end
table.insert(this.clients, num, client)
this.h_userinfo(num)
if this.emit('connect', client, firstTime) == false then
this.clients[num] = nil
return false
end
end
function this.h_disconnect(num)
this.emit('disconnect', this.clients[num])
table.remove(this.clients, num)
end
function this.h_begin(num)
this.h_userinfo(num, true)
this.emit('begin', this.clients[num])
end
function this.h_userinfo(num, partial)
local client = this.clients[num]
client.class = client.ent['sess.playerType']
client.team = client.ent['sess.sessionTeam']
if partial ~= true then
local userinfo = et.trap_GetUserinfo(num)
client.guid = et.Info_ValueForKey(userinfo, 'cl_guid')
client.ip = string.gsub(et.Info_ValueForKey(userinfo, 'ip'), ':[0-9]+$', '')
client.name = et.Info_ValueForKey(userinfo, 'name')
client.name_clean = et.Q_CleanStr(client.name)
this.emit('userinfo', this.clients[num])
end
end
function this.h_spawn(num, revived)
this.emit('spawn', this.clients[num], revived)
end
function this.h_command(num, command, ...)
return this.emit('command', this.clients[num], command, unpack(arg))
end
return this