Skip to content

Commit baed706

Browse files
authored
Merge pull request #82 from derechtenap/0.2.1/bugfix-impossible-score-state
Add helper function to detect busts for different checkout types
2 parents 3bf6c62 + 5256f7e commit baed706

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

renderer/lib/playing/stats/isBust.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Checkout } from "types/match";
2+
3+
/**
4+
* Checks if the current throw results in a bust situation based on the remaining score.
5+
* A bust occurs when a player attempts to throw a score that would exceed the remaining score or violates
6+
* specific throw rules (e.g., attempting a double with a score less than 2).
7+
*
8+
* @param {Checkout} checkout - The type of checkout being attempted. Can be "Any", "Single", "Double" or "Triple".
9+
* @param {number} remainingScore - The remaining score to be reduced. Should be a positive number.
10+
* @returns {boolean} - Returns `true` if it's a bust, `false` otherwise.
11+
*
12+
* @example
13+
* isBust("Double", 1); // returns true (because a Double requires at least 2 points)
14+
* isBust("Triple", 2); // returns true (because a Triple requires at least 3 points)
15+
* isBust("Double", 4); // returns false (valid Double checkout with remaining score of 4)
16+
*/
17+
const isBust = (checkout: Checkout, remainingScore: number): boolean => {
18+
if (remainingScore < 0) return true;
19+
20+
if (checkout === "Double" && remainingScore < 2) return true;
21+
if (checkout === "Triple" && remainingScore < 3) return true;
22+
23+
return false;
24+
};
25+
26+
export default isBust;

renderer/pages/[locale]/match/playing.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { useElapsedTime } from "use-elapsed-time";
6161
import { modals } from "@mantine/modals";
6262
import addMatchToDatabase from "@/lib/db/matches/addMatch";
6363
import getFirstNineAverage from "@/lib/playing/stats/getFirstNineAverage";
64+
import isBust from "@/lib/playing/stats/isBust";
6465

6566
const PlayingPage: NextPage = () => {
6667
const theme = useMantineTheme();
@@ -229,7 +230,7 @@ const PlayingPage: NextPage = () => {
229230

230231
const updatedMatchRound: MatchRound = {
231232
elapsedTime: elapsedTime,
232-
isBust: newScoreLeft < 0 ? true : false,
233+
isBust: isBust(checkout, newScoreLeft),
233234
roundAverage:
234235
matchRound.length > 0 ? totalRoundScore / matchRound.length : 0,
235236
roundTotal: totalRoundScore,
@@ -239,7 +240,6 @@ const PlayingPage: NextPage = () => {
239240
? matchRound
240241
: filledThrowDetails.slice(0, THROWS_PER_ROUND),
241242
};
242-
243243
const updatedCurrentPlayer: Player = {
244244
...currentPlayer,
245245
rounds: [...currentPlayer.rounds, updatedMatchRound],
@@ -248,7 +248,8 @@ const PlayingPage: NextPage = () => {
248248
scoreLeft:
249249
isWinner && newScoreLeft === 0
250250
? 0
251-
: newScoreLeft > 0
251+
: // Update score only if >0 and is not a bust!
252+
newScoreLeft > 0 && !isBust(checkout, newScoreLeft)
252253
? newScoreLeft
253254
: currentPlayer.scoreLeft,
254255
};

0 commit comments

Comments
 (0)