-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (46 loc) · 1.5 KB
/
main.py
File metadata and controls
52 lines (46 loc) · 1.5 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
from flask import Flask, request
from flask_cors import CORS, cross_origin
import time
from break_info import BreakInfo
from break_db import BreakDB
from user_db import UserDB
# Flask setup
app = Flask(__name__)
cors = CORS(app)
app.config["CORS_HEADERS"] = "Content-Type"
# handle requests to /billiards/api
@app.route("/billiards/api") # endpoint relative to how nginix is set up
@cross_origin()
def onRequest():
allow_submit = True
print("Request Received")
print(request.args)
try:
uniqueUserDB = UserDB('users.db')
# convert to regular dictionary (single value)
args = request.args.to_dict()
if "user" in args:
args["user"] = uniqueUserDB.get_discord_id(int(args["user"]))
else:
return ""
# unpack fields from dictionary
breakInfo = BreakInfo(**args)
breakInfo.timestamp = round(time.time())
# confirm data integrity
if breakInfo.checksum != breakInfo.calcChecksum():
print("Checksum Failed")
allow_submit = False
except (TypeError, ValueError) as error:
print(error)
# wrong argument count / name / type
allow_submit = False
if not allow_submit:
return ""
print(breakInfo)
if breakInfo.sunk + breakInfo.off >= 6:
breakDB = BreakDB('allBreaks.db')
breakDB.add(breakInfo)
usersDB = BreakDB('userBreaks.db')
usersDB.set_user_best(breakInfo)
# dont remove this, you need it
return f"<p>{breakInfo}</p>"