diff --git a/main.py b/main.py index 3e90556..e4f0bbc 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from src.driver.driver import Driver from src.engine.engine import * from src.race.race import Race +from src.race.raceResults import RaceResults from src.series.series import Series from src.team.team import Team from src.team.teamFactory import TeamFactory @@ -14,107 +15,246 @@ def main(): # instantiate the 2023 Formula One Championship formula_one_2023 = Championship(series = formula_one, year = 2023) + # instantiate drivers + + alb = Driver(surname = "Albon", givenname = "Alexander", number = 23, nationality = "Thailand") + alo = Driver(surname = "Alonso", givenname = "Fernando", number = 14, nationality = "Spain") + bot = Driver(surname = "Bottas", givenname = "Valtteri", number = 77, nationality = "Finland") + dev = Driver(surname = "de Vries", givenname = "Nyck", number = 21, nationality = "Netherlands") + gas = Driver(surname = "Gasly", givenname = "Pierre", number = 10, nationality = "France") + ham = Driver(surname = "Hamilton", givenname = "Lewis", number = 44, nationality = "United Kingdom") + hul = Driver(surname = "Hülkenberg", givenname = "Nico", number = 27, nationality = "Germany") + lec = Driver(surname = "Leclerc", givenname = "Charles", number = 16, nationality = "Monaco") + mag = Driver(surname = "Magnussen", givenname = "Kevin", number = 20, nationality = "Denmark") + nor = Driver(surname = "Norris", givenname = "Lando", number = 4, nationality = "United Kingdom") + oco = Driver(surname = "Ocon", givenname = "Esteban", number = 31, nationality = "France") + per = Driver(surname = "Pérez", givenname = "Sergio", number = 11, nationality = "Mexico") + pia = Driver(surname = "Piastri", givenname = "Oscar", number = 81, nationality = "Australia") + rus = Driver(surname = "Russell", givenname = "George", number = 63, nationality = "United Kingdom") + sai = Driver(surname = "Sainz", suffix = "Jr.", givenname = "Carlos", number = 55, nationality = "Spain") + sar = Driver(surname = "Sargeant", givenname = "Logan", number = 2, nationality = "United States") + stroll = Driver(surname = "Stroll", givenname = "Lance", number = 18, nationality = "Canada") # str is a reserved word + tsu = Driver(surname = "Tsunoda", givenname = "Yuki", number = 22, nationality = "Japan") + ver = Driver(surname = "Verstappen", givenname = "Max", number = 1, nationality = "Netherlands") + zho = Driver(surname = "Zhou", givenname = "Guanyu", number = 24, nationality = "China", surname_first = True) + # create teams using TeamFactory formula_one_2023.addTeamToChampionship( TeamFactory.createTeamFerrariEngine( name = "Alfa Romeo", country = "Switzerland", - drivers = [ - Driver(surname = "Bottas", givenname = "Valtteri", number = 77, nationality = "Finland"), - Driver(surname = "Zhou", givenname = "Guanyu", number = 24, nationality = "China", surname_first = True) - ] + drivers = [bot, zho] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamHondaRBPTEngine( name = "AlphaTauri", country = "Italy", - drivers = [ - Driver(surname = "de Vries", givenname = "Nyck", number = 21, nationality = "Netherlands"), - Driver(surname = "Tsunoda", givenname = "Yuki", number = 22, nationality = "Japan") - ] + drivers = [dev, tsu] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamRenaultEngine( name = "Alpine", country = "France", - drivers = [ - Driver(surname = "Gasly", givenname = "Pierre", number = 10, nationality = "France"), - Driver(surname = "Ocon", givenname = "Esteban", number = 31, nationality = "France") - ] + drivers = [gas, oco] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamMercedesEngine( name = "Aston Martin", country = "United Kingdom", - drivers = [ - Driver(surname = "Alonso", givenname = "Fernando", number = 14, nationality = "Spain"), - Driver(surname = "Stroll", givenname = "Lance", number = 18, nationality = "Canada") - ] + drivers = [alo, stroll] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamFerrariEngine( name = "Ferrari", # this is my second favorite team. They are terrible at strategy and there are multiple memes about how bad their strategy is country = "Italy", - drivers = [ - Driver(surname = "Leclerc", givenname = "Charles", number = 16, nationality = "Monaco"), - Driver(surname = "Sainz", suffix = "Jr.", givenname = "Carlos", number = 55, nationality = "Spain") - ] + drivers = [lec, sai] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamFerrariEngine( name = "Haas", country = "United States", - drivers = [ - # TODO add drivers - ] + drivers = [hul, mag] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamMercedesEngine( name = "McLaren", country = "United Kingdom", - drivers = [ - Driver(surname = "Norris", givenname = "Lando", number = 4, nationality = "United Kingdom"), - Driver(surname = "Piastri", givenname = "Oscar", number = 81, nationality = "Australia") - ] + drivers = [nor, pia] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamMercedesEngine( name = "Mercedes", # this is my favorite team country = "Germany", - drivers = [ - Driver(surname = "Hamilton", givenname = "Lewis", number = 44, nationality = "United Kingdom"), - Driver(surname = "Russell", givenname = "George", number = 63, nationality = "United Kingdom") - ] + drivers = [ham, rus] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamHondaRBPTEngine( name = "Red Bull", # you're not going to believe this, but Red Bull is actually a better team right now than Ferrari country = "Austria", - drivers = [ - # TODO add drivers - ] + drivers = [per, ver] ) ) formula_one_2023.addTeamToChampionship( TeamFactory.createTeamMercedesEngine( name = "Williams", country = "United Kingdom", - drivers = [ - # TODO add drivers - ] + drivers = [alb, sar] + ) + ) + + # add races and race results and print info + + # bahrain grand prix + + bahrain_gp = Race( + name = "Bahrain Grand Prix", + country = "Bahrain", + city = "Sakhir", + circuit = "Bahrain International Circuit" + ) + print(bahrain_gp.__str__()) + formula_one_2023.holdRace( + RaceResults( + race = bahrain_gp, + rankings = [ + ver, per, alo, sai, ham, stroll, rus, bot, gas, alb, tsu, sar, mag, dev, hul, zho, nor, oco, lec, pia + ], + fastestLap = zho + ) + ) + + # saudi arabian grand prix + + saudi_gp = Race( + name = "Saudi Arabian Grand Prix", + country = "Saudi Arabia", + city = "Jeddah", + circuit = "Jeddah Corniche Circuit" + ) + print(saudi_gp.__str__()) + formula_one_2023.holdRace( + RaceResults( + race = saudi_gp, + rankings = [ + per, ver, alo, rus, ham, sai, lec, oco, gas, mag, tsu, hul, zho, dev, pia, sar, nor, bot, alb, stroll + ], + fastestLap = ver + ) + ) + + # australian grand prix + + australian_gp = Race( + name = "Australian Grand Prix", + country = "Australia", + city = "Melbourne", + circuit = "Albert Park Circuit" + ) + print(australian_gp.__str__()) + formula_one_2023.holdRace( + RaceResults( + race = australian_gp, + rankings = None, # TODO + fastestLap = None # TODO + ) + ) + + # azerbaijan grand prix + + azerbaijan_gp = Race( + name = "Azerbaijan Grand Prix", + country = "Azerbaijan", + city = "Baku", + circuit = "Baku City Circuit" + ) + print(azerbaijan_gp.__str__()) + formula_one_2023.holdRace( + RaceResults( + race = azerbaijan_gp, + rankings = None, # TODO + fastestLap = None # TODO + ) + ) + + # miami grand prix + + miami_gp = Race( + name = "Miami Grand Prix", + country = "United States", + city = "Miami", + circuit = "Miami International Autodrome" + ) + print(miami_gp.__str__()) + formula_one_2023.holdRace( + RaceResults( + race = miami_gp, + rankings = None, # TODO + fastestLap = None # TODO + ) + ) + + # monaco grand prix + + monaco_gp = Race( + name = "Monaco Grand Prix", + country = "Monaco", + city = "Monaco", + circuit = "Circuit de Monaco" + ) + print(monaco_gp.__str__()) + formula_one_2023.holdRace( + RaceResults( + race = monaco_gp, + rankings = None, # TODO + fastestLap = None # TODO ) ) - # print standings to the console + # spanish grand prix + + spanish_gp = Race( + name = "Spanish Grand Prix", + country = "Spain", + city = "Barcelona", + circuit = "Circuit de Barcelona-Catalunya" + ) + print(spanish_gp.__str__()) + formula_one_2023.holdRace( + RaceResults( + race = spanish_gp, + rankings = None, # TODO + fastestLap = None # TODO + ) + ) + + # future races for the 2023 season + + canadian_gp = Race( + name = "Canadian Grand Prix", + country = "Canada", + city = "Montreal", + circuit = "Circuit Gilles Villeneuve" + ) + + austrian_gp = Race( + name = "Austrian Grand Prix", + country = "Austria", + city = "Spielberg", + circuit = "Red Bull Ring" + ) + + # TODO add the rest of the future races for 2023 + + # print current standings to the console print("\nTeam Standings") for x in formula_one_2023.getTeamStandings(): diff --git a/src/championship/championship.py b/src/championship/championship.py index 6285e2e..7c7c143 100644 --- a/src/championship/championship.py +++ b/src/championship/championship.py @@ -103,17 +103,41 @@ def getTeamStandings(self) -> List[str]: return printable - def holdRace(self, race: Race, results: RaceResults): + def holdRace(self, race_results: RaceResults): ''' holds a race and updates standings given the results params: - race: Race - results: RaceResults + race_results: RaceResults ''' - if not isinstance(race, Race): - raise TypeError(f"{race} must be of type Race, not {type(race)}") + + # type checking + + if not isinstance(race_results, RaceResults): + raise TypeError(f"{race_results} must be of type RaceResults, not {type(race_results)}") + + if not isinstance(race_results.race, Race): + raise TypeError(f"{race_results.race} must be of type Race, not {type(race_results.race)}") - # type check the results + if not isinstance(race_results.results, dict): + raise TypeError(f"{race_results.results} must be of type dict, not {type(race_results.results)}") + + # TODO update standings based on the results + + tms = list(self.team_standings.keys()) + + for i in range(len(tms)): + if not isinstance(tms[i], Team): + raise TypeError(f"") + + drs = list(tms[i].drivers) + + for j in range(len(drs)): + if not isinstance(drs[j], Driver): + raise TypeError(f"") + + new_points = race_results.getPointsForDriver(drs[j]) + + self.team_standings[tms[i]] += new_points + self.driver_standings[drs[j]] += new_points - # TODO update standings based on the results \ No newline at end of file diff --git a/src/race/race.py b/src/race/race.py index b3daf06..99d293d 100644 --- a/src/race/race.py +++ b/src/race/race.py @@ -4,17 +4,20 @@ class Race(): """ TODO """ - def __init__(self, name: str, country: str, city: str): + def __init__(self, name: str, country: str, city: str, circuit: str): if not isinstance(name, str): raise TypeError(f"{name} must be of type str, not {type(name)}") if not isinstance(country, str): raise TypeError(f"{country} must be of type str, not {type(country)}") if not isinstance(city, str): raise TypeError(f"{city} must be of type str, not {type(city)}") + if not isinstance(circuit, str): + raise TypeError(f"{circuit} must be of type str, not {type(circuit)}") self.name = name self.country = country self.city = city + self.circuit = circuit def __str__(self) -> str: - return f"{self.name} Grand Prix ({self.city}, {self.country})" \ No newline at end of file + return f"{self.name} ({self.circuit} in {self.city}, {self.country})" \ No newline at end of file diff --git a/src/race/raceResults.py b/src/race/raceResults.py index 6601ee9..81b3369 100644 --- a/src/race/raceResults.py +++ b/src/race/raceResults.py @@ -22,6 +22,8 @@ def __init__(self, race: Race, rankings: List[Driver], fastestLap: Driver): points_system = [None, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + rankings.insert(0, None) + for i in range(1,21): if not isinstance(rankings[i], Driver): raise TypeError(f"{rankings[i]} must be of type Driver, not {type(rankings[i])}") diff --git a/src/team/team.py b/src/team/team.py index a509c15..e9e3b1d 100644 --- a/src/team/team.py +++ b/src/team/team.py @@ -29,7 +29,10 @@ def __init__(self, name: str, engine: EngineInt, country: str, drivers: List[Dri def addDriverToTeam(self, driver: Driver): """ - TODO + adds a driver to the team + + params: + driver: Driver """ if not isinstance(driver, Driver): raise TypeError(f"{driver} must be of type Driver, not {type(driver)}") @@ -39,10 +42,16 @@ def addDriverToTeam(self, driver: Driver): def removeDriverFromTeam(self, driver: Driver): """ - TODO + removes a driver from the team + + params: + driver: Driver """ if not isinstance(driver, Driver): raise TypeError(f"{driver} must be of type Driver, not {type(driver)}") if driver in self.drivers: - self.drivers.remove(driver) \ No newline at end of file + self.drivers.remove(driver) + + def getDrivers(self) -> List[Driver]: + return self.drivers \ No newline at end of file diff --git a/tests/dummy_data.py b/tests/dummy_data.py new file mode 100644 index 0000000..ffd9765 --- /dev/null +++ b/tests/dummy_data.py @@ -0,0 +1,49 @@ +from src.championship.championship import Championship +from src.driver.driver import Driver +from src.engine.engine import Engine +from src.race.race import Race +from src.series.series import Series +from src.team.team import Team + +DUMMY_DRIVER = Driver( + surname = "Doe", + givenname = "John", + number = 0, + nationality = "Internet", + surname_first = False, + suffix = "Jr." +) + +DUMMY_DRIVER_TWO = Driver( + surname = "", + givenname = "", + number = -1, + nationality = "", + surname_first = False, + suffix = None +) + +DUMMY_ENGINE = Engine(engine = "Dummy Engine") + +DUMMY_RACE = Race( + name = "Dummy Race", + country = "Internet", + city = "Internet City", + circuit = "Internet Circuit" +) + +DUMMY_SERIES = Series( + name = "Dummy Series" +) + +DUMMY_CHAMPIONSHIP = Championship( + series = DUMMY_SERIES, + year = 0 +) + +DUMMY_TEAM = Team( + name = "Dummy Team", + engine = DUMMY_ENGINE, + country = "Internet", + drivers = [] +) \ No newline at end of file diff --git a/tests/test_driver.py b/tests/test_driver.py index a204b43..0a8025b 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -1,4 +1,29 @@ +from copy import deepcopy import pytest -from typing import Any +from src.driver.driver import Driver +from tests.dummy_data import DUMMY_DRIVER -# TODO \ No newline at end of file +@pytest.fixture +def driver_fixture(): + return deepcopy(DUMMY_DRIVER) + +def test___str___(driver_fixture: Driver): + + assert isinstance(driver_fixture, Driver) + + s = driver_fixture.__str__() + assert isinstance(s, str) + + assert s == "John DOE Jr. #0" + +def test___str___surname_first(driver_fixture: Driver): + + assert isinstance(driver_fixture, Driver) + + driver_fixture.surname_first = True + assert isinstance(driver_fixture, Driver) + + s = driver_fixture.__str__() + assert isinstance(s, str) + + assert s == "DOE John Jr. #0" \ No newline at end of file diff --git a/tests/test_team.py b/tests/test_team.py index 45970c9..100630e 100644 --- a/tests/test_team.py +++ b/tests/test_team.py @@ -1,6 +1,59 @@ +from copy import deepcopy import pytest from src.driver.driver import Driver -from src.engine.engine import * -from typing import Any, List +from src.team.team import Team +from tests.dummy_data import DUMMY_DRIVER, DUMMY_DRIVER_TWO, DUMMY_TEAM -# TODO \ No newline at end of file +@pytest.fixture +def driver_fixture(): + return deepcopy(DUMMY_DRIVER) + +@pytest.fixture +def team_fixture(): + return deepcopy(DUMMY_TEAM) + +def test_addDriverToTeam(team_fixture: Team, driver_fixture: Driver): + assert isinstance(team_fixture, Team) + assert isinstance(driver_fixture, Driver) + + assert len(team_fixture.drivers) == 0 + team_fixture.addDriverToTeam(driver_fixture) + + assert len(team_fixture.drivers) == 1 + assert team_fixture.drivers[0] == driver_fixture + + team_fixture.addDriverToTeam(driver_fixture) + assert len(team_fixture.drivers) == 1 + +def test_removeDriverFromTeam(team_fixture: Team, driver_fixture: Driver): + team_fixture = team_fixture + + assert isinstance(team_fixture, Team) + assert isinstance(driver_fixture, Driver) + + assert len(team_fixture.drivers) == 0 + team_fixture.addDriverToTeam(driver_fixture) + + assert len(team_fixture.drivers) == 1 + assert team_fixture.drivers[0] == driver_fixture + + team_fixture.removeDriverFromTeam(DUMMY_DRIVER_TWO) + assert len(team_fixture.drivers) == 1 + + team_fixture.removeDriverFromTeam(driver_fixture) + assert len(team_fixture.drivers) == 0 + +def test_getDrivers(team_fixture: Team, driver_fixture: Driver): + assert isinstance(team_fixture, Team) + + assert len(team_fixture.getDrivers()) == 0 + + team_fixture.addDriverToTeam(driver = driver_fixture) + + drivers = team_fixture.getDrivers() + + assert isinstance(drivers, list) + assert len(drivers) == 1 + + assert isinstance(drivers[0], Driver) + assert drivers[0] == driver_fixture \ No newline at end of file