Пример #1
0
    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"])
Пример #2
0
    def test_check_previous_answer_correctly_grades_answer(self):
        """
        test to check that check_previous_player_answer adds 
        'Correct!' to correct.txt if the players answer contains 
        the questions keyword. If it does not, it should write 'incorrect'
        """
        game_text_functions.wipe_game_text()
        should_write_incorrect = [{"question": ["What starts with a 'P', ends with an 'E'and has thousands of letters?",
                                                "The Post Office", "post"], "answer": ["wrong", "answer"],
                                   "previous": True, "incorrect guesses": "", "no": 1}]
        json_functions.dump_data(should_write_incorrect)
        gameplay_loop.check_previous_player_answer()
        with open("active-game-files/correct.txt", "r") as f:
            result = f.readlines()
        result_should_be = "<span class='incorrect'>Incorrect</span>"
        self.assertEqual("".join(result), result_should_be)

        game_text_functions.wipe_game_text()
        should_write_incorrect = [{"question": ["What starts with a 'P', ends with an 'E'and has thousands of letters?",
                                                "The Post Office", "post"], "answer": ["test", "post"],
                                   "previous": True, "incorrect guesses": "", "no": 1}]
        json_functions.dump_data(should_write_incorrect)
        gameplay_loop.check_previous_player_answer()
        with open("active-game-files/correct.txt", "r") as f:
            result = f.readlines()
        result_should_be = "<span class='correct'>Correct!</span>"
        self.assertEqual("".join(result), result_should_be)
Пример #3
0
    def test_ask_question_writes_current_player_question(self):
        """
        test to check that ask_question writes the appropriate 
        question tuple entry for the player with a turn value of
        True
        """
        game_text_functions.wipe_game_text()

        test_player_data = [
            {"previous": False, "last question correct": True, "no": 1, "incorrect guesses": "", "username": "******",
             "turn": False, "lives": 2,
             "question": ["This kind of coat can you put on only when it is wet. What is it?", "A coat of paint",
                          "paint"], "answer": ["a", "paint", "of", "coat"], "score": 5},
            {"previous": True, "last question correct": False, "no": 2, "incorrect guesses": "<br>the dictionary",
             "username": "******", "turn": False, "lives": 1, "question": [
                "I am a word. If you pronounce me rightly, it will be wrong. If you pronounce me wrong it is right? What word am I?",
                "Wrong", "wrong"], "answer": ["the", "dictionary"], "score": 4},
            {"previous": False, "last question correct": True, "no": 3, "incorrect guesses": "", "username": "******",
             "turn": True, "lives": 2,
             "question": ["It has Eighty-eight keys but can't open a single door? What is it?", "A piano", "piano"],
             "answer": "", "score": 4}]

        json_functions.dump_data(test_player_data)

        gameplay_loop.ask_question()

        result_should_be = "It has Eighty-eight keys but can't open a single door? What is it?<br>"

        with open("active-game-files/question.txt", "r") as f:
            result = f.readlines()

        self.assertEqual("".join(result), result_should_be)
Пример #4
0
    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"])
Пример #5
0
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)
Пример #6
0
    def test_get_current_player_does_not_return_players_with_turn_false(self):
        """
        test to check that get_current_player won't return players with a turn of
        false
        """
        one_true = [{"turn": False}, {"turn": False}, {"turn": True}, {"turn": False}]
        json_functions.dump_data(one_true)
        one_true_result = json_functions.get_current_player()
        self.assertNotEqual(one_true_result["turn"], False)
        self.assertTrue(one_true_result["turn"])

        all_false = [{"turn": False}, {"turn": False}]
        json_functions.dump_data(all_false)
        all_false_result = json_functions.get_current_player()
        self.assertEqual(all_false_result, {})
Пример #7
0
    def test_all_players_gone_returns_accurate_boolean(self):
        """
        test to check that all_players_gone returns a 
        boolean. Should be False if players.json is not 
        empty. Otherwise should be True
        """
        result = json_functions.all_players_gone()
        self.assertEqual(type(result), bool)

        empty_list = []
        json_functions.dump_data(empty_list)
        self.assertTrue(json_functions.all_players_gone())

        not_empty_list = [1, 2, 3]
        json_functions.dump_data(not_empty_list)
        self.assertFalse(json_functions.all_players_gone())
Пример #8
0
    def test_elimination_text_correctly_identifies_player(self):
        """
        test to check that add_eliminated_text correctly identifies
        the username of the player that was eliminated
        """
        game_text_functions.wipe_game_text()
        test_game_data = [{"no": 1, "lives": 2, "username": "******"},
                          {"no": 2, "question": ("", "", "", ""), "score": 2, "lives": 0, "username": "******"},
                          {"no": 3, "lives": 6, "username": "******"}]
        json_functions.dump_data(test_game_data)
        json_functions.eliminate_dead_players()
        with open("active-game-files/eliminated.txt", "r") as f:
            result = f.readlines()

        result_should_be = "success has been eliminated"
        self.assertEqual("".join(result), result_should_be)
Пример #9
0
    def test_get_previous_player_returns_empty_dictionary_if_no_previous_player(self):
        """
        test to check that get_previous_player returns an empty dictionary if json 
        file contains no previous player. Otherwise should return the player with a 
        previous value of True
        """
        no_previous_player = [{"username": "******", "turn": False, "previous": False},
                              {"username": "******", "turn": True, "previous": False},
                              {"username": "******", "turn": False, "previous": False},
                              {"username": "******", "turn": False, "previous": False}]
        json_functions.dump_data(no_previous_player)
        previous_player = json_functions.get_previous_player()
        self.assertEqual(previous_player, {})

        a_previous_player = [{"username": "******", "turn": False, "previous": True},
                             {"username": "******", "turn": True, "previous": False},
                             {"username": "******", "turn": False, "previous": False},
                             {"username": "******", "turn": False, "previous": False}]
        json_functions.dump_data(a_previous_player)
        previous_player = json_functions.get_previous_player()
        self.assertEqual(previous_player["username"], "p1")
Пример #10
0
    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"])
Пример #11
0
    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")
Пример #12
0
    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)
Пример #13
0
    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))