Calcoli di badge, obiettivi e percentuali spostati da route e componenti a src/lib
squadra: mediaPresenzeRosa, obiettiviCompletati, mediaProgressoObiettivi, quantiAlGrado; scout: percentualePiu; utils: percentuale (usata da profilo, RosaPresenze, CollezioneBadge). Test unitari per ciascuna. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
06c91149e7
commit
99265f6a1e
13 files changed
+106
-30
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useState, type ReactNode } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, percentuale } from "@/lib/utils";
|
||||
import type { Giocatore } from "@/lib/crapp-data";
|
||||
import {
|
||||
collezioneBadge,
|
||||
@@ -134,7 +134,7 @@ export function CollezioneBadge({
|
||||
const vicino = prossimoTraguardo(g);
|
||||
const social = badgeSocialVinti(votiSocial, g.id);
|
||||
const socialVinti = categorieSocial.filter((cat) => social[cat.id]);
|
||||
const pct = Math.round((c.ottenuti / c.totali) * 100);
|
||||
const pct = percentuale(c.ottenuti, c.totali);
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useState } from "react";
|
||||
import { BellRing, HelpCircle, Loader2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, percentuale } from "@/lib/utils";
|
||||
import { Card } from "@/components/crapp/ui-bits";
|
||||
import { Avatar } from "@/components/crapp/Avatar";
|
||||
import { Barra } from "@/components/motion/Barra";
|
||||
@@ -44,7 +44,7 @@ export function RosaPresenze({
|
||||
|
||||
const mancanti = rosa.filter((g) => !risposte[g.id]);
|
||||
const risposteN = rosa.length - mancanti.length;
|
||||
const perc = rosa.length ? Math.round((risposteN / rosa.length) * 100) : 0;
|
||||
const perc = percentuale(risposteN, rosa.length);
|
||||
const daSollecitare = mancanti.length + rosa.filter((g) => risposte[g.id] === "forse").length;
|
||||
const ordineVisibile = tipo === "evento" ? ordine.filter((s) => s !== "infortunato") : ordine;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState } from "react";
|
||||
import { BarChart3, Download, Info } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, percentuale } from "@/lib/utils";
|
||||
import { Card } from "@/components/crapp/ui-bits";
|
||||
import type { Giocatore } from "@/lib/crapp-data";
|
||||
import { csvScoutMatch, scaricaCsv } from "@/lib/scout-export";
|
||||
@@ -10,11 +10,9 @@ import {
|
||||
totaliPerGiocatore,
|
||||
totaliSquadra,
|
||||
type ScoutMatch,
|
||||
percentualePiu,
|
||||
} from "@/lib/scout-store";
|
||||
|
||||
const pct = (piu: number, meno: number) =>
|
||||
piu + meno === 0 ? null : Math.round((piu / (piu + meno)) * 100);
|
||||
|
||||
/** Lettura dello scout di una partita: squadra per fondamentale, set per set, dettaglio per giocatore. */
|
||||
export function ScoutReport({
|
||||
scout,
|
||||
@@ -149,7 +147,7 @@ export function ScoutReport({
|
||||
<div className="mt-3 grid grid-cols-2 gap-2">
|
||||
{fondamentali.map((f) => {
|
||||
const { piu, meno } = squadra[f.id];
|
||||
const p = pct(piu, meno);
|
||||
const p = percentualePiu(piu, meno);
|
||||
return (
|
||||
<div key={f.id} className="rounded-2xl bg-secondary p-3">
|
||||
<div className="flex items-baseline justify-between">
|
||||
|
||||
@@ -194,6 +194,14 @@ export function gradoRaggiunto(def: BadgeDef, valore: number): Grado | null {
|
||||
return grado;
|
||||
}
|
||||
|
||||
/** Quanti giocatori della rosa hanno raggiunto almeno `grado` per il badge `def`. */
|
||||
export function quantiAlGrado(def: BadgeDef, rosa: Giocatore[], grado: Grado): number {
|
||||
return rosa.filter((g) => {
|
||||
const raggiunto = gradoRaggiunto(def, def.valore(g));
|
||||
return raggiunto ? gradiOrdine.indexOf(raggiunto) >= gradiOrdine.indexOf(grado) : false;
|
||||
}).length;
|
||||
}
|
||||
|
||||
export function statoBadge(def: BadgeDef, g: Giocatore): BadgeStato {
|
||||
const valore = def.valore(g);
|
||||
const grado = gradoRaggiunto(def, valore);
|
||||
|
||||
@@ -415,6 +415,25 @@ export function progressoObiettivo(o: ObiettivoSquadra) {
|
||||
return Math.min(100, Math.round((o.valore / o.target) * 100));
|
||||
}
|
||||
|
||||
export function obiettiviCompletati(obiettivi: ObiettivoSquadra[]) {
|
||||
return obiettivi.filter((o) => progressoObiettivo(o) >= 100).length;
|
||||
}
|
||||
|
||||
export function mediaProgressoObiettivi(obiettivi: ObiettivoSquadra[]) {
|
||||
return obiettivi.length
|
||||
? Math.round(obiettivi.reduce((s, o) => s + progressoObiettivo(o), 0) / obiettivi.length)
|
||||
: 0;
|
||||
}
|
||||
|
||||
/** Media, in percentuale, della quota di eventi a cui ogni giocatore della rosa ha partecipato. */
|
||||
export function mediaPresenzeRosa(rosa: Giocatore[]) {
|
||||
return rosa.length
|
||||
? Math.round(
|
||||
(rosa.reduce((s, g) => s + g.presenze / (g.totaliEventi || 1), 0) / rosa.length) * 100,
|
||||
)
|
||||
: 0;
|
||||
}
|
||||
|
||||
export function obiettiviOrdinati(rosa?: Giocatore[], ctx?: ContestoObiettivi, oggi?: Date) {
|
||||
return obiettiviSquadra(rosa, ctx, oggi).sort((a, b) => {
|
||||
const pa = progressoObiettivo(a);
|
||||
|
||||
@@ -96,6 +96,11 @@ export function contaValutazioni(
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Percentuale di + sul totale (+ e −) di un fondamentale; `null` se non ci sono valutazioni. */
|
||||
export function percentualePiu(piu: number, meno: number): number | null {
|
||||
return piu + meno === 0 ? null : Math.round((piu / (piu + meno)) * 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Riepilogo delle valutazioni per la lettura nell'app: totali di squadra e per giocatore,
|
||||
* su tutta la partita o su un solo set. Chi non ha valutazioni nel periodo non compare.
|
||||
|
||||
@@ -4,3 +4,8 @@ import { twMerge } from "tailwind-merge";
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
/** Percentuale intera di `parte` su `totale`; 0 se il totale è 0. */
|
||||
export function percentuale(parte: number, totale: number): number {
|
||||
return totale ? Math.round((parte / totale) * 100) : 0;
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { createFileRoute, Link } from "@tanstack/react-router";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Flame, Trash2, Bell, Mail, LogOut, ShieldCheck, Bug, Lightbulb } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, percentuale } from "@/lib/utils";
|
||||
import { Card, PageHeader, StatTile, TeamLogo } from "@/components/crapp/ui-bits";
|
||||
import { BarraSottosezioni } from "@/components/crapp/BarraSottosezioni";
|
||||
import { Avatar } from "@/components/crapp/Avatar";
|
||||
@@ -87,7 +87,7 @@ function ProfiloGiocatore() {
|
||||
|
||||
const tecnici = totaliTecnici(scoutMatches, g.id);
|
||||
|
||||
const percPresenze = g.totaliEventi ? Math.round((g.presenze / g.totaliEventi) * 100) : 0;
|
||||
const percPresenze = percentuale(g.presenze, g.totaliEventi);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
+12
-17
@@ -7,7 +7,13 @@ import { BarraSottosezioni } from "@/components/crapp/BarraSottosezioni";
|
||||
import { Avatar } from "@/components/crapp/Avatar";
|
||||
import { formatData, inizialiDa } from "@/lib/crapp-data";
|
||||
import { ObiettivoDrawer } from "@/components/crapp/ObiettivoDrawer";
|
||||
import { microcopyObiettivo, progressoObiettivo } from "@/lib/obiettivi";
|
||||
import {
|
||||
mediaPresenzeRosa,
|
||||
mediaProgressoObiettivi,
|
||||
microcopyObiettivo,
|
||||
obiettiviCompletati,
|
||||
progressoObiettivo,
|
||||
} from "@/lib/obiettivi";
|
||||
import {
|
||||
useRosa,
|
||||
useObiettivi,
|
||||
@@ -45,7 +51,7 @@ import {
|
||||
descrizioneSoglie,
|
||||
gradiOrdine,
|
||||
gradoMeta,
|
||||
gradoRaggiunto,
|
||||
quantiAlGrado,
|
||||
collezioneBadge,
|
||||
} from "@/lib/badges";
|
||||
|
||||
@@ -111,20 +117,14 @@ function Squadra() {
|
||||
const [filtroAperto, setFiltroAperto] = useState(false);
|
||||
const obiettivi = useObiettivi();
|
||||
const team = totaliSquadraValutazioni(scoutMatches);
|
||||
const mediaPresenze = rosa.length
|
||||
? Math.round(
|
||||
(rosa.reduce((s, g) => s + g.presenze / (g.totaliEventi || 1), 0) / rosa.length) * 100,
|
||||
)
|
||||
: 0;
|
||||
const mediaPresenze = mediaPresenzeRosa(rosa);
|
||||
const ordinati = [...rosa].sort((a, b) => valore(b, criterio) - valore(a, criterio));
|
||||
const rank = classificaRank(ordinati.map((g) => valore(g, criterio)));
|
||||
const max = ordinati[0] ? valore(ordinati[0], criterio) || 1 : 1;
|
||||
const { data: csi } = useCsi();
|
||||
const matchGiocati = csi ? partiteGiocate(csi.partite).length : scoutMatches.length;
|
||||
const completati = obiettivi.filter((o) => progressoObiettivo(o) >= 100).length;
|
||||
const mediaObiettivi = obiettivi.length
|
||||
? Math.round(obiettivi.reduce((s, o) => s + progressoObiettivo(o), 0) / obiettivi.length)
|
||||
: 0;
|
||||
const completati = obiettiviCompletati(obiettivi);
|
||||
const mediaObiettivi = mediaProgressoObiettivi(obiettivi);
|
||||
|
||||
// Ogni tab è memoizzata: aprire/chiudere una card giocatore (stato `aperto`)
|
||||
// non deve ricalcolare badge e obiettivi delle altre sezioni.
|
||||
@@ -504,12 +504,7 @@ function Squadra() {
|
||||
<div className="mt-2 grid grid-cols-3 gap-1.5">
|
||||
{gradiOrdine.map((grado) => {
|
||||
const meta = gradoMeta[grado];
|
||||
const quanti = rosa.filter((g) => {
|
||||
const raggiunto = gradoRaggiunto(b, b.valore(g));
|
||||
return raggiunto
|
||||
? gradiOrdine.indexOf(raggiunto) >= gradiOrdine.indexOf(grado)
|
||||
: false;
|
||||
}).length;
|
||||
const quanti = quantiAlGrado(b, rosa, grado);
|
||||
return (
|
||||
<div
|
||||
key={grado}
|
||||
|
||||
Reference in new issue
Block a user