-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
466 lines (432 loc) · 17.1 KB
/
start.py
File metadata and controls
466 lines (432 loc) · 17.1 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import random
from flask import *
from flask_socketio import *
import hashlib
from dotenv import *
import secrets
import os
from gevent import pywsgi
import sqlite3
from colorama import Fore, init
import pandas as pd
import numpy as np
import datetime
load_dotenv()
app = Flask(__name__)
app.config['SECRET_KEY'] = secrets.token_hex(16)
init(autoreset=True)
curdir = os.path.dirname(os.path.abspath(__file__))
chartconstant = {}
songlist = {}
class_song = [
"Past",
'Present',
'Future',
'Beyond',
'Eternal',
]
class2num = {
"Past": 0,
'Present': 1,
'Future': 2,
'Beyond': 3,
'Eternal': 4,
}
with open(os.path.join(curdir, os.getenv('SONGLIST_PATH'))) as file:
songlist = json.load(file)
with open(os.path.join(curdir, os.getenv('CHARTCONSTANT_PATH'))) as file:
chartconstant = json.load(file)
def hash(password):
sha256 = hashlib.sha256()
sha256.update(password.encode('utf-8'))
return sha256.hexdigest()
def calcRating(difficulty, score):
if score == '' or not score:
return 0
if score >= 1000_0000:
return difficulty + 2
elif score >= 980_0000:
return difficulty + 1 + (score - 980_0000) / 20_0000
else:
return max(difficulty + (score - 950_0000) / 30_0000, 0)
@app.route('/')
def index():
if session.get('username'):
return redirect('/scores')
return send_from_directory(os.path.join(curdir, 'public'),'index.html')
@app.route('/<path:path>')
def route(path):
if os.path.isfile(os.path.join(curdir, 'public',f'{path}.html')):
return send_from_directory(os.path.join(curdir, 'public'),f'{path}.html')
else:
return send_from_directory(os.path.join(curdir, 'public'),"404.html")
@app.route('/scripts/<path:file>')
def scripts(file):
if os.path.isfile(os.path.join(curdir, 'scripts',f'{file}')):
return send_from_directory(os.path.join(curdir, 'scripts'),f'{file}')
else:
print(Fore.RED + f'Error: {file} not found')
Fore.WHITE
return None
@app.route('/style/<path:file>')
def stylesheets(file):
if os.path.isfile(os.path.join(curdir, 'style',f'{file}')):
return send_from_directory(os.path.join(curdir, 'style'),f'{file}')
else:
print(Fore.RED + f'Error: {file} not found')
Fore.WHITE
return None
@app.route('/template/<path:file>', methods=['GET'])
def templates(file):
if os.path.isfile(os.path.join(curdir, 'template',f'{file}.html')):
return send_from_directory(os.path.join(curdir, 'template'),f'{file}.html')
else:
print(Fore.RED + f'Error: {file}.html not found')
Fore.WHITE
return None
@app.route('/login', methods=['POST'])
def login():
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
username = request.form['username']
password = request.form['password']
cursor.execute('''SELECT * from users WHERE username = ?''', (username,))
rows = cursor.fetchall()
if len(rows) == 0:
return redirect('/')
if hash(password) == rows[0][2]:
session['username'] = username
return redirect('/scores')
else:
return redirect('/')
@app.route('/assets/<path:file>')
def serve_asset(file):
file = file.replace('&', '%26')
if os.path.isfile(os.path.join('./assets',file)):
return send_from_directory('./assets',file)
file = file.replace('_byd', '')
return send_from_directory('./assets', file)
@app.route('/favicon.ico')
def serve_favicon():
return send_from_directory('./','favicon.ico')
@app.route('/register', methods=['POST'])
def register():
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
username = request.form['username']
password = request.form['password']
confirm_password = request.form['confirm-password']
if password != confirm_password:
print(Fore.RED + f'Error: Passwords do not match')
Fore.WHITE
return jsonify({'message': "Passwords do not match"})
cursor.execute('''SELECT * from users WHERE username = ?''', (username,))
rows = cursor.fetchall()
if len(rows) == 0:
cursor.execute('''INSERT INTO users(username, password) VALUES (?, ?)''', (username, hash(password)))
conn.commit()
session['username'] = username
return redirect('/scores')
print(Fore.RED + f'Error: {username} already exists')
Fore.WHITE
return jsonify({'message': "Username already exists"})
@app.route('/username', methods=['GET'])
def get_username():
if session.get('username'):
return jsonify({'username': session['username']})
else:
return jsonify({'username': 'null'})
@app.route('/get_chart', methods=['POST'])
def get_chart():
rows = []
if session.get('username'):
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''SELECT * from songs WHERE username = ?''', (session['username'],))
rows = cursor.fetchall()
conn.close()
else:
return jsonify({'success': False}), 400
data = []
for song in songlist['songs']:
if song['id'] not in chartconstant:
continue
chart = chartconstant[song['id']]
for index in range(len(chart)):
if not chart[index]:
continue
res = [item for item in rows if item[0] == song['id'] and item[1] == class_song[index] and item[3] == session['username']]
data.append({
'id': song['id'],
'title': song['title_localized']['en'],
'artist': song['artist'],
'difficulty': chart[index]['constant'],
'class': class_song[index],
'score': res[0][2] if len(res) == 1 else 0,
'rating': calcRating(chart[index]['constant'], res[0][2] if len(res) == 1 else 0)
})
data.sort(key=lambda x: x['difficulty'])
data = data[::-1][:int(os.getenv('MAX_SONG_LOADED'))][::-1]
return jsonify(data)
@app.route('/update_chart', methods=['POST'])
def update_chart():
if not session.get('username'):
return jsonify({'success': False}), 400
data = request.get_json()
if data.get('score') == '':
data['score'] = 0
print(Fore.GREEN + f'{data}')
Fore.WHITE
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO songs VALUES (?, ?, ?, ?);
''',
(data.get('id'), data.get('class'), data.get('score'), session['username'])
)
conn.commit()
conn.close()
print('Chart updated!')
return jsonify({'success': True})
@app.route('/get_bests', methods=['GET'])
def get_bests():
if not session.get('username'):
return jsonify({'success': False}), 400
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''SELECT * from songs WHERE username = ?''', (session['username'],))
rows = cursor.fetchall()
data = []
for row in rows:
data.append({
'id': row[0],
'name': next(song for song in songlist['songs'] if song['id'] == row[0])['title_localized']['en'],
'class': row[1],
'difficulty': chartconstant[row[0]][class2num[row[1]]]['constant'],
'score': row[2],
'rating': round(calcRating(chartconstant[row[0]][class2num[row[1]]]['constant'], row[2]), 3)
})
data.sort(key=lambda x: x['rating'], reverse=True)
conn.close()
return jsonify(data[:50])
@app.route('/chat', methods=['POST'])
def chat():
if not session.get('username'):
return jsonify({'success': False}), 400
data = request.get_json()
if data.get('id') == 0:
df = pd.read_csv(os.path.join(curdir, os.getenv('RECOMMEND_PATH')))
df = df.to_numpy().flatten().tolist()
return jsonify({'message': df[random.randint(0, len(df)-1)]})
elif data.get('id') == 1:
if not session.get('username'):
return jsonify({'success': False}), 400
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''SELECT * from songs WHERE username = ?''', (session['username'],))
rows = cursor.fetchall()
data = []
for row in rows:
data.append({
'id': row[0],
'name': next(song for song in songlist['songs'] if song['id'] == row[0])['title_localized']['en'],
'class': row[1],
'difficulty': chartconstant[row[0]][class2num[row[1]]]['constant'],
'score': row[2],
'rating': round(calcRating(chartconstant[row[0]][class2num[row[1]]]['constant'], row[2]), 3)
})
data.sort(key=lambda x: x['rating'], reverse=True)
conn.close()
if len(data) < 40:
return jsonify({'message': "歌都没打几首,急着推分作甚?"})
else:
id = random.randint(30, 39)
return jsonify({'message': f"推荐这首{data[id]['name']}!目前的得分为{int(data[id]['score'])},不过不要强推,小心手癖!"})
elif data.get('id') == 2:
df = pd.read_csv(os.path.join(curdir, os.getenv('BIBLE_PATH')))
df = df.to_numpy().flatten().tolist()
return jsonify({'message': df[random.randint(0, len(df)-1)]})
else:
return jsonify({'message': "爆!"})
@app.route('/logout', methods=['POST'])
def logout():
if session.get('username'):
session.pop('username', None)
return redirect('/')
@app.route('/get_avatar', methods=['GET'])
def get_avatar():
if not session.get('username'):
return jsonify({'avatar': 'default.png'}), 400
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''SELECT * from custom WHERE username = ?''', (session['username'],))
rows = cursor.fetchall()
if len(rows) == 0:
conn.close()
return jsonify({'avatar': 'default.png'})
else:
conn.close()
return jsonify({'avatar': rows[0][1]})
@app.route('/avatar_list', methods=['GET'])
def avatar_list():
if not session.get('username'):
return jsonify({'success': False}), 400
return jsonify(os.listdir('./assets/avatars')), 200
@app.route('/set_avatar', methods=['POST'])
def set_avatar():
if not session.get('username'):
return jsonify({'success': False}), 400
data = request.get_json()
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''INSERT OR REPLACE INTO custom (username, avatar) VALUES (?, ?)''', (session['username'], data.get('avatar')))
conn.commit()
conn.close()
return jsonify({'success': True}), 200
@app.route('/get_p30', methods=['GET'])
def get_p30():
if not session.get('username'):
return jsonify({'success': False}), 400
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''SELECT * from songs WHERE username = ?''', (session['username'],))
rows = cursor.fetchall()
data = []
for row in rows:
if row[2] == '' or int(row[2]) < 1000_0000:
continue
data.append({
'id': row[0],
'name': next(song for song in songlist['songs'] if song['id'] == row[0])['title_localized']['en'],
'class': row[1],
'difficulty': chartconstant[row[0]][class2num[row[1]]]['constant'],
'score': row[2],
'rating': round(calcRating(chartconstant[row[0]][class2num[row[1]]]['constant'], row[2]), 3)
})
data.sort(key=lambda x: x['rating'], reverse=True)
conn.close()
return jsonify(data[:30])
@app.route('/get_max', methods=['GET'])
def get_max():
data = []
for song in songlist['songs']:
if song['id'] not in chartconstant:
continue
chart = chartconstant[song['id']]
for index in range(len(chart)):
if not chart[index]:
continue
data.append({
'id': song['id'],
'name': song['title_localized']['en'],
'artist': song['artist'],
'difficulty': chart[index]['constant'],
'class': class_song[index],
"score": 1000_0616,
"rating": calcRating(chart[index]['constant'], 1000_0000)
})
data.sort(key=lambda x: x['difficulty'])
data = data[::-1]
return jsonify(data[:30])
@app.route('/export_chart', methods=['POST'])
def export_chart():
if not session.get('username'):
return jsonify({'success': False}), 400
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''SELECT * from songs WHERE username = ?''', (session['username'],))
rows = cursor.fetchall()
data = []
for row in rows:
data.append({
'id': row[0],
'class': row[1],
'score': row[2],
})
df = pd.DataFrame(data)
df.to_csv(os.path.join(curdir, os.path.join(os.getenv('EXPORT_PATH')), f'{session["username"]}_chart_{datetime.date.today()}.csv'), index=False)
return jsonify({'success': True}), 200
@app.route('/import_chart', methods=['POST'])
def import_chart():
if not session.get('username'):
return jsonify({'success': False}), 400
data = request.files['file']
print(Fore.GREEN + f'{data}')
Fore.WHITE
try:
df = pd.read_csv(data)
data = df.to_dict('records')
except Exception as e:
print(Fore.RED + f'Error: {e}')
Fore.WHITE
return jsonify({'success': False}), 500
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
for row in data:
cursor.execute('''INSERT OR REPLACE INTO songs VALUES (?, ?, ?, ?)''', (row.get('id'), row.get('class'), row.get('score'), session['username']))
conn.commit()
conn.close()
return jsonify({'success': True}), 200
@app.route('/apply_sorter', methods=['POST'])
def apply_sorter():
if not session.get('username'):
return jsonify({'success': False}), 400
sorter = request.get_json()
print(Fore.GREEN + f'{sorter}')
Fore.WHITE
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
rows = []
conn = sqlite3.connect(os.path.join(curdir, os.getenv('DB_PATH')))
cursor = conn.cursor()
cursor.execute('''SELECT * from songs WHERE username = ?''', (session['username'],))
rows = cursor.fetchall()
conn.close()
data = []
for song in songlist['songs']:
if song['id'] not in chartconstant:
continue
chart = chartconstant[song['id']]
for index in range(len(chart)):
if not chart[index]:
continue
res = [item for item in rows if item[0] == song['id'] and item[1] == class_song[index] and item[3] == session['username']]
data.append({
'id': song['id'],
'title': song['title_localized']['en'],
'artist': song['artist'],
'difficulty': chart[index]['constant'],
'class': class_song[index],
'score': res[0][2] if len(res) == 1 else 0,
'rating': calcRating(chart[index]['constant'], res[0][2] if len(res) == 1 else 0)
})
data = [
song for song in data
if song['difficulty'] >= float(sorter['minDifficulty'])
and song['difficulty'] <= float(sorter['maxDifficulty'])
and sorter['classes'][song['class']]
]
match sorter['sorter']:
case 'byDifficulty':
data.sort(key=lambda x: x['difficulty'])
case 'byRating':
data.sort(key=lambda x: x['rating'])
case 'byName':
data.sort(key=lambda x: x['title'], reverse=True)
case 'byArtist':
data.sort(key=lambda x: x['artist'], reverse=True)
case _:
data.sort(key=lambda x: x['difficulty'])
return jsonify(data)
if __name__ == '__main__':
print( '==============================================================================================')
print(r' _____ _____ _____.__ .__ ')
print(r' / _ \_______ ____ _____ ____ _____ _____/ ____\/ ____\ | |__| ____ ____ ')
print(r' / /_\ \_ __ \_/ ___\\__ \ _/ __ \\__ \ / _ \ __\\ __\| | | |/ \_/ __ \ ')
print(r' / | \ | \/\ \___ / __ \\ ___/ / __ \_ ( <_> ) | | | | |_| | | \ ___/ ')
print(r' \____|__ /__| \___ >____ /\___ >____ / \____/|__| |__| |____/__|___| /\___ >')
print(r' \/ \/ \/ \/ \/ \/ \/ ')
print( '==============================================================================================')
print('ARCAEA OFFLINE IS RUNNING AT http://localhost:5000/')
server = pywsgi.WSGIServer(('0.0.0.0', int(os.getenv('PORT'))), app)
server.serve_forever()