コード例 #1
0
 def test_value_of_key__in_game_obj_can_be_extracted(self):
     users.add_player_to_data("testUser11", "none", game_single.create_game())
     points = game_single.extract_value_from_game("testUser11", "points")
     question_number = game_single.extract_value_from_game("testUser11", "question_number")
     self.assertEqual(5, points)
     self.assertEqual(1, question_number)
     
     users.remove_player_from_data("testUser11")
コード例 #2
0
 def test_that_question_number_can_be_increased(self):
     users.add_player_to_data("testUser12", "none", game_single.create_game())
     question_number = game_single.extract_value_from_game("testUser12", "question_number")
     self.assertEqual(1, question_number)
     
     game_single.increase_question_number("testUser12")
     question_number_updated = game_single.extract_value_from_game("testUser12", "question_number")
     self.assertEqual(2, question_number_updated)
     
     users.remove_player_from_data("testUser12")
コード例 #3
0
 def test_if_game_duration_can_be_set(self):
     users.add_player_to_data("testUser20", "none", game_single.create_game())
     time = game_single.extract_value_from_game("testUser20", "game_duration")
     self.assertEqual(0, time)
     
     game_single.set_game_duration("testUser20", 20)
     new_time = game_single.extract_value_from_game("testUser20", "game_duration")
     self.assertEqual(20, new_time)
     
     users.remove_player_from_data("testUser20")
コード例 #4
0
 def test_if_win_state_can_be_set(self):
     users.add_player_to_data("testUser18", "none", game_single.create_game())
     win_state = game_single.extract_value_from_game("testUser18", "win")
     self.assertEqual("not", win_state)
     
     game_single.set_win_state("testUser18", "won")
     new_win_state = game_single.extract_value_from_game("testUser18", "win")
     self.assertEqual("won", new_win_state)
     
     users.remove_player_from_data("testUser18")
コード例 #5
0
 def test_if_incorrect_answer_can_be_appended(self):
     users.add_player_to_data("testUser14", "none", game_single.create_game())
     list_of_incorrect_answers = game_single.extract_value_from_game("testUser14", "incorrect_answers")
     self.assertEqual([], list_of_incorrect_answers)
     self.assertEqual(0, len(list_of_incorrect_answers))
     
     game_single.append_incorrect_answers("testUser14", "im-a-wrong-answer")
     list_of_incorrect_answers_updated = game_single.extract_value_from_game("testUser14", "incorrect_answers")
     self.assertIn("im-a-wrong-answer", list_of_incorrect_answers_updated)
     self.assertEqual(1, len(list_of_incorrect_answers_updated))
     
     users.remove_player_from_data("testUser14")
コード例 #6
0
 def test_if_incorrect_answer_list_can_be_reset(self):
     users.add_player_to_data("testUser15", "none", game_single.create_game())
     game_single.append_incorrect_answers("testUser15", "im-a-wrong-answer")
     game_single.append_incorrect_answers("testUser15", "im-a-wrong-answer-too")
     
     list_of_incorrect_answers = game_single.extract_value_from_game("testUser15", "incorrect_answers")
     self.assertEqual(["im-a-wrong-answer", "im-a-wrong-answer-too"], list_of_incorrect_answers)
     
     game_single.reset_incorrect_answers("testUser15")
     list_of_incorrect_answers_updated = game_single.extract_value_from_game("testUser15", "incorrect_answers")
     self.assertEqual([], list_of_incorrect_answers_updated)
     
     users.remove_player_from_data("testUser15")
コード例 #7
0
 def test_if_can_reset_points(self):
     users.add_player_to_data("testUser17", "none", game_single.create_game())
     points = game_single.extract_value_from_game("testUser17", "points")
     self.assertEqual(5, points)
     
     game_single.decrease_round_points("testUser17")
     points_two = game_single.extract_value_from_game("testUser17", "points")
     self.assertEqual(3, points_two)
     
     game_single.reset_round_points("testUser17")
     points_two = game_single.extract_value_from_game("testUser17", "points")
     self.assertEqual(5, points_two)
     
     users.remove_player_from_data("testUser17")
コード例 #8
0
def create_leaderboard_player(username):
    score = users.return_player_score(username)
    game_duration = game_single.extract_value_from_game(username, "game_duration")
    player = {
        "username": username,
        "score": score,
        "duration": game_duration,
        "rank": 0
    }
    
    return player
コード例 #9
0
 def test_if_win_state_can_be_set_according_to_score(self):
     users.add_player_to_data("testUser19", "none", game_single.create_game())
     win_state = game_single.extract_value_from_game("testUser19", "win")
     score = users.return_player_score("testUser19")
     self.assertEqual("not", win_state)
     self.assertEqual(0, score)
     
     game_single.check_for_win(score, "testUser19")
     updated_win_state = game_single.extract_value_from_game("testUser19", "win")
     self.assertEqual("lost", updated_win_state)
     
     users.increase_player_score("testUser19", 100)
     updated_score = users.return_player_score("testUser19")
     self.assertEqual(100, updated_score)
     
     game_single.check_for_win(updated_score, "testUser19")
     updated_win_state_two = game_single.extract_value_from_game("testUser19", "win")
     self.assertEqual("won", updated_win_state_two)
     
     users.remove_player_from_data("testUser19")
コード例 #10
0
 def test_if_list_of_random_numbers_of_countries_in_game_obj_can_be_updated(self):
     users.add_player_to_data("testUser21", "none", game_single.create_game())
     list_of_numbers = game_single.extract_value_from_game("testUser21", "countries")
     
     def check_if_every_item_is_int_in_list(list_of_items):
         for item in list_of_items:
             if type(item) == int:
                 return True
             else:
                 return False
                 
     self.assertEqual(20, len(list_of_numbers))
     self.assertTrue(check_if_every_item_is_int_in_list(list_of_numbers))
     
     game_single.generate_new_list_of_random_countries("testUser21")
     new_list = game_single.extract_value_from_game("testUser21", "countries")
     self.assertEqual(20, len(new_list))
     self.assertTrue(check_if_every_item_is_int_in_list(new_list))
     self.assertNotEqual(list_of_numbers, new_list)
     
     users.remove_player_from_data("testUser21")
コード例 #11
0
def leaderboard(username):
    """
    Route for the leaderboard page which displays a global leaderboard available to
    any player who creates an account. The leaderboard displays only the top ten best players.
    """
    top_ten = leaderboard_single.create_top_ten_players_list(
        files.read_data_file("leaderboard"))
    list_of_times = leaderboard_single.list_game_duration_for_each_player(
        files.read_data_file("leaderboard"))

    win = game_single.extract_value_from_game(username, "win")
    time = game_single.extract_value_from_game(username, "game_duration")

    user_score = users.return_player_score(username)
    users.reset_player_score(username)

    game_single.reset_question_number(username)
    game_single.reset_incorrect_answers(username)
    game_single.reset_round_points(username)
    game_single.generate_new_list_of_random_countries(username)

    if request.method == "POST":
        game_single.set_win_state(username, "not-won")
        if "play-again" in request.form:
            game_single.set_game_duration_current(username)
            return redirect(url_for("gameSingle", username=username))
        elif "continue" in request.form:
            return redirect(url_for("leaderboard", username=username))

    return render_template("leaderboard-single.html",
                           username=username,
                           win=win,
                           score=user_score,
                           top_ten=top_ten,
                           time=time,
                           list_of_times=list_of_times)
コード例 #12
0
def gameSingle(username):
    user_score = users.return_player_score(username)

    question_number = game_single.extract_value_from_game(
        username, "question_number")
    points = game_single.extract_value_from_game(username, "points")
    incorrect_answers = game_single.extract_value_from_game(
        username, "incorrect_answers")
    list_of_countries = game_single.extract_value_from_game(
        username, "countries")

    country_for_current_round = list_of_countries[question_number - 1]
    flag = countries.return_value_from_country(country_for_current_round,
                                               "flag")
    official_name = countries.return_value_from_country(
        country_for_current_round, "name-official")
    common_name = countries.return_value_from_country(
        country_for_current_round, "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)

    if request.method == "POST":
        """
        Single player game functionality. Provides behaviour for buttons used to
        skip questions and to give up on the game, as well as conditions for what
        happens if the player submits an answer. Submitted information is checked against data
        to see whether it's correct or not. Upon winning the game or simply completing it by skipping
        the last question, player is redirected to the leaderboard page. Players are either added to 
        leaderboard data in the event of it being their first time playing or updated if they already exist 
        on the leaderboard. If a player gives up on the game, leaderboard data is not updated. Winning 
        conditions are based on the number of questions and total player score. Full points - 100 are 
        needed in order to win otherwise the game is just completed.
        """
        if "skip" in request.form:
            if question_number == 20:
                end_time = time.time()
                start_time = game_single.extract_value_from_game(
                    username, "game_duration")
                game_single.set_game_duration(
                    username,
                    misc.calculate_game_duration(start_time, end_time))
                game_single.check_for_win(user_score, username)
                leaderboard_player = leaderboard_single.create_leaderboard_player(
                    username)
                if leaderboard_single.check_if_player_already_on_leaderboard(
                        username, files.read_data_file("leaderboard")) == True:
                    leaderboard_single.update_player_on_leaderboard(
                        leaderboard_player,
                        files.read_data_file("leaderboard"))
                else:
                    files.append_to_file(leaderboard_player, "leaderboard")
                leaderboard_single.update_leaderboard(
                    files.read_data_file("leaderboard"))
                return redirect(url_for("leaderboard", username=username))
            else:
                game_single.increase_question_number(username)
                game_single.reset_incorrect_answers(username)
                game_single.reset_round_points(username)
                return redirect(url_for("gameSingle", username=username))
        elif "give-up" in request.form or "play-again" in request.form:
            end_time = time.time()
            start_time = game_single.extract_value_from_game(
                username, "game_duration")
            game_single.set_game_duration(
                username, misc.calculate_game_duration(start_time, end_time))
            game_single.check_for_win(user_score, username)
            return redirect(url_for("leaderboard", username=username))
        elif "answer" in request.form:
            answer = misc.string_to_lowercase_then_titlecase(
                request.form["answer"])
            if question_number == 20 and (answer == official_name
                                          or answer == common_name):
                users.increase_player_score(username, points)
                end_time = time.time()
                start_time = game_single.extract_value_from_game(
                    username, "game_duration")
                game_single.set_game_duration(
                    username,
                    misc.calculate_game_duration(start_time, end_time))
                game_single.check_for_win(user_score, username)
                leaderboard_player = leaderboard_single.create_leaderboard_player(
                    username)
                if leaderboard_single.check_if_player_already_on_leaderboard(
                        username, files.read_data_file("leaderboard")) == True:
                    leaderboard_single.update_player_on_leaderboard(
                        leaderboard_player,
                        files.read_data_file("leaderboard"))
                else:
                    files.append_to_file(leaderboard_player, "leaderboard")
                leaderboard_single.update_leaderboard(
                    files.read_data_file("leaderboard"))
                return redirect(url_for("leaderboard", username=username))
            else:
                if answer == official_name_title_cased or answer == common_name_title_cased:
                    game_single.increase_question_number(username)
                    users.increase_player_score(username, points)
                    game_single.reset_incorrect_answers(username)
                    game_single.reset_round_points(username)
                    return redirect(url_for("gameSingle", username=username))
                else:
                    game_single.append_incorrect_answers(
                        username, request.form["answer"])
                    game_single.decrease_round_points(username)
                    return redirect(url_for("gameSingle", username=username))

    return render_template("game-single.html",
                           username=username,
                           flag_url=flag,
                           question_number=question_number,
                           round_points=points,
                           incorrect_answers=incorrect_answers,
                           score=user_score)