def score_team_v_team(pool: Pool, teamA: Team, teamB: Team): tournament = Tournament.get() id_matches_to_consider_temp = [ match_id for match_id in teamA.match_list if tournament.matches[match_id].isPool and tournament.matches[match_id].origin == pool.id ] id_matches_to_consider = [ match_id for match_id in id_matches_to_consider_temp if tournament.matches[match_id].id_J1 in teamB.athletes_id or tournament.matches[match_id].id_J2 in teamB.athletes_id ] gagne = 0 for match_id in id_matches_to_consider: if tournament.matches[match_id].winner in teamA.athletes_id: gagne += 1 else: gagne -= 1 penalties = 0 score = 0 for match_id in id_matches_to_consider: match = tournament.matches[match_id] if match.id_J1 in teamA.athletes_id: score += 10 * (match.score_J1[0] - match.score_J2[0]) + 5 * ( match.score_J1[1] - match.score_J2[1]) penalties += match.score_J1[2] - match.score_J2[2] else: score -= 10 * (match.score_J1[0] - match.score_J2[0]) + 5 * ( match.score_J1[1] - match.score_J2[1]) penalties -= match.score_J1[2] - match.score_J2[2] return gagne, score, penalties
def add_team(): tournament = Tournament.get() logging.debug(f"Request received on add_team:\n{request}") json = request.json athletes_temp = json["user"] id_athletes_in_team = [] athlete_in_team = [] for athlete_dict in athletes_temp: if len(athlete_dict["name"]) > 0: try: weight = float(athlete_dict["weight"]) except: weight = -1 athlete = Athlete(athlete_dict["belt"], weight, athlete_dict["name"]) athlete.to_bdd(tournament.conn) id_athletes_in_team.append(athlete.id) athlete_in_team.append(athlete) tournament.add_athlete(athlete) if len(athlete_in_team) == 0: raise BadRequest if len(json["name"]) != 0: name = json["name"] else: name = athletes_temp[0]["name"] team = Team(json["origin"], name, id_athletes_in_team, athlete_in_team) team.to_bdd(tournament.conn) tournament.add_team(team) return ('', 200)
def delete_pool(): tournament = Tournament.get() id = request.json["id"] tournament.pools.pop(id, None) curs = tournament.conn.cursor() curs.execute("""DELETE FROM pools WHERE id=%s""", [id]) return ('', 200)
def add_pool(): tournament = Tournament.get() json = request.json teams_id = json["teams"] pool = Pool([tournament.teams[id] for id in teams_id], json["name"]) pool.to_bdd(tournament.conn) tournament.pools[pool.id] = pool return ('', 200)
def add_bracket(): tournament = Tournament.get() json = request.json bracket = Bracket(json["random"], json["teams"], json["name"], json["durations"]["time_match"], json["durations"]["time_wazahari"], json["durations"]["time_ippon"]) bracket.to_bdd() tournament.brackets[bracket.id] = bracket
def update_team(): tournament = Tournament.get() json_global = request.json json_users = json_global["user"] list_users = [] team = tournament.teams[json_global["id"]] curs = tournament.conn.cursor() logging.info(json_users) for json in json_users: id_user = json["id"] try: name = json["name"] except KeyError: pass try: weight = json["weight"] except KeyError: pass try: belt = json["belt"] except KeyError: pass if id_user != 0: athlete = tournament.athletes[id_user] athlete.name = name athlete.weight = weight athlete.belt = belt curs.execute( """UPDATE users SET name=%s, belt=%s, weight=%s WHERE id=%s""", [athlete.name, athlete.belt, athlete.weight, athlete.id]) else: athlete = Athlete(belt, weight, name) athlete.to_bdd(tournament.conn) tournament.athletes[athlete.id] = athlete list_users.append(athlete.id) for user_id in team.athletes_id: if user_id not in list_users: tournament.athletes.pop(user_id, None) curs.execute(f"""DELETE FROM users WHERE id={user_id}""") team.athletes_id = list_users team.athletes = [tournament.athletes[id] for id in list_users] curs.execute("""UPDATE teams SET athletes=%s WHERE id=%s""", [list_users, team.id]) tournament.conn.commit() curs.close() return ('', 200)
def get_time_match(): tournament = Tournament.get() json = request.json id = json["id"] if json["isPool"]: result = { "time_match": tournament.pools[id].time_match, "time_wazahari": tournament.pools[id].time_wazahari, "time_ippon": tournament.pools[id].time_ippon } else: result = { "time_match": tournament.brackets[id].time_match, "time_wazahari": tournament.brackets[id].time_wazahari, "time_ippon": tournament.brackets[id].time_ippon } return jsonify(result)
def get_pool(): tournament = Tournament.get() pool = tournament.pools[request.json["id"]] result = pool.to_dict() matches = [] for teamA in pool.teams: for teamB in pool.teams: if teamA.id != teamB.id: gagne, score, penalties = score_function(pool, teamA, teamB) victoire = (gagne > 0) - (gagne < 0) if victoire == 0: victoire = (score > 0) - (score < 0) if victoire == 0: victoire = (penalties < 0) - (penalties > 0) logging.info( f"Match entre {teamA.id} et {teamB.id}: {gagne}, {score}, {penalties}, victoire = {victoire}" ) matches.append((teamA.id, teamB.id, victoire)) result["scores"] = matches return jsonify(result)
def get_team_vs(): tournament = Tournament.get() json = request.json teamA = tournament.teams[json["team1"]] teamB = tournament.teams[json["team2"]] if json["isPool"]: gagne, score, penalties = score_function( tournament.pools[json["groupID"]], teamA, teamB) result = { "team1": teamA.to_dict(), "team2": teamB.to_dict(), "winner": gagne, "score": score, "penalties": penalties } matches = [] for match_id in teamA.match_list: match = tournament.matches[match_id] if match.isPool == json["isPool"]: if match.origin == json["groupID"] and match_id in teamB.match_list: inverse = (match.id_J1 in teamB.athletes_id) matches.append(match.to_dict(inverse)) result["matches"] = matches return jsonify(result)
def get_all_team(): tournament = Tournament.get() list_teams = [team.to_dict() for team in tournament.teams.values()] return jsonify(list_teams)
def get_team(): tournament = Tournament.get() json = request.json return jsonify(tournament.teams[json["id"]].to_dict())
def get_user(): tournament = Tournament.get() json = request.json return jsonify(tournament.athletes[json["id"]].to_dict())
def get_all_users(): tournament = Tournament.get() list_athletes = [athlete.to_dict() for athlete in tournament.athletes.values()] return jsonify(list_athletes)
def get_all_pools(): tournament = Tournament.get() return jsonify([pool.to_dict() for pool in tournament.pools.values()])