Skip to content

Commit

Permalink
fix statisctis on main timer
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanlundberg committed Oct 24, 2023
1 parent 4e957b7 commit 601f91c
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 51 deletions.
10 changes: 5 additions & 5 deletions src/components/timer/OverviewPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTimerStatistics } from "@/hooks/useTimerStatistics";

export default function OverviewPanel() {
const { lang, settings } = useSettingsModalStore();
const statistics = useTimerStatistics();
const { session } = useTimerStatistics();

return (
<div className="flex flex-col justify-center w-full h-full">
Expand All @@ -14,22 +14,22 @@ export default function OverviewPanel() {
<div className="font-medium">
{translation.timer["deviation"][lang]}
{": "}
{formatTime(statistics.deviation)}
{formatTime(session.deviation)}
</div>
<div className="font-medium">
{translation.timer["mean"][lang]}
{": "}
{formatTime(statistics.mean)}
{formatTime(session.mean)}
</div>
<div className="font-medium">
{translation.timer["best"][lang]}
{": "}
{formatTime(statistics.best)}
{formatTime(session.best)}
</div>
<div className="font-medium">
{translation.timer["counter"][lang]}
{": "}
{statistics.count}
{session.count}
</div>
</>
) : null}
Expand Down
11 changes: 5 additions & 6 deletions src/components/timer/StatisticsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ import { useTimerStatistics } from "@/hooks/useTimerStatistics";

export default function StatisticsPanel() {
const { settings } = useSettingsModalStore();
const statistics = useTimerStatistics();
const { session } = useTimerStatistics();

return (
<>
<div className="flex flex-col justify-center w-full h-full">
{settings.features.sessionStats.status ? (
<>
<div className="font-medium text-right">
Ao5: {statistics.ao5 === 0 ? "--" : formatTime(statistics.ao5)}
Ao5: {session.ao5 === 0 ? "--" : formatTime(session.ao5)}
</div>
<div className="font-medium text-right">
Ao12: {statistics.ao12 === 0 ? "--" : formatTime(statistics.ao12)}
Ao12: {session.ao12 === 0 ? "--" : formatTime(session.ao12)}
</div>
<div className="font-medium text-right">
Ao50: {statistics.ao50 === 0 ? "--" : formatTime(statistics.ao50)}
Ao50: {session.ao50 === 0 ? "--" : formatTime(session.ao50)}
</div>
<div className="font-medium text-right">
Ao100:{" "}
{statistics.ao100 === 0 ? "--" : formatTime(statistics.ao100)}
Ao100: {session.ao100 === 0 ? "--" : formatTime(session.ao100)}
</div>
</>
) : null}
Expand Down
29 changes: 13 additions & 16 deletions src/hooks/useTimerStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@ import { useEffect, useState } from "react";

export function useTimerStatistics() {
const { scramble, selectedCube } = useTimerStore();
const [statistics, setStatistics] = useState(defaultTimerStatistics);
const [statistics, setStatistics] = useState({
global: defaultTimerStatistics,
session: defaultTimerStatistics,
cubeSession: defaultTimerStatistics,
});

useEffect(() => {
if (selectedCube) {
const { count, best, ao3, ao5, ao12, ao50, ao100, deviation, mean } =
calcStatistics({
cubeId: selectedCube.id,
});
setStatistics({
count,
best,
ao3,
ao5,
ao12,
ao50,
ao100,
deviation,
mean,
const { global, session, cubeSession } = calcStatistics({
cube: selectedCube,
});
setStatistics({ global, session, cubeSession });
}
}, [scramble, selectedCube]);

return statistics;
return {
global: statistics.global,
session: statistics.session,
cubeSession: statistics.cubeSession,
};
}
116 changes: 92 additions & 24 deletions src/lib/calcStatistics.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,114 @@
import { sort } from "fast-sort";
import findCube from "./findCube";
import calculateBestAo from "./calculateBestAo";
import calculateCurrentAo from "./calculateCurrentAo";
import getDeviation from "./getDeviation";
import getMean from "./getMean";
import getSolvesMetrics from "./getSolvesMetrics";
import { defaultTimerStatistics } from "./const/defaultTimerStatistics";
import { Cube } from "@/interfaces/Cube";

export default function calcStatistics({
cubeId,
}: {
cubeId: string;
}): CubeStatistics {
const cube = findCube({ cubeId: cubeId });

export default function calcStatistics({ cube }: { cube: Cube | null }) {
if (!cube) {
return defaultTimerStatistics;
return {
global: defaultTimerStatistics,
session: defaultTimerStatistics,
cubeSession: defaultTimerStatistics,
};
}

const sortedSolves = sort(cube.solves.session).asc((u) => u.time);
if (sortedSolves.length === 0) {
return defaultTimerStatistics;
}
const { global, session, cubeSession } = getSolvesMetrics(
cube.category,
cube.name
);

const aoValues: number[] = [3, 5, 12, 50, 100];

const globalDefault: CubeStatistics = {
count: 0,
best: 0,
ao3: 0,
ao5: 0,
ao12: 0,
ao50: 0,
ao100: 0,
deviation: getDeviation(cubeSession),
mean: getMean(cubeSession),
};

const sessionDefault: CubeStatistics = {
count: 0,
best: 0,
ao3: 0,
ao5: 0,
ao12: 0,
ao50: 0,
ao100: 0,
deviation: getDeviation(session),
mean: getMean(session),
};

const defaultResult: CubeStatistics = {
count: sortedSolves.length,
best: sortedSolves[0].time,
const cubeSessionDefault: CubeStatistics = {
count: 0,
best: 0,
ao3: 0,
ao5: 0,
ao12: 0,
ao50: 0,
ao100: 0,
deviation: getDeviation(sortedSolves),
mean: getMean(sortedSolves),
deviation: 0,
mean: getMean(cubeSession),
};

const aoValues = [3, 5, 12, 50, 100];
if (session.length > 0) {
const sessionPB = sort(session).asc((u) => u.time);
sessionDefault.count = session.length;
sessionDefault.best = sessionPB[0].time;
sessionDefault.deviation = getDeviation(session);
sessionDefault.mean = getMean(session);

for (const aoValue of aoValues) {
if (sortedSolves.length >= aoValue) {
defaultResult[`ao${aoValue}` as keyof CubeStatistics] =
calculateCurrentAo(sortedSolves, aoValue);
for (const aoValue of aoValues) {
if (session.length >= aoValue) {
sessionDefault[`ao${aoValue}` as keyof CubeStatistics] =
calculateCurrentAo(session, aoValue);
}
}
}

return defaultResult;
if (global.length > 0) {
const globalPB = sort(global).asc((u) => u.time);
globalDefault.count = global.length;
globalDefault.best = globalPB[0].time;
globalDefault.deviation = getDeviation(global);
globalDefault.mean = getMean(global);

for (const aoValue of aoValues) {
if (global.length >= aoValue) {
globalDefault[`ao${aoValue}` as keyof CubeStatistics] = calculateBestAo(
global,
aoValue
);
}
}
}

if (cubeSession.length > 0) {
const cubeSessionPB = sort(cubeSession).asc((u) => u.time);
cubeSessionDefault.count = cubeSession.length;
cubeSessionDefault.best = cubeSessionPB[0].time;
cubeSessionDefault.deviation = getDeviation(cubeSession);
cubeSessionDefault.mean = getMean(cubeSession);

for (const aoValue of aoValues) {
if (cubeSession.length >= aoValue) {
cubeSessionDefault[`ao${aoValue}` as keyof CubeStatistics] =
calculateCurrentAo(cubeSession, aoValue);
}
}
}

return {
global: globalDefault,
session: sessionDefault,
cubeSession: cubeSessionDefault,
};
}

0 comments on commit 601f91c

Please sign in to comment.