Update app.py to spaceship adventure and SQLite routing
This commit is contained in:
parent
e2f4e0165f
commit
03ec6f9651
1 changed files with 53 additions and 27 deletions
78
app/app.py
78
app/app.py
|
|
@ -1,37 +1,63 @@
|
|||
from flask import Flask, request, redirect
|
||||
from flask import Flask, request
|
||||
import sqlite3
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/")
|
||||
def hello():
|
||||
return "Hallo aus meiner Flask-App!"
|
||||
def get_db():
|
||||
db = sqlite3.connect("adventure.db")
|
||||
db.row_factory = sqlite3.Row
|
||||
return db
|
||||
|
||||
@app.route("/frage", methods=["GET", "POST"])
|
||||
def frage():
|
||||
@app.route("/")
|
||||
def start():
|
||||
return """<style>
|
||||
body{background:#07121d;color:#d9e9f0;font-family:system-ui,sans-serif;
|
||||
max-width:760px;margin:3rem auto;padding:1.2rem;line-height:1.75}
|
||||
a,button{display:inline-block;background:#0f172a;color:#e2e8f0;
|
||||
border:1px solid #334155;border-radius:12px;padding:.75rem 1.2rem;
|
||||
margin:.4rem .3rem 0 0;text-decoration:none;font-weight:700}
|
||||
a:hover,button:hover{background:#1e293b}
|
||||
h1{font-size:2.4rem;margin-bottom:.5rem}
|
||||
p{max-width:700px}
|
||||
.container{padding:.8rem 1rem;border:1px solid #18324c;border-radius:18px;
|
||||
background:rgba(15,23,42,.9);box-shadow:0 18px 50px rgba(0,0,0,.25)}
|
||||
</style>""" + '<div class="container"><h1>Verlassenes Raumschiff</h1>' \
|
||||
'<p>Du bist der einzige Überlebende an Bord. Erkunde das Schiff und aktiviere das Notsignal.</p>' \
|
||||
'<a href="/app/raum/1">Mission starten</a></div>'
|
||||
|
||||
@app.route("/raum/<int:id>", methods=["GET", "POST"])
|
||||
def raum(id):
|
||||
db = get_db()
|
||||
r = db.execute("SELECT * FROM raeume WHERE id=?", (id,)).fetchone()
|
||||
if not r:
|
||||
db.close()
|
||||
return '<h1>Raum nicht gefunden</h1>'
|
||||
|
||||
if r["raetsel_frage"]:
|
||||
if request.method == "POST":
|
||||
if request.form.get("antwort") == "8":
|
||||
return "Richtig!"
|
||||
return "Leider falsch."
|
||||
return '<form method="post">Was ist 3 + 5? ' \
|
||||
if request.form.get("antwort", "").strip() == r["raetsel_antwort"]:
|
||||
db.close()
|
||||
return '<h1>Notsignal erfolgreich aktiviert. Rettung ist unterwegs.</h1>'
|
||||
db.close()
|
||||
return '<h1>Falscher Code</h1><p>Der KI-Kern reagiert nicht.</p>' \
|
||||
'<a href="/app/raum/' + str(id) + '">nochmal versuchen</a>'
|
||||
|
||||
db.close()
|
||||
return '<h1>' + r["name"] + '</h1>' \
|
||||
'<p>' + r["beschreibung"] + '</p>' \
|
||||
'<form method="post">' + r["raetsel_frage"] + \
|
||||
' <input name="antwort"><button>OK</button></form>'
|
||||
|
||||
# Pinnwand (in-memory)
|
||||
eintraege = []
|
||||
ausgaenge = db.execute(
|
||||
"SELECT richtung, nach_raum FROM ausgaenge WHERE von_raum=?", (id,)
|
||||
).fetchall()
|
||||
db.close()
|
||||
|
||||
@app.route("/pinnwand", methods=["GET", "POST"])
|
||||
def pinnwand():
|
||||
if request.method == "POST":
|
||||
nachricht = request.form.get("nachricht", "").strip()
|
||||
if nachricht:
|
||||
eintraege.append(nachricht)
|
||||
return redirect("/app/pinnwand")
|
||||
liste = ""
|
||||
for e in eintraege:
|
||||
liste = liste + "<li>" + e + "</li>"
|
||||
return '<h1>Pinnwand</h1>' \
|
||||
'<form method="post"><input name="nachricht">' \
|
||||
'<button>Senden</button></form><ul>' + liste + '</ul>'
|
||||
html = '<h1>' + r["name"] + '</h1>'
|
||||
html += '<p>' + r["beschreibung"] + '</p>'
|
||||
for a in ausgaenge:
|
||||
html += '<a href="/app/raum/' + str(a["nach_raum"]) + '">' + a["richtung"] + '</a> '
|
||||
return html
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Assuming room number 04 based on home directory isa4 -> port 9004
|
||||
app.run(host="0.0.0.0", port=9004)
|
||||
|
|
|
|||
Loading…
Reference in a new issue