Skip to content

Commit

Permalink
feat(core): improve logging to show current totals
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinfreund committed Jan 18, 2025
1 parent cf7772d commit 482dd9b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
22 changes: 1 addition & 21 deletions src/lib/calculateScore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { log } from '#utilities/log.ts'
import { notNullish } from '#utilities/notNullish.ts'
import { RANK_TO_CHIP_MAP, PLAYED_CARD_RETRIGGER_JOKER_NAMES, HELD_CARD_RETRIGGER_JOKER_NAMES, LUCKS } from './data.ts'
import { balanceMultWithLuck } from './balanceMultWithLuck.ts'
Expand Down Expand Up @@ -64,7 +63,7 @@ function getScore (state: State, playedHand: HandName, scoringCards: Card[], luc
// The Flint halves the base chips and multiplier.
const baseFactor = (state.blind.name === 'The Flint' && state.blind.active ? 0.5 : 1)
// The base score seems to be rounded here.
const score = createScoreProxy<ScoreValue[]>([])
const score: ScoreValue[] = []
score.push({
chips: ['+', Math.round(baseScore.chips * baseFactor)],
phase: 'base',
Expand Down Expand Up @@ -266,25 +265,6 @@ function getScore (state: State, playedHand: HandName, scoringCards: Card[], luc
return score
}

/**
* Create a proxy for the array holding the individual score values that also prints logs for each score-affecting change.
*/
function createScoreProxy<T extends ScoreValue[]> (value: T) {
return new Proxy<T>(value, {
get (target, prop) {
if (prop === 'push') {
return (...scoreValues: Parameters<typeof target.push>) => {
for (const scoreValue of scoreValues) {
log(scoreValue)
}
return target.push(...scoreValues)
}
}
return Reflect.get(target, prop)
},
})
}

function getPlayedCardTriggers ({ state, card, index }: { state: State, card: Card, index: number }): string[] {
const triggers = ['Regular']

Expand Down
6 changes: 6 additions & 0 deletions src/lib/doBigMath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { add, BigNumber, bignumber, divide, floor, multiply, pow } from 'mathjs'

import { log } from '#utilities/log.ts'
import type { DeckName, ScoreValue } from './types.ts'

export function doBigMath (initialScore: ScoreValue[], deck: DeckName) {
Expand All @@ -16,6 +17,11 @@ export function doBigMath (initialScore: ScoreValue[], deck: DeckName) {
const operation = operator === '+' ? add : multiply
multiplier = operation(multiplier, bignumber(value))
}

log(scoreValue, {
chips: chips.toString(),
multiplier: multiplier.toString(),
})
}

let actualScore: BigNumber
Expand Down
7 changes: 6 additions & 1 deletion src/utilities/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { ScoreValue } from '#lib/types.ts'

export const log = import.meta.env?.VITE_DEBUG === 'true' ? logFn : () => undefined

function logFn ({ chips, multiplier, phase, card, joker, type, trigger }: ScoreValue): void {
function logFn (
{ chips, multiplier, phase, card, joker, type, trigger }: ScoreValue,
currentScoreValue: { chips: string, multiplier: string },
): void {
let valueStr = ''
if (chips) {
const [operator, value] = chips
Expand Down Expand Up @@ -40,5 +43,7 @@ function logFn ({ chips, multiplier, phase, card, joker, type, trigger }: ScoreV
str += ` (trigger: ${trigger})`
}

str += ` → ${currentScoreValue.chips}×${currentScoreValue.multiplier}`

console.log(str)
}

0 comments on commit 482dd9b

Please sign in to comment.