Adventure aus DB
This commit is contained in:
parent
431613f9b3
commit
7c3fdb8321
1 changed files with 22 additions and 24 deletions
46
app/app.py
46
app/app.py
|
|
@ -1,34 +1,32 @@
|
||||||
from flask import Flask, request, redirect
|
from flask import Flask
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
eintraege = [] # speichert die Nachrichten (im Arbeitsspeicher)
|
|
||||||
|
|
||||||
@app.route("/pinnwand", methods=["GET", "POST"])
|
|
||||||
def pinnwand():
|
|
||||||
if request.method == "POST":
|
|
||||||
eintraege.append(request.form["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>'
|
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = sqlite3.connect("adventure.db")
|
||||||
|
db.row_factory = sqlite3.Row # Spalten per Name ansprechen
|
||||||
|
return db
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def hello():
|
def start():
|
||||||
return "Hallo aus meiner Flask-App!"
|
return '<h1>Verlies</h1><a href="/app/raum/1">Abenteuer starten</a>'
|
||||||
|
|
||||||
@app.route("/frage", methods=["GET", "POST"])
|
@app.route("/raum/<int:id>")
|
||||||
def frage():
|
def raum(id):
|
||||||
if request.method == "POST":
|
db = get_db()
|
||||||
if request.form["antwort"] == "8":
|
r = db.execute("SELECT name, beschreibung FROM raeume WHERE id=?", (id,)).fetchone()
|
||||||
return "Richtig!"
|
ausgaenge = db.execute(
|
||||||
return "Leider falsch."
|
"SELECT richtung, nach_raum FROM ausgaenge WHERE von_raum=?", (id,)
|
||||||
return '<form method="post">Was ist 3 + 5? ' \
|
).fetchall()
|
||||||
'<input name="antwort"><button>OK</button></form>'
|
db.close()
|
||||||
|
|
||||||
|
html = "<h1>" + r["name"] + "</h1>"
|
||||||
|
html = html + "<p>" + r["beschreibung"] + "</p>"
|
||||||
|
for a in ausgaenge:
|
||||||
|
html = html + '<a href="/app/raum/' + str(a["nach_raum"]) + '">' \
|
||||||
|
+ a["richtung"] + '</a> '
|
||||||
|
return html
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=9008)
|
app.run(host="0.0.0.0", port=9008)
|
||||||
Loading…
Reference in a new issue