示例#1
0
def best_transfer_suggestions(n_transfer, session_id, dbsession=DBSESSION):
    """
    Use our predicted playerscores to suggest the best transfers.
    """
    n_transfer = int(n_transfer)
    if not n_transfer in range(1,3):
        raise RuntimeError("Need to choose 1 or 2 transfers")
    if not validate_session_squad(session_id, dbsession):
        raise RuntimeError("Cannot suggest transfer without complete squad")

    budget = get_session_budget(session_id, dbsession)
    players = [p["id"] for p in get_session_players(session_id, dbsession)]
    t = Team(budget)
    for p in players:
        added_ok = t.add_player(p)
        if not added_ok:
            raise RuntimeError("Cannot add player {}".format(p))
    pred_tag = get_latest_prediction_tag()
    gw=get_next_gameweek(CURRENT_SEASON, dbsession)
    if n_transfer == 1:
        new_team, pid_out, pid_in = make_optimum_transfer(t, pred_tag)
    elif n_transfer == 2:
        new_team, pid_out, pid_in = make_optimum_double_transfer(t, pred_tag)
    return {
        "transfers_out": pid_out,
        "transfers_in": pid_in
    }
示例#2
0
def test_double_transfer():
    """
    mock squad with two players predicted low score, see if we get better players
    transferred in.
    """
    t = generate_dummy_squad()
    position_points_dict = {
        "GK": {0: 2, 1: 2, 100: 0, 101: 0, 200: 3, 201: 7},  # in the orig squad
        "DEF": {
            2: 2,
            3: 2,
            2: 2,
            5: 2,
            6: 2,  # in the orig squad
            103: 0,
            104: 0,
            105: 5,
            106: 2,
            107: 2,
            203: 0,
            204: 0,
            205: 1,
            206: 2,
            207: 2,
        },
        "MID": {
            7: 2,
            8: 2,
            9: 2,
            10: 2,
            11: 2,  # in the orig squad
            108: 2,
            109: 2,
            110: 3,
            111: 3,
            112: 0,
            208: 2,
            209: 2,
            210: 3,
            211: 3,
            212: 0,
        },
        "FWD": {12: 2, 13: 2, 14: 2, 113: 6, 114: 3, 115: 8},  # in the orig squad
    }
    mock_pred_points = predicted_point_mock_generator(position_points_dict)

    with mock.patch(
        "airsenal.framework.optimization_utils.get_predicted_points",
        side_effect=mock_pred_points,
    ):
        new_squad, pid_out, pid_in = make_optimum_double_transfer(t, "DUMMY", [1])
        ## we should expect 201 and 115 to be transferred in, and 1,15 to
        ## be transferred out.   115 should be captain
        assert 201 in pid_in
        assert 115 in pid_in
        print(new_squad)
        for p in new_squad.players:
            if p.player_id == 115:
                assert p.is_captain == True
            else:
                assert p.is_captain == False