-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
137 lines (116 loc) · 4.89 KB
/
Copy pathapp.py
File metadata and controls
137 lines (116 loc) · 4.89 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from flask import Flask, render_template, session, url_for, request, redirect, flash, jsonify
import json
import random
import os
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
current_sessions = []
def auth():
return 'username' in session
@app.route("/")
def home():
if not auth():
return redirect(url_for('pick'))
return render_template("index.html", username=session["username"])
@app.route("/pick/username", methods=['GET', 'POST'])
def pick():
if request.method == 'POST':
username = request.form.get('username', '').strip()
if not username:
flash("Please enter a valid username.")
return redirect(url_for('pick'))
session['username'] = username + str(random.randint(1000, 9999))
return redirect(url_for('home'))
return render_template('username.html')
@app.route("/create", methods=['POST'])
def create():
game_id = str(random.randint(100000, 999999))
current_sessions.append({"game_id": game_id, "players": [], "question":"", "imposter_question":"", "imposter_player":"", "submissions": [], "status": "Lobby", "started":False})
return game_id
@app.route("/host/<game_id>", methods=['GET'])
def host(game_id):
return render_template('host.html', game_id=game_id)
@app.route("/get_players/<game_id>", methods=['POST'])
def get_players(game_id):
for s in current_sessions:
if s["game_id"] == game_id:
return s["players"]
return render_template('host.html', game_id=game_id)
@app.route("/join", methods=['POST'])
def join():
game_id = request.form.get("code", "").strip()
game = next((g for g in current_sessions if g["game_id"] == game_id), None)
if game:
for pla in game["players"]:
if pla == session["username"]:
return redirect(url_for("game", game_id=game_id))
game["players"].append(session["username"])
return redirect(url_for("game", game_id=game_id))
else:
flash("Invalid game code.")
return redirect(url_for("home"))
@app.route("/updatestate", methods=['POST'])
def updatestate():
game_id = request.json["game_id"].strip()
state = request.json["state"].strip()
game = next((g for g in current_sessions if g["game_id"] == game_id), None)
game["status"] = state
return jsonify({"state":game["status"]})
@app.route("/submission", methods=['POST'])
def submission():
game_id = request.json["game_id"].strip()
username = request.json["username"].strip()
xsubmission = request.json["submission"].strip()
game = next((g for g in current_sessions if g["game_id"] == game_id), None)
for submission in game["submissions"]:
if submission["username"] == username:
return jsonify({"success": 0, "msg": "already submitted"})
d = {"username": username, "submission": xsubmission}
game["submissions"].append(d)
return jsonify({"success": 1, "msg": "submitted", "data" : d})
@app.route("/finished", methods=['POST'])
def finished():
game_id = request.json["game_id"].strip()
game = next((g for g in current_sessions if g["game_id"] == game_id), None)
if len(game["submissions"]) == len(game["players"]):
return jsonify({
"submissions": game["submissions"],
"imposter": game["imposter_player"],
"question": game["question"],
"ready": True
})
else:
return jsonify({
"ready": False
})
@app.route("/state", methods=['POST'])
def state():
game_id = request.json["game_id"].strip()
username = request.json["username"].strip()
game = next((g for g in current_sessions if g["game_id"] == game_id), None)
if game is None:
return jsonify({"state": "no"})
if game["status"] == "question":
for submission in game["submissions"]:
if submission["username"] == username:
return jsonify({"state":None})
if username == game["imposter_player"]:
return jsonify({"state":"question", "question": game["imposter_question"]})
return jsonify({"state":"question","question": game["question"]})
return jsonify({"state":game["status"]})
@app.route("/genquestion", methods=['POST'])
def question():
game_id = request.json["game_id"].strip()
game = next((g for g in current_sessions if g["game_id"] == game_id), None)
game["imposter_player"] = random.choice(game["players"])
with open('db/questions.json', 'r', encoding="utf-8") as f:
questions = json.load(f)
q = random.choice(questions)
game["question"] = q["question"]
game["imposter_question"] = random.choice(q["imposter_questions"])
game["submissions"] = []
game["status"] = "question"
return jsonify({"question": q["question"]})
@app.route("/game/<game_id>", methods=['GET'])
def game(game_id):
return render_template('game.html', game_id=game_id, username=session["username"])