-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
354 lines (277 loc) · 10.3 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
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
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify
from flask_restplus import Api, Resource, marshal
from flask_restplus.inputs import datetime_from_iso8601
from flask_jwt_extended import JWTManager, get_jwt_identity
from flask_sslify import SSLify
from flask_cors import CORS
import datetime
import logging
from views import createViews
from utils import admin_required
from parsers import *
import config
import orm
from controllers import users, users_matches, admin, matches, matches_stats, matchespending, matchespending_stats, seasons, seasons_matches, algos, users_stats, statistics, penalties, iot
# ========================= INIT
logging.basicConfig(level=logging.DEBUG)
secret = config.secret()
_app = Flask(__name__)
_app.config['JWT_SECRET_KEY'] = secret.get('secret')
_sslify = SSLify(_app)
_cors = CORS(_app)
_jwt = JWTManager(_app)
api = Api(_app,
version="alpha",
title="EkiTag API",
description="Custom tagpro matchmaking and ranking API",
authorizations={
'admin': {
'type': 'apiKey',
'in': 'header',
'name': 'Authorization',
}
})
createViews(api)
# ========================= GETTERS
def app():
"""Return the app object.
Returns:
the app object.
"""
return _app
# ========================= NAMESPACE V1
v1 = api.namespace(name="v1", validate=True)
# ------------------------- RED ALERT HOOK
@v1.route("/iot")
class RedAlert(Resource):
def get(self):
iot.ping()
return
@v1.route("/teams")
class Teams(Resource):
def post(self):
iot.ping()
obj = { 'type': 'message', 'text': 'Hop la.' }
return jsonify(obj)
# ------------------------- USERS
@v1.route("/users")
class Users(Resource):
@api.marshal_with(api.models['Message'])
@api.expect(parser_create_user)
def post(self):
args = parser_create_user.parse_args()
return users.create(**args)
@api.marshal_with(api.models['UserMin'], as_list=True)
def get(self):
return users.index()
@v1.route("/users/<int:user_id>")
class User(Resource):
@api.marshal_with(api.models['User'])
def get(self, user_id):
return users.show(user_id)
@api.marshal_with(api.models['Message'])
@api.expect(parser_update_user)
def put(self, user_id):
args = parser_update_user.parse_args()
return users.update(user_id, **args)
@api.marshal_with(api.models['Message'])
@api.doc(security='admin')
@admin_required
def delete(self, user_id):
return users.delete(user_id)
@v1.route("/users/<int:user_id>/matches")
class UserMatches(Resource):
@api.marshal_with(api.models['UserMatch'], as_list=True)
def get(self, user_id):
return users_matches.index(user_id)
@v1.route("/users/<int:user_id>/user_stats")
class UserStats(Resource):
@api.marshal_with(api.models['UserCustomStats'])
def get(self, user_id):
return users_stats.get_user_custom_stats(user_id)
@v1.route("/users/<int:user_id>/match_stats")
class UserMatchStats(Resource):
@api.marshal_with(api.models['UserCustomMatchStats'])
def get(self, user_id):
return users_stats.get_user_match_stats(user_id)
# ------------------------- ADMIN
@v1.route("/admin/")
class AdminAuth(Resource):
@api.expect(parser_login)
@api.marshal_with(api.models['Auth'])
def post(self):
args = parser_login.parse_args()
return admin.login(**args)
@v1.route("/users/<int:user_id>/promote")
class UserPromote(Resource):
@api.expect(parser_promote)
@api.marshal_with(api.models['Message'])
@api.doc(security='admin')
@admin_required
def post(self, user_id):
args = parser_promote.parse_args()
return admin.create(user_id, **args)
@api.marshal_with(api.models['Message'])
@api.doc(security='admin')
@admin_required
def delete(self, user_id):
return admin.delete(user_id)
# ------------------------- MATCHES
@v1.route("/matches")
class Matches(Resource):
@api.marshal_with(api.models['MatchMin'], as_list=True)
def get(self):
return matches.index()
@v1.route("/matches/<int:match_id>")
class Match(Resource):
@api.marshal_with(api.models['Match'])
def get(self, match_id):
return matches.show(match_id)
@api.marshal_with(api.models['Message'])
@api.doc(security='admin')
@admin_required
def delete(self, match_id):
return matches.delete(match_id)
@v1.route("/matches/pending")
class MatchesPending(Resource):
@api.marshal_with(api.models['MatchPending'], as_list=True)
def get(self):
return matchespending.index()
@api.expect(parser_create_match)
@api.marshal_with(api.models['Message'])
def post(self):
args = parser_create_match.parse_args()
args['datetime'] = datetime_from_iso8601(args['datetime'])
args['duration'] = datetime.timedelta(seconds=args['duration'])
return matchespending.create(**args)
@v1.route("/matches/pending/<int:match_id>")
class MatchPending(Resource):
@api.marshal_with(api.models['MatchPending'])
def get(self, match_id):
return matchespending.show(match_id)
@api.marshal_with(api.models['Message'])
@api.doc(security='admin')
@admin_required
def put(self, match_id):
return matchespending.convert(match_id, validator=get_jwt_identity())
@api.marshal_with(api.models['Message'])
@api.doc(security='admin')
@admin_required
def delete(self, match_id):
return matchespending.delete(match_id)
@v1.route("/matches/pending/<int:match_id>/stats")
class MatchPendingStats(Resource):
# @api.marshal_with(api.models['StatMin'], as_list=True)
# def get(self, match_id):
# return matchespending_stats.index(match_id)
@api.marshal_with(api.models['Message'])
@api.expect(parser_create_stats)
def post(self, match_id):
args = parser_create_stats.parse_args()
if args['hold']:
args['hold'] = datetime.timedelta(seconds=args['hold'])
if args['prevent']:
args['prevent'] = datetime.timedelta(seconds=args['prevent'])
return matchespending_stats.create(match_id, **args)
# ------------------------- SEASONS
@v1.route("/seasons")
class Seasons(Resource):
@api.marshal_with(api.models['Season'], as_list=True)
def get(self):
return seasons.index()
@api.marshal_with(api.models['Message'])
@api.expect(parser_create_season)
@api.doc(security='admin')
@admin_required
def post(self):
args = parser_create_season.parse_args()
if args['max_time'] is not None:
args['max_time'] = datetime.timedelta(seconds=args['max_time'])
return seasons.create(**args)
@v1.route("/seasons/<int:season_id>")
class Season(Resource):
@api.marshal_with(api.models['Season'])
def get(self, season_id):
return seasons.show(season_id)
@api.marshal_with(api.models['Message'])
@api.doc(security='admin')
@admin_required
def delete(self, season_id):
return seasons.delete(season_id)
@v1.route("/seasons/current")
class CurrentSeason(Resource):
@api.marshal_with(api.models['Season'])
def get(self):
return seasons.show_current()
@v1.route("/seasons/<int:season_id>/matches")
class SeasonMatches(Resource):
@api.marshal_with(api.models['MatchMin'], as_list=True)
def get(self, season_id):
return seasons_matches.index(season_id)
# ------------------------- ALGO
@v1.route("/algo/<string:algo>")
class Algo(Resource):
@api.marshal_with(api.models['Algo'], as_list=True)
@api.expect(parser_algo_get)
def get(self, algo):
args = parser_algo_get.parse_args()
iot.ping()
return algos.run(algo, args['ids'])
@v1.route("/algo/<string:algo>/users")
class AlgoUsers(Resource):
@api.marshal_with(api.models['AlgoUsers'])
@api.expect(parser_algo_users_get)
def get(self, algo):
args = parser_algo_users_get.parse_args()
return algos.index(algo, args['season_id'])
@v1.route("/algo/<string:algo>/from_pseudos")
class AlgoFromPseudos(Resource):
@api.marshal_with(api.models['AlgoPseudos'], as_list=True)
@api.expect(parser_algo_from_pseudos)
def get(self, algo):
args = parser_algo_from_pseudos.parse_args()
pseudos = args['pseudos'] + [None for x in range(12 - len(args['pseudos']))]
players = orm.to_json(orm.get_users_by_pseudo(*pseudos))
ids = [x['id'] for x in players]
algo = algos.run(algo, ids)
res = { 'r_pseudos' : [],
'b_pseudos': [],
'quality': algo['quality'] }
for id in algo['r_ids']:
res['r_pseudos'].append(next(pl['pseudo'] for pl in players if pl['id'] == id))
for id in algo['b_ids']:
res['b_pseudos'].append(next(pl['pseudo'] for pl in players if pl['id'] == id))
return res
@v1.route("/algo/<string:algo>/users/<int:user_id>")
class AlgoUser(Resource):
@api.marshal_with(api.models['AlgoUserMinSeason'], as_list=True)
def get(self, algo, user_id):
return algos.show(algo, user_id)
@v1.route("/algo/<string:algo>/users/<int:user_id>/<string:viz>")
class AlgoUserViz(Resource):
@api.marshal_with(api.models['AlgoUserVizHistory'])
def get(self, algo, user_id, viz):
return algos.viz(algo, user_id, viz)
# ------------------------- STAT RANKINGS
@v1.route("/statistics/ranking")
class StatisticsRanking(Resource):
@api.marshal_with(api.models['StatRankings'])
@api.expect(parser_statistics_get)
def post(self):
args = parser_statistics_get.parse_args()
return statistics.rank(args['stat'], args['method'], season_id=args['season_id'] if args['season_id'] else 'NULL')
# ------------------------- PENALTIES
@v1.route("/penalties")
class Penalties(Resource):
@api.marshal_with(api.models['Penalty'])
@api.expect(parser_penalties_create)
def post(self):
args = parser_penalties_create.parse_args()
return penalties.create(args['user_id'], args['season_id'], args['description'], args['value'])
@api.marshal_with(api.models['Penalty'], as_list=True)
@api.expect(parser_penalties_index)
def get(self):
args = parser_penalties_index.parse_args()
season_id = args['season_id'] if 'season_id' in args else None
return penalties.index(season_id)