forked from MiroslavSvec/project-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
169 lines (125 loc) · 5.33 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import json
from datetime import datetime
from random import shuffle
from flask import Flask, redirect, request, render_template, jsonify
## Custom .py
import helper
import riddle
app = Flask(__name__)
""" Data Sample """
## {"test": [{"name": "test", "login": "true", "created": "00:40:13"}]}
""" Rest API """
@app.route('/<user_name>/data', methods=["GET"])
def get(user_name):
profiles = helper.read_txt("data/profiles/all-profiles.txt")
profile = user_name + "\n"
if profile in profiles:
return jsonify(helper.read_json(f"data/profiles/{user_name}/{user_name}.json"))
else:
return jsonify("no profile")
@app.route('/<user_name>/data', methods=["POST"])
def post(user_name):
return helper.create_profile_data(user_name)
@app.route('/app_data')
def get_app_data():
app_data = helper.read_json('data/app_data.json')
return jsonify(app_data)
""" Create profile page """
@app.route('/')
def index():
app_data = helper.read_json('data/system/app_data.json')
app_data = app_data['1.1'][0]["members"] - 1984
# Render index.html by default
return render_template("index.html", members=app_data)
""" Chat in separate page """
@app.route('/chat')
def chat():
return render_template("chat.html")
""" Profile page """
@app.route('/<user_name>')
def profile_page(user_name):
riddle_profiles = helper.read_txt(
f'data/profiles/{user_name}/riddle_game/riddle_profiles.txt')
# Render default template
return render_template("profile.html",
user_name=user_name, riddle_profiles=riddle_profiles, page_title=f"{user_name}" + " profile")
""" Riddles Game Setting """
@app.route('/<user_name>/riddle-g-setting')
def riddle_setting(user_name):
riddle_profiles = helper.read_txt(
f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt")
return render_template("riddle-g-setting.html",
user_name=user_name, riddle_profiles=riddle_profiles, page_title="Riddle Game Setting")
# JSON requests to create save
@app.route('/postjson/<user_name>/riddle-g-setting', methods=["POST"])
def parse_setting(user_name):
data = request.get_json(force=True)
profiles = helper.read_txt(
f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt")
profile = data["riddle_game_data"]["riddle_profile_name"] + "\n"
if profile in profiles:
return jsonify(profile)
# Create new game
riddle.create_riddle_game(data)
return jsonify(data)
""" Riddles Game """
@app.route('/<user_name>/<riddle_profile>/riddle-game', methods=["GET"])
def get_results(user_name, riddle_profile):
riddle_profiles = helper.read_txt(
f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt")
profile = helper.read_json(helper.profile(user_name, riddle_profile))
profile = profile["game"][0]
if profile["mods"] == "limited":
return render_template("riddle-game.html",
user_name=user_name,
riddle_profiles=riddle_profiles,
riddle_profile=riddle_profile,
tries=int(profile["tries"]),
page_title="Riddle Game")
# Render riddle-game template by default
return render_template("riddle-game.html",
user_name=user_name,
riddle_profiles=riddle_profiles,
riddle_profile=riddle_profile,
tries=int(0),
page_title="Riddle Game")
# JSON POST to play the game
@app.route('/postjson/<user_name>/<riddle_profile>/riddle-game', methods=["POST", "GET"])
def parse_answer(user_name, riddle_profile):
# Main POST request for riddle-game
if request.method == "POST":
post_data = request.get_json(force=True)
if post_data["id"] == "answer":
data = riddle.riddle_game(user_name, riddle_profile, post_data)
return jsonify(data)
elif post_data["id"] == "skip_question":
data = riddle.skip_question(user_name, riddle_profile)
return jsonify(data)
else:
data = riddle.delete_question(user_name, riddle_profile)
return jsonify(data)
data = helper.read_json(helper.profile(user_name, riddle_profile))
return jsonify(data)
# Statistics for Ridddle game
@app.route('/<user_name>/statistics', methods=["GET"])
def show_statistics(user_name):
riddle_profiles = helper.read_txt(f"data/profiles/{user_name}/riddle_game/riddle_profiles.txt")
return render_template("statistics.html",
user_name=user_name,
riddle_profiles=riddle_profiles,
page_title="Statistics")
if __name__ == '__main__':
app.run(host=os.getenv('IP'),
port=os.getenv('PORT'),
debug=True)
""" Create 404 page and 500 error """
""" Stats """
""" Add confirmation messages when clicking / submiting in the game """
""" Hide number of tries when endless mode is selected"""
""" Hide skip question button if it is the last question """
""" Come up with some sort of search engine to get more questions injected from web """
""" Limit wrong answers """
""" View Friends """
""" Chat / Password / Send Invitaiton / """
""" Inject graphs to mini statistics in riddle-game.html """