49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import os
|
|
import sqlite3
|
|
from flask import Flask, request
|
|
|
|
app = Flask(__name__)
|
|
|
|
def get_db():
|
|
db = sqlite3.connect("adventure.db")
|
|
db.row_factory = sqlite3.Row
|
|
return db
|
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
@app.route("/app", methods=["GET", "POST"])
|
|
@app.route("/app/", methods=["GET", "POST"])
|
|
def start():
|
|
room_id = request.args.get("room", type=int)
|
|
if room_id is None:
|
|
return '<h1>Verlies</h1><a href="/app/?room=1">Abenteuer starten</a>'
|
|
|
|
db = get_db()
|
|
r = db.execute("SELECT name, beschreibung, raetsel_frage, raetsel_antwort FROM raeume WHERE id = ?", (room_id,)).fetchone()
|
|
ausgaenge = db.execute(
|
|
"SELECT richtung, nach_raum FROM ausgaenge WHERE von_raum = ?", (room_id,)
|
|
).fetchall()
|
|
db.close()
|
|
|
|
if r is None:
|
|
return '<h1>Raum nicht gefunden</h1>'
|
|
|
|
if r["raetsel_frage"]:
|
|
if request.method == "POST":
|
|
if request.form.get("antwort", "").strip() == r["raetsel_antwort"]:
|
|
return "<h1>Befreit!</h1>"
|
|
return 'Leider falsch. <a href="/app/?room=' + str(room_id) + '">nochmal</a>'
|
|
|
|
html = "<h1>" + r["name"] + "</h1>"
|
|
html += "<p>" + r["beschreibung"] + "</p>"
|
|
html += '<form method="post">' + r["raetsel_frage"] + ' <input name="antwort"><button>OK</button></form>'
|
|
return html
|
|
|
|
html = "<h1>" + r["name"] + "</h1>"
|
|
html += "<p>" + r["beschreibung"] + "</p>"
|
|
for a in ausgaenge:
|
|
html += '<a href="/app/?room=' + str(a["nach_raum"]) + '">' + a["richtung"] + '</a> '
|
|
return html
|
|
|
|
if __name__ == "__main__":
|
|
port = int(os.environ.get("PORT", "9003"))
|
|
app.run(host="0.0.0.0", port=port)
|