def create_tournament_from_lobby(lobby_id): if request.method == 'POST': # verify that TO is attempting to access lobby uid = g.user.id uname = g.user.username try: lobby = LobbyModel.query.filter_by(id=lobby_id).first_or_404() if uid != lobby.to_id: key = 'Unauthorized' val = \ f'{g.user.username} cannot create a tournament ' + \ 'using this lobby' content = {key: val} return content, status.HTTP_401_UNAUTHORIZED except NotFound as e: key = str(e).split(':')[0] val = f'lobby {lobby_id} does not exist' content = {key: val} return content, status.HTTP_404_NOT_FOUND # get all competitors in the lobby lobby_entrants = LobbySeed.query.filter_by(lobby_id=lobby_id).all() # construct a list of user_id, seed pairs tuple_list = [(e.user.username, e.seed) for e in lobby_entrants] bracket_type = BracketTypes.DOUBLE_ELIMINATION tournament_name = lobby.tournament_name # contruct a tournament object t = Tournament(tuple_list, bracket_type) t_id = t.post_to_db(tournament_name, uname) # set this lobby's tournament_id lobby.tournament_id = t_id for r in t.bracket.rounds: for m in r.matches: m.post_self_refs() # delete the lobby LobbyModel.query.filter_by(id=lobby_id).delete() LobbySeed.query.filter_by(lobby_id=lobby_id).delete() db.session.commit() content = { 'Success': 'Tournament created', 'tournament_id': t_id, 'tournament_name': tournament_name } return content, status.HTTP_200_OK
def create_tournament_1(): # create a new tournament if request.method == 'POST': users = request.json.get('users') seeds = request.json.get('seeds') tournament_name = request.json.get('tournament_name') bracket_type = BracketTypes.DOUBLE_ELIMINATION TO = g.user.username tuple_list = [(u, s) for u, s in zip(users, seeds)] t = Tournament(tuple_list, bracket_type) t_id = t.post_to_db(tournament_name, TO) # post self references in matches separately for r in t.bracket.rounds: for m in r.matches: m.post_self_refs() return jsonify({'tournament_id': t_id}) else: # return jsonify({ 'username': user.username }), 201, {'location': url_for('get_user', id = user.id, _external = True)} return