我目前正在使用扭曲的库在python中使用客户端/服务器多玩家卡片游戏解决方案。我是python的新手,这个游戏实际上是对我自己的概念证明,因为我只想展示自己,我可以做这样的事情。但我现在在这个问题上坚持了8个小时,我需要一些建议。 以下几行和以下说明应该让您知道,我在这里尝试做什么:
说明
来自玩家堆的牌被发送到客户端并打印出来
客户端现在应该可以按一个号码(卡号),然后应该将其发送回服务器打印出来的号码(只有打印没有意义,但它是唯一的来了在我看来:P)
现在开始解决问题:
我认为解释起来非常复杂,所以请提出任何可能出现的问题。 我该如何解决这个问题?
我可以以某种方式使服务器等待一段时间,以便客户端能够在此期间输入数字吗? 如果必须等待客户端上的输入,我是否需要打开一个始终打开的新连接?
client.py:
PICK = 0
factory = pb.PBClientFactory()
reactor.connectTCP("localhost", PORT, factory)
def1 = factory.getRootObject()
def1.addCallbacks(got_obj1, err_obj1)
ret = reactor.run()
def got_obj1(obj1):
def2 = obj1.callRemote("register","User A")
def2 = obj1.callRemote("start","User A")
def2.addCallback(show)
obj1.callRemote("pick",PICK) # Problem
def show(*var)
data_string = json.dumps(var)
decoded = json.loads(data_string)
for card in decoded[0]:
print(str(card['name'])
PICK = Input("Which card do you want to pick?") # Problem
server.py:
print("Starting server on ", PORT)
rpc = RPCs()
reactor.listenTCP(PORT, pb.PBServerFactory(rpc))
reactor.run()
class RPCs(pb.Root):
sessions = []
def remote_pick(self,pick):
print("picked: ", pick)
def remote_start(self, username):
for session in self.sessions :
if (session.getName() == username):
ret = self.hoststart(username)
return ret
def hoststart(self,username):
pile4client = []
card4pile {}
for session in self.sessions:
if (session.getName() == username):
ret = session.showpile(0)
for card in ret:
card4pile = { "name" : card.getName()}
pile4client .append(card4pile)
return pile4client
class Session():
piles = []
def showpile(self, num):
return self.piles[num]
答案 0 :(得分:0)
将obj1
传递给show
并在展示中调用其callRemote
。像这样:
def got_obj1(obj1):
...
def2.addCallback(show, obj1)
def show(value, obj1):
...
PICK = Input("Which card do you want to pick?")
obj1.callRemote("pick",PICK)