Skip to content

Commit

Permalink
refactor(core): inline modifier scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinfreund committed Mar 24, 2024
1 parent 7c00d11 commit afa69f9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 85 deletions.
109 changes: 77 additions & 32 deletions src/lib/balatro.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RANK_TO_CHIP_MAP, MODIFIER_DEFAULTS, PLAYED_CARD_RETRIGGER_JOKER_NAMES, HELD_CARD_RETRIGGER_JOKER_NAMES } from '#lib/data.js'
import type { Card, Joker, JokerCardEffect, JokerEffect, Modifier, Result, Score, State } from '#lib/types.js'
import { RANK_TO_CHIP_MAP, PLAYED_CARD_RETRIGGER_JOKER_NAMES, HELD_CARD_RETRIGGER_JOKER_NAMES } from '#lib/data.js'
import type { Card, Joker, JokerCardEffect, JokerEffect, Result, Score, State } from '#lib/types.js'
import { formatScore } from '#utilities/formatScore.js'
import { log, logGroup, logGroupEnd } from '#utilities/log.js'
import { isFaceCard } from '#utilities/isFaceCard.js'
Expand Down Expand Up @@ -60,14 +60,56 @@ function getScore (state: State): Score {
for (const [index, trigger] of triggers.entries()) {
log(`Trigger: ${index + 1} (${trigger})`)

// 1. Rank
if (card.enhancement !== 'stone') {
score.chips += RANK_TO_CHIP_MAP[card.rank]
log(score, '(+Chips from rank)')
}

scoreModifiers(score, MODIFIER_DEFAULTS.playedEnhancement[card.enhancement])
scoreModifiers(score, MODIFIER_DEFAULTS.edition[card.edition])
// 2. Enhancement
switch (card.enhancement) {
case 'stone': {
score.chips += 50
log(score, '(+Chips from stone enhancement)')
break
}
case 'bonus': {
score.chips += 30
log(score, '(+Chips from bonus enhancement)')
break
}
case 'mult': {
score.multiplier += 4
log(score, '(+Mult from mult enhancement)')
break
}
case 'glass': {
score.multiplier *= 2
log(score, '(xMult from glass enhancement)')
break
}
}

// 3. Edition
switch (card.edition) {
case 'foil': {
score.chips += 50
log(score, '(+Chips from foil edition)')
break
}
case 'holographic': {
score.multiplier += 10
log(score, '(+Mult from holographic edition)')
break
}
case 'polychrome': {
score.multiplier *= 1.5
log(score, '(xMult from polychrome edition)')
break
}
}

// 4. Joker effects for played cards
for (const joker of state.jokers) {
scoreJokerCardEffect(joker.playedCardEffect, { state, score, joker, card })
}
Expand All @@ -91,8 +133,16 @@ function getScore (state: State): Score {
for (const [index, trigger] of triggers.entries()) {
log(`Trigger: ${index + 1} (${trigger})`)

scoreModifiers(score, MODIFIER_DEFAULTS.heldEnhancement[card.enhancement])
// 1. Enhancement
switch (card.enhancement) {
case 'steel': {
score.multiplier *= 1.5
log(score, '(xMult from steel enhancement)')
break
}
}

// 2. Joker effects for held cards
for (const joker of state.jokers) {
scoreJokerCardEffect(joker.heldCardEffect, { state, score, joker, card })
log(score, `(${joker})`)
Expand All @@ -106,8 +156,29 @@ function getScore (state: State): Score {
log('\n3. Scoring jokers …')
for (const joker of state.jokers) {
logGroup(`\n→ ${joker}`, score)

// 1. EDITION
switch (joker.edition) {
case 'foil': {
score.chips += 50
log(score, '(+Chips from foil edition)')
break
}
case 'holographic': {
score.multiplier += 10
log(score, '(+Mult from holographic edition)')
break
}
case 'polychrome': {
score.multiplier *= 1.5
log(score, '(xMult from polychrome edition)')
break
}
}

// 2. JOKER EFFECTS
scoreJokerEffect(joker.effect, { state, score, joker })
scoreModifiers(score, MODIFIER_DEFAULTS.edition[joker.edition])

logGroupEnd(`← ${joker}`, score)
}
log('\n3. JOKER SCORE =>', score)
Expand Down Expand Up @@ -187,32 +258,6 @@ function getHeldCardTriggers ({ state, card }: { state: State, card: Card }): st
return triggers
}

function scoreModifiers (score: Score, modifier: Modifier) {
const {
plusChips,
plusMultiplier,
timesMultiplier,
} = modifier

// 1. +Chips
if (plusChips !== undefined) {
score.chips += plusChips
log(score, '(+Chips from edition)')
}

// 2. +Mult
if (plusMultiplier !== undefined) {
score.multiplier += plusMultiplier
log(score, '(+Mult from edition)')
}

// 3. xMult
if (timesMultiplier !== undefined) {
score.multiplier *= timesMultiplier
log(score, '(xMult from edition)')
}
}

function scoreJokerEffect (effect: JokerEffect | undefined, { state, score, joker }: { state: State, score: Score, joker: Joker }) {
const triggers = ['Regular']

Expand Down
41 changes: 1 addition & 40 deletions src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { flush, nOfAKind, straight } from '#lib/getHand.js'
import { isFaceCard } from '#utilities/isFaceCard.js'
import { isRank } from '#utilities/isRank.js'
import { isSuit } from '#utilities/isSuit.js'
import type { BlindName, DeckName, Edition, Enhancement, HandName, JokerDefinition, JokerEdition, JokerName, ModifierDefaults, Rank, Score, Seal, Suit } from '#lib/types.js'
import type { BlindName, DeckName, Edition, Enhancement, HandName, JokerDefinition, JokerEdition, JokerName, Rank, Score, Seal, Suit } from '#lib/types.js'

export const BLINDS: BlindName[] = ['Small Blind', 'Big Blind', 'The Hook', 'The Ox', 'The House', 'The Wall', 'The Wheel', 'The Arm', 'The Club', 'The Fish', 'The Psychic', 'The Goad', 'The Water', 'The Window', 'The Manacle', 'The Eye', 'The Mouth', 'The Plant', 'The Serpent', 'The Pillar', 'The Needle', 'The Head', 'The Tooth', 'The Flint', 'The Mark', 'Amber Acorn', 'Verdant Leaf', 'Violet Vessel', 'Crimson Heart', 'Cerulean Bell']

Expand All @@ -18,45 +18,6 @@ export const JOKER_EDITIONS: JokerEdition[] = ['base', 'foil', 'holographic', 'p
export const RANKS: Rank[] = ['Ace', 'King', 'Queen', 'Jack', '10', '9', '8', '7', '6', '5', '4', '3', '2']
export const SUITS: Suit[] = ['Clubs', 'Spades', 'Hearts', 'Diamonds']

export const MODIFIER_DEFAULTS: ModifierDefaults = {
edition: {
base: {},
foil: { plusChips: 50 },
holographic: { plusMultiplier: 10 },
polychrome: { timesMultiplier: 1.5 },
negative: {},
},
playedEnhancement: {
none: {},
bonus: { plusChips: 30 },
mult: { plusMultiplier: 4 },
wild: {},
glass: { timesMultiplier: 2 },
steel: {},
stone: { plusChips: 50 },
gold: {},
lucky: {},
},
heldEnhancement: {
none: {},
bonus: {},
mult: {},
wild: {},
glass: {},
steel: { timesMultiplier: 1.5 },
stone: {},
gold: {},
lucky: {},
},
seal: {
none: {},
gold: {},
red: { timesMultiplier: 2 },
blue: {},
purple: {},
},
}

export const PLANET_SCORE_SETS: Record<HandName, Score> = {
'Flush Five': { chips: 40, multiplier: 3 },
'Flush House': { chips: 40, multiplier: 3 },
Expand Down
13 changes: 0 additions & 13 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ export interface Score {
multiplier: number
}

export interface ModifierDefaults {
edition: Record<JokerEdition, Modifier>
playedEnhancement: Record<Enhancement, Modifier>
heldEnhancement: Record<Enhancement, Modifier>
seal: Record<Seal, Modifier>
}

export interface Modifier {
plusChips?: number
plusMultiplier?: number
timesMultiplier?: number
}

export interface InitialCard {
rank: Rank
suit: Suit
Expand Down

0 comments on commit afa69f9

Please sign in to comment.