Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>

This commit is contained in:
Matt Bruce 2025-12-29 13:51:26 -06:00
parent f1b834c47e
commit 2a55a16227
3 changed files with 43 additions and 23 deletions

View File

@ -929,20 +929,16 @@ final class GameState: SessionManagedGame {
} }
} }
// MARK: - Session History Management // MARK: - Session Cleanup Hooks
/// Deletes a session from history by ID. /// Clean up round history when deleting a session.
func deleteSession(id: UUID) { func onWillDeleteSession(id: UUID) {
sessionHistory.removeAll { $0.id == id }
sessionRoundHistories.removeValue(forKey: id.uuidString) sessionRoundHistories.removeValue(forKey: id.uuidString)
saveGameData()
} }
/// Deletes all session history. /// Clean up all round histories when deleting all sessions.
func deleteAllSessionHistory() { func onWillDeleteAllSessions() {
sessionHistory.removeAll()
sessionRoundHistories.removeAll() sessionRoundHistories.removeAll()
saveGameData()
} }
// MARK: - Session Round History // MARK: - Session Round History

View File

@ -1204,20 +1204,6 @@ final class GameState: SessionManagedGame {
allSessions.aggregatedBlackjackStats() allSessions.aggregatedBlackjackStats()
} }
// MARK: - Session History Management
/// Deletes a session from history by ID.
func deleteSession(id: UUID) {
sessionHistory.removeAll { $0.id == id }
saveGameData()
}
/// Deletes all session history.
func deleteAllSessionHistory() {
sessionHistory.removeAll()
saveGameData()
}
// MARK: - Game Reset // MARK: - Game Reset
/// Resets the entire game (keeps statistics). /// Resets the entire game (keeps statistics).

View File

@ -44,11 +44,23 @@ public protocol SessionManagedGame: AnyObject {
/// Current game style identifier (e.g., "vegas", "european"). /// Current game style identifier (e.g., "vegas", "european").
var currentGameStyle: String { get } var currentGameStyle: String { get }
/// Whether the end session confirmation dialog should be shown.
/// Games should bind this to their confirmation dialog.
var showEndSessionConfirmation: Bool { get set }
/// Called to persist game data after session changes. /// Called to persist game data after session changes.
func saveGameData() func saveGameData()
/// Called when starting a new session to reset game-specific state. /// Called when starting a new session to reset game-specific state.
func resetForNewSession() func resetForNewSession()
/// Called before a session is deleted. Override to clean up associated data.
/// Default implementation does nothing.
func onWillDeleteSession(id: UUID)
/// Called before all session history is deleted. Override to clean up associated data.
/// Default implementation does nothing.
func onWillDeleteAllSessions()
} }
// MARK: - Default Session Management Implementation // MARK: - Default Session Management Implementation
@ -170,6 +182,32 @@ extension SessionManagedGame {
public func aggregatedStats(forStyle style: String) -> AggregatedSessionStats { public func aggregatedStats(forStyle style: String) -> AggregatedSessionStats {
sessions(forStyle: style).aggregatedStats() sessions(forStyle: style).aggregatedStats()
} }
// MARK: - Session History Management
/// Default implementation - does nothing. Override to clean up session-specific data.
public func onWillDeleteSession(id: UUID) {
// Override in game to clean up associated data (e.g., round histories)
}
/// Default implementation - does nothing. Override to clean up all session data.
public func onWillDeleteAllSessions() {
// Override in game to clean up associated data (e.g., round histories)
}
/// Deletes a session from history by ID.
public func deleteSession(id: UUID) {
onWillDeleteSession(id: id)
sessionHistory.removeAll { $0.id == id }
saveGameData()
}
/// Deletes all session history.
public func deleteAllSessionHistory() {
onWillDeleteAllSessions()
sessionHistory.removeAll()
saveGameData()
}
} }
// MARK: - Session Formatter // MARK: - Session Formatter