Mercurial > public > geoquiz
comparison GeoQuiz/Controllers/CountryGameController.swift @ 29:f5a2c2dab208
fix files structure
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 10 Nov 2022 10:27:28 +0100 |
parents | GeoQuiz/Models/Controllers/CountryGameController.swift@f51b70c2cccc |
children | eb23effeede7 |
comparison
equal
deleted
inserted
replaced
28:f51b70c2cccc | 29:f5a2c2dab208 |
---|---|
1 // | |
2 // CountryGameController.swift | |
3 // GeoQuiz | |
4 // | |
5 // Created by Dennis Concepción Martín on 20/9/22. | |
6 // | |
7 | |
8 import Foundation | |
9 import AVFAudio | |
10 | |
11 class CountryGameController: Game, ObservableObject { | |
12 | |
13 // Define type of generics | |
14 typealias T = CountryModel.Country | |
15 | |
16 var data: [String: T] | |
17 var dataAsked = [String: T]() | |
18 | |
19 // Data | |
20 @Published var correctAnswer = ( | |
21 key: String(), | |
22 value: T(flag: String(), currency: String(), population: Int(), capital: String()) | |
23 ) | |
24 | |
25 // User | |
26 @Published var userChoices = [String: T]() | |
27 @Published var userScore = 0 | |
28 @Published var userLives = 3 | |
29 @Published var correctAnswers = [String: T]() | |
30 @Published var wrongAnswers = [String: T]() | |
31 | |
32 // Alerts | |
33 @Published var alertTitle = String() | |
34 @Published var alertMessage = String() | |
35 @Published var showingEndGameAlert = false | |
36 @Published var showingWrongAnswerAlert = false | |
37 @Published var showingExitGameAlert = false | |
38 | |
39 // Animations | |
40 @Published var scoreScaleAmount = 1.0 | |
41 @Published var livesScaleAmount = 1.0 | |
42 | |
43 // Sound effects | |
44 @Published var player: AVAudioPlayer? | |
45 | |
46 init() { | |
47 let data: CountryModel = Bundle.main.decode("countries.json") | |
48 self.data = data.countries | |
49 | |
50 let user = UserController() | |
51 userLives = user.data.numberOfLives | |
52 | |
53 if let userData = UserDefaults.standard.data(forKey: "UserData") { | |
54 if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) { | |
55 userLives = decodedUserData.numberOfLives | |
56 } | |
57 } | |
58 | |
59 askQuestion { | |
60 selector() | |
61 } | |
62 } | |
63 } | |
64 | |
65 extension CountryGameController { | |
66 func selector() { | |
67 | |
68 // Get random choices | |
69 var userChoices = [String: T]() | |
70 | |
71 while userChoices.count < 2 { | |
72 if let choice = data.randomElement() { | |
73 userChoices[choice.key] = choice.value | |
74 } else { | |
75 fatalError("Couldn't get a random value from data") | |
76 } | |
77 } | |
78 | |
79 // Get correct answer | |
80 let randomCountryKeys = data.keys.shuffled() | |
81 | |
82 let correctCountryKey = randomCountryKeys.first(where: { | |
83 !userChoices.keys.contains($0) && | |
84 !dataAsked.keys.contains($0) | |
85 | |
86 }) | |
87 | |
88 // Unwrap correct answer | |
89 if let correctCountryKey = correctCountryKey { | |
90 let correctCountryValue = data[correctCountryKey]! | |
91 | |
92 userChoices[correctCountryKey] = correctCountryValue | |
93 dataAsked[correctCountryKey] = correctCountryValue | |
94 | |
95 let correctAnswer = (key: correctCountryKey, value: correctCountryValue) | |
96 self.correctAnswer = correctAnswer | |
97 } else { | |
98 fatalError("Couldn't unwrap optional value") | |
99 } | |
100 | |
101 self.userChoices = userChoices | |
102 } | |
103 } |