Code:
import es
import playerlib
import vecmath
import random
import cmdlib
import spe
class ESTManager(object):
"""
This object is here to store all ESTCommand objects. It acts as a customized
container allowing us to access the individual stored objects by a given
name. This will be the manager for all the EST commands themselves and
provide a global interface.
"""
def __init__(self):
"""
Default constructor, initalize variables.
"""
self.commands = {}
def __del__(self):
"""
Default destructor, clear all the commands in turn executing the
desctructor of that object which unregisters that command object.
"""
self.commands.clear()
def __getitem__(self, name):
"""
Returns the EST command object specified by the name.
@param str name The name of the EST command
@return ESTCommand The object designated by the name
"""
return self.commands[name]
def __delitem__(self, name):
"""
Removes the EST command object which will unregister it.
@param str name The name of the EST command
"""
self.commands.__delitem__(name)
def __contains__(self, name):
"""
Test to see if the EST command is currently in use
@param str name The name of the EST command
@return bool Whether or not the EST command exists
"""
return name in self.commands
def registerESTCommand(name, description, callback, argCount):
"""
Registers a new EST command by the given parameters. This will register
it with cmdlib.
@param str name The name of the EST command
@param str description The help message of the command; contains paramter list
@param function callback The callback executed when the command is used
@param int|tuple argCount The amount of arguments which are valid for the command
"""
self.commands[name] = ESTCommand(name, description, callback, argCount)
def unregisterESTCommand(name):
"""
Unregisters an EST command.
@param str name The name of the EST command
"""
del self.commands[name]
def dumpCommands(self):
"""
Dumps the entire command list to
"""
es.dbgmsg(0, "\n\nEST Command list:")
es.dbgmsg(0, "-" * 50)
for commandName in sorted(self.commands):
commandObj = self.commands[commandName]
commandObj.displayHelp()
es.dbgmsg(0, "-" * 50)
es.dbgmsg(0, "\n\n")
class ESTCommand(object):
"""
This is an individual command object. This object will be created for all
EST commands. This wraps the cmdlib object in a specific API which is very
EST related. This enables us to have consistency throughout all of our
objects as it is handled automatically.
"""
def __init__(self, name, description, callback, argCount):
"""
Default constructor, intialise all variables and register the command.
We need to check if the command is lowercase, if not, register 2 commands
(one for the lowercase alternative as well.)
@param str name The name of the EST command
@param str description The help message of the command; contains paramter list
@param function callback The callback executed when the command is used
@param int|tuple argCount The amount of arguments which are valid for the command
"""
if not name.startswith("est_"):
name = "est_%s" % name
self.callback = callback
self.name = name
if not self.name.islower():
self.lowerName = name.lower()
self.description = description
if not hasattr(argCount, "__iter__"):
argCount = (argCount, )
self.argCount = argCount
cmdlib.registerServerCommand(name, description, self._callback)
if not self.name.islower():
cmdlib.registerServerCommand(lowerName, description, self._callback)
def __del__(self):
"""
Default destructor. Unregister the server commands.
"""
cmdlib.unregisterServerCommand(name)
if not self.name.islower():
cmdlib.unregisterServerCommand(self.name.lower())
def displayHelp(self):
"""
Display a help message about how to use this command. This will also
display the expected arg count.
"""
if len(self.argCount) == 1:
argCount = self.argCount[0]
else:
argCount = self.argCount
es.dbgmsg(0, "%s %s : expects %s args" % (self.name, self.description, argCount) )
def _callback(self, args):
"""
Executed when this command is executed. Test the argument length, if we
have any conflicts, display an error message and return early. Otherwise
we need to follow to the actuall callback function.
@param args A list of arguments after the command
"""
if len(args) not in self.argCount:
es.dbgmsg(0, "EST ERROR: Incorrect argument usaged, the correct usage is - %s %s" % (self.name, self.description) )
return
self.callback(args)
est = ESTManager()
def load():
"""
Executed when this addon loads, registers all the EST commands.
"""
est.registerESTCommand("AddDownload", "<path>", addDownload, 1)
est.registerESTCommand("Armor", "<est filter> <operator> <amount>", armor, 3)
est.registerESTCommand("ArmorAdd", "<est filter> <amount>", armorAdd, 2)
est.registerESTCommand("SetArmor", "<est filter> <amount>", armorSet, 2)
est.registerESTCommand("GetArmor", "<variable> <est player>", armorGet, 2)
est.registerESTCommand("Ban", '<est filter> <time> ["reason"]', ban, (2, 3))
def unload():
"""
Executed when this addon unloads, explicitly delete the est object from memory.
"""
del est
def getPlayersFromFilters(filterList):
"""
Return a list of players as playerlib filters from a given filter list. The
filter list supports EST filter list selections (or playerlib selections)
which accept either the filter tags (e.g. #c #t) or any valid user ident
such as partial names, steamid's or user ids.
@param string filterList A list of filters to search for the playerlib players
@return tuple|playerlib.Player A list of players for the given filter list
"""
playerTest = es.getuserid(filterList)
if es.exists('userid', playerTest):
return (playerlib.getPlayer(playerTest), )
if not isinstance(filterList, basestring):
return tuple()
addPlayers = True
players = set()
estToPlayerlibFilters = {
"c":"ct",
"t":"t",
"a":"all",
"l":"alive",
"s":"spec",
"h":"human",
"b":"bot",
"d":"dead",
"u":"un",
"0":"un",
"1":"spec",
"2":"t",
"3":"ct"
}
for filterLetter in filterList:
if filterLetter == "#":
addPlayers = True
elif filterLetter == "!":
addPlayers = False
else:
if filterLetter in estToPlayerlibFilters:
playerlibFilterLetter = estToPlayerlibFilters[filterLetter]
else:
playerlibFilterLetter = filterLetter
playerlibFilterLetter = "#" + playerlibFilterLetter
playerList = set(playerlib.getPlayerList(playerlibFilterLetter))
if addPlayers:
players.update(playerList)
else:
players = players.difference(playerList)
return players
def getUseridsFromFilters(filterList):
"""
Return a list of userids from a given filter list. The
filter list supports EST filter list selections (or playerlib selections)
which accept either the filter tags (e.g. #c #t) or any valid user ident
such as partial names, steamid's or user ids.
@param string filterList A list of filters to search for the playerlib players
@return tuple|int A list of userids for the given filter list
"""
return map(index, getPlayersFromFilters(filterList))
def applyOperatorToAttribute(player, attribute, operator, amount):
"""
Applies an operator to a certain player. This will use the playerlib
attributes so 'armor' will affect the player's armor etc. The only
operators supported are +, - and = to increment, decrement and assign
respectively as those are the operators supported by EST.
@param playerlib.Player player The playerlib object of the user
@param str attribute The playerlib attribute to apply the operator to
@param str operator The operator type to apply
@param int|float The amount to affect the value by.
"""
if operator == "+":
player[attribute] += amount
elif operator == "-":
player[attribute] -= amount
elif operator == "=":
player[attribute] = amount
else:
es.dbgmsg(0, "EST ERROR: est_%s cannot apply the operator %s. Only +, - and = are supported" % (attribute, operator))
def addDownload(args):
"""
Registers a file for download
@param str args[0] The path of the download file
"""
es.stringtable("downloadbles", args[0])
def armor(args):
"""
Applies an operator to a player's armor value.
@param str args[0] EST Player filter
@param str args[1] operator "+", "-" or "=" to reassign the armor
@param int args[2] amount The amount to alter the armor by
"""
for player in getPlayersFromFilter(args[0]):
applyOperatorToAttribute(player, "armor", args[1], int(args[2]))
def armorAdd(args):
"""
Adds a value to the player's current armor value
@param str args[0] EST player filter
@param int args[1] amount The amount to add to the armor
"""
for player in getPlayersFromFilter(args[0]):
applyOperatorToAttribute(player, "armor", "+", int(args[1]))
def armorSet(args):
"""
Assigns a value to the player's armor value
@param str args[0] EST player filter
@param int args[1] amount The amount to assign to the armor
"""
for player in getPlayersFromFilter(args[0]):
applyOperatorToAttribute(player, "armor", "=", int(args[1]))
def armorGet(args):
"""
Assigns a variable to hold the value of a player's current armor
@param str args[0] variablename The name of the variable to use
@param int args[1] EST Player An individual player to retrieve the armor
"""
userid = es.getuserid(args[1])
if es.exists('userid', userid):
player = playerlib.getPlayer(userid)
es.ServerVar(args[0]).set(int(player.armor))
def ban(args):
"""
Bans a list of players via their IP and ID.
@param str args[0] EST player filter
@param int args[1] time Amount of time to ban for (seconds, 0=permanent)
@param optional str args[2] reason The reason they were banned
"""
for player in getPlayersFromFilter(args[0]):
userid = int(player)
address = int(player.address)
reason = "Banned by admin"
if len(args) > 2:
reason = args[2]
es.server.queuecmd("kickid %s %s" % (userid, reason)
es.server.queuecmd("banid %s %s; addip %s %s" % (args[1], player.steamid, args[1], address))
es.server.queuecmd("writeid; writeip")