def end_game(user_name, riddle_profile_name, profile):
    user_profile = helper.read_json(f"data/profiles/{user_name}/{user_name}.json")
	# Delete remaining questions first
    profile["game"][0]["question"] = ""
	# Save data to user profile
    user_profile[f"{user_name}"][0]["finished_riddles"].append(
        profile["game"][0])
    helper.write_to_json(
        f"data/profiles/{user_name}/{user_name}.json", "w", user_profile)
    new_profiles = []
    profiles = helper.read_txt(
        f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt")
    riddle_profile_name = riddle_profile_name + "\n"
    for riddle_profile in profiles:
        if riddle_profile_name == riddle_profile:
            helper.write_to_txt(
                f"data/profiles/{user_name}/riddle_game/finished_riddles.txt", "a", riddle_profile_name)
        else:
            new_profiles.append(riddle_profile)
    if len(new_profiles) == 0:
        helper.write_to_txt(
            f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt", "w", "")
    else:
        helper.write_to_txt(
            f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt", "w", new_profiles)
	# Append finished game to statistics
    statistics = helper.read_json("data/riddle-game/statistics.json")
    statistics['profiles'].append(profile["game"][0])
    helper.write_to_json("data/riddle-game/statistics.json", "w", statistics)
    return profile
def riddle_game(user_name, riddle_profile_name, data):
    questions = helper.read_json(
        helper.questions(user_name, riddle_profile_name))
    profile = helper.read_json(helper.profile(user_name, riddle_profile_name))

    # Format both user as well as correct answer
    user_answer = string_format(data["data"])
    correct_answer = string_format(questions["questions"][0]["answer"])

    if user_answer == correct_answer:
        profile["game"][0]["right_answers"] += 1
        profile["game"][0]["remaining_questions"] -= 1
        profile["game"][0]["result"] = "Correct"
        questions["questions"].pop(0)
        if len(questions["questions"]) > 0:
            profile["game"][0]["question"] = pick_question(questions)
        else:
            profile = end_game(user_name, riddle_profile_name, profile)
        helper.write_to_json(helper.questions(
            user_name, riddle_profile_name), "w", questions)
        helper.write_to_json(helper.profile(
            user_name, riddle_profile_name), "w", profile)
        return profile
    else:
        profile["game"][0]["result"] = "Wrong"
        profile["game"][0]["wrong_answers"] += 1
        if profile["game"][0]["tries"] > 0 and profile["game"][0]["mods"] == "limited":
            profile["game"][0]["tries"] -= 1
            if profile["game"][0]["tries"] == 0:
                profile = end_game(user_name, riddle_profile_name, profile)

        helper.write_to_json(helper.profile(
            user_name, riddle_profile_name), "w", profile)
        return profile
def index():
    app_data = helper.read_json('data/system/app_data.json')
    app_data = app_data['1.4'][0]["members"]
    # Render index.html by default
    return render_template("index.html",
                           members=app_data,
                           page_title="Riddle Game")
def skip_question(user_name, riddle_profile_name):
    questions = helper.read_json(
        helper.questions(user_name, riddle_profile_name))
    profile = helper.read_json(
        helper.profile(user_name, riddle_profile_name))
    if len(questions["questions"]) == 1:
        return profile
    skipped_question = questions["questions"].pop(0)
    questions["questions"].append(skipped_question)

    profile["game"][0]["question"] = pick_question(questions)
    profile["game"][0]["skipped_questions"] += 1
    helper.write_to_json(helper.profile(
        user_name, riddle_profile_name), "w", profile)
    helper.write_to_json(helper.questions(
        user_name, riddle_profile_name), "w", questions)
    return profile
def log_in(user_name):
    profiles = helper.read_txt("data/profiles/all-profiles.txt")
    profile = user_name.lower() + "\n"
    if profile in profiles:
        session['user'] = {'user_name': user_name}
        return jsonify(
            helper.read_json(f"data/profiles/{user_name}/{user_name}.json"))
    else:
        return jsonify("no profile")
def delete_question(user_name, riddle_profile_name):
    questions = helper.read_json(
        helper.questions(user_name, riddle_profile_name))
    profile = helper.read_json(helper.profile(user_name, riddle_profile_name))
    profile["game"][0]["deleted_questions"] += 1
    questions["questions"].pop(0)
    profile["game"][0]["remaining_questions"] = len(questions["questions"])

    if len(questions["questions"]) > 0:
        profile["game"][0]["question"] = pick_question(questions)
    else:
        profile = end_game(user_name, riddle_profile_name, profile)

    helper.write_to_json(helper.profile(
            user_name, riddle_profile_name), "w", profile)
    helper.write_to_json(helper.questions(
        user_name, riddle_profile_name), "w", questions)
    return profile
def show_statistics(user_name):
    if 'user' in session:
        if user_name == session['user']['user_name']:
            user_profile = helper.read_json(
                f"data/profiles/{user_name}/{user_name}.json")
            finished_games = user_profile[f"{user_name}"][0][
                "finished_riddles"]
            riddle_profiles = helper.read_txt(
                f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt")
            statistics = helper.read_json("data/riddle-game/statistics.json")
            statistics = sorted(statistics['profiles'],
                                key=lambda k: k['right_answers'],
                                reverse=True)
            return render_template("statistics.html",
                                   finished_games=finished_games,
                                   riddle_profiles=riddle_profiles,
                                   user_name=user_name,
                                   statistics=statistics[:10],
                                   page_title="Statistics")
    return redirect(url_for('index'))
def parse_answer(user_name, riddle_profile):
    # Main POST request for riddle-game
    if request.method == "POST":
        post_data = request.get_json(force=True)
        if post_data["id"] == "answer":
            data = riddle.riddle_game(user_name, riddle_profile, post_data)
            return jsonify(data)
        elif post_data["id"] == "skip_question":
            data = riddle.skip_question(user_name, riddle_profile)
            return jsonify(data)
        else:
            data = riddle.delete_question(user_name, riddle_profile)
            return jsonify(data)
    data = helper.read_json(helper.profile(user_name, riddle_profile))
    return jsonify(data)
def create_questions_file(game_profile, user_name, riddle_profile_name):
    # Copy question file to work with fresh file
    if game_profile["game"][0]["categories"] == "all":
        copyfile("data/riddle-game/all.json",
                 helper.questions(user_name, riddle_profile_name))

    elif game_profile["game"][0]["categories"] == "general":
        copyfile("data/riddle-game/general.json",
                 helper.questions(user_name, riddle_profile_name))
    else:
        copyfile("data/riddle-game/mixed.json",
                 helper.questions(user_name, riddle_profile_name))

        # To shuffle the questions that every game is different
    questions = helper.read_json(helper.questions(user_name, riddle_profile_name))
    random.shuffle(questions["questions"])
    # Pick question from the database
    game_profile["game"][0]["question"] = pick_question(questions)
    return questions
Esempio n. 10
0
def get_results(user_name, riddle_profile):
    if 'user' in session:
        if user_name == session['user']['user_name']:
            riddle_profiles = helper.read_txt(
                f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt")
            profile = helper.read_json(
                helper.profile(user_name, riddle_profile))
            profile = profile["game"][0]
            if profile["mods"] == "limited":
                return render_template("riddle-game.html",
                                       user_name=user_name,
                                       riddle_profiles=riddle_profiles,
                                       riddle_profile=riddle_profile,
                                       tries=int(profile["tries"]),
                                       page_title="Riddle Game")
            else:
                # Render riddle-game template by default
                return render_template("riddle-game.html",
                                       user_name=user_name,
                                       riddle_profiles=riddle_profiles,
                                       riddle_profile=riddle_profile,
                                       tries=int(0),
                                       page_title="Riddle Game")
    return redirect(url_for('index'))
Esempio n. 11
0
def get_app_data():
    app_data = helper.read_json('data/app_data.json')
    return jsonify(app_data)