-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
136 lines (101 loc) · 3.24 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
import json
from random import sample
from flask import Flask, render_template, url_for, request, redirect, jsonify
app = Flask(__name__)
CATEGORIES = [
"perks",
"ammo_mods",
"field_upgrades"
]
ERROR_MESSAGES:dict = {
400 : "Bad Request",
404 : "Page not Found :(",
403 : "Forbidden"
}
ERROR_CODE_STATUS: dict = {
FileNotFoundError : 400,
UnboundLocalError : 400,
ValueError : 400
}
# FUNCTIONS
def get_category_data(category:str) -> json:
with open(f"data/{category}.json",'r') as f:
data = json.load(f)
return data[f"{category}"]
def get_sample_items(sub_category: list, sample_size:int) -> list:
temporal_items = []
for category in sub_category:
items = get_category_data(category)
for item in items:
temporal_items.append(item)
items_sample = sample(temporal_items,sample_size)
return items_sample
def get_random_category(category: str) -> list[str,str]:
categories = CATEGORIES[:]
categories.remove(category)
return categories
# PAGES
@app.route("/")
def home():
Field_Upgrades: list = get_category_data("field_upgrades")
Field_Upgrades.reverse()
Ammo_Mods: list = get_category_data("ammo_mods")
Ammo_Mods.reverse()
Perks: list = get_category_data("perks")
Perks.reverse()
return render_template("index.html", Perks=Perks, Ammo_Mods=Ammo_Mods, Field_Upgrades=Field_Upgrades)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/<category>/<id>")
def augments(category, id):
id = int(id)
category = category
elements = get_category_data(category)
for element in elements:
if(element["id"] == id):
item = element
sub_categories = get_random_category(category)
adittional_items = get_sample_items(sub_categories,4)
return render_template("augments.html", item=item, category=category, adittional_items=adittional_items)
@app.route("/tools")
def tools():
main_items = get_category_data("main_tools")
return render_template("tools.html", main_items=main_items)
@app.route("/fetch_tool_item")
def get_tool():
id_tool = request.args.get("id_tool")
tool = render_template(f"{id_tool}.html")
return jsonify({
"tool" : tool
})
# POST ROUTES
@app.route("/select_item/<category>/<id>", methods=['POST'])
def item_selector(category,id):
id = int(id)
category = category
elements = get_category_data(category)
new_index = id + int(request.form["value"])
if new_index > len(elements):
new_index = 1
elif new_index < 1:
new_index = len(elements)
return redirect(url_for('augments', category=category, id=new_index))
# ERROR HANLDERS
@app.route("/error/<id_error>")
def error_handler(id_error):
msg = ERROR_MESSAGES.get(int(id_error))
if msg == None:
msg = "Unauthorized"
return render_template("error.html", msg=msg)
@app.errorhandler(Exception)
def exception_hanlder(e):
id_error = ERROR_CODE_STATUS.get(type(e))
return redirect(url_for('error_handler',id_error=id_error))
@app.errorhandler(404)
def page_not_found(e):
id_error=404
return redirect(url_for('error_handler',id_error=id_error))
# MAIN
if __name__ == "__main__":
app.run(debug=True)