def test_if_can_increase_round_number_by_one(self): files.append_to_file(game_multi.create_game("testGame15"), "game_multi") round_number = game_multi.extract_value_from_game("testGame15", "round_number") self.assertEqual(1, round_number) game_multi.increase_round_number("testGame15") round_number_two = game_multi.extract_value_from_game("testGame15", "round_number") self.assertEqual(2, round_number_two) game_multi.remove_game_from_data("testGame15")
def test_if_can_overwrite_value_of_a_given_key_of_game(self): files.append_to_file(game_multi.create_game("testGame9"), "game_multi") flag_number = game_multi.extract_value_from_game("testGame9", "flag_number") self.assertEqual(0, flag_number) game_multi.overwrite_game_value("testGame9", "flag_number", 20) flag_number_two = game_multi.extract_value_from_game("testGame9", "flag_number") self.assertEqual(20, flag_number_two) game_multi.remove_game_from_data("testGame9")
def test_if_can_sort_players_for_leaderboard(self): files.append_to_file(game_multi.create_game("testGame27"), "game_multi") list_of_test_players = [{ "username": "******", "score": 43, "rank": 0, "start_time": 0.0, "elapsed_time": 100, "incorrect_answers": [] }, { "username": "******", "score": 66, "rank": 0, "start_time": 0.0, "elapsed_time": 100, "incorrect_answers": [] }, { "username": "******", "score": 2, "rank": 0, "start_time": 0.0, "elapsed_time": 50, "incorrect_answers": [] }, { "username": "******", "score": 66, "rank": 0, "start_time": 0.0, "elapsed_time": 99, "incorrect_answers": [] }] for player in list_of_test_players: game_multi.add_player_to_game("testGame27", player) list_of_players = game_multi.extract_value_from_game("testGame27", "players") self.assertEqual(list_of_test_players, list_of_players) self.assertEqual(list_of_test_players[1], list_of_players[1]) self.assertEqual(list_of_test_players[2], list_of_players[2]) sorted_players = game_multi.sort_players_for_leaderboard_display("testGame27") list_of_players_two = game_multi.extract_value_from_game("testGame27", "players") self.assertEqual(list_of_test_players[0], sorted_players[2]) self.assertEqual(list_of_test_players[1], sorted_players[1]) self.assertEqual(list_of_test_players[2], sorted_players[3]) self.assertEqual(list_of_test_players[3], sorted_players[0]) game_multi.remove_game_from_data("testGame27")
def test_if_can_set_rank_for_each_player_ascending(self): files.append_to_file(game_multi.create_game("testGame28"), "game_multi") list_of_test_players = [{ "username": "******", "score": 43, "rank": 0, "start_time": 0.0, "elapsed_time": 100, "incorrect_answers": [] }, { "username": "******", "score": 66, "rank": 0, "start_time": 0.0, "elapsed_time": 100, "incorrect_answers": [] }, { "username": "******", "score": 2, "rank": 0, "start_time": 0.0, "elapsed_time": 50, "incorrect_answers": [] }, { "username": "******", "score": 66, "rank": 0, "start_time": 0.0, "elapsed_time": 99, "incorrect_answers": [] }] for player in list_of_test_players: game_multi.add_player_to_game("testGame28", player) list_of_players = game_multi.extract_value_from_game("testGame28", "players") self.assertEqual(list_of_test_players, list_of_players) game_multi.update_player_list("testGame28") ranked_players = game_multi.extract_value_from_game("testGame28", "players") for index, player in enumerate(ranked_players): player_rank = player["rank"] # check if rank increases by 1 for each consecutive player in list self.assertEqual(index + 1, player_rank) game_multi.remove_game_from_data("testGame28")
def test_if_can_increase_and_reset_player_turn(self): files.append_to_file(game_multi.create_game("testGame14"), "game_multi") turn = game_multi.extract_value_from_game("testGame14", "turn_of_player") self.assertEqual(0, turn) game_multi.set_turn_of_player("testGame14", 2) turn_two = game_multi.extract_value_from_game("testGame14", "turn_of_player") self.assertEqual(1, turn_two) game_multi.set_turn_of_player("testGame14", 2) turn_three = game_multi.extract_value_from_game("testGame14", "turn_of_player") self.assertEqual(0, turn_three) game_multi.remove_game_from_data("testGame14")
def test_if_can_reset_round_number(self): files.append_to_file(game_multi.create_game("testGame16"), "game_multi") round_number = game_multi.extract_value_from_game("testGame16", "round_number") self.assertEqual(1, round_number) game_multi.increase_round_number("testGame16") game_multi.increase_round_number("testGame16") game_multi.increase_round_number("testGame16") round_number_two = game_multi.extract_value_from_game("testGame16", "round_number") self.assertEqual(4, round_number_two) game_multi.reset_round_number("testGame16") round_number_three = game_multi.extract_value_from_game("testGame16", "round_number") self.assertEqual(0, round_number_three) game_multi.remove_game_from_data("testGame16")
def test_if_can_reset_flag_number_to_zero(self): files.append_to_file(game_multi.create_game("testGame13"), "game_multi") flag_number = game_multi.extract_value_from_game("testGame13", "flag_number") self.assertEqual(0, flag_number) game_multi.increase_flag_number("testGame13") game_multi.increase_flag_number("testGame13") game_multi.increase_flag_number("testGame13") game_multi.increase_flag_number("testGame13") flag_number_two = game_multi.extract_value_from_game("testGame13", "flag_number") self.assertEqual(4, flag_number_two) game_multi.reset_flag_number("testGame13") flag_number_three = game_multi.extract_value_from_game("testGame13", "flag_number") self.assertEqual(0, flag_number_three) game_multi.remove_game_from_data("testGame13")
def test_if_can_decrease_points_by_two_if_less_than_two_set_to_zero(self): files.append_to_file(game_multi.create_game("testGame22"), "game_multi") points = game_multi.extract_value_from_game("testGame22", "points") self.assertEqual(5, points) game_multi.decrease_points("testGame22") points_two = game_multi.extract_value_from_game("testGame22", "points") self.assertEqual(3, points_two) game_multi.decrease_points("testGame22") points_three = game_multi.extract_value_from_game("testGame22", "points") self.assertEqual(1, points_three) game_multi.decrease_points("testGame22") points_four = game_multi.extract_value_from_game("testGame22", "points") self.assertEqual(0, points_four) game_multi.remove_game_from_data("testGame22")
def test_if_can_remove_player_by_index_from_game(self): files.append_to_file(game_multi.create_game("testGame11"), "game_multi") test_player = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] } game_multi.add_player_to_game("testGame11", test_player) player_list = game_multi.extract_value_from_game("testGame11", "players") self.assertIn(test_player, player_list) game_multi.remove_player_from_game_by_index("testGame11", 0) player_list_two = game_multi.extract_value_from_game("testGame11", "players") self.assertNotIn(test_player, player_list_two) game_multi.remove_game_from_data("testGame11")
def test_if_can_extract_value_for_given_key_from_target_game(self): files.append_to_file(game_multi.create_game("testGame7"), "game_multi") data = files.read_data_file("game_multi") game = game_multi.select_game("testGame7", data) self.assertEqual(1, game["round_number"]) extracted_value = game_multi.extract_value_from_game("testGame7", "round_number") self.assertEqual(extracted_value, game["round_number"]) game_multi.remove_game_from_data("testGame7")
def test_if_can_remove_games_older_than_a_day(self): files.append_to_file(game_multi.create_game("testGame31"), "game_multi") creation_time = game_multi.extract_value_from_game("testGame31", "time_of_creation") current_time = time.time() game_multi.remove_games_older_than_a_day(current_time) data = files.read_data_file("game_multi") game = game_multi.select_game("testGame31", data) self.assertIn(game, data) day_from_now = current_time + 86400 game_multi.remove_games_older_than_a_day(day_from_now) data_two = files.read_data_file("game_multi") self.assertNotIn(game, data_two)
def addPlayers(game_name): """ Route to add players page where up to five players can be added to the game created on the previous create room page. A list of random countries is generated for the game upon starting game, the number of them based on the number of players """ username_error = None max_number_of_players_error = None no_players_error = None player_list = game_multi.extract_value_from_game(game_name, "players") if request.method == "POST": for i in range(5): if str(i) in request.form: game_multi.remove_player_from_game_by_index(game_name, i) return redirect(url_for("addPlayers", game_name=game_name)) if "action" in request.form: if len(player_list) > 1: game_multi.overwrite_game_value(game_name, "players_copy", player_list) game_multi.overwrite_game_value( game_name, "list_of_countries", misc.generate_random_numbers((len(player_list) * 5))) return redirect(url_for("gameMulti", game_name=game_name)) else: no_players_error = "At least 2 players are needed to start the game" elif "username" in request.form: if not game_multi.check_if_five_players(game_name): if not game_multi.check_if_player_exists( game_name, request.form["username"]): game_multi.add_player_to_game( game_name, game_multi.create_player(request.form["username"])) return redirect(url_for("addPlayers", game_name=game_name)) else: username_error = "Username is taken" else: max_number_of_players_error = "The maximum number of players has been reached" return render_template( "add-players.html", username_error=username_error, max_number_of_players_error=max_number_of_players_error, no_players_error=no_players_error, player_list=player_list)
def test_if_can_reset_all_players_in_list_to_default_values(self): files.append_to_file(game_multi.create_game("testGame30"), "game_multi") list_of_test_players = [{ "username": "******", "score": 43, "rank": 2, "start_time": 1, "elapsed_time": 50, "incorrect_answers": ["wrong"] }, { "username": "******", "score": 66, "rank": 1, "start_time": 4, "elapsed_time": 100, "incorrect_answers": [] }] for player in list_of_test_players: game_multi.add_player_to_game("testGame30", player) game_multi.reset_players("testGame30") list_of_players = game_multi.extract_value_from_game("testGame30", "players") list_of_test_players_default = [{ "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] }, { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] }] self.assertEqual(list_of_test_players_default, list_of_players) game_multi.remove_game_from_data("testGame30")
def test_if_can_return_player_by_turn(self): files.append_to_file(game_multi.create_game("testGame23"), "game_multi") test_player = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] } test_player_two = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] } game_multi.add_player_to_game("testGame23", test_player) game_multi.add_player_to_game("testGame23", test_player_two) data = files.read_data_file("game_multi") game = game_multi.select_game("testGame23", data) list_of_players = game["players"] turn_of_player = game_multi.extract_value_from_game("testGame23", "turn_of_player") self.assertEqual(0, turn_of_player) self.assertEqual(2, len(list_of_players)) self.assertEqual(test_player, list_of_players[0]) self.assertEqual(test_player_two, list_of_players[1]) self.assertIn(test_player, list_of_players) self.assertIn(test_player_two, list_of_players) turn_one_player = game_multi.return_player_by_turn(0, "testGame23") self.assertEqual(test_player, turn_one_player) game_multi.set_turn_of_player("testGame23", 2) turn_two_player = game_multi.return_player_by_turn(1, "testGame23") self.assertEqual(test_player_two, turn_two_player) game_multi.set_turn_of_player("testGame23", 2) turn_one_player_repeat = game_multi.return_player_by_turn(0, "testGame23") self.assertEqual(test_player, turn_one_player_repeat) game_multi.remove_game_from_data("testGame23")
def gameMulti(game_name): """ Route for multiplayer game. Takes information from given game and the players of that game and updates/manipulates it in order to provide game functionality. """ players_initial = game_multi.extract_value_from_game( game_name, "players_copy") turn_of_player = game_multi.extract_value_from_game( game_name, "turn_of_player") list_of_players = game_multi.extract_value_from_game(game_name, "players") round_number = game_multi.extract_value_from_game(game_name, "round_number") list_of_country_index_numbers = game_multi.extract_value_from_game( game_name, "list_of_countries") game_flag_number = game_multi.extract_value_from_game( game_name, "flag_number") game_state = game_multi.extract_value_from_game(game_name, "game_state") points = game_multi.extract_value_from_game(game_name, "points") game_country_index = list_of_country_index_numbers[game_flag_number] flag = countries.return_value_from_country(game_country_index, "flag") official_name = countries.return_value_from_country( game_country_index, "name-official") common_name = countries.return_value_from_country(game_country_index, "name-common") official_name_title_cased = misc.string_to_lowercase_then_titlecase( official_name) common_name_title_cased = misc.string_to_lowercase_then_titlecase( common_name) current_player = game_multi.return_player_by_turn(turn_of_player, game_name) current_player_score = game_multi.return_player_score( turn_of_player, game_name) current_player_incorrect_answers = game_multi.return_player_incorrect_answers( turn_of_player, game_name) elapsed_times = game_multi.list_players_elapsed_time(game_name) number_of_players = len(list_of_players) index_num_of_last_player = len(list_of_players) - 1 if request.method == "POST": """ Multiplayer game main functionality, allowing players to play locally(on a single device) against each other. Provides behaviour for skipping questions, submitting answers and the round/turn system. Each player has a single turn each round. Players are provided with a total number(number_of_players * 5) of random flags. Each player get's to guess 5 flags(one per round). Player with most points wins. If multiple players hold the same score, whoever goes on top is decided by their total time taken to play the game. Skipping their turn awards no points. Once the game ends, players information is used to create a leaderboard which exists only for that game. """ if "play-again" in request.form: # Reset game and players for a new game. game_multi.overwrite_game_value(game_name, "flag_number", 0) game_multi.overwrite_game_value(game_name, "round_number", 1) game_multi.overwrite_game_value(game_name, "turn_of_player", 0) game_multi.overwrite_game_value(game_name, "players", players_initial) game_multi.overwrite_game_value(game_name, "game_state", "ongoing") game_multi.overwrite_game_value( game_name, "list_of_countries", misc.generate_random_numbers((len(list_of_players) * 5))) return redirect(url_for("gameMulti", game_name=game_name)) elif "continue" in request.form: game_multi.remove_game_from_data(game_name) return redirect(url_for("index")) elif request.form["answer"]: answer = misc.string_to_lowercase_then_titlecase( request.form["answer"]) if answer == official_name_title_cased or answer == common_name_title_cased: if round_number == 5 and turn_of_player == index_num_of_last_player: # Check if the last flag for the last round has been reached game_multi.add_to_elapsed_time_of_player_for_last_flag( game_name, time.time()) game_multi.update_player_list(game_name) game_multi.overwrite_game_value(game_name, "game_state", "ended") elif round_number < 5 and turn_of_player == index_num_of_last_player: game_multi.increase_round_number(game_name) if game_flag_number < (number_of_players) * 5 - 1: """ Check to see if the last flag of the game has been reached e.g 'number_of_players' = 5, 5 * 5 = 20 but index number of last flag is 24(25 flags in list, 5 for each player) hence - 1 Increases turn_of_player before adding to elapsed time of previous player so that on the first turn, last player's elapsed time wont be updated as a result of that player being classed as the previous player of the first player """ game_multi.increase_flag_number(game_name) game_multi.set_turn_of_player(game_name, number_of_players) game_multi.set_start_time_for_current_player(game_name) game_multi.add_to_previous_players_elapsed_time(game_name) game_multi.increase_player_score(turn_of_player, points, game_name) game_multi.reset_incorrect_answers(turn_of_player, game_name) game_multi.reset_points(game_name) return redirect(url_for("gameMulti", game_name=game_name)) else: game_multi.append_incorrect_answers(turn_of_player, request.form["answer"], game_name) game_multi.decrease_points(game_name) return redirect(url_for("gameMulti", game_name=game_name)) elif "skip" in request.form: if round_number == 5 and turn_of_player == index_num_of_last_player: game_multi.add_to_elapsed_time_of_player_for_last_flag( game_name, time.time()) game_multi.update_player_list(game_name) game_multi.overwrite_game_value(game_name, "game_state", "ended") elif round_number < 5 and turn_of_player == index_num_of_last_player: game_multi.increase_round_number(game_name) if game_flag_number < ((number_of_players) * 5) - 1: game_multi.increase_flag_number(game_name) game_multi.set_turn_of_player(game_name, number_of_players) game_multi.set_start_time_for_current_player(game_name) game_multi.add_to_previous_players_elapsed_time(game_name) game_multi.reset_incorrect_answers(turn_of_player, game_name) game_multi.reset_points(game_name) return redirect(url_for("gameMulti", game_name=game_name)) if game_flag_number == 0: """ Set start time of the first player for the first turn of the first round. If placed before conditions for request.form == "POST", sets the start time of the first player again at the start of the second turn resulting in an inaccurate elapsed time of the first turn of the first round. """ game_multi.set_start_time_for_current_player(game_name) return render_template("game-multi.html", players=list_of_players, times_list=elapsed_times, flag_url=flag, turn_of_player=turn_of_player, incorrect_answers=current_player_incorrect_answers, player_score=current_player_score, points=points, round_number=round_number, game_state=game_state, current_player=current_player, number_of_players=number_of_players)