Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Arcade9// File : Games/TrafficMayhem/TrafficSave.swift10// Description : Traffic Mayhem — persisted progress: best scores per11// junction and mode, unlocks and tutorial state.12// Module : Games13// Repository : https://git.spboucher.ai/orix-arcade14// Created : 2026-11-2115// Last Modified : 2026-11-2116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author : Simon-Pierre Boucher22// Position : Professor of Finance23// Institution : Université du Québec en Outaouais (UQO)24// Department : Département des sciences administratives25// Organization : SP Boucher26//27// Website : https://spboucher.ai28// Git : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General : info@spboucher.ai35// Professional : contact@spboucher.ai36// Direct : simon@spboucher.ai37// Research : research@spboucher.ai38// Development : dev@spboucher.ai39// Support : support@spboucher.ai40// Security/Admin : admin@spboucher.ai41// Notifications : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification : Public68// Distribution : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License : Proprietary, all rights reserved (see license file)82// License File : LICENSE83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192nonisolated struct TrafficSave: Codable, Equatable, Sendable {93 /// Best final score keyed by "<layout>-<mode>".94 var best: [String: Int] = [:]95 /// Most vehicles cleared in one run, keyed by layout id (any mode). Drives junction unlocks.96 var bestCleared: [String: Int] = [:]97 var tutorialDone = false98 var lastLayout = 099 var lastMode = TrafficSim.Mode.endless.rawValue100 var totalCleared = 0101 var runs = 0102103 init() {}104105 init(from decoder: Decoder) throws {106 let c = try decoder.container(keyedBy: CodingKeys.self)107 best = try c.decodeIfPresent([String: Int].self, forKey: .best) ?? [:]108 bestCleared = try c.decodeIfPresent([String: Int].self, forKey: .bestCleared) ?? [:]109 tutorialDone = try c.decodeIfPresent(Bool.self, forKey: .tutorialDone) ?? false110 lastLayout = (try c.decodeIfPresent(Int.self, forKey: .lastLayout) ?? 0).clamped(0, TrafficLayout.all.count - 1)111 lastMode = try c.decodeIfPresent(String.self, forKey: .lastMode) ?? TrafficSim.Mode.endless.rawValue112 if TrafficSim.Mode(rawValue: lastMode) == nil { lastMode = TrafficSim.Mode.endless.rawValue }113 totalCleared = max(0, try c.decodeIfPresent(Int.self, forKey: .totalCleared) ?? 0)114 runs = max(0, try c.decodeIfPresent(Int.self, forKey: .runs) ?? 0)115 }116117 static func key(layout: Int, mode: TrafficSim.Mode) -> String { "\(layout)-\(mode.rawValue)" }118119 func best(layout: Int, mode: TrafficSim.Mode) -> Int { best[Self.key(layout: layout, mode: mode)] ?? 0 }120 func bestCleared(layout: Int) -> Int { bestCleared["\(layout)"] ?? 0 }121 var overallBest: Int { best.values.max() ?? 0 }122123 /// Junction 0 is always open; each later junction needs enough vehicles cleared in one run on the previous one.124 func isUnlocked(_ layout: Int) -> Bool {125 guard layout > 0 else { return true }126 guard layout < TrafficLayout.all.count, isUnlocked(layout - 1) else { return false }127 return bestCleared(layout: layout - 1) >= TrafficLayout.all[layout].unlockCleared128 }129130 var unlockedCount: Int { TrafficLayout.all.indices.filter(isUnlocked).count }131132 /// Stores a finished run. Returns whether the score is a new best and which junction (if any) it unlocked.133 mutating func record(layout: Int, mode: TrafficSim.Mode, score: Int, cleared: Int) -> (isRecord: Bool, unlocked: Int?) {134 let before = unlockedCount135 let key = Self.key(layout: layout, mode: mode)136 let isRecord = score > (best[key] ?? 0)137 if isRecord { best[key] = score }138 bestCleared["\(layout)"] = max(bestCleared(layout: layout), cleared)139 totalCleared += cleared140 runs += 1141 lastLayout = layout142 lastMode = mode.rawValue143 let after = unlockedCount144 return (isRecord, after > before ? after - 1 : nil)145 }146}147