diff GeoQuiz/Logic/CountryGameController.swift @ 19:f140bb277c96

refactor code
author Dennis C. M. <dennis@denniscm.com>
date Sun, 23 Oct 2022 00:11:38 +0100
parents GeoQuiz/Logic/CountryGameClass.swift@f1967f8cc67b
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GeoQuiz/Logic/CountryGameController.swift	Sun Oct 23 00:11:38 2022 +0100
@@ -0,0 +1,96 @@
+//
+//  CountryGameController.swift
+//  GeoQuiz
+//
+//  Created by Dennis Concepción Martín on 20/9/22.
+//
+
+import Foundation
+import AVFAudio
+
+class CountryGameController: Game, ObservableObject {
+    
+    // Define type of generics
+    typealias T = CountryModel.Country
+    
+    var data: [String: T]
+    var dataAsked = [String: T]()
+    
+    // Data
+    @Published var correctAnswer = (
+        key: String(),
+        value: T(flag: String(), currency: String(), population: Int(), capital: String())
+    )
+    
+    // User
+    @Published var userChoices = [String: T]()
+    @Published var userScore = 0
+    @Published var userLives = 3
+    @Published var correctAnswers = [String: T]()
+    @Published var wrongAnswers = [String: T]()
+    
+    // Alerts
+    @Published var alertTitle = String()
+    @Published var alertMessage = String()
+    @Published var showingEndGameAlert = false
+    @Published var showingWrongAnswerAlert = false
+    @Published var showingExitGameAlert = false
+    
+    // Animations
+    @Published var scoreScaleAmount = 1.0
+    @Published var livesScaleAmount = 1.0
+    
+    // Sound effects
+    @Published var player: AVAudioPlayer?
+    
+    init() {
+        let data: CountryModel = FileController.load("countries.json")
+        self.data = data.countries
+        
+        let user = UserController()
+        userLives = user.data.numberOfLives
+        
+        if let userData = UserDefaults.standard.data(forKey: "UserData") {
+            if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) {
+                userLives = decodedUserData.numberOfLives
+            }
+        }
+        
+        askQuestion {
+            selector()
+        }
+    }
+}
+
+extension CountryGameController {
+    func selector() {
+        
+        // Get random choices
+        var userChoices = [String: T]()
+        
+        while userChoices.count < 2 {
+            if let choice = data.randomElement() {
+                userChoices[choice.key] = choice.value
+            } else {
+                fatalError("Couldn't get a random value from data")
+            }
+        }
+        
+        // Get question asked (correct answer)
+        let correctAnswer = data.first(where: {
+            !userChoices.keys.contains($0.key) &&  // Avoid duplicated countries
+            !dataAsked.keys.contains($0.key)       // Avoid countries already asked
+        })
+        
+        // Unwrap optional
+        if let correctAnswer = correctAnswer {
+            userChoices[correctAnswer.key] = correctAnswer.value
+            dataAsked[correctAnswer.key] = correctAnswer.value
+            self.correctAnswer = correctAnswer
+        } else {
+            fatalError("Couldn't unwrap optional value")
+        }
+        
+        self.userChoices = userChoices
+    }
+}