Skip to content

Commit

Permalink
Added support for a current team.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterHoburg committed Mar 5, 2024
1 parent 3da6f6e commit 4f5ec43
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Chips available are:
extra_drs
```

## Current Team
`<running_dir>/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
`<running_dir>/data/finishing_positions_qualifying.csv` and `<running_dir>/data/finishing_positions_race.csv`

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "f1-fantasy"
version = "0.5.0"
version = "0.6.0"
description = ""
authors = ["Peter <[email protected]>"]
readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions src/f1_fantasy/data/input/current_team.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name
4 changes: 4 additions & 0 deletions src/f1_fantasy/game_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -430,3 +432,5 @@ def load_prices(cls, constructor_prices: list[ConstructorPriceModel]):
DRIVERS_IGNORE_LIST = []

CONSTRUCTORS_IGNORE_LIST = []

CURRENT_TEAM: Team | None = None
47 changes: 46 additions & 1 deletion src/f1_fantasy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Driver,
Drivers,
Team,
CURRENT_TEAM
)
from f1_fantasy.models import (
ConstructorPriceModel,
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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}")

Expand All @@ -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"),
)

0 comments on commit 4f5ec43

Please sign in to comment.