-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
114 lines (97 loc) · 2.94 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from functools import wraps
import os
from flask import Flask, render_template, redirect, request, session, url_for
from flask_session import Session
from dnd import DND
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
dnd = DND()
@app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if "username" not in session:
return redirect("/")
return f(*args, **kwargs)
return decorated_function
@app.route("/")
def index():
if "username" in session:
return redirect("/waitingroom")
return render_template("index.html")
@app.route("/joingame", methods=["POST"])
def joingame():
session.clear()
session["username"] = request.form.get("username")
session["character_class"] = request.form.get("character_class")
dnd.add_user((session["username"], session["character_class"]))
return redirect("/waitingroom")
@app.route("/waitingroom")
@login_required
def waitingroom():
if dnd.get_is_started():
return redirect("/game")
return render_template("waitingroom.html", name=session["username"])
@app.route("/startgame")
@login_required
def startgame():
global dnd
try:
dnd.start_game()
return redirect("/game")
except ValueError:
return redirect("/waitingroom")
@app.route("/api/waitingroom")
@login_required
def waitingroom_status():
global dnd
if dnd.get_is_started():
return "", 200
return "", 204
@app.route("/game")
@login_required
def game():
global dnd
if not dnd.get_is_started:
return redirect("/waitingroom")
return render_template("game.html", character_1_name=dnd.character_1_name, character_1_health = dnd.character_1_health, character_2_name=dnd.character_2_name, character_2_health=dnd.character_2_health, is_first=(session["username"] == dnd.character_1_name))
@app.route("/api/getmessages")
@login_required
def getmessages():
global dnd
return dnd.content
@app.route("/api/gethealth")
@login_required
def gethealth():
global dnd
return [dnd.character_1_health, dnd.character_2_health]
@app.route("/api/action", methods=["POST"])
@login_required
def action():
global dnd
if request.form.get("action"):
dnd.user_submit_message(request.form.get("action"), session["username"])
return ""
return "Could not complete action."
@app.route("/jail")
def jail():
session.clear()
return render_template("jail.html")
@app.route("/endgame")
@login_required
def endgame():
global dnd
dnd = DND()
session.clear()
app.config['SECRET_KEY'] = os.urandom(32)
return redirect("/")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=6969)