def get_player_move_from_server(board, player, allowed_moves, server_url): """ Retrieve and parse the next move from an AI server. """ player_name = ser.save_player(player) payload = { 'board': ser.save_board(board), 'playerTurn': player_name, } url = urljoin(server_url, 'move') try: response = requests.post(url, json=payload, timeout=SERVER_REQUEST_TIMEOUT_SEC) if response.status_code != 200: msg = ('Player {0}: server has responded with an unexpected status code: {1}. ' 'Picking a random move instead') logger.warning(msg.format(player_name, response.status_code, response.content)) return random.choice(allowed_moves) except Exception as ex: msg = ('Player {0}: Error when requesting next move from the server: {1}. ' 'Picking a random move instead') logger.warning(msg.format(player_name, ex)) return random.choice(allowed_moves) try: return ser.load_move(response.json()) except Exception: msg = ('Player {0}: Unable to parse the move returned by the server ({1}). ' 'Picking a random move instead') logger.warning(msg.format(player_name, response.content)) return random.choice(allowed_moves)
def load_game(game_filename): """ Load the game moves and outcome from a replay file. """ with open(game_filename) as gf: game_data = json.load(gf) moves = [ser.load_move(move) for move in game_data['moves']] game_over_reason = ser.load_game_over_reason(game_data['gameOverReason']) return moves, game_over_reason
def test_serialize_capture_move(): move = CaptureMove(1, 12) assert load_move(save_move(move)) == move
def test_serialize_combo_capture_move(): move = ComboCaptureMove( [CaptureMove(1, 12), CaptureMove(12, 3), CaptureMove(3, 14)]) assert load_move(save_move(move)) == move
def test_serialize_forward_move(): move = ForwardMove(1, 6) assert load_move(save_move(move)) == move