def test_if_can_add_to_elapsed_time_for_last_flag_of_game(self): files.append_to_file(game_multi.create_game("testGame26"), "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("testGame26", test_player) game_multi.add_player_to_game("testGame26", test_player_two) game_multi.set_turn_of_player("testGame26", 2) game_multi.set_start_time_for_current_player("testGame26") player_start = game_multi.extract_value_from_player("testGame26", "testPlayer43", "start_time") player_elapsed = game_multi.extract_value_from_player("testGame26", "testPlayer43", "elapsed_time") self.assertEqual(0.0, player_elapsed) current_time = time.time() game_multi.add_to_elapsed_time_of_player_for_last_flag("testGame26", current_time) player_elapsed_two = game_multi.extract_value_from_player("testGame26", "testPlayer43", "elapsed_time") elapsed_time = current_time - player_start self.assertEqual(elapsed_time, player_elapsed_two) game_multi.remove_game_from_data("testGame26")
def test_if_can_set_start_time_of_player_to_current_time(self): files.append_to_file(game_multi.create_game("testGame24"), "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("testGame24", test_player) player_time = game_multi.extract_value_from_player("testGame24", "testPlayer38", "start_time") self.assertEqual(0.0, player_time) game_multi.set_start_time_for_current_player("testGame24") current_time = time.time() player_time_two = game_multi.extract_value_from_player("testGame24", "testPlayer38", "start_time") def within(current_time, player_time, max_difference): # check if current time is within .01sec of player_time_two difference = current_time - player_time if difference > max_difference: return False return True time_within = within(current_time, player_time_two, .01) self.assertTrue(time_within) game_multi.remove_game_from_data("testGame24")
def test_if_can_list_elapsed_time_for_each_player(self): files.append_to_file(game_multi.create_game("testGame29"), "game_multi") list_of_test_players = [{ "username": "******", "score": 43, "rank": 0, "start_time": 0.0, "elapsed_time": 50, "incorrect_answers": [] }, { "username": "******", "score": 66, "rank": 0, "start_time": 0.0, "elapsed_time": 100, "incorrect_answers": [] }] for player in list_of_test_players: game_multi.add_player_to_game("testGame29", player) list_of_times = game_multi.list_players_elapsed_time("testGame29") self.assertIn("1.67min", list_of_times) self.assertIn("50sec", list_of_times) game_multi.remove_game_from_data("testGame29")
def test_if_can_select_player_by_index_and_increase_score(self): files.append_to_file(game_multi.create_game("testGame18"), "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("testGame18", test_player) game_multi.add_player_to_game("testGame18", test_player_two) test_player_score = game_multi.return_player_score(0, "testGame18") test_player_two_score = game_multi.return_player_score(1, "testGame18") self.assertEqual(0, test_player_score) self.assertEqual(0, test_player_two_score) game_multi.increase_player_score(1, 5, "testGame18") test_player_score_two = game_multi.return_player_score(0, "testGame18") test_player_two_score_two = game_multi.return_player_score(1, "testGame18") self.assertEqual(0, test_player_score_two) self.assertEqual(5, test_player_two_score_two) game_multi.remove_game_from_data("testGame18")
def test_if_can_select_player_by_index_and_reset_incorrect_answers(self): files.append_to_file(game_multi.create_game("testGame21"), "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("testGame21", test_player) test_player_answers = game_multi.return_player_incorrect_answers(0, "testGame21") self.assertEqual([], test_player_answers) self.assertEqual(0, len(test_player_answers)) game_multi.append_incorrect_answers(0, "wrong answer", "testGame21") test_player_answers_two = game_multi.return_player_incorrect_answers(0, "testGame21") self.assertIn("wrong answer", test_player_answers_two) self.assertEqual(1, len(test_player_answers_two)) game_multi.reset_incorrect_answers(0, "testGame21") test_player_answers_three = game_multi.return_player_incorrect_answers(0, "testGame21") self.assertEqual([], test_player_answers_three) self.assertEqual(0, len(test_player_answers_three)) game_multi.remove_game_from_data("testGame21")
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 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_select_player_by_index_and_return_incorrect_answers(self): files.append_to_file(game_multi.create_game("testGame19"), "game_multi") test_player = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": ["wrong", "wrong-again"] } game_multi.add_player_to_game("testGame19", test_player) test_player_answers = game_multi.return_player_incorrect_answers(0, "testGame19") self.assertIn("wrong", test_player_answers) self.assertIn("wrong-again", test_player_answers) self.assertEqual(["wrong", "wrong-again"], test_player_answers) self.assertEqual(2, len(test_player_answers)) game_multi.remove_game_from_data("testGame19")
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_check_whether_player_exists_in_game_by_name(self): files.append_to_file(game_multi.create_game("testGame5"), "game_multi") data = files.read_data_file("game_multi") game = game_multi.select_game("testGame5", data) self.assertEqual([], game["players"]) test_player = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] } game_multi.add_player_to_game("testGame5", test_player) check = game_multi.check_if_player_exists("testGame5", "testPlayer26") self.assertTrue(check) game_multi.remove_game_from_data("testGame5")
def test_if_can_overwrite_value_of_given_key_of_player_in_game(self): files.append_to_file(game_multi.create_game("testGame10"), "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("testGame10", test_player) player_rank = game_multi.extract_value_from_player("testGame10", "testPlayer29", "rank") self.assertEqual(0, player_rank) game_multi.overwrite_player_value("testGame10", "testPlayer29", "rank", 1) player_rank_two = game_multi.extract_value_from_player("testGame10", "testPlayer29", "rank") self.assertEqual(1, player_rank_two) game_multi.remove_game_from_data("testGame10")
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_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 test_if_value_for_given_key_can_be_extracted_from_target_player_in_game(self): files.append_to_file(game_multi.create_game("testGame8"), "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("testGame8", test_player) data = files.read_data_file("game_multi") game = game_multi.select_game("testGame8", data) player_start_time = game["players"][0]["start_time"] self.assertEqual(0.0, player_start_time) extracted_value = game_multi.extract_value_from_player("testGame8", "testPlayer28", "score") self.assertEqual(extracted_value, player_start_time) game_multi.remove_game_from_data("testGame8")
def test_if_player_can_be_added_to_game(self): files.append_to_file(game_multi.create_game("testGame4"), "game_multi") data = files.read_data_file("game_multi") game = game_multi.select_game("testGame4", data) self.assertEqual([], game["players"]) test_player = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] } game_multi.add_player_to_game("testGame4", test_player) data_two = files.read_data_file("game_multi") game_two = game_multi.select_game("testGame4", data_two) self.assertIn(test_player, game_two["players"]) game_multi.remove_game_from_data("testGame4")
def test_if_can_add_to_previous_players_elapsed_time(self): files.append_to_file(game_multi.create_game("testGame25"), "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": [] } test_player_three = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] } game_multi.add_player_to_game("testGame25", test_player) game_multi.add_player_to_game("testGame25", test_player_two) game_multi.add_player_to_game("testGame25", test_player_three) game_multi.set_start_time_for_current_player("testGame25") game_multi.set_turn_of_player("testGame25", 3) game_multi.set_start_time_for_current_player("testGame25") game_multi.add_to_previous_players_elapsed_time("testGame25") player_start = game_multi.extract_value_from_player("testGame25", "testPlayer39", "start_time") player_elapsed = game_multi.extract_value_from_player("testGame25", "testPlayer39", "elapsed_time") player_two_start = game_multi.extract_value_from_player("testGame25", "testPlayer40", "start_time") elapsed_time = player_two_start - player_start self.assertEqual(elapsed_time, player_elapsed) game_multi.set_turn_of_player("testGame25", 3) game_multi.set_start_time_for_current_player("testGame25") game_multi.add_to_previous_players_elapsed_time("testGame25") player_two_start_two = game_multi.extract_value_from_player("testGame25", "testPlayer40", "start_time") player_two_elapsed = game_multi.extract_value_from_player("testGame25", "testPlayer40", "elapsed_time") player_three_start = game_multi.extract_value_from_player("testGame25", "testPlayer41", "start_time") elapsed_time_two = player_three_start - player_two_start_two self.assertEqual(elapsed_time_two, player_two_elapsed) game_multi.set_turn_of_player("testGame25", 3) game_multi.set_start_time_for_current_player("testGame25") game_multi.add_to_previous_players_elapsed_time("testGame25") player_three_start_two = game_multi.extract_value_from_player("testGame25", "testPlayer41", "start_time") player_three_elapsed = game_multi.extract_value_from_player("testGame25", "testPlayer41", "elapsed_time") player_start_two = game_multi.extract_value_from_player("testGame25", "testPlayer39", "start_time") elapsed_time_three = player_start_two - player_three_start_two self.assertEqual(elapsed_time_three, player_three_elapsed) game_multi.remove_game_from_data("testGame25")
def test_whether_game_contains_five_players(self): files.append_to_file(game_multi.create_game("testGame6"), "game_multi") data = files.read_data_file("game_multi") check = game_multi.check_if_five_players("testGame6") game = game_multi.select_game("testGame6", data) self.assertFalse(check) test_player = { "username": "******", "score": 0, "rank": 0, "start_time": 0.0, "elapsed_time": 0.0, "incorrect_answers": [] } game_multi.add_player_to_game("testGame6", test_player) game_multi.add_player_to_game("testGame6", test_player) game_multi.add_player_to_game("testGame6", test_player) game_multi.add_player_to_game("testGame6", test_player) game_multi.add_player_to_game("testGame6", test_player) check_two = game_multi.check_if_five_players("testGame6") self.assertTrue(check_two) game_multi.remove_game_from_data("testGame6")