From 09519c18fa67681cbb2b52619414693633430eb0 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Tue, 4 Feb 2025 21:56:30 +0100 Subject: [PATCH] Racepicks: Only allow picking active drivers and apply substitutions --- .../data/season/substitutions/+page.svelte | 2 +- src/routes/racepicks/+page.svelte | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/routes/data/season/substitutions/+page.svelte b/src/routes/data/season/substitutions/+page.svelte index 3ccab9e..e5cb231 100644 --- a/src/routes/data/season/substitutions/+page.svelte +++ b/src/routes/data/season/substitutions/+page.svelte @@ -2,7 +2,7 @@ import { get_by_value, get_driver_headshot_template } from "$lib/database"; import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton"; import type { PageData } from "./$types"; - import type { Race, Substitution } from "$lib/schema"; + import type { Driver, Race, Substitution } from "$lib/schema"; import { Button, Table, type TableColumn } from "$lib/components"; let { data }: { data: PageData } = $props(); diff --git a/src/routes/racepicks/+page.svelte b/src/routes/racepicks/+page.svelte index 0a9d481..ded7d5f 100644 --- a/src/routes/racepicks/+page.svelte +++ b/src/routes/racepicks/+page.svelte @@ -19,7 +19,7 @@ RACE_PICTOGRAM_HEIGHT, RACE_PICTOGRAM_WIDTH, } from "$lib/config"; - import type { CurrentPickedUser, RacePick } from "$lib/schema"; + import type { CurrentPickedUser, Driver, RacePick, Substitution } from "$lib/schema"; import { get_by_value, get_driver_headshot_template } from "$lib/database"; import { format } from "date-fns"; @@ -35,6 +35,28 @@ let pxx_select_value: string = $state(currentpick?.pxx ?? ""); let dnf_select_value: string = $state(currentpick?.dnf ?? ""); + const active_drivers_and_substitutes = ( + drivers: Driver[], + substitutions: Substitution[], + ): Driver[] => { + let active_and_substitutes: Driver[] = drivers.filter((driver: Driver) => driver.active); + + substitutions + .filter((substitution: Substitution) => substitution.race === currentpick.race) + .forEach((substitution: Substitution) => { + const for_index = active_and_substitutes.findIndex( + (driver: Driver) => driver.id === substitution.for, + ); + const sub_index = drivers.findIndex( + (driver: Driver) => driver.id === substitution.substitute, + ); + + active_and_substitutes[for_index] = drivers[sub_index]; + }); + + return active_and_substitutes.sort((a: Driver, b: Driver) => a.code.localeCompare(b.code)); + }; + const modalStore: ModalStore = getModalStore(); const create_guess_handler = async (event: Event) => { const modalSettings: ModalSettings = { @@ -44,7 +66,7 @@ racepick: currentpick, currentrace: data.currentrace, user: data.user, - drivers: await data.drivers, + drivers: active_drivers_and_substitutes(await data.drivers, await data.substitutions), disable_inputs: false, // TODO: Datelock headshot_template: get_driver_headshot_template(await data.graphics), pxx_select_value: pxx_select_value,