示例#1
0
def add_players_to_db(players_from_db, players_from_api, player_data_from_api,
                      dbsession):
    print("Updating player table...")
    # find the new player(s) from the API
    api_ids_from_db = [p.fpl_api_id for p in players_from_db]
    new_players = [p for p in players_from_api if p not in api_ids_from_db]
    for player_api_id in new_players:
        first_name = player_data_from_api[player_api_id]["first_name"]
        second_name = player_data_from_api[player_api_id]["second_name"]
        name = "{} {}".format(first_name, second_name)
        # check whether we alreeady have this player in the database -
        # if yes update that player's data, if no create a new player
        p = get_player(name, dbsession=dbsession)
        if p is None:
            print("Adding player {}".format(name))
            p = Player()
            update = False
        elif p.fpl_api_id is None:
            print("Updating player {}".format(name))
            update = True
        else:
            update = True
        p.fpl_api_id = player_api_id
        p.name = name
        if not update:
            dbsession.add(p)
    dbsession.commit()
    return len(new_players)
def fill_players():
    """
    fill a bunch of dummy players
    """
    team_list = list(alternative_team_names.keys())
    season = CURRENT_SEASON
    gameweek = 1
    with test_session_scope() as ts:
        if len(ts.query(Player).all()) > 0:
            return
        for i, n in enumerate(dummy_players):
            p = Player()
            p.player_id = i
            p.fpl_api_id = i
            p.name = n
            print("Filling {} {}".format(i, n))
            try:
                ts.add(p)
            except Exception:
                print("Error adding {} {}".format(i, n))
            # now fill player_attributes
            if i % 15 < 2:
                pos = "GK"
            elif i % 15 < 7:
                pos = "DEF"
            elif i % 15 < 12:
                pos = "MID"
            else:
                pos = "FWD"
            team = team_list[i % 20]
            # make the first 15 players affordable,
            # the next 15 almost affordable,
            # the next 15 mostly unaffordable,
            # and rest very expensive
            price = value_generator(i // 15, pos)
            pa = PlayerAttributes()
            pa.season = season
            pa.team = team
            pa.gameweek = gameweek
            pa.price = price
            pa.position = pos
            player = ts.query(Player).filter_by(player_id=i).first()
            pa.player = player
            ts.add(pa)
        ts.commit()
示例#3
0
def test_get_position():
    """
    Check Player.position() returns appropriate value both if details are available
    in attributes table for requested gameweek, and if they're not available.
    """
    player_id = 1
    gameweek = 1
    price = 50
    pos_dict = {"1819": "MID", "1920": "FWD"}  # season: position
    team = "TST"

    player = Player()
    player.player_id = player_id
    player.name = "Test Player"
    player.attributes = []

    for season, position in pos_dict.items():
        pa = PlayerAttributes()
        pa.season = season
        pa.team = team
        pa.gameweek = gameweek
        pa.price = price
        pa.position = position
        pa.player_id = player_id
        player.attributes.append(pa)

    # season available in attributes table
    assert player.position("1819") == pos_dict["1819"]
    assert player.position("1920") == pos_dict["1920"]
    # season not available
    assert player.position("1011") is None
示例#4
0
def update_players(season, dbsession):
    """
    See if any new players have been added to FPL since we last filled the 'player'
    table in the db.  If so, add them.
    """
    players_from_db = list_players(
        position="all", team="all", season=season, dbsession=dbsession
    )
    player_data_from_api = fetcher.get_player_summary_data()
    players_from_api = list(player_data_from_api.keys())

    if len(players_from_db) == len(players_from_api):
        print("Player table already up-to-date.")
        return 0
    elif len(players_from_db) > len(players_from_api):
        raise RuntimeError(
            "Something strange has happened - more players in DB than API"
        )
    else:
        print("Updating player table...")
        # find the new player(s) from the API
        api_ids_from_db = [p.fpl_api_id for p in players_from_db]
        new_players = [p for p in players_from_api if p not in api_ids_from_db]
        for player_api_id in new_players:
            first_name = player_data_from_api[player_api_id]["first_name"]
            second_name = player_data_from_api[player_api_id]["second_name"]
            name = "{} {}".format(first_name, second_name)
            # check whether we alreeady have this player in the database -
            # if yes update that player's data, if no create a new player
            p = get_player(name, dbsession=dbsession)
            if p is None:
                print("Adding player {}".format(name))
                p = Player()
                update = False
            elif p.fpl_api_id is None:
                print("Updating player {}".format(name))
                update = True
            else:
                update = True
            p.fpl_api_id = player_api_id
            p.name = name
            if not update:
                dbsession.add(p)
        dbsession.commit()
        return len(new_players)
示例#5
0
def fill_player_table_from_api(season, session):
    """
    use the FPL API
    """
    df = FPLDataFetcher()
    pd = df.get_player_summary_data()

    for k, v in pd.items():
        p = Player()
        p.fpl_api_id = k
        first_name = v["first_name"]  # .encode("utf-8")
        second_name = v["second_name"]  # .encode("utf-8")
        name = "{} {}".format(first_name, second_name)

        print("PLAYER {} {}".format(season, name))
        p.name = name
        session.add(p)
    session.commit()
示例#6
0
def fill_player_table_from_file(filename, season, session):
    """
    use json file
    """
    jplayers = json.load(open(filename))
    n_new_players = 0
    for i, jp in enumerate(jplayers):
        new_entry = False
        name = jp["name"]
        print("PLAYER {} {}".format(season, name))
        p = find_player_in_table(name, session)
        if not p:
            n_new_players += 1
            new_entry = True
            p = Player()
            #            p.player_id = (
            #                max_id_in_table(session) + n_new_players
            #            )  # next id sequentially
            p.name = name
        if new_entry:
            session.add(p)
    session.commit()
示例#7
0
def test_get_team():
    """
    Check Player.team() returns appropriate value both if details are available
    in attributes table for requested gameweek, and if they're not available.
    """
    player_id = 1
    season = "1920"
    price = 50
    position = "MID"
    team_dict = {2: "ABC", 5: "XYZ"}  # gw: team

    player = Player()
    player.player_id = player_id
    player.name = "Test Player"
    player.attributes = []

    for gw, team in team_dict.items():
        pa = PlayerAttributes()
        pa.season = season
        pa.team = team
        pa.gameweek = gw
        pa.price = price
        pa.position = position
        pa.player_id = player_id
        player.attributes.append(pa)

    # gameweek available in attributes table
    assert player.team(season, 2) == team_dict[2]
    # gameweek before earliest available: return first available
    assert player.team(season, 1) == team_dict[2]
    # gameweek after last available: return last available
    assert player.team(season, 6) == team_dict[5]
    # gameweek between two available values: return nearest
    assert player.team(season, 3) == team_dict[2]
    assert player.team(season, 4) == team_dict[5]
    # no gameweek available for seaaon: return None
    assert player.team("1011", 1) is None
示例#8
0
def test_get_price():
    """
    Check Player.price() returns appropriate value both if details are available
    in attributes table for requested gameweek, and if they're not available.
    """
    player_id = 1
    season = "1920"
    team = "TST"
    position = "MID"
    price_dict = {2: 50, 4: 150}  # gw: price

    player = Player()
    player.player_id = player_id
    player.name = "Test Player"
    player.attributes = []

    for gw, price in price_dict.items():
        pa = PlayerAttributes()
        pa.season = season
        pa.team = team
        pa.gameweek = gw
        pa.price = price
        pa.position = position
        pa.player_id = player_id
        player.attributes.append(pa)

    # gameweek available in attributes table
    assert player.price(season, 2) == price_dict[2]
    # gameweek before earliest available: return first available
    assert player.price(season, 1) == price_dict[2]
    # gameweek after last available: return last available
    assert player.price(season, 5) == price_dict[4]
    # gameweek between two available values: interpolate
    assert player.price(season, 3) == (price_dict[2] + price_dict[4]) / 2
    # no gameweek available for seaaon: return None
    assert player.price("1011", 1) is None
示例#9
0
def test_is_injured_or_suspended():
    """
    Check Player.is_injured_or_suspended() returns appropriate value both if details are
    available in attributes table for requested gameweek, and if they're not available.
    """
    player_id = 1
    season = "1920"
    price = 50
    position = "MID"
    team = "ABC"
    # gw: (chance_of_playing_next_round, return_gameweek)
    team_dict = {
        2: (100, None),
        3: (75, None),
        4: (50, 5),
        5: (0, None),
    }

    player = Player()
    player.player_id = player_id
    player.name = "Test Player"
    player.attributes = []

    for gw, attr in team_dict.items():
        pa = PlayerAttributes()
        pa.season = season
        pa.team = team
        pa.gameweek = gw
        pa.price = price
        pa.position = position
        pa.player_id = player_id
        pa.chance_of_playing_next_round = attr[0]
        pa.return_gameweek = attr[1]
        player.attributes.append(pa)

    # gameweek available in attributes table
    # not injured, 100% available
    assert player.is_injured_or_suspended(season, 2, 2) is False
    assert player.is_injured_or_suspended(season, 2, 4) is False
    # not injured, 75% available
    assert player.is_injured_or_suspended(season, 3, 3) is False
    assert player.is_injured_or_suspended(season, 3, 5) is False
    # 50% available, expected back gw 5
    assert player.is_injured_or_suspended(season, 4, 4) is True
    assert player.is_injured_or_suspended(season, 4, 5) is False
    # 100% unavailable, mo return gameweek
    assert player.is_injured_or_suspended(season, 5, 6) is True
    assert player.is_injured_or_suspended(season, 5, 7) is True
    # gameweek before earliest available: return status as of first available
    assert player.is_injured_or_suspended(season, 1, 1) is False
    # gameweek after last available: return status as of last available
    assert player.is_injured_or_suspended(season, 6, 1) is True