From 4f5ec43ceccc0dd06875d7d6c61c4a9e3c271e9f Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 5 Mar 2024 18:54:56 -0500 Subject: [PATCH] Added support for a current team. --- README.md | 5 +++ pyproject.toml | 2 +- src/f1_fantasy/data/input/current_team.csv | 1 + src/f1_fantasy/game_objects.py | 4 ++ src/f1_fantasy/main.py | 47 +++++++++++++++++++++- 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/f1_fantasy/data/input/current_team.csv diff --git a/README.md b/README.md index 72549fc..a6bd934 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ Chips available are: extra_drs ``` +## Current Team +`/data/current_team.csv` +Your current team. If put all 5 drivers and 2 constructors in the file the transfer costs will be taken into account. + + ## Finishing Positions qualifying/race `/data/finishing_positions_qualifying.csv` and `/data/finishing_positions_race.csv` diff --git a/pyproject.toml b/pyproject.toml index 809e9c6..1344e5c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "f1-fantasy" -version = "0.5.0" +version = "0.6.0" description = "" authors = ["Peter "] readme = "README.md" diff --git a/src/f1_fantasy/data/input/current_team.csv b/src/f1_fantasy/data/input/current_team.csv new file mode 100644 index 0000000..f121bdb --- /dev/null +++ b/src/f1_fantasy/data/input/current_team.csv @@ -0,0 +1 @@ +name diff --git a/src/f1_fantasy/game_objects.py b/src/f1_fantasy/game_objects.py index 60413ab..edc3ef4 100644 --- a/src/f1_fantasy/game_objects.py +++ b/src/f1_fantasy/game_objects.py @@ -267,10 +267,12 @@ def __init__(self, drivers, constructors): self.price = sum(driver.price for driver in self.drivers) + sum( constructor.price for constructor in self.constructors ) + self.transfer_cost = 0 def compute_points(self): self.points = 0 # get driver with most points and second most points + self.points += self.transfer_cost for driver in self.drivers: driver.extra_drs = False driver.drs = False @@ -430,3 +432,5 @@ def load_prices(cls, constructor_prices: list[ConstructorPriceModel]): DRIVERS_IGNORE_LIST = [] CONSTRUCTORS_IGNORE_LIST = [] + +CURRENT_TEAM: Team | None = None diff --git a/src/f1_fantasy/main.py b/src/f1_fantasy/main.py index 19c9f8e..4ae0e41 100644 --- a/src/f1_fantasy/main.py +++ b/src/f1_fantasy/main.py @@ -14,6 +14,7 @@ Driver, Drivers, Team, + CURRENT_TEAM ) from f1_fantasy.models import ( ConstructorPriceModel, @@ -89,6 +90,22 @@ def compute_constructor_combinations(): return all_constructor_set +def compute_transfer_cost(new_team: Team, old_team: Team): + point_cost = 20 + for driver in new_team.drivers: + if driver not in old_team.drivers: + point_cost -= 10 + + for constructor in new_team.constructors: + if constructor not in old_team.constructors: + point_cost -= 10 + + if point_cost > 0: + return 0 + return point_cost + + + def compute_driver_constructor_combinations(drivers_set: set[Driver], constructors_set: set[Constructor]): all_combinations = itertools.product(drivers_set, constructors_set) all_teams_set = set() @@ -100,6 +117,8 @@ def compute_driver_constructor_combinations(drivers_set: set[Driver], constructo if cost > MAX_TOTAL_COST: continue team = Team(drivers=list(combo[0]), constructors=list(combo[1])) + if CURRENT_TEAM is not None: + team.transfer_cost = compute_transfer_cost(team, CURRENT_TEAM) team.compute_points() if team.points >= highest_score: highest_score = team.points @@ -108,7 +127,7 @@ def compute_driver_constructor_combinations(drivers_set: set[Driver], constructo highest_score_set = set() for team in all_teams_set: if team.points >= highest_score: - team_copy = Team(drivers=deepcopy(team.drivers), constructors=deepcopy(team.constructors)) + team_copy = deepcopy(team) team_copy.compute_points() highest_score_set.add(team_copy) return highest_score_set @@ -208,6 +227,27 @@ def set_ignores_from_csvs(ignore_constructors: Path, ignore_drivers: Path): DRIVERS_IGNORE_LIST.append(driver) +def set_current_team_from_csv(current_team_csv: Path): + global CURRENT_TEAM + + with current_team_csv.open() as f: + reader = csv.DictReader(f) + drivers = [] + constructors = [] + for row in reader: + name = row["name"].strip().upper() + if name in DriversEnum.__members__: + drivers.append(Drivers.get(DriversEnum[name])) + else: + constructors.append(Constructors.get(ConstructorsEnum[name])) + + if len(drivers) != 5 or len(constructors) != 2: + print("Not setting current team. Must have 5 drivers and 2 constructors") + CURRENT_TEAM = None + return + team = Team(drivers=drivers, constructors=constructors) + CURRENT_TEAM = team + def write_csv_to_output(f: TextIO, csv_path: Path): with csv_path.open() as read_csv: reader = csv.DictReader(read_csv) @@ -230,6 +270,7 @@ def main( price_drivers_path: Path, special_points_path: Path, output_file_path: Path, + current_team_csv: Path, ): driver_prices = drivers_prices_from_csv(price_drivers_path) constructor_prices = constructors_prices_from_csv(price_constructors_path) @@ -238,6 +279,7 @@ def main( special_points = special_points_from_csv(special_points_path) set_chips_from_csv(chips_path) set_ignores_from_csvs(ignore_constructors=ignore_constructors_path, ignore_drivers=ignore_drivers_path) + set_current_team_from_csv(current_team_csv) _max_score_teams = calculations( driver_prices=driver_prices, @@ -266,6 +308,8 @@ def main( write_csv_to_output(f, price_constructors_path) write_csv_to_output(f, price_drivers_path) write_csv_to_output(f, special_points_path) + write_csv_to_output(f, current_team_csv) + print(f"Output written to {output_file_path}") @@ -281,4 +325,5 @@ def main( price_drivers_path=Path(ROOT_DIR / "data" / "input" / "price_drivers.csv"), special_points_path=Path(ROOT_DIR / "data" / "input" / "special_points.csv"), output_file_path=Path(ROOT_DIR / "data" / "output" / f"{time.time()}"), + current_team_csv=Path(ROOT_DIR / "data" / "input" / "current_team.csv"), )