2026-08-06 08:36:47 +02:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import type { Giocatore } from "./crapp-data";
|
2026-09-07 13:58:54 +02:00
|
|
|
import type { Evento } from "./eventi";
|
|
|
|
|
import { useEventi } from "./eventi";
|
|
|
|
|
import { dataOggi } from "./scout-live";
|
2026-08-06 08:36:47 +02:00
|
|
|
import { useRispostePresenze, type MappaPresenze } from "./presenze";
|
|
|
|
|
|
|
|
|
|
/** giocatoreId -> numero di eventi (allenamenti + partite) con stato "infortunato". */
|
|
|
|
|
export type ContoInfortuni = Record<string, number>;
|
|
|
|
|
|
2026-09-07 13:58:54 +02:00
|
|
|
/**
|
|
|
|
|
* Un giocatore può segnarsi infortunato o in ritardo anche su un evento futuro
|
|
|
|
|
* (l'UI lo permette finché l'evento non è passato): finché quell'evento non è
|
|
|
|
|
* avvenuto davvero non deve contare per i badge, altrimenti si sbloccherebbero
|
|
|
|
|
* in anticipo. Un evento non più in `eventi` (es. cancellato) non viene contato:
|
|
|
|
|
* non potendo verificarne la data, si esclude per prudenza.
|
|
|
|
|
*/
|
|
|
|
|
function contaStato(
|
|
|
|
|
presenze: MappaPresenze,
|
|
|
|
|
stato: string,
|
|
|
|
|
eventi: Evento[],
|
|
|
|
|
oggi: string = dataOggi(),
|
|
|
|
|
): ContoInfortuni {
|
|
|
|
|
const dataPerEvento = new Map(eventi.map((e) => [e.id, e.data]));
|
2026-08-06 08:36:47 +02:00
|
|
|
const out: ContoInfortuni = {};
|
|
|
|
|
for (const eventoId of Object.keys(presenze)) {
|
2026-09-07 13:58:54 +02:00
|
|
|
const dataEvento = dataPerEvento.get(eventoId);
|
|
|
|
|
if (dataEvento === undefined || dataEvento >= oggi) continue;
|
2026-08-06 08:36:47 +02:00
|
|
|
const evento = presenze[eventoId] ?? {};
|
|
|
|
|
for (const giocatoreId of Object.keys(evento)) {
|
|
|
|
|
if (evento[giocatoreId] === stato) out[giocatoreId] = (out[giocatoreId] ?? 0) + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-07 13:58:54 +02:00
|
|
|
/** Conta gli infortuni dalla mappa presenze già in cache: ogni evento passato vale una volta sola. */
|
|
|
|
|
export function contaInfortuni(
|
|
|
|
|
presenze: MappaPresenze,
|
|
|
|
|
eventi: Evento[],
|
|
|
|
|
oggi: string = dataOggi(),
|
|
|
|
|
): ContoInfortuni {
|
|
|
|
|
return contaStato(presenze, "infortunato", eventi, oggi);
|
2026-08-06 08:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-07 13:58:54 +02:00
|
|
|
/** Conta i ritardi dalla stessa mappa presenze: ogni evento passato vale una volta sola. */
|
|
|
|
|
export function contaRitardi(
|
|
|
|
|
presenze: MappaPresenze,
|
|
|
|
|
eventi: Evento[],
|
|
|
|
|
oggi: string = dataOggi(),
|
|
|
|
|
): ContoInfortuni {
|
|
|
|
|
return contaStato(presenze, "ritardo", eventi, oggi);
|
2026-08-06 08:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function conInfortuni<T extends Giocatore>(
|
|
|
|
|
g: T,
|
|
|
|
|
conto: ContoInfortuni,
|
|
|
|
|
contoRitardi: ContoInfortuni = {},
|
|
|
|
|
): T {
|
|
|
|
|
return {
|
|
|
|
|
...g,
|
|
|
|
|
infortuni: conto[g.id] ?? g.infortuni ?? 0,
|
|
|
|
|
ritardi: contoRitardi[g.id] ?? g.ritardi ?? 0,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-07 13:58:54 +02:00
|
|
|
/** Nessuna query aggiuntiva: riusa le cache di risposte presenze ed eventi. */
|
2026-08-06 08:36:47 +02:00
|
|
|
export function useInfortuni(): ContoInfortuni {
|
|
|
|
|
const { presenze } = useRispostePresenze();
|
2026-09-07 13:58:54 +02:00
|
|
|
const { eventi } = useEventi();
|
|
|
|
|
return useMemo(() => contaInfortuni(presenze, eventi), [presenze, eventi]);
|
2026-08-06 08:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-07 13:58:54 +02:00
|
|
|
/** Nessuna query aggiuntiva: riusa le cache di risposte presenze ed eventi. */
|
2026-08-06 08:36:47 +02:00
|
|
|
export function useRitardi(): ContoInfortuni {
|
|
|
|
|
const { presenze } = useRispostePresenze();
|
2026-09-07 13:58:54 +02:00
|
|
|
const { eventi } = useEventi();
|
|
|
|
|
return useMemo(() => contaRitardi(presenze, eventi), [presenze, eventi]);
|
2026-08-06 08:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Un solo hook per entrambi i conteggi: evita hook extra nei componenti. */
|
|
|
|
|
export function useInfortuniERitardi(): { infortuni: ContoInfortuni; ritardi: ContoInfortuni } {
|
|
|
|
|
const { presenze } = useRispostePresenze();
|
2026-09-07 13:58:54 +02:00
|
|
|
const { eventi } = useEventi();
|
2026-08-06 08:36:47 +02:00
|
|
|
return useMemo(
|
2026-09-07 13:58:54 +02:00
|
|
|
() => ({
|
|
|
|
|
infortuni: contaInfortuni(presenze, eventi),
|
|
|
|
|
ritardi: contaRitardi(presenze, eventi),
|
|
|
|
|
}),
|
|
|
|
|
[presenze, eventi],
|
2026-08-06 08:36:47 +02:00
|
|
|
);
|
2026-09-01 13:38:33 +02:00
|
|
|
}
|