async def get_game_state(self): """ Connect to server and receive messages. Process information from server. """ task = asyncio.create_task(self.tick_log()) async with aiohttp.ClientSession() as session: async with session.ws_connect('http://' + self.hostname + ':8080/receiver/') as ws: # for loop is finished when client disconnects from server async for message in ws: message = message.json() if "game_state" in message: self.state = State.whole_from_dict(message) self.reset_last_robots() if self.window is None: self.window = create_window( self.state, self.window_draw) if "available_robots" in message: self.available_robots = self.state.robots_from_dict( {"robots": message["available_robots"]}) if 'log' in message: self.log_to_play.extend(message['log']) if "winner" in message: self.state.winners = message["winner"] self.log_to_play.append(None) task.cancel()
def set_game_state(self, message, robot_name, own_robot_name): """ Set game attributes using data from server message: - create game state, call set_robots. """ self.game_state = State.whole_from_dict(message) self.set_robots(message["game_state"], robot_name, own_robot_name)
def test_state_from_dict(): """ Check if state.from_dict can load State from JSON. """ state = State.get_start_state("maps/test_maps/test_3.json") data = state.whole_as_dict("maps/test_maps/test_3.json") state_recovered = State.whole_from_dict(data) assert state_recovered.robots[0].coordinates == (0, 1) assert state_recovered.robots[1].damages == 0 assert state_recovered._board[0, 11][0].direction == Direction.N assert state_recovered._board[2, 5][0].name == "ground"
async def process_message(self): """ Connect to server and receive messages. Process information from server. """ async with aiohttp.ClientSession() as session: async with session.ws_connect('http://' + self.hostname + ':8080/receiver/') as ws: # Loop "for" is finished when client disconnects from server async for message in ws: message = message.json() if "game_state" in message: self.state = State.whole_from_dict(message) if self.window is None: self.window = create_window( self.window_draw, self.on_mouse_press, self.on_text, self.on_text_motion, ) if "available_robots" in message: self.available_robots = self.state.robots_from_dict( {"robots": message["available_robots"]})