def set_game_state(self, message, robot_name): self.game_state = State.from_dict(message) self.state.players = self.game_state.robots for robot in self.state.players: if robot.name == robot_name: self.state.robot = robot index = self.state.players.index(robot) del self.state.players[index]
async def client(): global state async with aiohttp.ClientSession() as session: async with session.ws_connect('http://localhost:8080/ws/') as ws: # Waiting for message from server and print them async for msg in ws: # Cycle "for" is finished when client disconnect from server if msg.type == aiohttp.WSMsgType.TEXT: message = msg.data state_dict = json.loads(message) state = State.from_dict(state_dict) print(state_dict)
async def client(self): async with aiohttp.ClientSession() as session: async with session.ws_connect( 'http://localhost:8080/receiver/') as ws: # Waiting for message from server and print them async for msg in ws: # Cycle "for" is finished when client disconnects from server message = msg.json() if message["game_state"]: self.state = State.from_dict(message) if self.window is None: self.window = create_window(self.state) self.window.push_handlers(on_draw=self.window_draw)
def test_state_from_dict(): """ Check if state.from_dict can load State from JSON. """ data = { 'board': { 'height': 12, 'infinite': False, 'layers': [{ 'data': [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], 'height': 12, 'id': 1, 'name': 'layer 1', 'opacity': 1, 'type': 'tilelayer', 'visible': True, 'width': 12, 'x': 0, 'y': 0 }, { 'data': [ 0, 2684354573, 0, 2684354573, 0, 2684354573, 2684354573, 0, 2684354573, 0, 2684354573, 38, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3221225485, 0, 0, 0, 2684354578, 0, 2684354578, 0, 38, 1610612754, 0, 0, 0, 13, 0, 2684354578, 33, 2684354578, 0, 2, 1610612754, 0, 1610612754, 0, 3221225485, 0, 0, 2, 2684354578, 0, 39, 30, 0, 1610612754, 0, 0, 0, 13, 0, 2684354578, 0, 2684354578, 0, 0, 1610612754, 32, 1610612754, 0, 3221225485, 0, 0, 31, 2684354578, 0, 2684354578, 0, 0, 1610612754, 2, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3221225485, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 41, 0, 42, 0, 43, 44, 0, 45, 0, 46, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 'height': 12, 'id': 2, 'name': 'layer 2', 'opacity': 1, 'type': 'tilelayer', 'visible': True, 'width': 12, 'x': 0, 'y': 0 }, { 'data': [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 2684354584, 2684354584, 2684354584, 2684354584, 2684354584, 2684354584, 2684354584, 2684354584, 2684354585, 0, 0, 24, 0, 2684354573, 0, 2684354573, 2684354573, 0, 2684354573, 0, 3221225496, 0, 0, 24, 13, 0, 0, 0, 0, 0, 0, 3221225485, 3221225496, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 3221225496, 0, 0, 24, 13, 0, 0, 0, 0, 0, 0, 3221225485, 3221225496, 0, 0, 24, 0, 1610612749, 0, 1610612749, 1610612749, 0, 1610612749, 0, 3221225496, 0, 0, 1610612761, 1610612760, 1610612760, 1610612760, 1610612760, 1610612760, 1610612760, 1610612760, 1610612760, 3221225497, 0, 0, 0, 1610612749, 0, 1610612749, 0, 0, 1610612749, 13, 1610612749, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 13, 0, 13, 0, 13, 13, 13, 0, 13, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 'height': 12, 'id': 3, 'name': 'layer 3', 'opacity': 1, 'type': 'tilelayer', 'visible': True, 'width': 12, 'x': 0, 'y': 0 }], 'nextlayerid': 5, 'nextobjectid': 1, 'orientation': 'orthogonal', 'renderorder': 'right-up', 'tiledversion': '1.2.1', 'tileheight': 64, 'tilesets': [{ 'firstgid': 1, 'source': 'development_tileset.json' }], 'tilewidth': 64, 'type': 'map', 'version': 1.2, 'width': 12 }, 'robots': [{ 'name': 'hanka', 'coordinates': [0, 1], 'lives': 3, 'flags': 0, 'damages': 4, 'power down': False, 'direction': 0, 'start coordinates': [0, 1] }, { 'name': 'ivet', 'coordinates': [1, 1], 'lives': 3, 'flags': 0, 'damages': 4, 'power down': False, 'direction': 0, 'start coordinates': [1, 1] }] } state = State.from_dict(data) assert state.robots[0].coordinates == (0, 1) assert state.robots[1].damages == 4 assert state._board[0, 11][0].direction == Direction.N assert state._board[2, 5][0].name == "ground"