This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
196 lines (169 loc) · 7.2 KB
/
main.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
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
from config import Config
from objects.flag import Staff
from flask.json import JSONEncoder
from flask import Flask, send_from_directory, session, redirect, url_for, request
from flask_socketio import SocketIO, send, emit
from datetime import datetime
from functools import wraps
from objects import osuapi, mysql
import re, os
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, datetime):
return obj.isoformat()
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
db = mysql.DB()
app = Flask(__name__, instance_relative_config=True)
app.config.from_object(Config)
socketio = SocketIO(app)
# Pick ban Socket.io
@socketio.on('firstdata', namespace='/pickban')
def handle_data(d):
if session["match_set_id"] > 98:
return emit(f'data_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full_championship(session["match_set_id"]))
return emit(f'data_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full(session["match_set_id"]))
@socketio.on('pickban', namespace='/pickban')
def handle_data(d):
if not session:
return emit('new_result', 'You are not login yet?')
t_data = db.query('SELECT team as id FROM player WHERE user_id=%s', session['user_id'])
db.query('INSERT INTO match_sets_banpick (`set_id`, `map_id`, `from`, `type`) VALUES (%s, %s, %s, %s);', [d['set'], d['map'], t_data['id'], d['type']])
if session["match_set_id"] > 98:
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full_championship(session["match_set_id"]), broadcast=True)
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full(session["match_set_id"]), broadcast=True)
@socketio.on('ready', namespace='/pickban')
def handle_data(d):
if not session:
return emit('new_result', 'You are not login yet?')
if session["match_set_id"] > 98:
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full_championship(session["match_set_id"]), broadcast=True)
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full(session["match_set_id"]), broadcast=True)
@socketio.on('disconnect', namespace='/pickban')
def handle_dis():
try:
db.query("UPDATE `tourney`.`player` SET `online`='0' WHERE `user_id`=%s;", session['user_id'])
except KeyError:
print("Someone disconnecting socket with Anonymous person")
print('disconnect activated')
if session["match_set_id"] > 98:
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full_championship(session["match_set_id"]), broadcast=True)
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full(session["match_set_id"]), broadcast=True)
@socketio.on('connect', namespace='/pickban')
def handle_dis():
try:
db.query("UPDATE `tourney`.`player` SET `online`='1' WHERE `user_id`=%s;", session['user_id'])
except KeyError:
print("Someone connecting socket with Anonymous person")
if session["match_set_id"] > 98:
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full_championship(session["match_set_id"]), broadcast=True)
return emit(f'new_result_{session["match_set_id"]}', db.get_match_sets_ban_pick_full(session["match_set_id"]), broadcast=True)
# End of Pick ban Socket.io
from blueprints.stream import stream
app.register_blueprint(stream, url_prefix='/stream')
from blueprints.backend import backend
app.register_blueprint(backend, url_prefix='/manager')
from blueprints.api import api
app.register_blueprint(api, url_prefix='/api')
from blueprints.frontend import frontend
app.register_blueprint(frontend, url_prefix='/')
from blueprints.pickban import pickban
app.register_blueprint(pickban, url_prefix='/pickban')
app.json_encoder = CustomJSONEncoder
def let_login(user):
session.clear()
session.permanent = True
session['id'] = user['id']
session['user_id'] = user['user_id']
session['username'] = user['username']
@app.route('/login')
def callback():
if request.args.get('code'):
u = osuapi.get_token(request.args['code'])
try:
next_url = request.args.get('state').split('->')
user = osuapi.get2(u['access_token'], me='')
player = db.query('SELECT id, user_id, username from player WHERE `user_id`=%s', [user['id']])
staff = db.get_staff(user_id=user['id'])
if staff:
let_login(staff)
print('Someone Logged to This Website With Staff Perm')
return redirect(Config.BASE_URL + next_url[1])
elif player:
let_login(player)
print('Someone Logged to This Website With Player Perm')
return redirect(Config.BASE_URL + next_url[1])
else:
return redirect(Config.BASE_URL)
except Exception as e:
print(e)
return redirect(Config.BASE_URL)
@app.route('/logout')
def logout():
session.clear()
return redirect(Config.BASE_URL)
@app.route('/favicon.ico')
def faviconico():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.context_processor
def rounds():
return dict(active_rounds=db.active_rounds)
@app.context_processor
def current_round():
return dict(current_round=db.current_round)
@app.context_processor
def tourney_info():
return dict(tourney=db.tourney)
@app.template_filter('num')
def num_filter(num):
if type(num) == int:
return f'{num:,}'
remain_amount = '%0.2f' % (num * 100 / 100.0)
return re.sub(r"(\d)(?=(\d\d\d)+(?!\d))", r"\1,", remain_amount)
@app.template_filter('floatfix')
def num_filter(num):
remain_amount = '%0.2f' % (float(num))
return re.sub(r"(\d)(?=(\d\d\d)+(?!\d))", r"\1,", remain_amount)
@app.template_filter('timef')
def timef(num):
return '%d:%02d' % (num//60, num%60)
@app.template_filter('datetime')
def dtf(date_time: datetime, sep='T', timespec='auto'):
return date_time.isoformat(sep, timespec)
@app.template_filter('strdtf')
def strdtf(str_dt: str, sep='T', timespec='auto'):
return datetime.fromisoformat(str_dt).isoformat(sep, timespec)
@app.template_filter('flag_url')
def flag_url(flag_name: str):
if not flag_name:
return ''
flag_name = flag_name.split('.')
if flag_name[0] == 'avatar':
return f'https://a.ppy.sh/{flag_name[1]}'
elif flag_name[0] == 'local':
return f'/team_flag/{flag_name[1]}'
elif flag_name[0] == 'url':
return flag_name[1]
elif flag_name[0] == 'none':
return ''
@app.template_filter('privilege')
def privilege(num):
return str(Staff(num))[6:].replace('|', ', ')
@app.errorhandler(404)
def page_not_foubd(error):
return error
@app.template_global()
def base_url():
return str(os.environ.get('BASE_URL', 'http://localhost:5000'))
if __name__ == '__main__':
socketio.run(
app,
host=str(os.environ.get('HOST', '127.0.0.1')),
port=int(os.environ.get('PORT', 5000)),
debug=bool(os.environ.get('DEBUG', True)),
)