forked from cdanis/flask-esipy-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
520 lines (432 loc) · 19.3 KB
/
app.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# -*- encoding: utf-8 -*-
from datetime import datetime
from esipy import EsiApp
from esipy import EsiClient
from esipy import EsiSecurity
from esipy.exceptions import APIException
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
from flask import session
from flask import url_for
from flask_login import LoginManager
from flask_login import UserMixin
from flask_login import current_user
from flask_login import login_required
from flask_login import login_user
from flask_login import logout_user
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import UniqueConstraint
from sqlalchemy import func
import config
import hashlib
import hmac
import logging
import random
import time
# logger stuff
logger = logging.getLogger(__name__)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(formatter)
logger.addHandler(console)
# init app and load conf
app = Flask(__name__)
app.config.from_object(config)
# init db
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# init flask login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# -----------------------------------------------------------------------
# Database models
# -----------------------------------------------------------------------
class Tournament(db.Model):
tournament_id = db.Column(
db.Integer,
primary_key=True,
)
#tournament_owner=db.Column(db.Integer, db.ForeignKey('user.character_id'), nullable=False)
tournament_name = db.Column(db.String(255))
tournament_description = db.Column(db.String(1024))
tournament_url = db.Column(db.String(255))
tournament_type = db.Column(db.String(40))
active = db.Column(db.Integer)
current_match_id=db.Column(db.Integer, db.ForeignKey('match.match_id'), nullable=True)
current_match=db.Relationship("Match", foreign_keys='Tournament.current_match_id')
players = db.relationship('Player', foreign_keys='Player.tournament_id', backref='tournament')
team_size = db.Column(db.Integer)
matches = db.relationship('Match', foreign_keys='Match.tournament_id', backref='tournament')
class Match(db.Model):
match_id = db.Column(db.Integer, primary_key=True)
tournament_id = db.Column(db.Integer, db.ForeignKey('tournament.tournament_id'), nullable=False)
red_team_name = db.Column(db.String(255))
red_team_members = db.Column(db.String(2048)) #a list of character ids, comma separated
blue_team_name = db.Column(db.String(255)) #a list of character ids, comma separated
blue_team_members = db.Column(db.String(2048))
#returns a list of character ids
def get_red_players(self):
return(self.red_team_members.split(','))
def get_blue_players(self):
return(self.blue_team_members.split(','))
class Player(db.Model):
competitor_id = db.Column(db.Integer, primary_key=True)
character_id = db.Column(db.Integer)
tournament_id = db.Column(db.Integer, db.ForeignKey('tournament.tournament_id'), nullable=False)
active = db.Column(db.Integer)
wins = db.Column(db.Integer)
losses = db.Column(db.Integer)
matches = db.Column(db.Integer)
class SoloQueue(Tournament):
def join_lobby(self, character):
lobby_member_list = [s for s in self.players if s.character_id==character]
#if this is a new entrant, create a Player record for them
if len(lobby_member_list) == 0:
lobby_member = Player(character_id=character,tournament_id=self.tournament_id,active=1,wins=0,losses=0,matches=0)
#If they were inactive, set them back to active. If they were already active & in the tournament, this is a no-op
elif len(lobby_member_list) == 1:
lobby_member = lobby_member_list[0]
lobby_member.active = 1
#TODO complain if there's >1 entry in the lobby for this character
db.session.merge(lobby_member)
db.session.commit()
# We leave lobbies by setting the player's active flag to 0. This allows them to rejoin with their metadata intact.
def leave_lobby(self, character):
lobby_member_list = [s for s in self.players if s.character_id==character]
lobby_member = lobby_member_list[0]
lobby_member.active = 0
db.session.merge(lobby_member)
db.session.commit()
def pick_players_from_lobby(self):
#Get all competitors in the tournament
lobby = Player.query.filter(Player.tournament_id==self.tournament_id, Player.active==1)
max_matches = db.session.query(func.max(Player.matches).filter(Player.active==1),Player.tournament_id==self.tournament_id).scalar()
min_matches = db.session.query(func.min(Player.matches).filter(Player.active==1),Player.tournament_id==self.tournament_id).scalar()
this_match_players = [] #list of Players
# Turn the list of competitors into a dict of competitors grouped by number of matches played
by_matches = {} #dict of Players
for elem in lobby:
try:
by_matches[elem.matches].append(elem)
except KeyError:
by_matches[elem.matches] = [elem]
#Starting with the list of players who have played the fewest matches, shuffle the list of
#players at a given match count, take the first 2*self.team_size (aka enough to make a match)
#then keep going if we don't have a full match yet
for i in range(min_matches, max_matches + 1):
if(len(this_match_players) < 2*self.team_size):
random.shuffle(by_matches[i])
this_match_players.extend(by_matches[i][0:2*self.team_size - len(this_match_players)])
#Shuffle our list of players again and return it. The caller will use the first team_size elements as red team,
#and the remainder as blue team.
random.shuffle(this_match_players)
return this_match_players #returns a list of Players
def create_match(self):
if(Player.query.filter(Player.tournament_id==self.tournament_id,Player.active==1).count() < self.team_size*2):
#TODO throw some exception or smth
return
this_match_players = self.pick_players_from_lobby() #Players, shuffled.
red_team_str = ','.join([str(p.character_id) for p in this_match_players[0:self.team_size]])
blue_team_str = ','.join([str(p.character_id) for p in this_match_players[self.team_size:self.team_size*2]])
match = Match(tournament_id=self.tournament_id, red_team_name='red', red_team_members=red_team_str, blue_team_name='blue', blue_team_members = blue_team_str)
self.current_match = match
db.session.merge(match)
db.session.merge(self)
db.session.commit()
def resolve_match(self, winning_team_name):
match = self.current_match
blue_team_wins = self.current_match.blue_team_name == winning_team_name
red_team_wins = self.current_match.red_team_name == winning_team_name
if(blue_team_wins == red_team_wins):
raise Exception('BadWinningTeamName',winning_team_name)
for character_id in self.current_match.get_red_players():
comp = Player.query.filter(Player.character_id == character_id,Player.tournament_id == self.tournament_id).first()
comp.matches = comp.matches + 1
if red_team_wins:
comp.wins = comp.wins + 1
db.session.merge(comp)
for character_id in self.current_match.get_blue_players():
comp = Player.query.filter(Player.character_id == character_id,Player.tournament_id == self.tournament_id).first()
if blue_team_wins:
comp.wins = comp.wins + 1
comp.matches = comp.matches + 1
db.session.merge(comp)
self.current_match = None
db.session.merge(self)
db.session.commit()
class User(db.Model, UserMixin):
# our ID is the character ID from EVE API
character_id = db.Column(
db.BigInteger,
primary_key=True,
autoincrement=False
)
character_owner_hash = db.Column(db.String(255))
character_name = db.Column(db.String(200))
# SSO Token stuff
access_token = db.Column(db.String(4096))
access_token_expires = db.Column(db.DateTime())
refresh_token = db.Column(db.String(100))
def get_id(self):
""" Required for flask-login """
return self.character_id
def get_sso_data(self):
""" Little "helper" function to get formated data for esipy security
"""
return {
'access_token': self.access_token,
'refresh_token': self.refresh_token,
'expires_in': (
self.access_token_expires - datetime.utcnow()
).total_seconds()
}
def update_token(self, token_response):
""" helper function to update token data from SSO response """
self.access_token = token_response['access_token']
self.access_token_expires = datetime.fromtimestamp(
time.time() + token_response['expires_in'],
)
if 'refresh_token' in token_response:
self.refresh_token = token_response['refresh_token']
class Character(db.Model):
character_id = db.Column(
db.BigInteger,
primary_key=True,
autoincrement=False
)
character_name = db.Column(db.String(200))
# -----------------------------------------------------------------------
# Helpers
# -----------------------------------------------------------------------
class Display_Player:
name = "placeholder"
matches = 0
wins = 0
def __init__(self, name, matches, wins):
self.name = name
self.matches = matches
self.wins = wins
class Display_Match:
red_team = ["placeholder"]
red_team_name = "placeholder"
blue_team = ["placeholder"]
blue_team_name = "placeholder"
def __init__(self, red_team, blue_team, blue_team_name="Blue placeholder", red_team_name="Red placeholder"):
self.red_team = red_team
self.red_team_name = red_team_name
self.blue_team = blue_team
self.blue_team_name = blue_team_name
def get_current_tournaments():
return(Tournament.query.filter(Tournament.active==1).all())
def get_char_name(char_id):
try:
char = Character.query.filter(
Character.character_id == char_id
).one()
except NoResultFound:
char = Character(character_id=char_id)
op = esiapp.op['characters_character_id'](character_id=char_id)
char_info = esiclient.request(op)
char.character_name = char_info.data.name
db.session.merge(char)
db.session.commit()
return char.character_name
# -----------------------------------------------------------------------
# Flask Login requirements
# -----------------------------------------------------------------------
@login_manager.user_loader
def load_user(character_id):
""" Required user loader for Flask-Login """
return User.query.get(character_id)
# -----------------------------------------------------------------------
# ESIPY Init
# -----------------------------------------------------------------------
# create the app
esiapp = EsiApp().get_latest_swagger
# init the security object
esisecurity = EsiSecurity(
redirect_uri=config.ESI_CALLBACK,
client_id=config.ESI_CLIENT_ID,
secret_key=config.ESI_SECRET_KEY,
headers={'User-Agent': config.ESI_USER_AGENT}
)
# init the client
esiclient = EsiClient(
security=esisecurity,
cache=None,
headers={'User-Agent': config.ESI_USER_AGENT}
)
# -----------------------------------------------------------------------
# Individual Tournament Route
# Just doing soloQ for now
# -----------------------------------------------------------------------
@app.route('/tournament/<int:tid>')
def display_tournament(tid):
tournament = SoloQueue.query.filter(SoloQueue.tournament_id==tid).first()
players = [Display_Player(name=get_char_name(p.character_id), matches=p.matches, wins=p.wins) for p in tournament.players]
if tournament.current_match is not None:
matches = Display_Match(red_team=[get_char_name(p) for p in tournament.current_match.red_team_members.split(',')], red_team_name=tournament.current_match.red_team_name, blue_team=[get_char_name(p) for p in tournament.current_match.blue_team_members.split(',')], blue_team_name=tournament.current_match.blue_team_name)
return render_template("tournament.html",tournament=tournament, players=players, match=matches)
else:
return render_template("tournament.html",tournament=tournament, players=players)
@app.route('/join_lobby/<int:tid>')
def join_lobby(tid):
tournament = SoloQueue.query.filter(SoloQueue.tournament_id==tid).first()
tournament.join_lobby(current_user.character_id)
return redirect(url_for("display_tournament",tid=tournament.tournament_id))
@app.route('/leave_lobby/<int:tid>')
def leave_lobby(tid):
tournament = SoloQueue.query.filter(SoloQueue.tournament_id==tid).first()
tournament.leave_lobby(current_user.character_id)
return redirect(url_for("display_tournament",tid=tournament.tournament_id))
@app.route('/fill_lobby/<int:tid>')
def fill_lobby(tid):
tournament = SoloQueue.query.filter(SoloQueue.tournament_id==tid).first()
tournament_players = [2113791869,90870204,2112657046,94546718,2119449883,2112676318,2122551208,2117249104,2118364597,975862704,2115476544,96894412,2117568225,96913527,1684204265,938041033,1869635534,93087693,1045876147,1353088896,95228453,2114159965,727382616]
for e in tournament_players:
tournament.join_lobby(e)
return redirect(url_for("display_tournament",tid=tournament.tournament_id))
@app.route('/declare_victory/<int:tid>/<team_name>')
def declare_victory(tid, team_name):
tournament = SoloQueue.query.filter(SoloQueue.tournament_id==tid).first()
tournament.resolve_match(team_name)
return redirect(url_for("display_tournament",tid=tournament.tournament_id))
@app.route('/generate_match/<int:tid>')
def generate_match(tid):
tournament = SoloQueue.query.filter(SoloQueue.tournament_id==tid).first()
tournament.create_match()
return redirect(url_for("display_tournament",tid=tournament.tournament_id))
@app.route('/create_tournament')
def create_tournament():
tournament = SoloQueue(tournament_name="Demo solo queue", tournament_description="Demo solo queue. Team size hardcoded to 7.", tournament_url="https://eveonline.com", tournament_type="soloq", team_size=7,active=1)
db.session.merge(tournament)
db.session.commit()
tournament = SoloQueue.query.filter(SoloQueue.tournament_name=="Demo solo queue", SoloQueue.active==1).first()
return redirect(url_for("display_tournament",tid=tournament.tournament_id))
@app.route('/close_tournament/<int:tid>')
def close_tournament(tid):
tournament = SoloQueue.query.filter(SoloQueue.tournament_id==tid).first()
tournament.active=0
db.session.merge(tournament)
db.session.commit()
return redirect(url_for("index"))
# -----------------------------------------------------------------------
# Login / Logout Routes
# -----------------------------------------------------------------------
def generate_token():
"""Generates a non-guessable OAuth token"""
chars = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
rand = random.SystemRandom()
random_string = ''.join(rand.choice(chars) for _ in range(40))
return hmac.new(
config.SECRET_KEY.encode('utf-8'),
random_string.encode('utf-8'),
hashlib.sha256
).hexdigest()
@app.route('/sso/login')
def login():
""" this redirects the user to the EVE SSO login """
token = generate_token()
session['token'] = token
return redirect(esisecurity.get_auth_uri(
state=token,
scopes=['publicData']
))
@app.route('/sso/logout')
@login_required
def logout():
logout_user()
return redirect(url_for("index"))
@app.route('/sso/callback')
def callback():
""" This is where the user comes after he logged in SSO """
# get the code from the login process
code = request.args.get('code')
token = request.args.get('state')
# compare the state with the saved token for CSRF check
sess_token = session.pop('token', None)
if sess_token is None or token is None or token != sess_token:
return 'Login EVE Online SSO failed: Session Token Mismatch', 403
# now we try to get tokens
try:
auth_response = esisecurity.auth(code)
except APIException as e:
return 'Login EVE Online SSO failed: %s' % e, 403
# we get the character informations
cdata = esisecurity.verify()
# if the user is already authed, we log him out
if current_user.is_authenticated:
logout_user()
# Check to see if we already have a record for this character
try:
character = Character.query.filter(Character.character_id == cdata['sub'].split(':')[2],
).one()
except NoResultFound:
character = Character()
character.character_id = cdata['sub'].split(':')[2]
# now check to see if we have a record for this user
# actually we'd have to also check with character_owner_hash, to be
# sure the owner is still the same, but that's an example only...
try:
user = User.query.filter(
User.character_id == cdata['sub'].split(':')[2],
).one()
except NoResultFound:
user = User()
user.character_id = cdata['sub'].split(':')[2]
user.character_owner_hash = cdata['owner']
user.character_name = cdata['name']
character.character_name = cdata['name']
user.update_token(auth_response)
# now the user is ready, so update/create it and character if needed and log the user
try:
db.session.merge(user)
db.session.merge(character)
db.session.commit()
login_user(user)
session.permanent = True
except:
logger.exception("Cannot login the user - uid: %d" % user.character_id)
db.session.rollback()
logout_user()
return redirect(url_for("index"))
# -----------------------------------------------------------------------
# Index Routes
# -----------------------------------------------------------------------
@app.route('/')
def index():
if current_user.is_authenticated:
# give the token data to esisecurity, it will check alone
# if the access token need some update
esisecurity.update_token(current_user.get_sso_data())
return render_template('base.html')
@app.route('/demo')
def demo():
if current_user.is_authenticated:
# give the token data to esisecurity, it will check alone
# if the access token need some update
esisecurity.update_token(current_user.get_sso_data())
tournaments = get_current_tournaments()
return render_template('demo.html', current_tournaments=tournaments)
else:
return render_template('base.html')
#@app.route('/debug')
#def debug():
# test = SoloQueue.query.all()[0]
# test.create_match()
# test.resolve_match('red')
# test.leave_lobby(18)
# test.create_match()
# test.resolve_match('blue')
# test.join_lobby(18)
# raise Exception("EverythingWorked","Sounds fake...")
if __name__ == '__main__':
app.run(port=config.PORT, host=config.HOST)