2013-08-28 21 views
2

因此,我正在尝试制作一个非常基本的MUD,并且可能会以错误的方式解决这个问题,但我只需要一些编码帮助。这段代码包含了扭曲的内容,我已经调整了测试方法来尝试理解它,但是我遇到了一个障碍。如何使用Twisted调用特定协议

from twisted.internet.protocol import Factory 
from twisted.protocols.basic import LineReceiver 
from twisted.internet import reactor 
import time 


class Chat(LineReceiver): 

    def __init__(self, users): 
     self.users = users 
     self.name = None 
     self.health = 10 
     self.state = "GETNAME" 

    def connectionMade(self): 
     self.sendLine("What's your name?") 

    def connectionLost(self, reason): 
     if self.users.has_key(self.name): 
      print self.name, "has disconnected" 
      del self.users[self.name] 

    def lineReceived(self, line): 
     if self.state == "GETNAME": 
      self.handle_GETNAME(line) 
     else: 
      if self.state == "CHAT": 
       self.handle_CHAT(line) 
      else: 
       if self.state == "ATTACK": 
        self.handle_ATTACK(line) 

    def handle_GETNAME(self, name): 
     if self.users.has_key(name): 
      self.sendLine("Name taken, please choose another.") 
      return 
     self.sendLine("Welcome, %s!" % (name,)) 
     print name, "has connected" 
     self.sendLine("You currently have %s health..." % (self.health)) 
     self.name = name 
     self.users[name] = self 
     for name, protocol in self.users.iteritems(): 
      if protocol != self: 
       message = "%s has joined" % (self.name,) 
       protocol.sendLine(message) 
     self.state = "CHAT" 

    def handle_CHAT(self, message): 
     if(message[0:3] == 'say'): 
      try: 
       message = message[4:] 
       message = "<%s> %s" % (self.name, message) 
       print message 
       for name, protocol in self.users.iteritems(): 
        if protocol != self: 
         protocol.sendLine(message) 
      except: 
       print "Chat failed" 
     if(message == 'test'): 
      try: 
       self.handle_TEST() 
      except: 
       print "Testing Failed" 
     if(message == 'attack'): 
      try: 
       self.handle_ATTACKINIT() 
      except: 
       print "Attack Failed" 

    def handle_ATTACKINIT(self): 
     self.sendLine("Who are you attacking?") 
     self.state = "ATTACK" 

    def handle_ATTACK(self, target): 
     for target, protocol in self.users.iteritems(): 
      if protocol == target: 
       target.sendLine("You have been attacked!") 
       protocol.sendLine("You now have %s health remaining..." % (self.health,)) 
      else: 
       self.sendLine("No target with that name") 

    def handle_TEST(self): 
     print name, "is Testing" 
     self.sendLine("This is a test") 
     self.state = "CHAT" 


class ChatFactory(Factory): 

    def __init__(self): 
     self.users = {} # maps user names to Chat instances 

    def buildProtocol(self, addr): 
     return Chat(self.users) 


reactor.listenTCP(8123, ChatFactory()) 
reactor.run() 

主要功能我需要帮助这个函数...

def handle_ATTACK(self, target): 
     for target, protocol in self.users.iteritems(): 
      if protocol == target: 
       target.sendLine("You have been attacked!") 
       protocol.sendLine("You now have %s health remaining..." % (self.health,)) 
      else: 
       self.sendLine("No target with that name") 

我需要找到“目标的”协议向它发送一个消息,并造成伤害到它。

我发现它将名称/协议匹配保存在self.users列表中,我猜我在set self.users.iteritems()中寻找“target”协议,但我有访问特定协议并利用它遇到麻烦。

任何帮助初学新手,搞乱扭曲?

回答

0

你的阴影你target参数与循环变量:

def handle_ATTACK(self, target): 
    for target, protocol in self.users.iteritems(): 
     if protocol == target: 

循环开始前,target识别用户的攻击。循环开始后,它会标识self.users字典中的一个键。

然后,您将该密钥与 - 它是self.users字典的之一进行比较。这可能不是你想要的。

尝试将target参数与self.users中的键进行比较,然后使用相应的值作为用于发送数据的协议。

+0

好吧我要继续前进,现在检查出来,然后回来。感谢您的回应 – Lorinth