-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support really big scores
Support calculating really (really!) big scores by introducing mathjs to handle the final score compounding.
- Loading branch information
1 parent
9027ee4
commit a4b6dbe
Showing
43 changed files
with
310 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,22 +1,24 @@ | ||
import { add, BigNumber, bignumber, divide, floor, multiply, pow } from 'mathjs' | ||
|
||
import type { DeckName, Score } from './types.js' | ||
|
||
export function doBigMath (initialScore: Score, deck: DeckName) { | ||
const chips = initialScore.chips.reduce((totalChips, chips) => { | ||
return totalChips + chips | ||
}, 0) | ||
return add(totalChips, bignumber(chips)) | ||
}, bignumber(0)) | ||
const multiplier = initialScore.multiplier.reduce((totalMultiplier, [operation, multiplier]) => { | ||
return operation === '+' | ||
? totalMultiplier + multiplier | ||
: totalMultiplier * multiplier | ||
}, 0) | ||
const operator = operation === '+' ? add : multiply | ||
return operator(totalMultiplier, bignumber(multiplier)) | ||
}, bignumber(0)) | ||
|
||
let actualScore | ||
let actualScore: BigNumber | ||
if (deck === 'Plasma Deck') { | ||
actualScore = Math.pow((chips + multiplier) / 2, 2) | ||
actualScore = pow(divide(add(chips, multiplier), bignumber(2)), bignumber(2)) as BigNumber // mathjs type bug | ||
} else { | ||
actualScore = chips * multiplier | ||
actualScore = multiply(chips, multiplier) as BigNumber // mathjs type bug | ||
} | ||
|
||
// Balatro seems to round values starting at a certain threshold and it seems to round down. 🤔 | ||
return actualScore > 10000 ? Math.floor(actualScore) : actualScore | ||
const score = actualScore.greaterThan(10000) ? floor(actualScore) : actualScore | ||
return score.toString() | ||
} |
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,12 +1,22 @@ | ||
export function formatScore (score: number): string { | ||
if (score < 1_000_000_000_000) { | ||
return new Intl.NumberFormat('en', { maximumFractionDigits: 0 }).format(score) | ||
} else { | ||
const almostCorrect = new Intl.NumberFormat('en', { | ||
notation: 'scientific', | ||
maximumSignificantDigits: 4, | ||
}).format(score).toLowerCase() | ||
const [prefix, suffix] = almostCorrect.split('e') | ||
return prefix!.padEnd(5, '0') + 'e' + suffix! | ||
import { BigNumber, bignumber, divide, round } from 'mathjs' | ||
|
||
export function formatScore (score: string): string { | ||
if (score.includes('e+')) { | ||
const [prefix, suffix] = score.split('e+') as [string, string] | ||
const number = Number(prefix.substring(0, 6)) | ||
|
||
return `${number.toFixed(3)}e${suffix}` | ||
} | ||
|
||
const bigNumber = bignumber(score) | ||
if (bigNumber.lessThan(1_000_000_000_000)) { | ||
return new Intl.NumberFormat('en', { maximumFractionDigits: 0 }) | ||
.format(bigNumber.toNumber()) | ||
} | ||
|
||
const decimalValue = divide(bigNumber, bignumber(Math.pow(10, score.length - 1))) as BigNumber | ||
const roundedValue = round(decimalValue, 3).toString() | ||
const prefix = roundedValue + (roundedValue.includes('.') ? '' : '.') | ||
|
||
return `${prefix.padEnd(5, '0')}e${String(score.length - 1)}` | ||
} |
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
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
Oops, something went wrong.