1+ import * as v from "valibot" ;
12import type { AttemptEntity } from "../../database/schemas/attempt-entity" ;
23import type { Brand } from "../../_core/utils/brand" ;
34import { Coins } from "../../recognition/coins/coins" ;
@@ -7,6 +8,7 @@ import { Laps } from "../../recognition/laps/laps";
78import { Pause } from "../../recognition/pause/pause" ;
89import { Shrooms } from "../../recognition/shrooms/shrooms" ;
910import { Time } from "../../recognition/time/time" ;
11+ import { TimeSchema } from "../../database/schemas/time" ;
1012import { Track } from "../../recognition/track/track" ;
1113import { createAttemptHandler } from "../utils/attempt-handler" ;
1214import { 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" ;
0 commit comments