Exemplo n.º 1
0
    def league_table(
            league: Optional[str] = None,
            season: Optional[int] = None,
            matchday: Optional[int] = None
    ):
        """
        Calculates the current league table and displays it for the user.
        Can also show a league table based on a user's bets if the
        GET parameter 'use_bets' is set to 1
        :param league: The league to display
        :param season: The season to display
        :param matchday: The matchday to display
        :return: The response
        """
        validated = validate_matchday(league, season, matchday)
        if validated is None:
            return abort(404)
        league, season, matchday = validated

        user = None
        title = "Aktuelle Ligatabelle"
        if request.args.get("use_bets") == "1":
            user = current_user
            title = f"Tabelle nach {current_user.username}'s Tipps"

        table = LeagueTable(league, season, matchday, user)
        return render_template(
            "info/league_table.html",
            league_table=table,
            title=title
        )
Exemplo n.º 2
0
    def leaderboard(
            league: Optional[str] = None,
            season: Optional[int] = None,
            matchday: Optional[int] = None
    ):
        """
        Displays a leaderboard.
        :param league: The league for which to display the leaderboard
        :param season: The season for which to display the leaderboard
        :param matchday: The matchday for which to display the leaderboard
        :return: The Response
        """
        validated = validate_matchday(league, season, matchday)
        if validated is None:
            return abort(404)
        league, season, matchday = validated

        matchday_leaderboard = Leaderboard(
            league,
            season,
            matchday,
            DisplayBotsSettings.get_state(current_user)
        )

        return render_template(
            "ranking/leaderboard.html",
            leaderboard=matchday_leaderboard
        )
Exemplo n.º 3
0
    def user(user_id: int,
             league: Optional[str] = None,
             season: Optional[int] = None,
             matchday: Optional[int] = None):
        """
        Shows a page describing a user + statistics
        :param league: The league to display
        :param season: The season to display
        :param matchday: The matchday to display
        :param user_id: The ID of the user to display
        :return: The request response
        """
        validated = validate_matchday(league, season, matchday)
        if validated is None:
            return abort(404)
        league, season, matchday = validated

        user_data = User.query.get(user_id)
        if user_data is None:
            return abort(404)
        show_bots = DisplayBotsSettings.get_state(current_user) or \
            DisplayBotsSettings.bot_symbol() in user_data.username

        user_stats = UserStatsGenerator(league, season, matchday, user_data,
                                        show_bots)

        return render_template("stats/user.html",
                               user=user_data,
                               user_stats=user_stats)
Exemplo n.º 4
0
 def get_current_bets():
     """
     Displays all matches for the current matchday with entries for betting.
     :return: The response
     """
     league, season, matchday = validate_matchday(None, None, None)
     return get_bets(league, season, matchday)
Exemplo n.º 5
0
    def get_bets(league: str, season: int, matchday: int):
        """
        Displays all matches for a matchday with entries for betting
        :param league: The league to display
        :param season: The season to display
        :param matchday: The matchday to display
        :return: The response
        """
        validated = validate_matchday(league, season, matchday)
        if validated is None:
            return abort(404)
        league, season, matchday = validated

        matches: List[Match] = Match.query.filter_by(
            matchday=matchday, season=season,
            league=league).options(db.joinedload(Match.home_team),
                                   db.joinedload(Match.away_team)).all()
        if len(matches) == 0:
            flash("Den angegebenen Spieltag gibt es nicht", "danger")
            return redirect(url_for("bets.get_bets"))
        matches.sort(key=lambda x: x.kickoff)
        has_started = matches[0].has_started
        all_started = matches[-1].has_started

        bets = Bet.query.filter_by(matchday=matchday,
                                   season=season,
                                   league=league,
                                   user_id=current_user.id).all()

        matchday_points = 0
        for bet in bets:
            if bet.points is not None:
                matchday_points += bet.points

        bet_match_map = {(x.home_team_abbreviation, x.away_team_abbreviation):
                         x
                         for x in bets}

        bet_infos = []
        for match_item in matches:
            index = (match_item.home_team_abbreviation,
                     match_item.away_team_abbreviation)
            bet_infos.append((match_item, bet_match_map.get(index)))

        leaderboard = None
        if has_started:
            leaderboard = Leaderboard(
                league, season, matchday,
                DisplayBotsSettings.get_state(current_user))

        return render_template("betting/bets.html",
                               matchday=matchday,
                               season=season,
                               league=league,
                               bet_infos=bet_infos,
                               matchday_points=matchday_points,
                               has_started=has_started,
                               all_started=all_started,
                               leaderboard=leaderboard)
Exemplo n.º 6
0
    def api_matches(league: str, season: int, matchday: Optional[int] = None):
        """
        Retrieves the matches for a particular matchday
        :return: The list of available leagues
        """
        validated = validate_matchday(league, season, matchday)
        if validated is None:
            return abort(404)
        league, season, matchday = validated

        matches = Match.query.filter_by(league=league,
                                        season=season,
                                        matchday=matchday).all()
        matches.sort(key=lambda x: x.kickoff)
        matches_json = [
            match.__json__(include_children=False) for match in matches
        ]
        return {"matches": matches_json}
Exemplo n.º 7
0
    def stats(
        league: Optional[str] = None,
        season: Optional[int] = None,
        matchday: Optional[int] = None,
    ):
        """
        Displays various global statistics
        :param league: The league to display
        :param season: The season to display
        :param matchday: The matchday to display
        :return: None
        """
        validated = validate_matchday(league, season, matchday)
        if validated is None:
            return abort(404)
        league, season, matchday = validated

        show_bots = DisplayBotsSettings.get_state(current_user)
        global_stats = StatsGenerator(league, season, matchday, show_bots)

        return render_template("stats/stats.html", stats=global_stats)
Exemplo n.º 8
0
    def api_get_league_table(league: str,
                             season: int,
                             matchday: Optional[int] = None):
        """
        Retrieves a league table
        :param league: The league for which to retrieve the table
        :param season: The season for which to retrieve the table
        :param matchday: The matchday for which to retrieve the table
        :return: The leaderboard table
        """
        validated = validate_matchday(league, season, matchday)
        if validated is None:
            return abort(404)
        league, season, matchday = validated

        table_data = LeagueTable(league, season, matchday, None)\
            .calculate_table()
        league_table = []
        for entry in table_data:
            league_table.append(
                (entry[0], entry[1].__json__(), entry[2], entry[3], entry[4],
                 entry[5], entry[6], entry[7], entry[8], entry[9]))

        return {"league_table": league_table}