From ea1cf7ec590509dc520b075659cc4d51725011fa Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Tue, 20 Feb 2024 18:13:19 +0100 Subject: [PATCH] Validate entered race result --- backend_model.py | 29 ++++++++++++++++++++++++++--- formula10.py | 1 + validation_utils.py | 3 +++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/backend_model.py b/backend_model.py index b76f304..169d13c 100644 --- a/backend_model.py +++ b/backend_model.py @@ -152,19 +152,42 @@ def find_or_create_race_result(race_name: str) -> RaceResult: return race_result -def update_race_result(race_name: str, pxx_driver_names_list: List[str], dnf_driver_names_list: List[str], excluded_driver_names: List[str]) -> Response: +def update_race_result(race_name: str, pxx_driver_names_list: List[str], dnf_driver_names_list: List[str], excluded_driver_names_list: List[str]) -> Response: # Use strings as keys, as these dicts will be serialized to json pxx_driver_names: Dict[str, str] = {str(position + 1): driver for position, driver in enumerate(pxx_driver_names_list)} dnf_driver_names: Dict[str, str] = {str(position + 1): driver for position, driver in enumerate(pxx_driver_names_list) if driver in dnf_driver_names_list} - # TODO: This validation is incomplete + # This one is only used for validation + excluded_driver_names: Dict[str, str] = {str(position + 1): driver for position, driver in enumerate(pxx_driver_names_list) if driver in excluded_driver_names_list} + best_excluded_driver_position: int = sorted(list(map(int, list(excluded_driver_names.keys()))))[0] if len(excluded_driver_names) > 0 else 21 + worst_dnf_driver_position: int = sorted(list(map(int, list(dnf_driver_names.keys()))), reverse=True)[0] if len(dnf_driver_names) > 0 else best_excluded_driver_position - 1 + if not positions_are_contiguous(list(dnf_driver_names.keys())): return redirect(f"/result/{quote(race_name)}") + if not positions_are_contiguous(list(excluded_driver_names.keys())): + return redirect(f"/result/{quote(race_name)}") + + # DNF + exclude is exclusive + for driver_name in dnf_driver_names_list: + if driver_name in excluded_driver_names_list: + return redirect(f"/result/{quote(race_name)}") + + # Excluded drivers occupy the last positions, if any are excluded + if len(excluded_driver_names_list) > 0 and not "20" in excluded_driver_names: + return redirect(f"/result/{quote(race_name)}") + + # DNF drivers occupy the positions between finishing and excluded drivers + if len(dnf_driver_names_list) > 0 and worst_dnf_driver_position + 1 != best_excluded_driver_position: + print(dnf_driver_names, worst_dnf_driver_position) + print(excluded_driver_names, best_excluded_driver_position) + return redirect(f"/result/{quote(race_name)}") + + race_result: RaceResult = find_or_create_race_result(race_name) race_result.pxx_driver_names = pxx_driver_names race_result.dnf_driver_names = dnf_driver_names if len(dnf_driver_names_list) > 0 else {"20": "NONE"} - race_result.excluded_driver_names = excluded_driver_names + race_result.excluded_driver_names = excluded_driver_names_list db.session.commit() diff --git a/formula10.py b/formula10.py index fba364e..55f7dbe 100644 --- a/formula10.py +++ b/formula10.py @@ -21,6 +21,7 @@ db.init_app(app) # - A lot of validation (esp. in the model), each input should be checked (e.g. DNF, excluded order)... # - NONE driver handling: If PXX = NONE is selected, excluded drivers have to be taken into account +# - Show place when entering race result # - Show cards of previous race results, like with season guesses? # - Make the season card grid left-aligned? So e.g. 2 cards are not spread over the whole screen with large gaps? diff --git a/validation_utils.py b/validation_utils.py index 47473a9..b8e231d 100644 --- a/validation_utils.py +++ b/validation_utils.py @@ -12,6 +12,9 @@ def any_is_none(*args: Any) -> bool: def positions_are_contiguous(positions: List[str]) -> bool: + if len(positions) == 0: + return True + positions_unique = set(positions) # Remove duplicates positions_sorted: List[int] = sorted([int(position) for position in positions_unique])