def test_new_chosen_and_previous_player_can_restart_index(self): """ test to check that set_new_chosen_and_previous_player returns to index 0 when and only when the final player in the list has a turn value of True """ should_not_be_index_0_turn = [{"username": "******", "turn": False, "previous": True}, {"username": "******", "turn": True, "previous": False}, {"username": "******", "turn": False, "previous": False}, {"username": "******", "turn": False, "previous": False}] json_functions.dump_data(should_not_be_index_0_turn) json_functions.set_new_chosen_and_previous_player() result = json_functions.get_json_data() self.assertFalse(result[0]["turn"]) should_be_index_0_turn = [{"username": "******", "turn": False, "previous": False}, {"username": "******", "turn": False, "previous": False}, {"username": "******", "turn": False, "previous": True}, {"username": "******", "turn": True, "previous": False}] json_functions.dump_data(should_be_index_0_turn) json_functions.set_new_chosen_and_previous_player() result = json_functions.get_json_data() self.assertTrue(result[0]["turn"])
def test_return_player_to_game_replaces_previous_value(self): """ test to check that return_player_to_game_data replaces the player entered as an argument. The player should be replaced in the dictionary, not appended """ test_game_data = [ { "no": 10, "test": "original value", }, {"no": 5, "test": "original value" } ] json_functions.dump_data(test_game_data) original_data = json_functions.get_json_data() replacement_player = { "no": 5, "test": "success!", "additional key": True } json_functions.return_player_to_game_data(replacement_player) new_data = json_functions.get_json_data() self.assertEqual(len(original_data), len(new_data)) self.assertNotEqual(original_data, new_data) self.assertEqual(new_data[1]["test"], "success!") self.assertTrue(new_data[1]["additional key"])
def render_game(): """ main game loop """ wipe_game_text() game_data = get_json_data() if request.method == "POST": # only runs after form has been submitted once. Won't run first time page is loaded set_new_chosen_and_previous_player() set_previous_answer() was_correct = check_previous_player_answer() update_lives_and_score(was_correct) eliminated_player = eliminate_dead_players() if eliminated_player: get_correct_answer(eliminated_player) if not all_players_gone(): set_player_question() ask_question() game_data = get_json_data() else: # runs the first time the page is loaded, before the users submit any answers. Doesn't run again initialize_used_question() game_data[0]["turn"] = True dump_data(game_data) set_player_question() ask_question() if all_players_gone( ): # adds game over and correct answer text and renders leaderboard template wipe_game_text(True) add_game_over_text() leaderboard_data = get_sorted_scores() round_text = get_round_text() return render_template("leaderboard.html", leaderboard_data=leaderboard_data, answer_text="".join(round_text["answer text"]), game_over_text="".join( round_text["game over text"])) col_size = get_col_size(game_data) col_sm_size = get_col_sm_size(game_data) round_text = get_round_text() game_data = get_json_data() return render_template( "game.html", correct_text="".join(round_text["correct text"]), eliminated_text="".join(round_text["eliminated text"]), answer_text="".join(round_text["answer text"]), host_text="".join(round_text["host text"]), question_text="".join(round_text["question text"]), incorrect_guesses_text="".join(round_text["incorrect guesses text"]), col_size=col_size, col_sm_size=col_sm_size, game_data=game_data)
def test_chosen_player_changes_to_previous_player(self): """ test to check that set_new_chosen_and_previous_player() the player with turn: True has turn changed to False and previous turned to true """ test_players = [{"username": "******", "turn": False, "previous": True}, {"username": "******", "turn": True, "previous": False}, {"username": "******", "turn": False, "previous": False}] json_functions.dump_data(test_players) test_players = json_functions.get_json_data() self.assertTrue(test_players[1]["turn"]) self.assertFalse(test_players[1]["previous"]) json_functions.set_new_chosen_and_previous_player() test_players = json_functions.get_json_data() self.assertFalse(test_players[1]["turn"]) self.assertTrue(test_players[1]["previous"])
def test_players_with_0_lives_are_eliminated(self): """ test to check that eliminate_dead_players will remove a player with 0 lives """ no_dead_players = [{"no": 1, "lives": 2, "username": "******"}, {"no": 2, "lives": 1, "username": "******"}, {"no": 3, "lives": 6, "username": "******"}] json_functions.dump_data(no_dead_players) json_functions.eliminate_dead_players() result = json_functions.get_json_data() self.assertEqual(len(no_dead_players), len(result)) one_dead_player = [{"no": 1, "lives": 2, "username": "******"}, {"no": 2, "question": ("", "", "", ""), "score": 2, "lives": 0, "username": "******"}, {"no": 3, "lives": 6, "username": "******"}] json_functions.dump_data(one_dead_player) json_functions.eliminate_dead_players() result = json_functions.get_json_data() self.assertEqual(len(result), len(one_dead_player) - 1) for player in result: self.assertNotEqual(player["no"], 2)
def test_dump_data_and_get_data_return_equal_values(self): """ test to check that the data entered into dump data will be the same as the value returned by get_json_data """ data_to_dump = [{ "should be 1": 1, "should be false": False, "should be 'test'": "test" }, { "another": "dictionary" }] json_functions.dump_data(data_to_dump) retrieved_data = json_functions.get_json_data() retrieved_dictionary = retrieved_data[0] self.assertEqual(len(retrieved_data), 2) self.assertEqual(retrieved_dictionary["should be 1"], 1) self.assertEqual(retrieved_dictionary["should be false"], False) self.assertEqual(retrieved_dictionary["should be 'test'"], "test")
def test_correctly_add_or_sum_score_and_lives(self): """ test to check that update_lives_and_score adds 1 to the previous player's score if True is entered as an argument. If false is entered, it should subtract 1 from the previous player's life """ test_data = [{"previous": True, "lives": 0, "score": 0, "no": 1}] json_functions.dump_data(test_data) to_add_to_score = 5 to_subtract_from_lives = 3 for x in range(to_add_to_score): gameplay_loop.update_lives_and_score(True) for y in range(to_subtract_from_lives): gameplay_loop.update_lives_and_score(False) result = json_functions.get_json_data() self.assertEqual(result[0]["score"], to_add_to_score) self.assertEqual(result[0]["lives"], (- to_subtract_from_lives))
def ask_question(): """ asks question to the user also adds introduction host text """ current_player = get_current_player() game_data = get_json_data() number_of_players = len(game_data) if number_of_players > 1: add_host_text("You're up {}".format(current_player["username"])) if not last_question_correct(current_player): add_host_text("Let's try that again...") add_incorrect_guesses_text(current_player) question = current_player["question"] if question_is_picture_question(question): img_text = "<img src ='{}'>".format(question[0]) add_question_text(img_text) add_question_text(question[1]) else: add_question_text(question[0])