所以我试图制作一个非常基本的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()中寻找“target”的协议但是我遇到了麻烦访问特定协议并使用它。
对于刚开始搞乱的新手有什么帮助?
答案 0 :(得分:0)
您已使用循环变量隐藏了target
参数:
def handle_ATTACK(self, target):
for target, protocol in self.users.iteritems():
if protocol == target:
在循环开始之前,target
标识要攻击的用户。循环开始后,它会识别self.users
字典中的一个键。
然后,您将该密钥与protocol
进行比较 - 这是self.users
字典的值之一。这可能不是你想要的。
尝试将target
参数与来自self.users
的密钥进行比较,然后使用相应的值作为用于发送数据的协议。