在twisted python中,如何在反应炉.run()期间对用户输入做出反应?

时间:2022-04-20 20:56:26

Im currently working on a client/server multi player cardgame solution in python using twisted libraries. Im pretty new to python, and this game actually is a proof of concept to myself, as i just want to show myself, that i can do such stuff. but im stuck ~8 hours on this problem now, and i need some advice. following lines and the following description should let you know, what im trying to do here:

Im目前正在使用twisted库在python中使用客户机/服务器多玩家cardgame解决方案。我对python很陌生,这个游戏对我来说是一个概念的证明,因为我只想证明我能做这样的事情。但是我现在在这个问题上花了8个小时,我需要一些建议。下面的几行和下面的描述应该让你知道,我想在这里做什么:

Description:

描述:

  • Server ist started
  • 服务器是启动
  • as soon as the demo client connects, server does some stuff in the background (registering, creating pile of cards for the player)
  • 一旦演示客户端连接,服务器就会在后台做一些事情(注册,为玩家创建一堆卡片)
  • the demo client now starts() the cardgame
  • 演示客户端现在开始()cardgame
  • remote_start is called and calls function hoststart if its started by certain player (to be implemented)
  • remote_start被调用并调用函数hoststart(如果它是由某个播放器启动的)
  • the cards from the players pile are sent to the client and print out

    玩家牌堆中的牌被发送到客户端并打印出来

  • the client should now be able to press a number (number of card), which then should be sent back to the server to print out the number (only printing doesnt make sense, but its the only thing that came to my mind right now :P)

    客户现在应该能够按下一个数字(卡号),然后将其发送回服务器,打印出数字(只有打印没有意义,但这是我现在唯一想到的东西:P)

now here starts the problem:

现在问题来了:

  • as soon as the reactor.run() is started, it seems as whole script is executed (user registered, game started) and the input at the client is done after the whole script is executed. so the sending of the entered number is done, before the number is entered within the input function and therefore returns 0 to the server.
  • 一旦反应器.run()启动,似乎就执行了整个脚本(用户注册,游戏启动),并且在整个脚本执行之后,客户机上的输入就完成了。因此,在输入函数中输入数字并因此向服务器返回0之前,已经完成了输入数字的发送。

I think this is very complex to explain, so please ask any questions which may come up. How can i resolve this problem?

我认为这是一个非常复杂的解释,所以请提出任何可能出现的问题。我如何解决这个问题?

Can i somehow make the server wait an amount of seconds so that the client is able to input the Number during that time? Do i need to open a new connection which allways opens if have to wait for input on the client?

我是否可以让服务器等待一段时间,以便客户端能够在这段时间内输入数字?如果需要等待客户端的输入,是否需要打开一个一直打开的新连接?


client.py:

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:

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]

1 个解决方案

#1


0  

Pass obj1 to show and call its callRemote in show. Something like this:

传递obj1以显示并调用它的callRemote in show。是这样的:

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)

#1


0  

Pass obj1 to show and call its callRemote in show. Something like this:

传递obj1以显示并调用它的callRemote in show。是这样的:

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)