Mercurial > public > geoquiz
annotate 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 |
rev | line source |
---|---|
0 | 1 // |
19 | 2 // CountryGameController.swift |
0 | 3 // GeoQuiz |
4 // | |
5 // Created by Dennis Concepción Martín on 20/9/22. | |
6 // | |
7 | |
8 import Foundation | |
5 | 9 import AVFAudio |
0 | 10 |
19 | 11 class CountryGameController: Game, ObservableObject { |
6 | 12 |
3 | 13 // Define type of generics |
19 | 14 typealias T = CountryModel.Country |
6 | 15 |
16 var data: [String: T] | |
17 var dataAsked = [String: T]() | |
0 | 18 |
3 | 19 // Data |
6 | 20 @Published var correctAnswer = ( |
21 key: String(), | |
22 value: T(flag: String(), currency: String(), population: Int(), capital: String()) | |
23 ) | |
3 | 24 |
25 // User | |
6 | 26 @Published var userChoices = [String: T]() |
0 | 27 @Published var userScore = 0 |
28 @Published var userLives = 3 | |
6 | 29 @Published var correctAnswers = [String: T]() |
30 @Published var wrongAnswers = [String: T]() | |
3 | 31 |
32 // Alerts | |
33 @Published var alertTitle = String() | |
34 @Published var alertMessage = String() | |
35 @Published var showingEndGameAlert = false | |
0 | 36 @Published var showingWrongAnswerAlert = false |
4 | 37 @Published var showingExitGameAlert = false |
0 | 38 |
3 | 39 // Animations |
40 @Published var scoreScaleAmount = 1.0 | |
41 @Published var livesScaleAmount = 1.0 | |
42 | |
5 | 43 // Sound effects |
44 @Published var player: AVAudioPlayer? | |
45 | |
0 | 46 init() { |
26 | 47 let data: CountryModel = Bundle.main.decode("countries.json") |
6 | 48 self.data = data.countries |
9 | 49 |
19 | 50 let user = UserController() |
14 | 51 userLives = user.data.numberOfLives |
10
a793f33f05fb
refactor code and fix layout
Dennis C. M. <dennis@denniscm.com>
parents:
9
diff
changeset
|
52 |
14 | 53 if let userData = UserDefaults.standard.data(forKey: "UserData") { |
19 | 54 if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) { |
14 | 55 userLives = decodedUserData.numberOfLives |
9 | 56 } |
57 } | |
58 | |
8 | 59 askQuestion { |
60 selector() | |
61 } | |
0 | 62 } |
63 } | |
8 | 64 |
19 | 65 extension CountryGameController { |
8 | 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 | |
28
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
79 // Get correct answer |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
80 let randomCountryKeys = data.keys.shuffled() |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
81 |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
82 let correctCountryKey = randomCountryKeys.first(where: { |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
83 !userChoices.keys.contains($0) && |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
84 !dataAsked.keys.contains($0) |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
85 |
8 | 86 }) |
87 | |
28
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
88 // Unwrap correct answer |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
89 if let correctCountryKey = correctCountryKey { |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
90 let correctCountryValue = data[correctCountryKey]! |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
91 |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
92 userChoices[correctCountryKey] = correctCountryValue |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
93 dataAsked[correctCountryKey] = correctCountryValue |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
94 |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
95 let correctAnswer = (key: correctCountryKey, value: correctCountryValue) |
8 | 96 self.correctAnswer = correctAnswer |
97 } else { | |
98 fatalError("Couldn't unwrap optional value") | |
99 } | |
100 | |
101 self.userChoices = userChoices | |
102 } | |
103 } |