einbau highscore
This commit is contained in:
parent
e2e71f8ca8
commit
88cd686a49
1 changed files with 78 additions and 4 deletions
|
|
@ -1,8 +1,11 @@
|
|||
from flask import Flask, request
|
||||
from flask import Flask, request, session
|
||||
from html import escape
|
||||
import sqlite3
|
||||
import time
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = "stadion-escape-secret"
|
||||
|
||||
|
||||
STYLE = """
|
||||
|
|
@ -35,6 +38,9 @@ STYLE = """
|
|||
background: #020617;
|
||||
color: #e5e7eb;
|
||||
}
|
||||
ol {
|
||||
padding-left: 1.4rem;
|
||||
}
|
||||
</style>
|
||||
"""
|
||||
|
||||
|
|
@ -45,9 +51,61 @@ def get_db():
|
|||
return db
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def highscore_html(db):
|
||||
eintraege = db.execute(
|
||||
"""
|
||||
SELECT name, fertig_zeit - start_zeit AS dauer
|
||||
FROM spieler
|
||||
WHERE fertig_zeit IS NOT NULL
|
||||
ORDER BY dauer
|
||||
LIMIT 10
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
if not eintraege:
|
||||
return ""
|
||||
|
||||
html = "<h2>Highscore</h2><ol>"
|
||||
for eintrag in eintraege:
|
||||
html += "<li>" + escape(eintrag["name"]) + " - " + str(round(eintrag["dauer"], 1)) + " Sekunden</li>"
|
||||
html += "</ol>"
|
||||
return html
|
||||
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
@app.route("/app/", methods=["GET", "POST"])
|
||||
def start():
|
||||
return STYLE + '<h1>Stadion Escape</h1><a href="/app/raum/stadiontor">Abenteuer starten</a>'
|
||||
if request.method == "POST":
|
||||
name = request.form["name"].strip()
|
||||
if not name:
|
||||
name = "Unbekannter Fan"
|
||||
|
||||
db = get_db()
|
||||
cursor = db.execute(
|
||||
"INSERT INTO spieler (name, start_zeit, fertig_zeit) VALUES (?, ?, NULL)",
|
||||
(name, time.time()),
|
||||
)
|
||||
spieler_id = cursor.lastrowid
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
session["spieler_id"] = spieler_id
|
||||
session["spieler_name"] = name
|
||||
|
||||
return STYLE + (
|
||||
"<h1>Eintrittskarte gelöst</h1>"
|
||||
"<p>Willkommen, " + escape(name) + ". Dein Weg durchs Stadion beginnt.</p>"
|
||||
'<a href="/app/raum/stadiontor">Zum Stadiontor</a>'
|
||||
)
|
||||
|
||||
return STYLE + (
|
||||
"<h1>Stadion Escape</h1>"
|
||||
"<p>Trage deinen Namen auf der Eintrittskarte ein. Ab dann läuft deine Zeit.</p>"
|
||||
'<form method="post">'
|
||||
'<input name="name" placeholder="Dein Name" autocomplete="off">'
|
||||
"<button>Abenteuer starten</button>"
|
||||
"</form>"
|
||||
)
|
||||
|
||||
|
||||
@app.route("/raum/<raum_id>", methods=["GET", "POST"])
|
||||
|
|
@ -67,7 +125,23 @@ def raum(raum_id):
|
|||
db.close()
|
||||
|
||||
if antwort == richtige_antwort:
|
||||
return STYLE + "<h1>Geschafft!</h1><p>Die Nordkurve feiert mit dir.</p>"
|
||||
db = get_db()
|
||||
spieler_id = session.get("spieler_id")
|
||||
if spieler_id:
|
||||
db.execute(
|
||||
"UPDATE spieler SET fertig_zeit=? WHERE id=? AND fertig_zeit IS NULL",
|
||||
(time.time(), spieler_id),
|
||||
)
|
||||
db.commit()
|
||||
html = (
|
||||
"<h1>Geschafft!</h1>"
|
||||
"<p>Die Nordkurve feiert mit dir, "
|
||||
+ escape(session.get("spieler_name", "Fan"))
|
||||
+ ".</p>"
|
||||
)
|
||||
html += highscore_html(db)
|
||||
db.close()
|
||||
return STYLE + html
|
||||
return STYLE + (
|
||||
'<h1>Leider falsch</h1>'
|
||||
'<p>Erwin schüttelt den Kopf. Die Botschaft stimmt noch nicht.</p>'
|
||||
|
|
|
|||
Loading…
Reference in a new issue