import { StatsToggle } from "(app)/_components/StatsToggle"; import { prisma } from "@repo/db"; import { getServerSession } from "api/auth/[...nextauth]/auth"; import { PlaneIcon } from "lucide-react"; export const PilotStats = async () => { const session = await getServerSession(); if (!session) return null; const user = session.user; const mostFlownStationsIds = await prisma.missionOnStationUsers.groupBy({ where: { userId: user?.id, }, by: ["stationId"], orderBy: { _count: { stationId: "desc", }, }, take: 1, _count: { stationId: true, }, }); let mostFlownStation = null; if (mostFlownStationsIds[0]?.stationId) { mostFlownStation = await prisma.station.findUnique({ where: { id: mostFlownStationsIds[0]?.stationId, }, }); } const dispoSessions = await prisma.connectedAircraft.findMany({ where: { userId: user?.id, logoutTime: { not: null, }, }, select: { loginTime: true, logoutTime: true, }, }); const totalFlownMissions = await prisma.missionOnStationUsers.count({ where: { userId: user?.id, }, }); // Get the user's rank by missions flown const missionsFlownRanks = await prisma.missionOnStationUsers.groupBy({ by: ["userId"], _count: { userId: true }, orderBy: { _count: { userId: "desc" } }, }); const ownRankMissionsFlown = missionsFlownRanks.findIndex((rank) => rank.userId === user?.id) + 1; const totalUserCount = await prisma.user.count({ where: { NOT: { id: user?.id, }, }, }); const totalStationsCount = await prisma.station.count(); const unflownStationsCount = totalStationsCount - mostFlownStationsIds.length; const totalPilotTime = dispoSessions.reduce((acc, session) => { const logoffTime = new Date(session.logoutTime!).getTime(); const logonTime = new Date(session.loginTime).getTime(); return acc + (logoffTime - logonTime); }, 0); const hours = Math.floor(totalPilotTime / (1000 * 60 * 60)); const minutes = Math.floor((totalPilotTime % (1000 * 60 * 60)) / (1000 * 60)); const totalFlownMissionsPercent = ((ownRankMissionsFlown * 100) / totalUserCount).toFixed(0); return (
Einsätze geflogen
{totalFlownMissions}
Du bist damit unter den top{" "} {!isNaN(Number(totalFlownMissionsPercent)) ? totalFlownMissionsPercent : 0}%!
Pilot Login Zeit
{hours}h {minutes}min
{mostFlownStation && (
{mostFlownStation?.bosCallsign}
War bisher dein Rettungsmittel der Wahl
{unflownStationsCount > 0 && (
{unflownStationsCount} {unflownStationsCount > 1 ? "Stationen" : "Station"} warten noch auf dich!
)} {unflownStationsCount === 0 && (
Du hast alle Stationen geflogen! Krass...
)}
)}
); }; export const DispoStats = async () => { const session = await getServerSession(); if (!session) return null; const user = session.user; const dispoSessions = await prisma.connectedDispatcher.findMany({ where: { userId: user?.id, logoutTime: { not: null, }, }, select: { loginTime: true, logoutTime: true, }, }); const mostDispatchedStationIds = await prisma.mission.groupBy({ where: { createdUserId: user?.id, }, by: ["missionStationIds"], orderBy: { _count: { missionStationIds: "desc", }, }, take: 1, _count: { missionStationIds: true, }, }); let mostDispatchedStation = null; if (mostDispatchedStationIds[0]?.missionStationIds[0]) { mostDispatchedStation = await prisma.station.findUnique({ where: { id: mostDispatchedStationIds[0]?.missionStationIds[0], }, }); } const totalDispatchedMissions = await prisma.mission.count({ where: { createdUserId: user?.id, }, }); const totalDispoTime = dispoSessions.reduce((acc, session) => { const logoffTime = new Date(session.logoutTime!).getTime(); const logonTime = new Date(session.loginTime).getTime(); return acc + (logoffTime - logonTime); }, 0); const hours = Math.floor(totalDispoTime / (1000 * 60 * 60)); const minutes = Math.floor((totalDispoTime % (1000 * 60 * 60)) / (1000 * 60)); return (
Einsätze disponiert
{totalDispatchedMissions}
Disponent Login Zeit
{hours}h {minutes}m
{mostDispatchedStation && (
{mostDispatchedStation?.bosCallsign}
Wurde von dir am meisten Disponiert
{mostDispatchedStationIds[0]?._count.missionStationIds} Einsätze
)}
); }; export const Stats = async ({ stats }: { stats: "pilot" | "dispo" }) => { const session = await getServerSession(); if (!session) return null; return ( <> {session.user.permissions.includes("DISPO") && }
{stats === "dispo" && } {stats === "pilot" && }
); };