Files
CRAPP/src/lib/rosa.ts
T

88 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-08-06 08:36:47 +02:00
import { useMemo } from "react";
import { giocatori, type Giocatore } from "./crapp-data";
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";
2026-09-01 22:42:35 +02:00
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
/**
* Rosa completa con tutte le statistiche personali (presenze, MVP, media voto,
* palloni, infortuni, ritardi, cacche). Usa solo cache già in memoria:
* nessuna query aggiuntiva rispetto a quelle che l'app fa comunque.
*/
export function useRosa(): Giocatore[] {
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 giocatori.map((g) => ({
2026-08-06 08:36:47 +02:00
...g,
2026-09-01 22:42:35 +02:00
presenze: contaPresenzeGiocatore(g.id, eventi, mappaPresenze),
totaliEventi: totaliEventiGiocatore(g.id, eventi),
mvp: mvpVinti[g.id] ?? 0,
mediaVoto: medie[g.id]?.media ?? 0,
2026-08-06 08:36:47 +02:00
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,
}));
2026-09-01 22:42:35 +02:00
}, [
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
}
export { giocatori };