示例#1
0
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)
        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]["wrong_answers"] += 1
        if profile["game"][0]["tries"] > 0 and profile["game"]:
            pass
        profile["game"][0]["result"] = "Wrong"
        helper.write_to_json(helper.profile(user_name, riddle_profile_name),
                             "w", profile)
        return profile
示例#2
0
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))
    questions["questions"].pop(0)

    if len(questions["questions"]) > 0:
        profile["game"][0]["question"] = pick_question(questions)

    profile["game"][0]["deleted_questions"] += 1
    profile["game"][0]["remaining_questions"] = len(questions["questions"])
    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
示例#3
0
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
示例#4
0
def create_questions_file(game_profile, user_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))

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

## To shuffle the questions that every game is different
    questions = helper.read_json(helper.questions(user_name))
    random.shuffle(questions["questions"])
    # Pick question from the database
    game_profile["game"][0]["question"] = pick_question(questions)
    return questions
示例#5
0
def create_riddle_game(data):
    user_name = data["riddle_game_data"]["id"]
    game_profile = create_game_profile(data, user_name)
    questions = create_questions_file(game_profile, user_name)
    # Write data
    helper.write_to_txt(f"data/riddle-game/all-players.txt", "a",
                        f"{user_name}" + '\n')
    helper.write_to_json(helper.profile(user_name), "w", game_profile)
    helper.write_to_json(helper.questions(user_name), "w", questions)
    return jsonify(game_profile)
示例#6
0
def create_riddle_game(data):
    user_name = data["riddle_game_data"]["id"]
    riddle_profile_name = data["riddle_game_data"]["riddle_profile_name"]
    game_profile = create_game_profile(data, user_name, riddle_profile_name)
    questions = create_questions_file(game_profile, user_name,
                                      riddle_profile_name)
    game_profile["game"][0]["questions_in_total"] = len(questions["questions"])
    game_profile["game"][0]["remaining_questions"] = game_profile["game"][0][
        "questions_in_total"]
    # Write data
    helper.write_to_txt(f"data/riddle-game/all-players.txt", "a",
                        f"{riddle_profile_name}" + '\n')
    helper.write_to_txt(
        f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt", "a",
        f"{riddle_profile_name}" + '\n')
    helper.write_to_json(helper.profile(user_name, riddle_profile_name), "w",
                         game_profile)
    helper.write_to_json(helper.questions(user_name, riddle_profile_name), "w",
                         questions)
    return jsonify(game_profile)
示例#7
0
def riddle_game(user_name):
    questions = helper.read_json(helper.questions(user_name))
    profile = helper.read_json(helper.profile(user_name))
    data = request.get_json(force=True)
    ## Format both user as well as correct answer
    user_answer = string_format(data["answer"])
    correct_answer = string_format(questions["questions"][0]["answer"])

    if user_answer == correct_answer:
        profile["game"][0]["right_answers"] += 1
        profile["game"][0]["result"] = "Correct"
        helper.write_to_json(helper.profile(user_name), "w", profile)
        return profile
    else:
        profile["game"][0]["wrong_answers"] += 1
        profile["game"][0]["result"] = "Wrong"
        helper.write_to_json(helper.profile(user_name), "w", profile)
        return profile
    print(correct_answer)
    print(user_answer)
    print(profile)

    return correct_answer