def get_previous_points_for_same_fixture(player, fixture_id): """ Search the past matches for same fixture in past seasons, and how many points the player got. """ if isinstance(player, str): player_record = session.query(Player).filter_by(name=player).first() if not player_record: print("Can't find player {}".format(player)) return {} player_id = player_record.player_id else: player_id = player fixture = session.query(Fixture).filter_by(fixture_id=fixture_id).first() if not fixture: print("Couldn't find fixture_id {}".format(fixture_id)) return {} home_team = fixture.home_team away_team = fixture.away_team previous_matches = (session.query(Fixture).filter_by( home_team=home_team).filter_by(away_team=away_team).order_by( Fixture.season).all()) fixture_ids = [(f.fixture_id, f.season) for f in previous_matches] previous_points = {} for fid in fixture_ids: scores = (session.query(PlayerScore).filter_by( player_id=player_id, fixture_id=fid[0]).all()) for s in scores: previous_points[fid[1]] = s.points return previous_points
def get_player_id(player_name, dbsession=None): """ lookup player id, for machine readability """ if not dbsession: dbsession = session p = dbsession.query(Player).filter_by(name=player_name).first() if p: return p.player_id ## not found by name in DB - try alternative names for k, v in alternative_player_names.items(): if player_name in v: p = session.query(Player).filter_by(name=k).first() if p: return p.player_id break ## still not found print("Unknown player_name {}".format(player_name)) return None
def get_sell_price_for_player(player_id, gameweek=None): """ find the price we bought the player for, and the price at the specified gameweek, if the price increased in that time, we only get half the profit. if gameweek is None, get price we could sell the player for now. """ buy_price = 0 transactions = session.query(Transaction) transactions = transactions.filter_by(player_id=player_id) transactions = transactions.order_by(Transaction.gameweek).all() gw_bought = None for t in transactions: if gameweek and t.gameweek > gameweek: break if t.bought_or_sold == 1: gw_bought = t.gameweek if not gw_bought: print("Player {} is was not in the team at gameweek {}".format( player_id, gameweek)) # to query the API we need to use the fpl_api_id for the player rather than player_id player_api_id = get_player(player_id).fpl_api_id pdata_bought = fetcher.get_gameweek_data_for_player( player_api_id, gw_bought) ## will be a list - can be more than one match in a gw - just use the 1st. price_bought = pdata_bought[0]["value"] if not gameweek: # assume we want the current (i.e. next) gameweek price_now = fetcher.get_player_summary_data( )[player_api_id]["now_cost"] else: pdata_now = fetcher.get_gameweek_data_for_player( player_api_id, gw_bought) price_now = pdata_now[0]["value"] ## take off our half of the profit - boo! if price_now > price_bought: value = (price_now + price_bought) // 2 # round down else: value = price_now return value
def database_is_empty(): """ Basic check to determine whether the database is empty """ return session.query(Team).first() is None
def num_players_in_table(session): """ how many players already in player table """ players = session.query(Player).all() return len(players)
def find_player_in_table(name, session): """ see if we already have the player """ player = session.query(Player).filter_by(name=name).first() return player if player else None
def get_player_scores_for_fixture(fixture, dbsession=session): """Get player scores for a fixture.""" player_scores = session.query(PlayerScore).filter_by(fixture=fixture).all() return player_scores
def get_result_for_fixture(fixture, dbsession=session): """Get result for a fixture.""" result = session.query(Result).filter_by(fixture=fixture).all() return result