Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine AoX calculation and include test attributes #301

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"docwriter.progress.trackFunctions": true,
"docwriter.style": "JSDoc",
"docwriter.progress.trackClasses": true,
"docwriter.progress.trackTypes": true
"docwriter.progress.trackTypes": true,
"cSpell.words": ["testid"]
}
4 changes: 3 additions & 1 deletion src/app/solves/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function SolvesPage() {
handleGetDeleteData,
isOpenDeleteModal,
} = useSolvesPage();
const { selectedCube } = useTimerStore();
const { selectedCube, setTimerStatistics } = useTimerStore();
const { lang } = useSettingsModalStore();

return (
Expand Down Expand Up @@ -91,6 +91,7 @@ export default function SolvesPage() {
onConfirm={() => {
setIsOpenMoveModal(false);
handleMoveAll();
setTimerStatistics();
}}
data={handleGetMoveData}
/>
Expand All @@ -101,6 +102,7 @@ export default function SolvesPage() {
onConfirm={() => {
setIsOpenDeleteModal(false);
handleTrashAll();
setTimerStatistics();
}}
data={handleGetDeleteData}
/>
Expand Down
13 changes: 10 additions & 3 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Cube } from "@/interfaces/Cube";

export default function Select() {
const [open, setOpen] = useState<boolean>(false);
const { selectedCube, cubes } = useTimerStore();
const { selectedCube, cubes, setTimerStatistics } = useTimerStore();
const { lang } = useSettingsModalStore();
const componentRef = useRef<HTMLDivElement | null>(null);
const { backgroundImage } = useBackgroundImageStore();
Expand Down Expand Up @@ -138,8 +138,14 @@ function Option({
cubeId: string;
handleClose: () => void;
}) {
const { selectedCube, setSelectedCube, setNewScramble, setLastSolve, cubes } =
useTimerStore();
const {
selectedCube,
setSelectedCube,
setNewScramble,
setLastSolve,
cubes,
setTimerStatistics,
} = useTimerStore();

return (
<div
Expand All @@ -148,6 +154,7 @@ function Option({
const selectCube = cubes?.find((cube: Cube) => cube.id === cubeId);
if (!selectCube) return;
setSelectedCube(selectCube);
setTimerStatistics();
setNewScramble(selectCube);
setLastSolve(null);
}
Expand Down
10 changes: 8 additions & 2 deletions src/components/cubes/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import { getAllCubes, saveCube } from "@/db/dbOperations";
export default function TableRow({ cube }: { cube: Cube }) {
const { lang } = useSettingsModalStore();
const router = useRouter();
const { setSelectedCube, setNewScramble, setLastSolve, setCubes } =
useTimerStore();
const {
setSelectedCube,
setNewScramble,
setLastSolve,
setCubes,
setTimerStatistics,
} = useTimerStore();
const { setEditingCube, setModalOpen, setCubeName, setSelectedCategory } =
useCubesModalStore();
const setFavorite = async (cube: Cube) => {
Expand Down Expand Up @@ -42,6 +47,7 @@ export default function TableRow({ cube }: { cube: Cube }) {
const divIndex = Array.from(e.currentTarget.children).indexOf(targetDiv);
if (divIndex > 0 && divIndex < e.currentTarget.children.length - 1) {
setSelectedCube(cube);
setTimerStatistics();
setNewScramble(cube);
setLastSolve(null);
router.push("/");
Expand Down
3 changes: 2 additions & 1 deletion src/components/menu-settings/ImportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getAllCubes } from "@/db/dbOperations";
import translation from "@/translations/global.json";
export default function ImportModal() {
const { setImportModalOpen, importModalOpen, lang } = useSettingsModalStore();
const { setSelectedCube, setCubes } = useTimerStore();
const { setSelectedCube, setCubes, setTimerStatistics } = useTimerStore();
const [isImporting, setIsImporting] = useState(false);
const dataInputRef = useRef<HTMLInputElement>(null);
const componentRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -53,6 +53,7 @@ export default function ImportModal() {
setCubes(cubesDB);
router.push("/cubes");
setSelectedCube(null);
setTimerStatistics();
setImportModalOpen(false);
alert(
`${translation.backup["alert-category"][lang]}`
Expand Down
16 changes: 12 additions & 4 deletions src/components/timer/OverviewPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,30 @@ export default function OverviewPanel() {
<div className="font-medium">
{translation.timer["deviation"][lang]}
{": "}
{formatTime(timerStatistics.session.deviation)}
<span data-testid="timer-session-deviation">
{formatTime(timerStatistics.session.deviation)}
</span>
</div>
<div className="font-medium">
{translation.timer["mean"][lang]}
{": "}
{formatTime(timerStatistics.session.mean)}
<span data-testid="timer-session-mean">
{formatTime(timerStatistics.session.mean)}
</span>
</div>
<div className="font-medium">
{translation.timer["best"][lang]}
{": "}
{formatTime(timerStatistics.session.best)}
<span data-testid="timer-session-best">
{formatTime(timerStatistics.session.best)}
</span>
</div>
<div className="font-medium">
{translation.timer["counter"][lang]}
{": "}
{timerStatistics.session.count}
<span data-testid="timer-session-count">
{timerStatistics.session.count}
</span>
</div>
</>
) : null}
Expand Down
32 changes: 20 additions & 12 deletions src/components/timer/StatisticsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export default function StatisticsPanel() {
}`}
>
Ao5:{" "}
{timerStatistics.session.ao5 === 0
? "--"
: formatTime(timerStatistics.session.ao5)}
<span data-testid={"timer-session-ao5"}>
{timerStatistics.session.ao5 === 0
? "--"
: formatTime(timerStatistics.session.ao5)}
</span>
</div>
</div>
<div className="flex justify-end w-full font-medium text-right">
Expand All @@ -43,9 +45,11 @@ export default function StatisticsPanel() {
}`}
>
Ao12:{" "}
{timerStatistics.session.ao12 === 0
? "--"
: formatTime(timerStatistics.session.ao12)}
<span data-testid={"timer-session-ao12"}>
{timerStatistics.session.ao12 === 0
? "--"
: formatTime(timerStatistics.session.ao12)}
</span>
</div>
</div>
<div className="flex justify-end w-full font-medium text-right">
Expand All @@ -60,9 +64,11 @@ export default function StatisticsPanel() {
}`}
>
Ao50:{" "}
{timerStatistics.session.ao50 === 0
? "--"
: formatTime(timerStatistics.session.ao50)}
<span data-testid={"timer-session-ao50"}>
{timerStatistics.session.ao50 === 0
? "--"
: formatTime(timerStatistics.session.ao50)}
</span>
</div>
</div>
<div className="flex justify-end w-full font-medium text-right">
Expand All @@ -77,9 +83,11 @@ export default function StatisticsPanel() {
}`}
>
Ao100:{" "}
{timerStatistics.session.ao100 === 0
? "--"
: formatTime(timerStatistics.session.ao100)}
<span data-testid={"timer-session-ao100"}>
{timerStatistics.session.ao100 === 0
? "--"
: formatTime(timerStatistics.session.ao100)}
</span>
</div>
</div>
</>
Expand Down
12 changes: 10 additions & 2 deletions src/hooks/useModalCube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ export default function useModalCube() {
} = useCubesModalStore();

const { lang } = useSettingsModalStore();
const { setCubes, setSelectedCube, setNewScramble, selectedCube, cubes } =
useTimerStore();
const {
setCubes,
setSelectedCube,
setNewScramble,
selectedCube,
cubes,
setTimerStatistics,
} = useTimerStore();
const [error, setError] = useState<boolean>(false);
const [isDuplicate, setDuplicate] = useState<boolean>(false);
const [showDeleteConfirmation, setShowDeleteConfirmation] = useState(false);
Expand Down Expand Up @@ -95,6 +101,7 @@ export default function useModalCube() {

if (editingCube.id === selectedCube?.id) {
setSelectedCube(null);
setTimerStatistics();
}

setModalOpen(false);
Expand Down Expand Up @@ -145,6 +152,7 @@ export default function useModalCube() {
if (selectedCube && selectedCube.id === editingCube.id) {
setSelectedCube(null);
setNewScramble(null);
setTimerStatistics();
}

await deleteCubeById(editingCube.id);
Expand Down
22 changes: 17 additions & 5 deletions src/lib/calculateBestAo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Solve } from "@/interfaces/Solve";
* @returns {number} The best average of X (AoX) for the given solves.
*/
export default function calculateBestAo(solves: Solve[], ao: number): number {
// If the number of solves is less than the desired average length, return 0
// If the number of solves is less than the desired average, return 0
if (!solves || solves.length < ao) {
return 0;
}
Expand All @@ -17,14 +17,26 @@ export default function calculateBestAo(solves: Solve[], ao: number): number {

for (let i = 0; i <= n - ao; i++) {
let sum = 0;
let minTime = Infinity;
let maxTime = -Infinity;

// Calculate the sum of solve times directly in the loop
// Calculate the sum of solve times and find the min and max time in the range
for (let j = i; j < i + ao; j++) {
sum += solves[j].time;
const time = solves[j].time;
sum += time;
if (time < minTime) {
minTime = time;
}
if (time > maxTime) {
maxTime = time;
}
}

// Calculate the current AoX and update the bestAo if needed
const currentAo = sum / ao;
// Adjust the sum by removing the min and max time
sum -= minTime + maxTime;

// Calculate the adjusted average
const currentAo = sum / (ao - 2);
bestAo = Math.min(bestAo, currentAo);
}

Expand Down
Loading