All pastes #594591 Raw Edit

Anoymous

public text v1 · immutable
#594591 ·published 2007-06-29 00:29 UTC
rendered paste body
import es

# this will be our global list of filters
filters = {}

# 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']

  # 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] = filterfunction

def 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
     filter(filters[filtername], playerlist)
  
  return playerlist