Validate entered race result
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
This commit is contained in:
@ -152,19 +152,42 @@ def find_or_create_race_result(race_name: str) -> RaceResult:
|
|||||||
return race_result
|
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
|
# 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)}
|
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}
|
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())):
|
if not positions_are_contiguous(list(dnf_driver_names.keys())):
|
||||||
return redirect(f"/result/{quote(race_name)}")
|
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: RaceResult = find_or_create_race_result(race_name)
|
||||||
race_result.pxx_driver_names = pxx_driver_names
|
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.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()
|
db.session.commit()
|
||||||
|
|
||||||
|
@ -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)...
|
# - 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
|
# - 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?
|
# - 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?
|
# - Make the season card grid left-aligned? So e.g. 2 cards are not spread over the whole screen with large gaps?
|
||||||
|
@ -12,6 +12,9 @@ def any_is_none(*args: Any) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def positions_are_contiguous(positions: List[str]) -> bool:
|
def positions_are_contiguous(positions: List[str]) -> bool:
|
||||||
|
if len(positions) == 0:
|
||||||
|
return True
|
||||||
|
|
||||||
positions_unique = set(positions) # Remove duplicates
|
positions_unique = set(positions) # Remove duplicates
|
||||||
positions_sorted: List[int] = sorted([int(position) for position in positions_unique])
|
positions_sorted: List[int] = sorted([int(position) for position in positions_unique])
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user