ensured all defaults set with default settings

Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2025-12-31 15:32:49 -06:00
parent 1c28e43e07
commit 9aa646e89d
3 changed files with 44 additions and 8 deletions

View File

@ -191,7 +191,7 @@ final class GameSettings: GameSettingsProtocol {
} }
if let balance = defaults.object(forKey: Keys.startingBalance) as? Int { if let balance = defaults.object(forKey: Keys.startingBalance) as? Int {
self.startingBalance = balance self.startingBalance = Self.validatedStartingBalance(balance)
} }
if defaults.object(forKey: Keys.showAnimations) != nil { if defaults.object(forKey: Keys.showAnimations) != nil {
@ -199,7 +199,7 @@ final class GameSettings: GameSettingsProtocol {
} }
if let speed = defaults.object(forKey: Keys.dealingSpeed) as? Double { if let speed = defaults.object(forKey: Keys.dealingSpeed) as? Double {
self.dealingSpeed = speed self.dealingSpeed = Self.validatedDealingSpeed(speed)
} }
if defaults.object(forKey: Keys.showCardsRemaining) != nil { if defaults.object(forKey: Keys.showCardsRemaining) != nil {
@ -242,7 +242,7 @@ final class GameSettings: GameSettingsProtocol {
} }
if let balance = store.object(forKey: Keys.startingBalance) as? Int { if let balance = store.object(forKey: Keys.startingBalance) as? Int {
self.startingBalance = balance self.startingBalance = Self.validatedStartingBalance(balance)
} }
if store.object(forKey: Keys.showAnimations) != nil { if store.object(forKey: Keys.showAnimations) != nil {
@ -250,7 +250,7 @@ final class GameSettings: GameSettingsProtocol {
} }
if let speed = store.object(forKey: Keys.dealingSpeed) as? Double { if let speed = store.object(forKey: Keys.dealingSpeed) as? Double {
self.dealingSpeed = speed self.dealingSpeed = Self.validatedDealingSpeed(speed)
} }
if store.object(forKey: Keys.showCardsRemaining) != nil { if store.object(forKey: Keys.showCardsRemaining) != nil {
@ -311,6 +311,24 @@ final class GameSettings: GameSettingsProtocol {
} }
} }
/// Validates a dealing speed value, returning normal if invalid.
/// This handles legacy data that may have been saved with incorrect values.
private static func validatedDealingSpeed(_ speed: Double) -> Double {
let validSpeeds = [
CasinoDesign.DealingSpeed.fast,
CasinoDesign.DealingSpeed.normal,
CasinoDesign.DealingSpeed.slow
]
return validSpeeds.contains(speed) ? speed : CasinoDesign.DealingSpeed.normal
}
/// Validates a starting balance value, returning default if invalid.
/// This handles legacy data that may have been saved with incorrect values.
private static func validatedStartingBalance(_ balance: Int) -> Int {
let validBalances = [1_000, 5_000, 10_000, 25_000, 50_000, 100_000]
return validBalances.contains(balance) ? balance : 1_000
}
/// Resets all settings to defaults. /// Resets all settings to defaults.
func resetToDefaults() { func resetToDefaults() {
deckCount = .eight deckCount = .eight

View File

@ -228,7 +228,7 @@ final class GameSettings: GameSettingsProtocol {
self.tableLimits = limits self.tableLimits = limits
} }
self.startingBalance = data.startingBalance self.startingBalance = Self.validatedStartingBalance(data.startingBalance)
self.dealerHitsSoft17 = data.dealerHitsSoft17 self.dealerHitsSoft17 = data.dealerHitsSoft17
self.doubleAfterSplit = data.doubleAfterSplit self.doubleAfterSplit = data.doubleAfterSplit
self.resplitAces = data.resplitAces self.resplitAces = data.resplitAces
@ -238,7 +238,7 @@ final class GameSettings: GameSettingsProtocol {
self.insuranceAllowed = data.insuranceAllowed self.insuranceAllowed = data.insuranceAllowed
self.neverAskInsurance = data.neverAskInsurance self.neverAskInsurance = data.neverAskInsurance
self.showAnimations = data.showAnimations self.showAnimations = data.showAnimations
self.dealingSpeed = data.dealingSpeed self.dealingSpeed = Self.validatedDealingSpeed(data.dealingSpeed)
self.showCardsRemaining = data.showCardsRemaining self.showCardsRemaining = data.showCardsRemaining
self.showHistory = data.showHistory self.showHistory = data.showHistory
self.showHints = data.showHints self.showHints = data.showHints
@ -276,6 +276,24 @@ final class GameSettings: GameSettingsProtocol {
persistence.save(data) persistence.save(data)
} }
/// Validates a dealing speed value, returning normal if invalid.
/// This handles legacy data that may have been saved with incorrect values.
private static func validatedDealingSpeed(_ speed: Double) -> Double {
let validSpeeds = [
CasinoDesign.DealingSpeed.fast,
CasinoDesign.DealingSpeed.normal,
CasinoDesign.DealingSpeed.slow
]
return validSpeeds.contains(speed) ? speed : CasinoDesign.DealingSpeed.normal
}
/// Validates a starting balance value, returning default if invalid.
/// This handles legacy data that may have been saved with incorrect values.
private static func validatedStartingBalance(_ balance: Int) -> Int {
let validBalances = [1_000, 5_000, 10_000, 25_000, 50_000, 100_000]
return validBalances.contains(balance) ? balance : 1_000
}
/// Resets all settings to defaults. /// Resets all settings to defaults.
func resetToDefaults() { func resetToDefaults() {
gameStyle = .vegas gameStyle = .vegas

View File

@ -52,12 +52,12 @@ struct BlackjackSettingsData: PersistableGameData {
roundsPlayed: 0, roundsPlayed: 0,
gameStyle: "vegas", gameStyle: "vegas",
deckCount: 6, deckCount: 6,
tableLimits: "low", tableLimits: "casual",
startingBalance: 1_000, startingBalance: 1_000,
dealerHitsSoft17: false, dealerHitsSoft17: false,
doubleAfterSplit: true, doubleAfterSplit: true,
resplitAces: false, resplitAces: false,
lateSurrender: true, lateSurrender: false,
noHoleCard: false, noHoleCard: false,
blackjackPayout: 1.5, blackjackPayout: 1.5,
insuranceAllowed: true, insuranceAllowed: true,