From 13e7c35a1a56b2efc32105bae924998e4ea86160 Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Thu, 26 May 2022 10:27:26 +0200 Subject: [PATCH] Add base game functionnality --- .gitignore | 3 ++- app.py | 27 ++++++++++++++++++++++++++- game.py | 31 +++++++++++++++++++++++++++++++ templates/base.html | 1 + templates/game/answer.html | 4 ++++ templates/game/new.html | 5 +++++ templates/game/question.html | 13 +++++++++++++ templates/index.html | 1 - templates/menu.html | 6 ++++++ 9 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 game.py create mode 100644 templates/game/answer.html create mode 100644 templates/game/new.html create mode 100644 templates/game/question.html create mode 100644 templates/menu.html diff --git a/.gitignore b/.gitignore index 40b9826..1709f0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ venv config.py -__pycache__ \ No newline at end of file +__pycache__ +data/ \ No newline at end of file diff --git a/app.py b/app.py index bba6710..8909503 100644 --- a/app.py +++ b/app.py @@ -9,6 +9,7 @@ from werkzeug.security import generate_password_hash, check_password_hash from config import secret_key, database_uri from model import db, User +import game as Game app = Flask(__name__) app.secret_key = secret_key @@ -65,4 +66,28 @@ def login(): def logout(): # Remove username from the session if it's there session.pop("username", None) - return redirect(url_for("home")) \ No newline at end of file + return redirect(url_for("home")) + +# Game routes + +@app.route("/game") +def game(): + return redirect(url_for('new_game')) + +@app.route("/game/new") +def new_game(): + question = Game.new_question() + session["question"] = question + return render_template("game/question.html", question=question) + +@app.route("/game/answer", methods=["POST", "GET"]) +def game_answer(): + if request.method == "POST": + answer = request.form["answer"] + if answer == session["question"]["species"]: + message = "You are correct !" + else: + message = "You are not correct !" + return render_template("game/answer.html", message=message) + elif request.method == "GET": + return render_template("game/new.html") \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..c1df3ca --- /dev/null +++ b/game.py @@ -0,0 +1,31 @@ +import random +import os +from glob import glob + +bird_species_folders = list(map(os.path.basename, glob("static/data/src_audio/*"))) + +format_name = lambda folder_name : folder_name.replace('_', ' ') + +def get_proposals(question_species_name, n): + proposals = [question_species_name] + for i in range(n): + proposition = format_name(random.choice(bird_species_folders)) + while proposition == question_species_name: + proposition = format_name(random.choice(bird_species_folders)) + proposals.append(proposition) + random.shuffle(proposals) + return proposals + +def new_question(): + question_species_folder = random.choice(bird_species_folders) + question_species_name = format_name(question_species_folder) + print(question_species_folder) + question = {} + audio_paths = list(map(os.path.basename, glob(f"static/data/src_audio/{question_species_folder}/*.mp3"))) + print(audio_paths) + audio_path = random.choice(audio_paths) + question["species"] = question_species_name + question["species_folder"] = question_species_folder + question["audio_path"] = audio_path + question["proposals"] = get_proposals(question_species_name, 5) + return question diff --git a/templates/base.html b/templates/base.html index d6ae020..c1fcad8 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,6 +9,7 @@
+ {% include 'menu.html' %}

BirdQuizz

{% if username is defined %}

Welcome {{ username }}

diff --git a/templates/game/answer.html b/templates/game/answer.html new file mode 100644 index 0000000..2fb15c9 --- /dev/null +++ b/templates/game/answer.html @@ -0,0 +1,4 @@ +{% extends "base.html" %} +{% block content %} +

{{ message }}

+{% endblock %} \ No newline at end of file diff --git a/templates/game/new.html b/templates/game/new.html new file mode 100644 index 0000000..1adc497 --- /dev/null +++ b/templates/game/new.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} + + + New Game + \ No newline at end of file diff --git a/templates/game/question.html b/templates/game/question.html new file mode 100644 index 0000000..dfe8a83 --- /dev/null +++ b/templates/game/question.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% block content %} + +
+ + +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 6726a1a..a4de76c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,4 @@ {% extends "base.html" %} - {% block content %} Welcome to BirdQuizz ! {% endblock %} \ No newline at end of file diff --git a/templates/menu.html b/templates/menu.html new file mode 100644 index 0000000..7c61e92 --- /dev/null +++ b/templates/menu.html @@ -0,0 +1,6 @@ + \ No newline at end of file