Add base game functionnality

This commit is contained in:
Samuel Ortion 2022-05-26 10:27:26 +02:00
parent 6a52b0cba5
commit 13e7c35a1a
9 changed files with 88 additions and 3 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
venv
config.py
__pycache__
__pycache__
data/

27
app.py
View File

@ -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"))
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")

31
game.py Normal file
View File

@ -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

View File

@ -9,6 +9,7 @@
</head>
<body>
<header>
{% include 'menu.html' %}
<h1>BirdQuizz</h1>
{% if username is defined %}
<p>Welcome <span class="username">{{ username }}</span></p>

View File

@ -0,0 +1,4 @@
{% extends "base.html" %}
{% block content %}
<p>{{ message }}</p>
{% endblock %}

5
templates/game/new.html Normal file
View File

@ -0,0 +1,5 @@
{% extends "base.html" %}
<a href="/game/new" class="button">
New Game
</a>

View File

@ -0,0 +1,13 @@
{% extends "base.html" %}
{% block content %}
<audio src="/static/data/src_audio/{{ question['species_folder'] }}/{{ question['audio_path'] }}" controls></audio>
<form action="/game/answer" method="POST">
<select name="answer" id="answer">
{% for item in question['proposals'] %}
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select>
<input type="submit" value="Send Answer">
</form>
{% endblock %}

View File

@ -1,5 +1,4 @@
{% extends "base.html" %}
{% block content %}
Welcome to BirdQuizz !
{% endblock %}

6
templates/menu.html Normal file
View File

@ -0,0 +1,6 @@
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/game">Game</a></li>
</ul>
</nav>