示例#1
0
class MudProtocol(basic.LineReceiver):

    clients = []

    def __init__(self):
        self.clients.append(self)
        self.id = None
        makeTemporary(self)
        self.d = None
        self.doing = ""
        self.channel = Mud.getInstance().channel
        self.channel.addListener(self)
        self.character = Character()
        self.character.addListener(self)
        persist.persist(self.character)

    def command_help(self,*args):
        self.sendLine("No help yet.")
        self.prompt()

    def command_quit(self,*args):
        self.sendLine("Goodbye.")
        self.transport.loseConnection()

    def lineReceived(self, line):
        self.execute_command(line)

    def execute_command(self,line):
        command,_, rest = line.partition(" ")
        command_fn = "command_{0}".format(command)
        args = rest.split(" ")
        if hasattr(self,command_fn):
            return getattr(self,command_fn)(*args)
        elif hasattr(self.character,command_fn):
            return getattr(self.character,command_fn)(*args)
        else:
            return self.character.command_say(*[command] + args)

    def receiveMessage(self,message):
        self.sendMessage(message.dict['message'])

    def sendMessage(self,string):
        self.sendLine(string)
        self.prompt()

    def sendString(self,string):
        self.transport.write(string)

    def prompt(self):
        if self.character.task:
            self.sendString("({0})>".format(self.character.task))
        else:
            self.sendString(">")