Files
CRAPP/src/lib/rosa.ts
T

92 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-08-06 08:36:47 +02:00
import { useMemo } from "react";
import { nascitaPerId, type Giocatore } from "./crapp-data";
import { nomeCompleto, useGiocatoriSquadra } from "./giocatori-squadra";
2026-09-01 22:42:35 +02:00
import { mvpVintiPerGiocatore, useVotiMvp } from "./mvp-voti";
2026-08-06 08:36:47 +02:00
import { mediePagelle, usePagelle } from "./pagelle";
import { statisticheCacche, useCacche } from "./cacche";
import { conteggioTurni } from "./palloni-core";
import { useTurniPalloni } from "./palloni";
import { useInfortuniERitardi } from "./infortuni";
2026-09-01 22:42:35 +02:00
import { useGiocatoreId } from "./user-store";
2026-08-06 08:36:47 +02:00
import { useEventi } from "./eventi";
import { contaPresenzeGiocatore, totaliEventiGiocatore, useRispostePresenze } from "./presenze";
2026-08-06 08:36:47 +02:00
import { obiettiviOrdinati } from "./obiettivi";
import { useCsi } from "./csi";
import { partiteGiocate } from "./csi-core";
2026-08-06 08:36:47 +02:00
function iniziali(nome: string, cognome: string): string {
return `${nome[0] ?? ""}${cognome[0] ?? ""}`.toUpperCase();
}
2026-08-06 08:36:47 +02:00
/**
* Rosa completa con tutte le statistiche personali (presenze, MVP, media voto,
* palloni, infortuni, ritardi, cacche). Legge l'anagrafica da `giocatori_squadra`
* (DD-015): solo i giocatori attivi, gli altri restano nel database ma spariscono
* dagli elenchi correnti. Usa solo cache già in memoria: nessuna query aggiuntiva
* rispetto a quelle che l'app fa comunque.
2026-08-06 08:36:47 +02:00
*/
export function useRosa(): Giocatore[] {
const { righe: squadra } = useGiocatoriSquadra();
2026-08-06 08:36:47 +02:00
const voti = useVotiMvp();
const { voti: pagelle } = usePagelle();
const { righe: cacche } = useCacche();
const { turni } = useTurniPalloni();
const { infortuni, ritardi } = useInfortuniERitardi();
2026-09-01 22:42:35 +02:00
const { eventi } = useEventi();
const { presenze: mappaPresenze } = useRispostePresenze();
2026-08-06 08:36:47 +02:00
const votiMvp = voti.data ?? [];
return useMemo(() => {
const medie = mediePagelle(pagelle);
const statCacche = statisticheCacche(cacche);
const palloni = conteggioTurni(turni);
2026-09-01 22:42:35 +02:00
const mvpVinti = mvpVintiPerGiocatore(votiMvp);
return squadra
.filter((g) => g.attivo)
.map((g) => ({
id: g.id,
nome: nomeCompleto(g),
numero: g.numero,
ruolo: g.ruolo,
nascita: nascitaPerId[g.id] ?? "",
iniziali: iniziali(g.nome, g.cognome),
presenze: contaPresenzeGiocatore(g.id, eventi, mappaPresenze),
totaliEventi: totaliEventiGiocatore(g.id, eventi),
streak: 0,
serieAllenamenti: 0,
seriePartite: 0,
serieConferme: 0,
mvp: mvpVinti[g.id] ?? 0,
mediaVoto: medie[g.id]?.media ?? 0,
palloni: palloni[g.id] ?? 0,
cacche: statCacche[g.id]?.giornateTop ?? 0,
cacchePartita: statCacche[g.id]?.media ?? 0,
infortuni: infortuni[g.id] ?? 0,
ritardi: ritardi[g.id] ?? 0,
}));
}, [squadra, votiMvp, pagelle, cacche, turni, infortuni, ritardi, eventi, mappaPresenze]);
2026-08-06 08:36:47 +02:00
}
/** Il giocatore selezionato sul dispositivo, con le statistiche complete. */
export function useIo(): Giocatore | null {
2026-09-01 22:42:35 +02:00
const id = useGiocatoreId();
2026-08-06 08:36:47 +02:00
const rosa = useRosa();
2026-09-01 22:42:35 +02:00
if (!id) return null;
return rosa.find((g) => g.id === id) ?? null;
2026-08-06 08:36:47 +02:00
}
/** Obiettivi collaborativi calcolati sui dati reali già in cache. */
export function useObiettivi() {
const rosa = useRosa();
const { eventi } = useEventi();
const { presenze } = useRispostePresenze();
const { voti: pagelle } = usePagelle();
const { data: csi } = useCsi();
const vittorie = csi
? partiteGiocate(csi.partite).filter((p) => (p.setNostri ?? 0) > (p.setLoro ?? 0)).length
: 0;
return obiettiviOrdinati(rosa, { eventi, presenze, pagelle, vittorie });
2026-08-06 08:36:47 +02:00
}