Reworked commands.lua

* improved documentation
* removed default W:ET client commands, moved to separate files
This commit is contained in:
Timo Smit 2017-01-21 11:57:14 +01:00
parent 86a427964d
commit 004ebea736
5 changed files with 152 additions and 81 deletions

View File

@ -22,6 +22,7 @@ local auth = require (wolfa_getLuaPath()..".auth.auth")
local shrubbot = {} local shrubbot = {}
-- available shrubflags: lqyFHY
local flags = { local flags = {
[auth.PERM_ADMINTEST] = "a", [auth.PERM_ADMINTEST] = "a",
[auth.PERM_HELP] = "h", [auth.PERM_HELP] = "h",

View File

@ -0,0 +1,28 @@
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2017 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 commands = require (wolfa_getLuaPath()..".commands.commands")
function commandCallVote(clientId, cmdArguments)
local voteArguments = {}
for i = 2, et.trap_Argc() - 1 do
voteArguments[(i - 1)] = et.trap_Argv(i)
end
return events.trigger("onCallvote", clientId, et.trap_Argv(1), voteArguments)
end
commands.addclient("callvote", commandCallVote, "", "", false)

View File

@ -0,0 +1,42 @@
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2017 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 commands = require (wolfa_getLuaPath()..".commands.commands")
local players = require (wolfa_getLuaPath()..".players.players")
function commandSay(clientId, cmdArguments)
if players.isMuted(clientId, players.MUTE_CHAT) then
et.trap_SendServerCommand(clientId, "cp \"^1You are muted\"")
return true
end
end
commands.addclient("say", commandSay, "", "", false)
commands.addclient("say_team", commandSay, "", "", false)
commands.addclient("say_teamnl", commandSay, "", "", false)
commands.addclient("say_buddy", commandSay, "", "", false)
function commandVoiceSay(clientId, cmdArguments)
if players.isMuted(clientId, players.MUTE_VOICE) then
et.trap_SendServerCommand(clientId, "cp \"^1You are voicemuted\"")
return true
end
end
commands.addclient("vsay", commandVoiceSay, "", "", false)
commands.addclient("vsay_team", commandVoiceSay, "", "", false)

View File

@ -0,0 +1,43 @@
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2017 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 commands = require (wolfa_getLuaPath()..".commands.commands")
local players = require (wolfa_getLuaPath()..".players.players")
function commandTeam(clientId, cmdArguments)
if players.isTeamLocked(clientId) then
local clientTeam = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))
local teamName = util.getTeamName(clientTeam)
local teamColor = util.getTeamColor(clientTeam)
et.trap_SendServerCommand(clientId, "cp \"^7You are locked to the "..teamColor..teamName.." ^7team")
return true
end
local team = util.getTeamFromCode(et.trap_Argv(1))
if teams.isLocked(team) then
local teamName = util.getTeamName(team)
local teamColor = util.getTeamColor(team)
et.trap_SendServerCommand(clientId, "cp \""..teamColor..teamName.." ^7team is locked")
return true
end
end
commands.addclient("team", commandTeam, "", "", false)

View File

@ -30,7 +30,6 @@ local settings = require (wolfa_getLuaPath()..".util.settings")
local commands = {} local commands = {}
-- available shrubflags: lqyFHY
local clientcmds = {} local clientcmds = {}
local servercmds = {} local servercmds = {}
local admincmds = {} local admincmds = {}
@ -84,7 +83,7 @@ function commands.addadmin(command, func, flag, help, syntax, hidden)
} }
end end
function commands.loadfiles(dir) function commands.loadFiles(dir)
local amount = 0 local amount = 0
local files = files.ls("commands/"..dir.."/") local files = files.ls("commands/"..dir.."/")
@ -102,9 +101,9 @@ end
function commands.load() function commands.load()
local functionStart = et.trap_Milliseconds() local functionStart = et.trap_Milliseconds()
local clientAmount = commands.loadfiles("client") local clientAmount = commands.loadFiles("client")
local serverAmount = commands.loadfiles("server") local serverAmount = commands.loadFiles("server")
local adminAmount = commands.loadfiles("admin") local adminAmount = commands.loadFiles("admin")
local totalAmount = clientAmount + serverAmount + adminAmount local totalAmount = clientAmount + serverAmount + adminAmount
@ -164,13 +163,13 @@ function commands.log(clientId, command, cmdArguments)
et.trap_FS_FCloseFile(fileDescriptor) et.trap_FS_FCloseFile(fileDescriptor)
end end
function commands.oninit() function commands.onGameInit()
commands.load() commands.load()
end end
events.handle("onGameInit", commands.oninit) events.handle("onGameInit", commands.onGameInit)
function commands.onservercommand(cmdText) function commands.onServerCommand(cmdText)
local wolfCmd = string.lower(et.trap_Argv(0)) local wolfCmd = string.lower(cmdText)
local cmdArguments = {} local cmdArguments = {}
if servercmds[wolfCmd] and servercmds[wolfCmd]["function"] then if servercmds[wolfCmd] and servercmds[wolfCmd]["function"] then
@ -180,8 +179,7 @@ function commands.onservercommand(cmdText)
return servercmds[wolfCmd]["function"](clientId, cmdArguments) and 1 or 0 return servercmds[wolfCmd]["function"](clientId, cmdArguments) and 1 or 0
end end
-- TODO: merge with commands.onclientcommand
local shrubCmd = cmdText local shrubCmd = cmdText
if string.find(cmdText, "!") == 1 then if string.find(cmdText, "!") == 1 then
@ -192,85 +190,48 @@ function commands.onservercommand(cmdText)
for i = 1, et.trap_Argc() - 1 do for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i) cmdArguments[i] = et.trap_Argv(i)
end end
admincmds[shrubCmd]["function"](-1337, cmdArguments)
if not admincmds[shrubCmd]["hidden"] then if not admincmds[shrubCmd]["hidden"] then
commands.log(-1337, shrubCmd, cmdArguments) commands.log(-1337, shrubCmd, cmdArguments)
end end
admincmds[shrubCmd]["function"](-1337, cmdArguments)
end end
end end
events.handle("onServerCommand", commands.onservercommand) events.handle("onServerCommand", commands.onServerCommand)
function commands.onclientcommand(clientId, cmdText) function commands.onClientCommand(clientId, cmdText)
local wolfCmd = string.lower(et.trap_Argv(0)) local wolfCmd = string.lower(cmdText)
local cmdArguments = {} local cmdArguments = {}
-- mod-specific or custom commands loading -- mod-specific or custom commands loading
-- syntax: command arg1 arg2 ... argN
if clientcmds[wolfCmd] and clientcmds[wolfCmd]["function"] and clientcmds[wolfCmd]["flag"] then if clientcmds[wolfCmd] and clientcmds[wolfCmd]["function"] and clientcmds[wolfCmd]["flag"] then
if clientcmds[wolfCmd]["flag"] == "" or auth.isallowed(clientId, clientcmds[wolfCmd]["flag"]) == 1 then if clientcmds[wolfCmd]["flag"] == "" or auth.isallowed(clientId, clientcmds[wolfCmd]["flag"]) == 1 then
for i = 1, et.trap_Argc() - 1 do for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i) cmdArguments[i] = et.trap_Argv(i)
end end
return clientcmds[wolfCmd]["function"](clientId, cmdArguments) and 1 or 0 local isFinished = clientcmds[wolfCmd]["function"](clientId, cmdArguments)
if isFinished ~= nil then
return isFinished and 1 or 0
end
end end
end end
-- all Wolfenstein-related commands defined separately for now
if wolfCmd == "team" then
if players.isTeamLocked(clientId) then
local clientTeam = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))
local teamName = util.getTeamName(clientTeam)
local teamColor = util.getTeamColor(clientTeam)
et.trap_SendServerCommand(clientId, "cp \"^7You are locked to the "..teamColor..teamName.." ^7team")
return 1
end
local team = util.getTeamFromCode(et.trap_Argv(1))
if teams.isLocked(team) then
local teamName = util.getTeamName(team)
local teamColor = util.getTeamColor(team)
et.trap_SendServerCommand(clientId, "cp \""..teamColor..teamName.." ^7team is locked")
return 1
end
elseif wolfCmd == "callvote" then
local voteArguments = {}
for i = 2, et.trap_Argc() - 1 do
voteArguments[(i - 1)] = et.trap_Argv(i)
end
return events.trigger("onCallvote", clientId, et.trap_Argv(1), voteArguments)
elseif wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_teamnl" or wolfCmd == "say_buddy" then
if players.isMuted(clientId, players.MUTE_CHAT) then
et.trap_SendServerCommand(clientId, "cp \"^1You are muted\"")
return 1
end
elseif wolfCmd == "vsay" or wolfCmd == "vsay_team" then
if players.isMuted(clientId, players.MUTE_VOICE) then
et.trap_SendServerCommand(clientId, "cp \"^1You are voicemuted\"")
return 1
end
end
-- client cmds -- client cmds
-- syntax: say or say_*
local clientCmd = nil local clientCmd = nil
-- say and say_*
if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "/") == 1 then if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "/") == 1 then
cmdArguments = util.split(et.trap_Argv(1), " ") cmdArguments = util.split(et.trap_Argv(1), " ")
-- say "/command param1 param2 paramN" -- say "/command arg1 arg2 argN"
if #cmdArguments > 1 then if #cmdArguments > 1 then
clientCmd = string.sub(cmdArguments[1], 2, string.len(cmdArguments[1])) clientCmd = string.sub(cmdArguments[1], 2, string.len(cmdArguments[1]))
table.remove(cmdArguments, 1) table.remove(cmdArguments, 1)
-- say /command param1 param2 paramN -- say /command arg1 arg2 argN
else else
clientCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1))) clientCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1)))
@ -279,15 +240,9 @@ function commands.onclientcommand(clientId, cmdText)
end end
if cmdArguments[1] == et.trap_Argv(1) then table.remove(cmdArguments, 1) end if cmdArguments[1] == et.trap_Argv(1) then table.remove(cmdArguments, 1) end
end end
-- !command
elseif string.find(wolfCmd, "!") == 1 then
clientCmd = string.sub(wolfCmd, 2, string.len(wolfCmd))
for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i)
end
end end
-- handle client cmds
if clientCmd then if clientCmd then
clientCmd = string.lower(clientCmd) clientCmd = string.lower(clientCmd)
@ -301,15 +256,16 @@ function commands.onclientcommand(clientId, cmdText)
-- shrub cmds -- shrub cmds
local shrubCmd = nil local shrubCmd = nil
-- say and say_* -- syntax: say or say_*
if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "!") == 1 then if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "!") == 1 then
cmdArguments = util.split(et.trap_Argv(1), " ") cmdArguments = util.split(et.trap_Argv(1), " ")
-- say "!command param1 param2 paramN" -- syntax: say "!command arg1 arg2 ... argN"
if #cmdArguments > 1 then if #cmdArguments > 1 then
shrubCmd = string.sub(cmdArguments[1], 2, string.len(cmdArguments[1])) shrubCmd = string.sub(cmdArguments[1], 2, string.len(cmdArguments[1]))
table.remove(cmdArguments, 1) table.remove(cmdArguments, 1)
-- say !command param1 param2 paramN -- syntax: say !command arg1 arg2 ... argN
else else
shrubCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1))) shrubCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1)))
@ -318,7 +274,7 @@ function commands.onclientcommand(clientId, cmdText)
end end
if cmdArguments[1] == et.trap_Argv(1) then table.remove(cmdArguments, 1) end if cmdArguments[1] == et.trap_Argv(1) then table.remove(cmdArguments, 1) end
end end
-- !command -- syntax: !command arg1 arg2 ... argN
elseif string.find(wolfCmd, "!") == 1 then elseif string.find(wolfCmd, "!") == 1 then
shrubCmd = string.sub(wolfCmd, 2, string.len(wolfCmd)) shrubCmd = string.sub(wolfCmd, 2, string.len(wolfCmd))
@ -326,7 +282,8 @@ function commands.onclientcommand(clientId, cmdText)
cmdArguments[i] = et.trap_Argv(i) cmdArguments[i] = et.trap_Argv(i)
end end
end end
-- handle shrub commands
if shrubCmd then if shrubCmd then
shrubCmd = string.lower(shrubCmd) shrubCmd = string.lower(shrubCmd)
@ -351,6 +308,6 @@ function commands.onclientcommand(clientId, cmdText)
return 0 return 0
end end
events.handle("onClientCommand", commands.onclientcommand) events.handle("onClientCommand", commands.onClientCommand)
return commands return commands