All pastes #75776 Raw Edit

Anonymous

public python v1 · immutable
#75776 ·published 2006-06-30 15:35 UTC
rendered paste body
class ResayAddon(Addon):  Name = "Mattie's Resay Addon"  Version = "v0.2"  Description = "Player can send !re via console and choose from a menu to resay their last messages."  Help = "This plugin requires Mattie's ESPython Plugin"  Author = "Mattie"  # internal dictionary of players  players = {}  # called when we get loaded  def load(self):    server.log('Starting Resay...\n')    # we only need to register for 3 events    addons.registerForEvent(self, 'es_player_chat', self.es_player_chat)    addons.registerForEvent(self, 'es_client_command', self.es_client_command)    addons.registerForEvent(self, 'player_disconnect', self.player_disconnect)     # store their history  def es_player_chat(self, event_var):    steamid = event_var['es_steamid']    # create them if not in the list already    if not self.players.has_key(steamid):      self.players[steamid] = ResayPlayer()    # add the chat message to their list    self.players[steamid].addchat(ResayMsg(int(event_var['teamonly']), event_var['text']))  # trigger menu and respond to menu options  def es_client_command(self, event_var):    steamid = event_var['es_steamid']    if self.players.has_key(steamid):      # if !re we need to show them the menu and mark them as menu'd      if event_var['command'] == "!re":          self.sendresaymenu(event_var['userid'], self.players[steamid])      # if they have the menu open and send menuselect      if self.players[steamid].ResayMenu and event_var['command'] == "menuselect":          server.log("menuselect received.\n")          self.players[steamid].ResayMenu = False          option = int(event_var['commandstring'])          server.log("menuselect received:\n"   str(option))                   if option > 0 and option < 10:            self.sendresaychat(event_var['userid'], option-1, self.players[steamid])  # clean the old players up  def player_disconnect(self, event_var):    steamid = event_var['es_steamid']    if self.players.has_key(steamid):      # remove them      self.players.pop(steamid)     # send a resay chat on behalf of the user  def sendresaychat(self, userid, msgid, player):    server.log("sendchat entered")    saymsg = player.SayHistory[msgid]    scmd = ''    if saymsg.TeamSay:      scmd = "say_team"    else:      scmd = "say"    server.cmd("es_xsexec "   str(userid)   " "   scmd   ' "'   saymsg.Message   '";\n')    server.log("sendchat done")       # show the user their resay menu  def sendresaymenu(self, userid, player):    menumsg = 'Resay Menu\\n\\n'    i = 0    # build each line from their SayHistory    for saymsg in player.SayHistory:      i =1      line = ''      line  = str(i)   ":"      if saymsg.TeamSay:        line  = "(Team) "      else:        line  = " "      line  = saymsg.Message      # we can only support ~23 letters per line      line = line[:23]   "\\n"      menumsg  = line    menumsg  = " \\n0.Exit\\n\\n"    player.ResayMenu = True    # Show them the menu    server.cmd("es_xmenu 0 "   str(userid)   ' "'   menumsg   '";')# a type we'll use for storing some of their saysclass ResayMsg:  # was it team only?  TeamSay = False  # what was the text?  Message = ""  # called whenever a ResayMsg is created  def __init__(self, teamsay, message):    if teamsay == 0:      self.TeamSay = False    else:      self.TeamSay = True    self.Message = message# represents the player for their history and menu stateclass ResayPlayer:  # do they have the menu open?  ResayMenu = False  # this stores a list of their last x say commands  SayHistory = []  def __init__(self):    self.ResayMenu = False    self.SayHistory = []  # add a message to that player's say history  def addchat(self, resaymsg):    if len(self.SayHistory)>8:      del self.SayHistory[8]    self.SayHistory.insert(0,resaymsg)# instantiate addon so es_load can use itresay = ResayAddon()