SPB Git

Repository Public License

SaveData.swift 4.6 KB 114 lines swift

1// ORIX Arcade2// File: Persistence/SaveData.swift3// Description: Versioned, Codable save model — settings, statistics, achievements, resume marker and the three per-game saves.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89nonisolated enum EffectsQuality: String, Codable, Sendable, CaseIterable {10    case auto, high, low11}1213nonisolated struct ArcadeSettings: Codable, Equatable, Sendable {14    var musicOn = true15    var sfxOn = true16    var musicVolume = 0.617    var sfxVolume = 0.8518    var hapticsOn = true19    /// 0 disables screen shake, 1 is full strength.20    var shakeIntensity = 0.721    var effects = EffectsQuality.auto22    var reduceMotion = false23    var highContrast = false24    var leftHanded = false2526    init() {}2728    init(from decoder: Decoder) throws {29        let c = try decoder.container(keyedBy: CodingKeys.self)30        let d = ArcadeSettings()31        musicOn = try c.decodeIfPresent(Bool.self, forKey: .musicOn) ?? d.musicOn32        sfxOn = try c.decodeIfPresent(Bool.self, forKey: .sfxOn) ?? d.sfxOn33        musicVolume = (try c.decodeIfPresent(Double.self, forKey: .musicVolume) ?? d.musicVolume).clamped(0, 1)34        sfxVolume = (try c.decodeIfPresent(Double.self, forKey: .sfxVolume) ?? d.sfxVolume).clamped(0, 1)35        hapticsOn = try c.decodeIfPresent(Bool.self, forKey: .hapticsOn) ?? d.hapticsOn36        shakeIntensity = (try c.decodeIfPresent(Double.self, forKey: .shakeIntensity) ?? d.shakeIntensity).clamped(0, 1)37        effects = try c.decodeIfPresent(EffectsQuality.self, forKey: .effects) ?? d.effects38        reduceMotion = try c.decodeIfPresent(Bool.self, forKey: .reduceMotion) ?? d.reduceMotion39        highContrast = try c.decodeIfPresent(Bool.self, forKey: .highContrast) ?? d.highContrast40        leftHanded = try c.decodeIfPresent(Bool.self, forKey: .leftHanded) ?? d.leftHanded41    }42}4344nonisolated struct SessionRecord: Codable, Equatable, Sendable, Identifiable {45    var id = UUID()46    var game: GameID47    var date: Date48    var score: Int49    var duration: TimeInterval50    /// Short human summary, e.g. "Level 4 · 3 stars" or "412 m deep".51    var headline: String52    var isRecord: Bool53}5455nonisolated struct ArcadeStats: Codable, Equatable, Sendable {56    var playTime: [String: TimeInterval] = [:]57    var sessionCount: [String: Int] = [:]58    var recent: [SessionRecord] = []5960    static let recentLimit = 406162    var totalPlayTime: TimeInterval { playTime.values.reduce(0, +) }63    var totalSessions: Int { sessionCount.values.reduce(0, +) }6465    init() {}6667    init(from decoder: Decoder) throws {68        let c = try decoder.container(keyedBy: CodingKeys.self)69        playTime = try c.decodeIfPresent([String: TimeInterval].self, forKey: .playTime) ?? [:]70        sessionCount = try c.decodeIfPresent([String: Int].self, forKey: .sessionCount) ?? [:]71        recent = Array((try c.decodeIfPresent([SessionRecord].self, forKey: .recent) ?? []).prefix(Self.recentLimit))72    }73}7475/// Written when a game is opened and cleared on a normal exit, so the lobby can offer to jump back in.76nonisolated struct InterruptedSession: Codable, Equatable, Sendable {77    var game: GameID78    var date: Date79    var note: String?80}8182nonisolated struct SaveData: Codable, Equatable, Sendable {83    static let currentSchema = 18485    var schemaVersion = SaveData.currentSchema86    var settings = ArcadeSettings()87    var stats = ArcadeStats()88    var achievements: [String: Date] = [:]89    var interrupted: InterruptedSession?90    var hasSeenWelcome = false91    var domino = DominoSave()92    var dig = DigSave()93    var traffic = TrafficSave()9495    init() {}9697    init(from decoder: Decoder) throws {98        let c = try decoder.container(keyedBy: CodingKeys.self)99        schemaVersion = try c.decodeIfPresent(Int.self, forKey: .schemaVersion) ?? SaveData.currentSchema100        settings = try c.decodeIfPresent(ArcadeSettings.self, forKey: .settings) ?? ArcadeSettings()101        stats = try c.decodeIfPresent(ArcadeStats.self, forKey: .stats) ?? ArcadeStats()102        achievements = try c.decodeIfPresent([String: Date].self, forKey: .achievements) ?? [:]103        interrupted = try c.decodeIfPresent(InterruptedSession.self, forKey: .interrupted)104        hasSeenWelcome = try c.decodeIfPresent(Bool.self, forKey: .hasSeenWelcome) ?? false105        domino = try c.decodeIfPresent(DominoSave.self, forKey: .domino) ?? DominoSave()106        dig = try c.decodeIfPresent(DigSave.self, forKey: .dig) ?? DigSave()107        traffic = try c.decodeIfPresent(TrafficSave.self, forKey: .traffic) ?? TrafficSave()108    }109}110111extension Comparable {112    nonisolated func clamped(_ low: Self, _ high: Self) -> Self { min(max(self, low), high) }113}114
Files main
Menu

Theme