diff --git a/CasinoKit/Sources/CasinoKit/Models/CasinoGameState.swift b/CasinoKit/Sources/CasinoKit/Models/CasinoGameState.swift index 7ca36a8..5ec1b00 100644 --- a/CasinoKit/Sources/CasinoKit/Models/CasinoGameState.swift +++ b/CasinoKit/Sources/CasinoKit/Models/CasinoGameState.swift @@ -140,8 +140,10 @@ public extension CasinoGameState { /// 5. Save data func performResetGame() { // 1. End current session FIRST while balance still shows actual state + // Use .brokeOut if balance is 0, otherwise .manualEnd if currentSession != nil { - endCurrentSession(reason: .manualEnd) + let reason: SessionEndReason = balance <= 0 ? .brokeOut : .manualEnd + endCurrentSession(reason: reason) } // 2. Reset balance to starting balance diff --git a/CasinoKit/Sources/CasinoKit/Models/Session/SessionManager.swift b/CasinoKit/Sources/CasinoKit/Models/Session/SessionManager.swift index b3c270d..203e157 100644 --- a/CasinoKit/Sources/CasinoKit/Models/Session/SessionManager.swift +++ b/CasinoKit/Sources/CasinoKit/Models/Session/SessionManager.swift @@ -75,12 +75,23 @@ extension SessionManagedGame { } /// Starts a new session. + /// + /// This uses the current `balance` as the session's starting balance. If you need to + /// start fresh with the default starting balance from settings, call `endSessionAndStartNew()` + /// instead, which properly resets the balance before creating the new session. public func startNewSession() { // End current session if exists if currentSession != nil { endCurrentSession(reason: .manualEnd) } + // If balance is 0 or below minimum bet, reset to starting balance from settings. + // This handles the case where the player went broke and the app was restarted + // before they clicked "Play Again". + if balance <= 0 { + balance = startingBalance + } + // Create new session with current settings currentSession = GameSession( gameStyle: currentGameStyle,