Glueck-Auf/flask_app/app.py
2026-07-01 16:39:17 +02:00

178 lines
4.8 KiB
Python

from flask import Flask, request, session
from html import escape
import sqlite3
import time
app = Flask(__name__)
app.secret_key = "stadion-escape-secret"
STYLE = """
<style>
body {
background: #0f172a;
color: #e5e7eb;
font-family: system-ui, sans-serif;
max-width: 640px;
margin: 3rem auto;
padding: 0 1rem;
line-height: 1.6;
}
a, button {
display: inline-block;
border: 1px solid #334155;
border-radius: 8px;
padding: .45em 1em;
margin: .3em .3em 0 0;
background: #1e293b;
color: #e5e7eb;
text-decoration: none;
font: inherit;
cursor: pointer;
}
input {
padding: .45em .7em;
border-radius: 8px;
border: 1px solid #334155;
background: #020617;
color: #e5e7eb;
}
ol {
padding-left: 1.4rem;
}
</style>
"""
def get_db():
db = sqlite3.connect("adventure.db")
db.row_factory = sqlite3.Row
return db
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():
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"])
@app.route("/app/raum/<raum_id>", methods=["GET", "POST"])
def raum(raum_id):
db = get_db()
r = db.execute("SELECT * FROM raeume WHERE id=?", (raum_id,)).fetchone()
if r is None:
db.close()
return STYLE + '<h1>Raum nicht gefunden</h1><a href="/app/">Zurück zum Start</a>', 404
if r["raetsel_frage"]:
if request.method == "POST":
antwort = request.form["antwort"].strip().lower()
richtige_antwort = r["raetsel_antwort"].strip().lower()
db.close()
if antwort == richtige_antwort:
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>'
'<a href="/app/raum/' + raum_id + '">Nochmal versuchen</a>'
)
html = "<h1>" + r["name"] + "</h1>"
html += "<p>" + r["beschreibung"] + "</p>"
html += '<form method="post">'
html += "<p>" + r["raetsel_frage"] + "</p>"
html += '<input name="antwort" autocomplete="off">'
html += "<button>OK</button>"
html += "</form>"
db.close()
return STYLE + html
ausgaenge = db.execute(
"SELECT richtung, nach_raum FROM ausgaenge WHERE von_raum=?",
(raum_id,),
).fetchall()
db.close()
html = "<h1>" + r["name"] + "</h1>"
html += "<p>" + r["beschreibung"] + "</p>"
for a in ausgaenge:
html += '<a href="/app/raum/' + a["nach_raum"] + '">'
html += a["richtung"]
html += "</a> "
return STYLE + html
if __name__ == "__main__":
app.run(host="0.0.0.0", port=9000)