App.py Name abfragen und Spieler anlegen
This commit is contained in:
parent
352f96b4fb
commit
da76a35f60
1 changed files with 19 additions and 22 deletions
39
app/app.py
39
app/app.py
|
|
@ -1,16 +1,31 @@
|
||||||
from flask import Flask, request
|
from flask import Flask, request, session, redirect
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import time
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.secret_key = "irgendein-geheimer-text-9009"
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = sqlite3.connect("adventure.db")
|
db = sqlite3.connect("adventure.db")
|
||||||
db.row_factory = sqlite3.Row # Spalten per Name ansprechen
|
db.row_factory = sqlite3.Row # Spalten per Name ansprechen
|
||||||
return db
|
return db
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/", methods=["GET", "POST"])
|
||||||
def start():
|
def start():
|
||||||
return '<h1>Verlies</h1><a href="/app/raum/1">Abenteuer starten</a>'
|
if request.method == "POST":
|
||||||
|
name = request.form["name"].strip()
|
||||||
|
db = get_db()
|
||||||
|
cur = db.execute(
|
||||||
|
"INSERT INTO spieler (name, start_zeit) VALUES (?, ?)",
|
||||||
|
(name, time.time())
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
session["spieler_id"] = cur.lastrowid
|
||||||
|
db.close()
|
||||||
|
return redirect("/app/raum/1")
|
||||||
|
|
||||||
|
return '<h1>Verlies</h1>' \
|
||||||
|
+ '<form method="post">Dein Name: <input name="name"><button>Start</button></form>'
|
||||||
|
|
||||||
@app.route("/raum/<int:id>", methods=["GET", "POST"])
|
@app.route("/raum/<int:id>", methods=["GET", "POST"])
|
||||||
def raum(id):
|
def raum(id):
|
||||||
|
|
@ -25,21 +40,3 @@ def raum(id):
|
||||||
db.close()
|
db.close()
|
||||||
return 'Leider falsch. <a href="/app/raum/' + str(id) + '">nochmal</a>'
|
return 'Leider falsch. <a href="/app/raum/' + str(id) + '">nochmal</a>'
|
||||||
db.close()
|
db.close()
|
||||||
return "<h1>" + r["name"] + "</h1><p>" + r["beschreibung"] + "</p>" \
|
|
||||||
+ '<form method="post">' + r["raetsel_frage"] \
|
|
||||||
+ ' <input name="antwort"><button>OK</button></form>'
|
|
||||||
|
|
||||||
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> '
|
|
||||||
return html
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(host="0.0.0.0", port=9009)
|
|
||||||
Loading…
Reference in a new issue