-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e957b7
commit 601f91c
Showing
4 changed files
with
115 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |