2

所以我正在尝试制作一个非常基本的 MUD,我可能会以错误的方式进行操作,但我只需要一些编码帮助。这段代码包括我为测试方法进行了调整以尝试理解它的扭曲,但是我遇到了障碍。

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() 中寻找“目标”协议,但我无法访问特定的协议和使用它。

对搞乱扭曲的初学者有什么帮助吗?

4

1 回答 1

0

您已经target使用循环变量隐藏了参数:

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

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

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

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

于 2013-08-29T11:26:56.910 回答