Ejemplo n.º 1
0
 def update_game(self, dt):
     print(GameHandler.get_game_data())
     game = GameHandler.get_game_instance()
     if not game:
         return
     self.delete_all_widgets()
     if not game.is_ended:
         self.create_game_widgets()
 def on_message(self, ws, message):
     message = json.loads(message)
     print("message received on socket:", message)
     game_json = message["game"]
     game_json["me"] = message["me"]
     game_json["possible_moves"] = [Card.from_str(card) for card in message["possible_moves"] or []]
     self.update_shared_data("type", message["type"])
     self.update_shared_data("data", game_json)
     print(game_json)
     print(GameHandler.get_game_instance().__dict__)
     if message["type"] == "game_start" and not GameHandler.get_game_data().get("is_started", False):
         GameHandler.update_data("is_started", True)
         ws.close()
Ejemplo n.º 3
0
 def __init__(self, **kwargs):
     super(RootWidget, self).__init__(**kwargs)
     socket_handler = SocketHandler()
     ws = websocket.WebSocketApp(
         "ws://127.0.0.1:8081/ws/game/patta/{}/".format(
             GameHandler.get_game_data()["name"]),
         on_message=lambda ws, msg: socket_handler.on_message(ws, msg),
         on_error=lambda ws, msg: socket_handler.on_error(ws, msg),
         on_close=lambda ws: socket_handler.on_close(ws))
     ws.on_open = lambda ws: socket_handler.on_open(ws)
     self.ws = ws
     p1 = multiprocessing.Process(target=ws.run_forever, args=())
     p1.start()
     Clock.schedule_interval(self.update_game, 5)