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
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
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 parse_answer(user_name): ## Main POST request for riddle-game if request.method == "POST": data = riddle.riddle_game(user_name) return jsonify(data) data = helper.read_json(helper.profile(user_name)) return jsonify(data)
def parse_setting(user_name): # Create new game if request.method == "POST": data = request.get_json(force=True) riddle.create_riddle_game(data) return jsonify(data) data = helper.read_json(helper.profile(user_name)) return jsonify(data)
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)
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
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 get_results(user_name, riddle_profile): 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") # 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")
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)