示例#1
0
def get_update_date():
    """Get the date of the update to insert into the database"""

    today = TimeService.get_time()
    stats_date = today.subtract(days=1)

    return stats_date
示例#2
0
    def unique_player_picks(cls, pick_type, league):
        session = DbSessionFactory.create_session()

        season_row = session.query(SeasonInfo).filter(
            SeasonInfo.id == "1").first()
        current_season = season_row.current_season

        all_star_game_query = session.query(
            SeasonInfo.all_star_game_date).first()
        all_star_game_date_string = str(all_star_game_query[0])
        all_star_game_datetime = pendulum.parse(all_star_game_date_string,
                                                tz="America/New_York")

        converted_date = pendulum.instance(all_star_game_datetime)
        all_star_game = converted_date.at(19)

        now_time = TimeService.get_time()

        # Calculate Unique Picks at season start
        if now_time < all_star_game:

            # Unique picks are 2 players - trying ct = 2 (instead of ct=1

            txtstr = "UPDATE PlayerPicks SET multiplier=2 WHERE player_id IN "
            txtstr += "(SELECT player_id FROM (select DISTINCT(player_id), COUNT(player_id) AS ct FROM PlayerPicks WHERE "
            midstr = " GROUP BY player_id)PlayerPicks WHERE ct<3) "

            condstr = (" pick_type=" + str(pick_type) + " AND season=" +
                       str(current_season))

            condstr += " AND league_id=" + str(league)

            txtstr += condstr + midstr + "AND " + condstr

            # print(txtstr)

            session.execute(txtstr)
            session.commit()

        else:
            txtstr = (
                "UPDATE PlayerPicks SET multiplier=2 WHERE changed=1 and player_id IN "
            )
            txtstr += "(SELECT player_id FROM (select DISTINCT(player_id), COUNT(player_id) AS ct FROM PlayerPicks WHERE "
            midstr = " GROUP BY player_id)PlayerPicks WHERE ct<3) "

            condstr = (" pick_type=" + str(pick_type) + " AND season=" +
                       str(current_season))

            condstr += " AND league_id=" + str(league)

            txtstr += condstr + midstr + "AND " + condstr

            # print(txtstr)

            session.execute(txtstr)
            session.commit()

        session.close()
示例#3
0
    def season(self):
        """View the standings for a given season from the user choosing in the bullet list in index"""
        vm = StandingsPointsViewModel()
        vm.from_dict(self.data_dict)

        season = self.request.matchdict["id"]
        season_year = pendulum.parse(season)

        current_standings = StandingsService.display_weekly_standings(season)

        session = DbSessionFactory.create_session()

        date_query = (session.query(WeeklyPlayerResults.update_date).order_by(
            WeeklyPlayerResults.update_date.desc()).first())

        if date_query is None:
            self.redirect("/home")

        else:

            if TimeService.get_time() > GameDayService.last_game_date():
                date_updated = "Final Standings"

            elif TimeService.get_time() < GameDayService.season_opener_date():
                date_updated = "Season Has Not Started"

            else:
                date = str(date_query[0])
                date_convert = pendulum.from_format(date, "YYYY-MM-DD")
                string_date = date_convert.format("MMMM Do YYYY")

                date_updated = "Standings through " + string_date

            return {
                "current_standings": current_standings,
                "season": season,
                "date_updated": date_updated,
            }

        session.close()
示例#4
0
    def index(self):
        """GET request for the admin homepage.  If the database is empty, redirect to new_install"""
        session = DbSessionFactory.create_session()
        su__query = (
            session.query(Account.id)
            .filter(Account.is_super_user == 1)
            .filter(Account.id == self.logged_in_user_id)
            .first()
        )

        if su__query is None:
            print("You must be an administrator to view this page")
            self.redirect("/home")

        get_first_name = (
            session.query(Account.first_name)
            .filter(Account.id == self.logged_in_user_id)
            .first()
        )
        first_name = get_first_name[0]

        season_info = session.query(SeasonInfo).all()

        if GameDayService.admin_check() is None:
            self.redirect("/admin/new_season")

        else:

            season_start_date = GameDayService.season_opener_date()
            picks_due = GameDayService.picks_due()
            time_due = GameDayService.time_due()

            # Use the string above in a Pendulum instance and get the time deltas needed
            now_time = TimeService.get_time()

            days = GameDayService.delta_days()
            hours = GameDayService.delta_hours()
            minutes = GameDayService.delta_minutes()

            return {
                "picks_due": picks_due,
                "time_due": time_due,
                "days": days,
                "hours": hours,
                "minutes": minutes,
                "first_name": first_name,
                "season_info": season_info,
            }

        session.close()
示例#5
0
    def get_hitter_trade(
        cls,
        player_id: int,
        team_id: int,
        hr: int,
        ba: float,
        ab: int,
        hits: int,
        pa: int,
        games: int,
        rbi: int,
    ):

        session = DbSessionFactory.create_session()

        season_row = (session.query(
            SeasonInfo.current_season).filter(SeasonInfo.id == 1).first())
        season = season_row.current_season

        dt = TimeService.get_time()

        # Update the player's team to the new team
        for player in (session.query(ActiveMLBPlayers.player_id).filter(
                ActiveMLBPlayers.player_id == player_id).filter(
                    season == season)):
            session.query(ActiveMLBPlayers.player_id).filter(
                ActiveMLBPlayers.team_id).update({"team_id": team_id})

        # Update the InterLeague Trade Table
        hitter_trade = InterleagueTrades(
            player_id=player_id,
            season=season,
            home_runs=hr,
            batting_average=ba,
            at_bats=ab,
            hits=hits,
            plate_appearances=pa,
            player_games_played=games,
            RBI=rbi,
            update_date=dt,
        )

        session.add(hitter_trade)

        session.commit()
        session.close()
示例#6
0
    def get_pitcher_trade(
        cls,
        player_id: int,
        team_id: int,
        games: int,
        saves: int,
        era: float,
        er: int,
        ip: float,
    ):
        # Update the database with the new information

        session = DbSessionFactory.create_session()

        season_row = (session.query(
            SeasonInfo.current_season).filter(SeasonInfo.id == 1).first())
        season = season_row.current_season

        dt = TimeService.get_time()

        for player in (session.query(ActiveMLBPlayers.player_id).filter(
                ActiveMLBPlayers.player_id == player_id).filter(
                    season == season)):
            session.query(ActiveMLBPlayers.player_id).filter(
                ActiveMLBPlayers.team_id).update({"team_id": team_id})

        # Update the InterLeague Trade Table
        pitcher_trade = InterleagueTrades(
            season=season,
            player_id=player_id,
            player_games_played=games,
            saves=saves,
            ERA=era,
            earned_runs=er,
            innings_pitched=ip,
            update_date=dt,
        )
        session.add(pitcher_trade)

        session.commit()
        session.close()
示例#7
0
    def submit_player_picks(self):

        if not self.logged_in_user_id:
            print("Cannot view picks page, you must be logged in")
            self.redirect("/account/signin")

        session = DbSessionFactory.create_session()
        season_row = (session.query(
            SeasonInfo.current_season).filter(SeasonInfo.id == "1").first())
        season = season_row.current_season

        season_info = session.query(SeasonInfo).all()

        season_start_date = GameDayService.season_opener_date()
        picks_due = GameDayService.picks_due()
        time_due = GameDayService.time_due()

        # Do not change this for testing - change in TimeService
        now_time = TimeService.get_time()

        # Check if the season has already started
        if now_time > season_start_date:
            print("Too late!  The season has already started.")
            self.redirect("/picks/too-late")

        else:

            days = GameDayService.delta_days()
            hours = GameDayService.delta_hours()
            minutes = GameDayService.delta_minutes()
            current_datetime = now_time.to_day_datetime_string()

            # Check if user has already submitted picks

            user_query = (session.query(PlayerPicks.user_id).filter(
                PlayerPicks.user_id == self.logged_in_user_id).filter(
                    PlayerPicks.season == season).first())

            if user_query is None:

                # Data / Service access
                al_east_list = PlayerPicksService.get_division_team_list(0, 1)
                al_central_list = PlayerPicksService.get_division_team_list(
                    0, 2)
                al_west_list = PlayerPicksService.get_division_team_list(0, 3)

                nl_east_list = PlayerPicksService.get_division_team_list(1, 1)
                nl_central_list = PlayerPicksService.get_division_team_list(
                    1, 2)
                nl_west_list = PlayerPicksService.get_division_team_list(1, 3)

                # Pass the P as the pitcher position and the query to get the list != P
                al_batter_list = PlayerPicksService.get_hitter_list(0, "P")
                nl_batter_list = PlayerPicksService.get_hitter_list(1, "P")

                # List of all Pitchers
                al_pitcher_list = PlayerPicksService.get_pitcher_list(0, "P")
                nl_pitcher_list = PlayerPicksService.get_pitcher_list(1, "P")

                # List of all teams to pick the Wild Card from each league
                al_wildcard_list = PlayerPicksService.get_al_wildcard()
                nl_wildcard_list = PlayerPicksService.get_nl_wildcard()

                # Create a range of 0-162 for players to pick how many wins the Twins will finish with
                twins_wins_pick_list = list(range(0, 163))

                # Get the user ID
                user_id = self.logged_in_user_id
                get_first_name = (session.query(Account.first_name).filter(
                    Account.id == self.logged_in_user_id).first())
                first_name = get_first_name[0]

                # Return the models
                return {
                    "season": season,
                    "user_id": user_id,
                    "first_name": first_name,
                    "al_east": al_east_list,
                    "al_central": al_central_list,
                    "al_west": al_west_list,
                    "nl_east": nl_east_list,
                    "nl_central": nl_central_list,
                    "nl_west": nl_west_list,
                    "al_hitter_list": al_batter_list,
                    "nl_hitter_list": nl_batter_list,
                    "al_pitcher_list": al_pitcher_list,
                    "nl_pitcher_list": nl_pitcher_list,
                    "al_wildcard_list": al_wildcard_list,
                    "nl_wildcard_list": nl_wildcard_list,
                    "twins_wins_pick_list": twins_wins_pick_list,
                    "picks_due": picks_due,
                    "time_due": time_due,
                    "days": days,
                    "hours": hours,
                    "minutes": minutes,
                    "current_datetime": current_datetime,
                    "season_info": season_info,
                }

            else:
                print("You have already submitted picks for this season")
                self.redirect("/picks/change-picks")

            session.close()
示例#8
0
    def change_player_picks_post(self):
        vm = PlayerPicksViewModel()
        vm.from_dict(self.request.POST)

        # Pass a player's picks to the service to be inserted in the db

        session = DbSessionFactory.create_session()
        season_row = (session.query(
            SeasonInfo.current_season).filter(SeasonInfo.id == "1").first())
        season = season_row.current_season
        vm.user_id = self.logged_in_user_id
        vm.season = season

        now_time = TimeService.get_time()

        if GameDayService.season_opener_date() < now_time:
            total_changes = CountService.change_picks_count(
                vm.user_id,
                vm.season,
                vm.al_east_winner_pick,
                vm.al_east_second_pick,
                vm.al_east_last_pick,
                vm.al_central_winner_pick,
                vm.al_central_second_pick,
                vm.al_central_last_pick,
                vm.al_west_winner_pick,
                vm.al_west_second_pick,
                vm.al_west_last_pick,
                vm.nl_east_winner_pick,
                vm.nl_east_second_pick,
                vm.nl_east_last_pick,
                vm.nl_central_winner_pick,
                vm.nl_central_second_pick,
                vm.nl_central_last_pick,
                vm.nl_west_winner_pick,
                vm.nl_west_second_pick,
                vm.nl_west_last_pick,
                vm.al_losses_pick,
                vm.nl_losses_pick,
                vm.al_wins_pick,
                vm.nl_wins_pick,
                vm.al_hr_pick,
                vm.nl_hr_pick,
                vm.al_ba_pick,
                vm.nl_ba_pick,
                vm.al_rbi_pick,
                vm.nl_rbi_pick,
                vm.al_saves_pick,
                vm.nl_saves_pick,
                vm.al_era_pick,
                vm.nl_era_pick,
                vm.al_wildcard1_pick,
                vm.nl_wildcard1_pick,
                vm.al_wildcard2_pick,
                vm.nl_wildcard2_pick,
            )

            print(now_time, "Total number of changes is", total_changes)

            if total_changes >= 15:
                self.redirect("/picks/too-many")

            else:

                PlayerPicksService.change_player_picks(
                    vm.al_east_winner_pick,
                    vm.al_east_second_pick,
                    vm.al_east_last_pick,
                    vm.al_central_winner_pick,
                    vm.al_central_second_pick,
                    vm.al_central_last_pick,
                    vm.al_west_winner_pick,
                    vm.al_west_second_pick,
                    vm.al_west_last_pick,
                    vm.nl_east_winner_pick,
                    vm.nl_east_second_pick,
                    vm.nl_east_last_pick,
                    vm.nl_central_winner_pick,
                    vm.nl_central_second_pick,
                    vm.nl_central_last_pick,
                    vm.nl_west_winner_pick,
                    vm.nl_west_second_pick,
                    vm.nl_west_last_pick,
                    vm.al_hr_pick,
                    vm.nl_hr_pick,
                    vm.al_rbi_pick,
                    vm.nl_rbi_pick,
                    vm.al_ba_pick,
                    vm.nl_ba_pick,
                    vm.al_saves_pick,
                    vm.nl_saves_pick,
                    vm.al_era_pick,
                    vm.nl_era_pick,
                    vm.al_wildcard1_pick,
                    vm.al_wildcard2_pick,
                    vm.nl_wildcard1_pick,
                    vm.nl_wildcard2_pick,
                    vm.al_wins_pick,
                    vm.nl_wins_pick,
                    vm.al_losses_pick,
                    vm.nl_losses_pick,
                    vm.user_id,
                )
        else:

            PlayerPicksService.change_player_picks(
                vm.al_east_winner_pick,
                vm.al_east_second_pick,
                vm.al_east_last_pick,
                vm.al_central_winner_pick,
                vm.al_central_second_pick,
                vm.al_central_last_pick,
                vm.al_west_winner_pick,
                vm.al_west_second_pick,
                vm.al_west_last_pick,
                vm.nl_east_winner_pick,
                vm.nl_east_second_pick,
                vm.nl_east_last_pick,
                vm.nl_central_winner_pick,
                vm.nl_central_second_pick,
                vm.nl_central_last_pick,
                vm.nl_west_winner_pick,
                vm.nl_west_second_pick,
                vm.nl_west_last_pick,
                vm.al_hr_pick,
                vm.nl_hr_pick,
                vm.al_rbi_pick,
                vm.nl_rbi_pick,
                vm.al_ba_pick,
                vm.nl_ba_pick,
                vm.al_saves_pick,
                vm.nl_saves_pick,
                vm.al_era_pick,
                vm.nl_era_pick,
                vm.al_wildcard1_pick,
                vm.al_wildcard2_pick,
                vm.nl_wildcard1_pick,
                vm.nl_wildcard2_pick,
                vm.al_wins_pick,
                vm.nl_wins_pick,
                vm.al_losses_pick,
                vm.nl_losses_pick,
                vm.user_id,
            )

        # Log that a user changed picks

        self.log.notice("Picks changed by {}.".format(
            self.logged_in_user.email))

        get_first_name = (session.query(Account.first_name).filter(
            Account.id == self.logged_in_user_id).first())
        first_name = get_first_name[0]

        get_last_name = (session.query(Account.last_name).filter(
            Account.id == self.logged_in_user_id).first())
        last_name = get_last_name[0]

        message = f"Picks updated by MLBPool2 user:  {first_name} {last_name}"
        print(message)

        SlackService.send_message(message)

        session.close()

        # redirect
        self.redirect("/account")
示例#9
0
    def change_player_picks(self):

        if not self.logged_in_user_id:
            print("Cannot view picks page, you must be logged in")
            self.redirect("/account/signin")

        # Check if user has already submitted picks

        session = DbSessionFactory.create_session()
        season_row = (session.query(
            SeasonInfo.current_season).filter(SeasonInfo.id == "1").first())
        season = season_row.current_season

        user_query = (session.query(PlayerPicks.user_id).filter(
            PlayerPicks.user_id == self.logged_in_user_id).filter(
                PlayerPicks.season == season).first())

        find_changes = CountService.find_changes(self.logged_in_user_id)

        if user_query is None:

            print("You have not submitted picks for this season")
            self.redirect("/picks/submit-picks")

        elif find_changes is True:
            print(find_changes)
            self.redirect("/picks/too-late-break")

        else:

            now_time = TimeService.get_time()

            if (now_time < GameDayService.season_opener_date()
                    and GameDayService.all_star_break(now_time) is False):

                self.redirect("/picks/too-late")

            elif (now_time > GameDayService.season_opener_date()
                  and GameDayService.all_star_break(now_time) is False):

                self.redirect("/picks/too-late")

            else:

                session = DbSessionFactory.create_session()
                season_row = (session.query(SeasonInfo.current_season).filter(
                    SeasonInfo.id == "1").first())
                season = season_row.current_season

                # Data / Service access
                al_east_list = PlayerPicksService.get_division_team_list(0, 1)
                al_central_list = PlayerPicksService.get_division_team_list(
                    0, 2)
                al_west_list = PlayerPicksService.get_division_team_list(0, 3)

                nl_east_list = PlayerPicksService.get_division_team_list(1, 1)
                nl_central_list = PlayerPicksService.get_division_team_list(
                    1, 2)
                nl_west_list = PlayerPicksService.get_division_team_list(1, 3)

                # Pass the P as the pitcher position and the query to get the list != P
                al_batter_list = PlayerPicksService.get_hitter_list(0, "P")
                nl_batter_list = PlayerPicksService.get_hitter_list(1, "P")

                # List of all Pitchers
                al_pitcher_list = PlayerPicksService.get_pitcher_list(0, "P")
                nl_pitcher_list = PlayerPicksService.get_pitcher_list(1, "P")

                # List of all teams to pick the Wild Card from each league
                al_wildcard_list = PlayerPicksService.get_al_wildcard()
                nl_wildcard_list = PlayerPicksService.get_nl_wildcard()

                # Create a range of 0-162 for players to pick how many wins the Twins will finish with
                twins_wins_pick_list = list(range(0, 163))

                # Get the user ID
                user_id = self.logged_in_user_id
                get_first_name = (session.query(Account.first_name).filter(
                    Account.id == self.logged_in_user_id).first())
                first_name = get_first_name[0]

                # Get the original picks the player made
                all_picks = ViewPicksService.display_picks(
                    self.logged_in_user_id, season)

                # Return the models
                return {
                    "season": season,
                    "user_id": user_id,
                    "first_name": first_name,
                    "al_east": al_east_list,
                    "al_central": al_central_list,
                    "al_west": al_west_list,
                    "nl_east": nl_east_list,
                    "nl_central": nl_central_list,
                    "nl_west": nl_west_list,
                    "al_hitter_list": al_batter_list,
                    "nl_hitter_list": nl_batter_list,
                    "al_pitcher_list": al_pitcher_list,
                    "nl_pitcher_list": nl_pitcher_list,
                    "al_wildcard_list": al_wildcard_list,
                    "nl_wildcard_list": nl_wildcard_list,
                    "twins_wins_pick_list": twins_wins_pick_list,
                    "all_picks": all_picks,
                }

            session.close()
示例#10
0
    def unique_team_picks(cls, pick_type, league=None, div=None, rank=None):
        session = DbSessionFactory.create_session()

        season_row = session.query(SeasonInfo).filter(
            SeasonInfo.id == "1").first()
        current_season = season_row.current_season

        all_star_game_query = session.query(
            SeasonInfo.all_star_game_date).first()
        all_star_game_date = str(all_star_game_query[0])
        start_time = all_star_game_date + " 19:00"
        all_star_game = pendulum.from_format(start_time, "%Y-%m-%d %H:%M")

        now_time = TimeService.get_time()

        # Calculate Unique Picks at season start
        if now_time < all_star_game:

            txtstr = "UPDATE PlayerPicks SET multiplier=2 WHERE team_id IN "
            txtstr += "(SELECT team_id FROM (select DISTINCT(team_id), COUNT(team_id) AS ct FROM PlayerPicks WHERE "
            midstr = " GROUP BY team_id)PlayerPicks WHERE ct<3) "

            condstr = ("pick_type=" + str(pick_type) + " AND season=" +
                       str(current_season))

            if league is not None:
                condstr += " AND league_id=" + str(league)
                if div is not None:
                    condstr += " AND division_id=" + str(div)
                    if rank is not None:
                        condstr += " AND rank=" + str(rank)

            txtstr += condstr + midstr + "AND " + condstr

            # print(txtstr)

            session.execute(txtstr)
            session.commit()

        else:

            # TODO Add the changed=1 column to the query below AND give players half the point value for changed picks

            txtstr = (
                "UPDATE PlayerPicks SET multiplier=2 WHERE changed=1 and team_id IN "
            )
            txtstr += "(SELECT team_id FROM (select DISTINCT(team_id), COUNT(team_id) AS ct FROM PlayerPicks WHERE "
            midstr = " GROUP BY team_id)PlayerPicks WHERE ct<3) "

            condstr = ("pick_type=" + str(pick_type) + " AND season=" +
                       str(current_season))

            if league is not None:
                condstr += " AND league_id=" + str(league)
                if div is not None:
                    condstr += " AND division_id=" + str(div)
                    if rank is not None:
                        condstr += " AND rank=" + str(rank)

            txtstr += condstr + midstr + "AND " + condstr

            # print(txtstr)

            session.execute(txtstr)
            session.commit()

        session.close()
示例#11
0
import pendulum
from mlbpool.data.seasoninfo import SeasonInfo
from mlbpool.data.dbsession import DbSessionFactory
from mlbpool.services.time_service import TimeService


# Set the timezone we will be working with
timezone = pendulum.timezone("America/New_York")

# Change now_time for testing
# Use this one for production:
# now_time = pendulum.now(tz=pendulum.timezone('America/New_York'))
# Use this one for testing:
now_time = TimeService.get_time()


def season_opener():

    session = DbSessionFactory.create_session()

    season_start_query = session.query(SeasonInfo.season_start_date).first()
    # print("Season Start Query:", season_start_query)

    # season_start_query is returned as a tuple and need to get the first part of the tuple:
    season_opener_date = str(season_start_query[0])

    # Convert the start date to a string that Pendulum can work with
    # season_start_date_convert = \
    #    pendulum.from_format(season_opener_date, '%Y-%m-%d %H:%M:%S', timezone).to_datetime_string()

    # Use the string above in a Pendulum instance and get the time deltas needed