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
80
app/app.py
80
app/app.py
|
|
@ -1,37 +1,63 @@
|
||||||
from flask import Flask, request, redirect
|
from flask import Flask, request
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = sqlite3.connect("adventure.db")
|
||||||
|
db.row_factory = sqlite3.Row
|
||||||
|
return db
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def hello():
|
def start():
|
||||||
return "Hallo aus meiner Flask-App!"
|
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("/frage", methods=["GET", "POST"])
|
@app.route("/raum/<int:id>", methods=["GET", "POST"])
|
||||||
def frage():
|
def raum(id):
|
||||||
if request.method == "POST":
|
db = get_db()
|
||||||
if request.form.get("antwort") == "8":
|
r = db.execute("SELECT * FROM raeume WHERE id=?", (id,)).fetchone()
|
||||||
return "Richtig!"
|
if not r:
|
||||||
return "Leider falsch."
|
db.close()
|
||||||
return '<form method="post">Was ist 3 + 5? ' \
|
return '<h1>Raum nicht gefunden</h1>'
|
||||||
'<input name="antwort"><button>OK</button></form>'
|
|
||||||
|
|
||||||
# Pinnwand (in-memory)
|
if r["raetsel_frage"]:
|
||||||
eintraege = []
|
if request.method == "POST":
|
||||||
|
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>'
|
||||||
|
|
||||||
@app.route("/pinnwand", methods=["GET", "POST"])
|
db.close()
|
||||||
def pinnwand():
|
return '<h1>' + r["name"] + '</h1>' \
|
||||||
if request.method == "POST":
|
'<p>' + r["beschreibung"] + '</p>' \
|
||||||
nachricht = request.form.get("nachricht", "").strip()
|
'<form method="post">' + r["raetsel_frage"] + \
|
||||||
if nachricht:
|
' <input name="antwort"><button>OK</button></form>'
|
||||||
eintraege.append(nachricht)
|
|
||||||
return redirect("/app/pinnwand")
|
ausgaenge = db.execute(
|
||||||
liste = ""
|
"SELECT richtung, nach_raum FROM ausgaenge WHERE von_raum=?", (id,)
|
||||||
for e in eintraege:
|
).fetchall()
|
||||||
liste = liste + "<li>" + e + "</li>"
|
db.close()
|
||||||
return '<h1>Pinnwand</h1>' \
|
|
||||||
'<form method="post"><input name="nachricht">' \
|
html = '<h1>' + r["name"] + '</h1>'
|
||||||
'<button>Senden</button></form><ul>' + liste + '</ul>'
|
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__":
|
if __name__ == "__main__":
|
||||||
# Assuming room number 04 based on home directory isa4 -> port 9004
|
|
||||||
app.run(host="0.0.0.0", port=9004)
|
app.run(host="0.0.0.0", port=9004)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue