def check_country(): args = request.args id = get_arg(args, "id", required=True) expected = get_arg(args, "expected", required=True) observed = get_arg(args, "observed", required=True) return str(country_system.check_answer(id, expected, observed))
def from_dict(question_data): options = question_data["options"] question_num = question_data["question_num"] expected_answer = get_arg(question_data, "expected_answer", required=False) observed_answers = get_arg(question_data, "observed_answers", required=False, default=set()) if observed_answers is None: observed_answers = [] time_asked_str = get_arg(question_data, "time_asked", required=False) time_asked = Question._str_to_datetime(time_asked_str) time_answered_str = get_arg(question_data, "time_answered", required=False) time_answered = Question._str_to_datetime(time_answered_str) return Question(options, question_num, expected_answer=expected_answer, observed_answers=observed_answers, time_asked=time_asked, time_answered=time_answered)
def get_game_trophies(): args = request.args user_id = get_arg(args, "user", required=False) game_id = get_arg(args, "game", required=True) trophies = country_system.get_trophies_for_game(user_id, game_id) return json.dumps(trophies)
def update_public_scores(): args = request.args name = get_arg(args, "name", required=True) requested_state = get_arg(args, "new", required=True) success_status = country_system.update_public_scores(name, requested_state) if success_status: return SUCCESS else: return FAILURE
def update_user(): args = request.args new_name = get_arg(args, "name", required=True) old_name = get_arg(args, "old", required=False) email = get_arg(args, "email", required=True) name_was_updated = country_system.update_name( new_name, old_name, email) if name_was_updated: return new_name else: return FAILURE
def new_game(): args = request.args given = get_arg(args, "given", required=True) asked_for = get_arg(args, "asked_for", required=True) users_unique_name = get_arg(args, "users_unique_name", required=True) country_data = firebase_routes.get_country_data() # list of jsons new_game = country_system.new_game(country_data, given, asked_for) # pass in the logged in players name or the string not_a_user # users_unique_name = get_arg(args, "users_unique_name", required=True) update_user_data(users_unique_name, new_game.id) print("NEW GAME ID:", new_game.id, type(new_game.id)) return new_game.id
def get_global_board_for_game_mode(): args = request.args game_id = get_arg(args, "game_id", required=True) game_data = firebase_routes.get_game_data_by_id(game_id) mode = game_data['mode'] all_users_with_games_and_scores = retrieve_all_users_with_games_and_scores() filtered_games_and_scores = filter_games_by_mode( all_users_with_games_and_scores, mode) return json.dumps(filtered_games_and_scores)
def _get_existing_trophy_data(self, user_data): if user_data is None: existing_trophy_data = None else: existing_trophy_data = get_arg(user_data, "trophies", required=False) return existing_trophy_data
def rankRivalAndDistanceToRival(): args = request.args game_id = get_arg(args, "game_id", required=True) user_name = get_arg(args, "user_name", required=True) if user_name == "not_a_user": return json.dumps({"rival_info": "Sign in to see your rival."}) all_users_with_games_and_scores = retrieve_all_users_with_games_and_scores() all_games = firebase_routes.get_all_games() all_user_data = firebase_routes.get_all_users() mode_of_this_game = mode_string_to_id(all_games[game_id]["mode"]) all_users_names = list(all_users_with_games_and_scores.keys()) rank_list_of_relevant_game_data = [] for users_name in all_users_names: games_played_by_user = all_users_with_games_and_scores[users_name] for gameName in games_played_by_user: game_data = all_users_with_games_and_scores[users_name][gameName] if game_data['Mode'] == mode_of_this_game: rank_list_of_relevant_game_data.append( {"name": users_name, "score": game_data["Score"], "gameID": all_user_data[users_name]["gameIDs"]["gamesPlayed"][gameName]}) sorted_rank_list_of_relevant_game_data = sorted( rank_list_of_relevant_game_data, key=lambda k: k['score'], reverse=True) this_game_rank = 1 this_game_score = 0 for relevant_game_data in sorted_rank_list_of_relevant_game_data: if relevant_game_data["gameID"] == game_id: this_game_score = relevant_game_data["score"] break this_game_rank += 1 your_highest_game_rank = 1 for relevant_game_data in sorted_rank_list_of_relevant_game_data: if relevant_game_data["name"] == user_name: break your_highest_game_rank += 1 string_to_return = "You placed Number " + str(this_game_rank) + " out of " + str( len(sorted_rank_list_of_relevant_game_data)) + " on the World leaderboard. " if your_highest_game_rank <= 10: if your_highest_game_rank == this_game_rank: if this_game_rank == 1: string_to_return += "You are the global leader. Congratulations!" else: string_to_return += rival_finder(sorted_rank_list_of_relevant_game_data, user_name, this_game_rank-2, this_game_score, "") else: if your_highest_game_rank == 1: string_to_return += "You still remain the global leader with a score of " + \ str(sorted_rank_list_of_relevant_game_data[0] ["score"]) + ". Congratulations!" else: string_to_return += "You are still " + str(your_highest_game_rank) + " out of " + str( len(sorted_rank_list_of_relevant_game_data)) + " on the World leaderboard. " string_to_return += rival_finder(sorted_rank_list_of_relevant_game_data, user_name, your_highest_game_rank-2, this_game_score, "") else: string_to_return += "Try to aim for the top 10 next time!" return json.dumps({"rival_info": string_to_return})
def get_user_trophies(): args = request.args user_id = get_arg(args, "user", required=True) return json.dumps(firebase_routes.get_user_trophies(user_id), sort_keys=True)
def get_players_scoreboard(): args = request.args name = get_arg(args, "name", required=True) data = extract_games_and_score_for_user(name) return json.dumps(data)
def get_id_from_email(): args = request.args email = get_arg(args, "email", required=True) return firebase_routes.get_user_id_by_email(email)
def get_results(): args = request.args id = get_arg(args, "id", required=True) return json.dumps(country_system.get_results(id))
def random_countries(): args = request.args id = get_arg(args, "id", required=True) amount = int(get_arg(args, "amount", required=False, default=1)) return json.dumps(country_system.random_countries(id, amount))