Add base game functionnality
This commit is contained in:
parent
6a52b0cba5
commit
13e7c35a1a
|
@ -1,3 +1,4 @@
|
||||||
venv
|
venv
|
||||||
config.py
|
config.py
|
||||||
__pycache__
|
__pycache__
|
||||||
|
data/
|
25
app.py
25
app.py
|
@ -9,6 +9,7 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
|
|
||||||
from config import secret_key, database_uri
|
from config import secret_key, database_uri
|
||||||
from model import db, User
|
from model import db, User
|
||||||
|
import game as Game
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = secret_key
|
app.secret_key = secret_key
|
||||||
|
@ -66,3 +67,27 @@ def logout():
|
||||||
# Remove username from the session if it's there
|
# Remove username from the session if it's there
|
||||||
session.pop("username", None)
|
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")
|
|
@ -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
|
|
@ -9,6 +9,7 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
|
{% include 'menu.html' %}
|
||||||
<h1>BirdQuizz</h1>
|
<h1>BirdQuizz</h1>
|
||||||
{% if username is defined %}
|
{% if username is defined %}
|
||||||
<p>Welcome <span class="username">{{ username }}</span></p>
|
<p>Welcome <span class="username">{{ username }}</span></p>
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
{% block content %}
|
||||||
|
<p>{{ message }}</p>
|
||||||
|
{% endblock %}
|
|
@ -0,0 +1,5 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
<a href="/game/new" class="button">
|
||||||
|
New Game
|
||||||
|
</a>
|
|
@ -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 %}
|
|
@ -1,5 +1,4 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
Welcome to BirdQuizz !
|
Welcome to BirdQuizz !
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -0,0 +1,6 @@
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/">Home</a></li>
|
||||||
|
<li><a href="/game">Game</a></li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
Loading…
Reference in New Issue