import es# this will be our global list of filtersfilters = {}def load(): registerPlayerListFilter("#all", returnAll) registerPlayerListFilter("#ct", returnTeam3) registerPlayerListFilter("#t", returnTeam2) registerPlayerListFilter("#dead", returnDead) registerPlayerListFilter("#human", returnHuman) registerPlayerListFilter("#bot", returnBot) registerPlayerListFilter("#alive", returnAlive) registerPlayerListFilter("#spec", returnSpec)# Scripters won't instatiate this class directly. They'll probably use:# myPlayer = playerlib.getPlayer(userid)# myPlayerList = playerlib.getPlayerList(filtername)# THOUGHTS: Right now it seems a Player object will be somewhat of a static# view of a player for some information, but might be on-the-fly for playergetset# info. How do we make that clear to scripters? Or do we take the performance# hit and make everything dynamic even though right now that requires# refreshing all of the es.createplayerlist() attributes at once?class Player: userid = 0 # default initialization routine. Give us a userid. def __init__(self, playerid): # We'll be nice and convert the playerid to an int in case they gave # us a string userid. self.userid = int(playerid) # Maybe use es.createplayerlist(userid) to get initial values? self.refreshAttributes() # TODO: What else do we need to initialize? def refreshAttributes(self): """ Refresh the attributes for this player from ES. These are cached in the player object for performance. """ # If this fails maybe they gave us the wrong userid, so maybe it should # except all the way up? Probably need a cleaner exception, though. self.attributes = es.createplayerlist(self.userid)[self.userid] def getHealth(self): """ Returns the player's health as of the last attribute refresh for this player's instance. """ # return the player's health return self.attributes['health'] def __int__(self): return self.userid # more stuff in the class# now for the module functions# allow them to save a a filter function for things like "#all", "#t", etc.def registerPlayerListFilter(filtername, filterfunction): filters[filtername] = filterfunctiondef getPlayer(userid): return Player(userid)def getPlayerList(filtername = None): """ Retrieves a list of Player objects. """ # I haven't tested it, but this should create a list of Players objects # mapped to the current user list. playerlist = map(getPlayer, es.getUseridList()) if filtername is not None: # use the filtername to lookup from filters dictionary # apply this filter to the playerlist # I have no idea if this works, I've never tried filter() before # playerlist = filter(filters[filtername], playerlist) for y in filtername.split(","): playerlist = filter(filters[y], playerlist) return playerlistdef returnAll(x): return xdef returnTeam3(x): if x.attributes['teamid'] == 3: return True else: return Falsedef returnTeam2(x): if x.attributes['teamid'] == 2: return True else: return Falsedef returnSpec(x): if x.attributes['teamid'] == 1: return True else: return Falsedef returnHuman(x): if x.attributes['isbot'] == 0: return True else: return Falsedef returnBot(x): if x.attributes['isbot'] == 1: return True else: return Falsedef returnDead(x): if x.attributes['isdead'] == 1: return True else: return Falsedef returnAlive(x): if x.attributes['isdead'] == 0: return True else: return False