from flask import Flask
import sqlite3

app = Flask(__name__)

def get_db():
    db = sqlite3.connect("adventure.db")
    db.row_factory = sqlite3.Row   # Spalten per Name ansprechen
    return db

@app.route("/")
def start():
    return '<h1>Verlies</h1><a href="/app/raum/1">Abenteuer starten</a>'

@app.route("/raum/<int:id>")
def raum(id):
    db = get_db()
    r = db.execute("SELECT name, beschreibung FROM raeume WHERE id=?", (id,)).fetchone()
    ausgaenge = db.execute(
        "SELECT richtung, nach_raum FROM ausgaenge WHERE von_raum=?", (id,)
    ).fetchall()
    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> &nbsp; '
    return html

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=9008)