def finish_round(): db = get_db() id = request.json['id'] round = request.json['round'] winner = "" ranking = [] bracket = list(db.tournaments.find({"_id" : ObjectId(id)}, {"_id": 0, "brackets": 1}))[0]['brackets'] if round + 1 < int(len(bracket)): bracket[round + 1] = make_next_round(bracket[round], round) else: if bracket[round][0]['player_1']['score'] > bracket[round][0]['player_2']['score']: winner = bracket[round][0]['player_1']['name'] else: winner = bracket[round][0]['player_2']['name'] for index, game in enumerate(bracket): position = [] for player in game: if "winner" in player['player_1']: if not player['player_1']['winner']: position.append(player['player_1']['name']) else: if not player['player_2']['winner']: position.append(player['player_2']['name']) ranking.append(position) db.tournaments.update({"_id" : ObjectId(id)}, {"$set":{ "brackets": bracket, "winner": winner, "round_to_finish": None, "ranking": ranking }} ) return jsonify({"rounds": bracket})
def login(): db = get_db() if request.method == 'POST': username = request.json["body"]['username'] password = request.json["body"]['password'] message = None success = False user = db.users.find_one({'username': username}) if not user: db.users.insert_one({ "username": username, "password": generate_password_hash(password) }) user = db.users.find_one({'username': username}) elif not check_password_hash(user['password'], password): message = 'Password does not match' if message is None: session.clear() session['username'] = user['username'] message = 'authenticated' success = True return jsonify({"success": success, "message": message}) return redirect(url_for('index'))
def start_tournament(): db = get_db() id = request.json['id'] db.tournaments.update({"_id" : ObjectId(id)}, {"$set":{ "started": True }} ) return jsonify({ "success": True, "message": "Tournament started" })
def tournament_view(id): db = get_db() tournament = list(db.tournaments.find({"_id" : ObjectId(id)})) return jsonify({ 'tournament': tournament[0], })
def signup_player_to_tournament(): db = get_db() id = request.json['id'] db.tournaments.update({"_id" : ObjectId(id)}, { "$push": { "players_joined": get_current_user() } } ) tournament = list(db.tournaments.find({"_id" : ObjectId(id)})) return jsonify({ 'tournament': tournament[0], })
def user_tournaments_view(): db = get_db() user = get_current_user() tournaments = list(db.tournaments.find({"creator" : user})) return jsonify({ 'tournaments': tournaments, })
def players_view(): db = get_db() players = db.players.find() names = {p['name'] for p in players} return jsonify({ 'players': names, })
def new_tournaments_view(): db = get_db() user = get_current_user() name = request.json['name'] game = request.json['game'] daterange = request.json['daterange'] description = request.json['description'] if not name: return jsonify({ "success": False, "message": "Enter the tournament name" }) if not game: return jsonify({ "success": False, "message": "Enter the tournament game" }) if not daterange: return jsonify({ "success": False, "message": "Enter the tournament daterange" }) tourney_id = db.tournaments.insert_one({ 'name': name.strip(), 'game': game.strip(), 'dates': daterange, 'description': description.strip(), 'creator': user, 'directors': [user], 'assistant_directors': [], 'players_joined': [], 'players_interested': [], 'watchers': [], 'location': None, 'type': 'Bracket', 'active': True, 'created_ts': datetime.datetime.now(), }) return jsonify({ "success": True, "message": "Tournament created" })
def place_players(): db = get_db() id = request.json['id'] players = list(db.tournaments.find({"_id" : ObjectId(id)}, {"_id": 0, "players_joined": 1})) players = players[0]['players_joined'] random.shuffle(players) config = get_tournament_config(players) rounds = [] rounds.append(generate_first_round(players)) for i in range(config['rounds']-1): rounds.append(make_next_round(rounds[i], i)) db.tournaments.update({"_id" : ObjectId(id)}, {"$set":{ "brackets": rounds }} ) return jsonify({ 'brackets': rounds, })
def player_win(): db = get_db() id = request.json['id'] round = request.json['round'] game = request.json['game'] player = request.json['player'] bracket = list(db.tournaments.find({"_id" : ObjectId(id)}, {"_id": 0, "brackets": 1}))[0]['brackets'] not_games = 0 winners = 0 for games in bracket[round]: if 'Winner of' in games['player_1']['name'] or 'Winner of' in games['player_2']['name']: not_games += 1 if 'BYE' in games['player_1']['name'] or 'BYE' in games['player_2']['name']: not_games += 1 if "winner" in games['player_1'] or "winner" in games['player_2']: if games['player_1']['winner'] or games['player_2']['winner']: winners += 1 bracket[round][game][player]['score'] = 1 bracket[round][game][player]['winner'] = True if player == 'player_1': bracket[round][game]['player_2']['winner'] = False else: bracket[round][game]['player_1']['winner'] = False round_to_finish = None is_able_to_finish = False if winners == (int(len(bracket[round]))-1)-not_games: is_able_to_finish = True round_to_finish = round db.tournaments.update({"_id" : ObjectId(id)}, {"$set":{ "brackets": bracket, "round_to_finish": round_to_finish }} ) return jsonify({"is_able_to_finish": is_able_to_finish}) return {"dale": True}
def generate_ranking(id): db = get_db() ranking = list(db.tournaments.find({"_id" : ObjectId(id)}, {"_id": 0, "ranking": 1}))[0]['ranking'] return jsonify({"rounds": ranking})