def overwrite_game(username, game, user_data): data = user_data for user in data: if user["username"] == username: user["game"] = game files.overwrite_file("users", data)
def overwrite_game(game_name, target_game, multi_data): data = multi_data for index, game in enumerate(data): if game["game_name"] == game_name: data[index] = target_game files.overwrite_file("game_multi", data)
def overwrite_game_value(game_name, key_name, value): data = files.read_data_file("game_multi") for game in data: if game["game_name"] == game_name: game[key_name] = value files.overwrite_file("game_multi", data)
def remove_game_from_data(game_name): data = files.read_data_file("game_multi") updated_data = [] for game in data: if game["game_name"] != game_name: updated_data.append(game) files.overwrite_file("game_multi", updated_data)
def update_player_list(game_name): data = files.read_data_file("game_multi") sorted_players = change_rank_of_players((game_name)) for game in data: if game["game_name"] == game_name: game["players"] = sorted_players files.overwrite_file("game_multi", data)
def remove_player_from_data(username): data = files.read_data_file("users") updated_data = [] for obj in data: if obj["username"] != username: updated_data.append(obj) files.overwrite_file("users", updated_data)
def remove_player_from_data_by_index(index): data = files.read_data_file("users") if index > len(data): return False else: data.pop(index) files.overwrite_file("users", data)
def test_if_file_can_be_overwritten(self): data = files.read_data_file("countries") data_copy = data self.assertEqual(200, len(data)) files.overwrite_file("countries", "hello") data_overwritten = files.read_data_file("countries") self.assertEqual(5, len(data_overwritten)) self.assertEqual("hello", data_overwritten) files.overwrite_file("countries", data_copy)
def test_if_can_append_to_file(self): data = files.read_data_file("countries") data_copy = data self.assertNotIn("hello", data) files.append_to_file("hello", "countries") data_updated = files.read_data_file("countries") self.assertIn("hello", data_updated) files.overwrite_file("countries", data_copy)
def reset_player_score(username): data = files.read_data_file("users") current_user = {} for user in data: if user["username"] == username: current_user = user current_user["score"] = 0 files.overwrite_file("users", data)
def increase_player_score(username, points): data = files.read_data_file("users") current_user = {} for user in data: if user["username"] == username: current_user = user current_user["score"] += points files.overwrite_file("users", data)
def add_to_elapsed_time_of_player_for_last_flag(game_name, end_time): data = files.read_data_file("game_multi") list_of_players = extract_value_from_game(game_name, "players") last_player = list_of_players[-1] last_player["elapsed_time"] += end_time - last_player["start_time"] list_of_players[-1] = last_player for game in data: if game["game_name"] == game_name: game["players"] = list_of_players files.overwrite_file("game_multi", data)
def set_start_time_for_current_player(game_name): data = files.read_data_file("game_multi") list_of_players = extract_value_from_game(game_name, "players") turn_of_player = extract_value_from_game(game_name, "turn_of_player") for player in list_of_players: if player == list_of_players[turn_of_player]: player["start_time"] = time.time() for game in data: if game["game_name"] == game_name: game["players"] = list_of_players files.overwrite_file("game_multi", data)
def reset_players(game_name): data = files.read_data_file("game_multi") list_of_players = extract_value_from_game(game_name, "players") for player in list_of_players: player["incorrect_answers"] = [] player["rank"] = 0 player["elapsed_time"] = 0.0 player["start_time"] = 0.0 player["score"] = 0 for game in data: if game["game_name"] == game_name: game["players"] = list_of_players files.overwrite_file("game_multi", data)
def add_to_previous_players_elapsed_time(game_name): data = files.read_data_file("game_multi") list_of_players = extract_value_from_game(game_name, "players") turn_of_player = extract_value_from_game(game_name, "turn_of_player") current_player = list_of_players[turn_of_player] if((turn_of_player - 1) < 0): last_player_index_num = len(list_of_players) - 1 previous_player = list_of_players[last_player_index_num] else: previous_player_index_num = turn_of_player - 1 previous_player = list_of_players[previous_player_index_num] elapsed_time = current_player["start_time"] - previous_player["start_time"] previous_player["elapsed_time"] += elapsed_time for game in data: if game["game_name"] == game_name: game["players"] = list_of_players files.overwrite_file("game_multi", data)
def add_player_to_data(username, password, game): if check_if_username_exists(username) == False: data = files.read_data_file("users") data.append(create_player(username, password, 0, game)) files.overwrite_file("users", data)
def update_leaderboard(data): updated_data = change_rank_ascending(data) files.overwrite_file("leaderboard", updated_data)