-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
139 lines (129 loc) · 4.39 KB
/
server.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
# so this is the part where i have no idea what im doing
# forgive me
import asyncio
import json
import sqlite3
import subprocess
from http.server import SimpleHTTPRequestHandler
from websockets.server import serve
from src.account_manager import AccountManager
from src.scores_manager import Score, ScoresManager
from src.charts_manager import ChartsManager
Handler = SimpleHTTPRequestHandler
FILES_PORT = 6969
acc = AccountManager()
sco = ScoresManager()
cht = ChartsManager()
con = sqlite3.connect("charts.db")
cur = con.cursor()
#region Create tables
cur.execute("""CREATE TABLE IF NOT EXISTS charts(
id INTEGER PRIMARY KEY AUTOINCREMENT,
checksum STRING NOT NULL,
songname STRING,
difficulty REAL,
artist STRING,
author STRING,
author_id INTEGER,
description STRING,
filenames STRING NOT NULL,
status INTEGER DEFAULT 0,
upload_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_time DATETIME,
chart_version INTEGER DEFAULT 0,
chart_checksums STRING
)""")
cur.execute("""CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username STRING NOT NULL,
discriminator INTEGER NOT NULL DEFAULT 0,
password STRING NOT NULL,
description STRING,
is_bot INTEGER DEFAULT 0
)""")
cur.execute("""CREATE TABLE IF NOT EXISTS scores(
id INTEGER PRIMARY KEY AUTOINCREMENT,
chart_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
rank STRING NOT NULL,
score INTEGER NOT NULL,
accuracy INTEGER NOT NULL,
judgements STRING,
upload_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)""")
#endregion
# cur.execute("""ALTER TABLE charts
# ADD filenames STRING""")
con.commit()
async def echo(websocket):
async for message in websocket:
data:dict = json.loads(message)
response = {
"code": 404,
"content": "what"
}
match data["type"]:
case "whats_9_plus_10":
response = {
"code": 21,
"content": "twenty_one"
}
case "register":
if ("username" in data) and ("password" in data):
result = acc.create_account(data["username"], data["password"])
response = {
"code": 200 if result[0] else 401,
"content": result[1]
}
case "login":
if ("username" in data) and ("password" in data):
print(data)
result = acc.login(data["username"], data["password"])
print(result)
if not result[0]:
response = {
"code": 401,
"content": "wrong_account"
}
else:
response = {
"code": 200,
"content": result
}
case "post_score":
if "token" in data:
if data["token"] in acc.logged_in_users:
score = Score(data["score_data"])
result = sco.post_score(score, acc.logged_in_users[data["token"]].user_id)
response = {
"code": 200,
"content": result
}
case "get_score":
result = sco.get_score(data["score_id"])
response = {
"code": 200,
"content": result
}
case "chart_data":
result = cur.execute("SELECT * FROM charts WHERE id=?",
(data["chart_id"],)).fetchone()
response = {
"code": 200,
"content": result
}
case _:
response = {
"code": 404,
"content": "empty_request"
}
await websocket.send(json.dumps(response))
async def main():
print("ws at port 8765")
async with serve(echo, "localhost", 8765):
await asyncio.Future() # run forever
if __name__ == "__main__":
subprocess.Popen(['flask', '--app', './src/file_server', 'run', '--port', str(FILES_PORT)])
# app.run(port=FILES_PORT, ssl_context="adhoc")
while True:
asyncio.run(main())