def delete_player_with_body(identifiers): players_list = assist.deserialize_players() match_index = list() for index, player in enumerate(players_list): for indentifier_index, (key, value) in enumerate(identifiers.iteritems()): if not player.get_player()[key] == value: break if indentifier_index == len(identifiers)-1: match_index.append(index) if len(match_index) == 1: match_index = match_index[0] players_list.pop(match_index) assist.serialize_players(players_list) return "OK", status.HTTP_200_OK if len(match_index) > 1: return "results containt more than one player", status.HTTP_400_BAD_REQUEST else: return "No results were found", status.HTTP_400_BAD_REQUEST
def put_player_with_id(playerid): """process run if the request method is PUT""" original_list = assist.deserialize_players() players_list = original_list for index, _ in enumerate(players_list): if players_list[index].playerid == playerid: body = request.get_json() try: changes = body["changes"] except Exception as error: return error, status.HTTP_400_BAD_REQUEST for key, value in changes.items(): if key in players_list[index].get_player(): if type(value) == type( players_list[index].get_player()[key]): setattr(players_list[index], key, value) else: return "incorrect data type", status.HTTP_400_BAD_REQUEST assist.serialize_players(original_list) return "ACCEPTED", status.HTTP_202_ACCEPTED return "Player ID does not exist", status.HTTP_400_BAD_REQUEST
def players(): """Returns a list of all players""" players_details = [ player.get_player() for player in assist.deserialize_players() ] return jsonify(players_details)
def get_player_with_id(playerid): """process run if the request method does not have a body""" players_list = assist.deserialize_players() if playerid is not None: result = list(filter(lambda player: player.get_player()["playerid"] == playerid, players_list)) return [player.get_player() for player in result], status.HTTP_200_OK else: return "Requires a player ID or a body", status.HTTP_400_BAD_REQUEST
def get_player_with_body(body): """process run if the request method contains a body""" players_list = assist.deserialize_players() if body is None: return "Request must have a body: {}".format(body), status.HTTP_400_BAD_REQUEST else: for item in body: print(players_list) players_list = filter(lambda player: player.get_player()[item] == body[item], players_list) return [player.get_player() for player in players_list], status.HTTP_200_OK
def delete_player_with_id(playerid): """process run if the request method is DELETE""" players_list = assist.deserialize_players() for player in players_list: if player.playerid == playerid: players_list.remove(player) assist.serialize_players(players_list) return "OK", status.HTTP_200_OK return "Player ID does not exist", status.HTTP_400_BAD_REQUEST
def leaderboard(): """Returns a list of all players in order of win/loss ratio unless specified otherwise""" reverse = True orderby = "ratio" players = assist.deserialize_players() try: content = request.get_json() except werkzeug.exceptions.BadRequest: pass else: if 'reverse' in content.keys() and isinstance(content['reverse'], bool): reverse = content['reverse'] if 'orderby' in content.keys() and isinstance(content['orderby'], unicode): orderby = content['orderby'] ways_to_order = [["ratio", "wins", "firstname"], ["wins", "ratio", "firstname"], ["losses", "ratio", "firstname"], ["firstname", "lastname", "ratio"], ["lastname", "firstname", "ratio"]] for order in ways_to_order: if orderby == order[0]: sorted_players = sorted( players, key=lambda player: [(getattr(player, attribute)) for attribute in order], reverse=reverse) return jsonify([player.get_player() for player in sorted_players]) return "Cannot order", status.HTTP_400_BAD_REQUEST
def put_player_with_body(body): original_list = assist.deserialize_players() players_list = original_list try: identification = body["identification"] changes = body["changes"] except Exception as error: return error, status.HTTP_400_BAD_REQUEST for item in identification: players_list = filter( lambda player: player.get_player()[item] == identification[item], players_list) if players_list == list(): return "'{}' was not found amongst any players".format( item), status.HTTP_400_BAD_REQUEST if len(players_list) >= 2: return "results containt more than 1 player. \ try to identify more specifically, or use the uuid", status.HTTP_400_BAD_REQUEST else: player = players_list[0] for key, value in changes.items(): if key in player.get_player(): if type(value) == type(player.get_player()[key]): setattr(player, key, value) else: return "incorrect data type", status.HTTP_400_BAD_REQUEST assist.serialize_players(original_list) return "ACCEPTED", status.HTTP_202_ACCEPTED
def match(): """Perform a match between 2 players""" players_list = assist.deserialize_players() match_index = list() ####################################### # Check for request errors ################### try: body = request.get_json() except werkzeug.exceptions.BadRequest as error: return "Request must contain a body: {}".format( error), status.HTTP_400_BAD_REQUEST for parameter in ["winner", "loser"]: if not parameter in body: return "{} is not in the request body".format( parameter), status.HTTP_400_BAD_REQUEST try: player_guide = players_list[0].get_player() except: return "There are no players", status.HTTP_405_METHOD_NOT_ALLOWED winner = body["winner"] loser = body["loser"] for (index, parameter) in enumerate(winner): if not parameter in player_guide: return "{} is not a valid parameter".format( parameter), status.HTTP_400_BAD_REQUEST elif not type(winner[parameter]) == type(player_guide[parameter]): return "{} is not the correct datatype, it should be: {}".format( type(winner[parameter]), type(player_guide[parameter])) ####################################### # Identify the winner and the loser ################### for index, player in enumerate(players_list): # looking for winner for identifier_index, (key, value) in enumerate(winner.iteritems()): if not player.get_player()[key] == value: break if identifier_index == len(winner) - 1: player.wins += 1 player.recalculate_ratio() # looking for loser for identifier_index, (key, value) in enumerate(loser.iteritems()): if not player.get_player()[key] == value: break if identifier_index == len(loser) - 1: player.losses += 1 player.recalculate_ratio() assist.serialize_players(players_list) return "ACCEPTED", status.HTTP_202_ACCEPTED
default="info", help="set logging level") args = parser.parse_args() app = Flask("__name__") try: exec("level = logging.{}".format(args.logging_level.upper())) except Exception: print("could not assign logging level. Defaulting to INFO") level = logging.INFO logging.basicConfig(level=level) players_list = assist.deserialize_players() ####################################### # Routes ################### @app.route('/docs') def docs(): return render_template('docs.html') @app.route('/player', methods=["POST", "GET", "PUT", "DELETE"]) @app.route('/player/<playerid>', methods=["GET", "PUT", "DELETE"]) def player(playerid=None): """Parses the request type and sends the request to the correct method accordingly"""