Skip to content

Commit efa5a6b

Browse files
committed
fix: open the switch right menu (keep pressing the home button) should not break the application
1 parent fc31c03 commit efa5a6b

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/domains/attempt/compositions/use-attempt-manager.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as v from "valibot";
12
import type { AttemptEntity } from "../../database/schemas/attempt-entity";
23
import type { Brand } from "../../_core/utils/brand";
34
import { Coins } from "../../recognition/coins/coins";
@@ -7,6 +8,7 @@ import { Laps } from "../../recognition/laps/laps";
78
import { Pause } from "../../recognition/pause/pause";
89
import { Shrooms } from "../../recognition/shrooms/shrooms";
910
import { Time } from "../../recognition/time/time";
11+
import { TimeSchema } from "../../database/schemas/time";
1012
import { Track } from "../../recognition/track/track";
1113
import { createAttemptHandler } from "../utils/attempt-handler";
1214
import { isDefined } from "remeda";
@@ -83,13 +85,20 @@ function useAttemptManagerFactory() {
8385
const isPause = Pause.isPause(image, putImageData);
8486
const isNotEqualToLastSplit = !attempt.isEqualToLastSplit(time);
8587
const isFinished = isFinalTime(time, isPause, rate);
86-
87-
// If you restart a game during the last lap you could have false detection, hence the conditions on the last lap.
88-
// The bump should not be problematic in this context (we rely on the fact that pause trigger a 1 as lap)
89-
if (isNotEqualToLastSplit && isFinished && attempt.isRawLastLap(lap)) {
90-
attempt.addFinalSplit({
88+
const finalSplit = attempt.getFinalSplit(time);
89+
90+
// If you restart a game during the last lap, you could have false detection, hence the conditions on the last lap.
91+
// The bump should not be problematic in this context (we rely on the fact that pause triggers a 1 recognized as the current lap).
92+
// Moreover, it is possible that finalSplit to be negative (=not valid TimeSchema) if you keep pressed on the home button to display the side menu.
93+
if (
94+
isNotEqualToLastSplit &&
95+
isFinished &&
96+
attempt.isRawLastLap(lap) &&
97+
v.safeParse(TimeSchema, finalSplit).success
98+
) {
99+
attempt.addSplit({
91100
shrooms: shrooms,
92-
time: time,
101+
time: finalSplit,
93102
coins: coins,
94103
});
95104
state = "WAITING_ATTEMPT";

src/domains/attempt/utils/attempt-handler.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ export function createAttemptHandlerFactory(pTrack: string, pRawLaps: string) {
2424
});
2525
};
2626

27-
const addFinalSplit = (rawSplit: RawSplit): void => {
27+
const getFinalSplit = (time: string): string => {
2828
// The final raw split has the particularity that the time is not the split time, but the total time.
29-
const totalTime = Time.parse(rawSplit.time);
29+
const totalTime = Time.parse(time);
3030
const splitTime = splits.reduce((acc, split): number => acc - Time.parse(split.time), totalTime);
3131

32-
addSplit({
33-
...rawSplit,
34-
time: Time.format(splitTime),
35-
});
32+
return Time.format(splitTime);
3633
};
3734

3835
const isEqualToLastSplit = (time: string): boolean => {
@@ -66,7 +63,7 @@ export function createAttemptHandlerFactory(pTrack: string, pRawLaps: string) {
6663

6764
return {
6865
addSplit,
69-
addFinalSplit,
66+
getFinalSplit,
7067
isEqualToLastSplit,
7168
isLastLap,
7269
isRawLastLap,

0 commit comments

Comments
 (0)