Mercurial > public > geoquiz
annotate GeoQuiz/Controllers/CountryGameController.swift @ 30:eb23effeede7
add DatasetView
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 10 Nov 2022 11:51:52 +0100 |
parents | f5a2c2dab208 |
children | 6d574bd1644f |
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") |
30 | 48 let shuffledCountries = data.countries.shuffled().prefix(100) |
49 | |
50 var countries = [String: T]() | |
51 for shuffledCountry in shuffledCountries { | |
52 countries[shuffledCountry.key] = shuffledCountry.value | |
53 } | |
54 | |
55 self.data = countries | |
9 | 56 |
19 | 57 let user = UserController() |
14 | 58 userLives = user.data.numberOfLives |
10
a793f33f05fb
refactor code and fix layout
Dennis C. M. <dennis@denniscm.com>
parents:
9
diff
changeset
|
59 |
14 | 60 if let userData = UserDefaults.standard.data(forKey: "UserData") { |
19 | 61 if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) { |
14 | 62 userLives = decodedUserData.numberOfLives |
9 | 63 } |
64 } | |
65 | |
8 | 66 askQuestion { |
67 selector() | |
68 } | |
0 | 69 } |
70 } | |
8 | 71 |
19 | 72 extension CountryGameController { |
8 | 73 func selector() { |
74 | |
75 // Get random choices | |
76 var userChoices = [String: T]() | |
77 | |
78 while userChoices.count < 2 { | |
79 if let choice = data.randomElement() { | |
80 userChoices[choice.key] = choice.value | |
81 } else { | |
82 fatalError("Couldn't get a random value from data") | |
83 } | |
84 } | |
85 | |
28
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
86 // Get correct answer |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
87 let randomCountryKeys = data.keys.shuffled() |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
88 |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
89 let correctCountryKey = randomCountryKeys.first(where: { |
30 | 90 !userChoices.keys.contains($0) && // Avoid duplicated countries |
91 !dataAsked.keys.contains($0) // Avoid countries already asked | |
8 | 92 }) |
93 | |
28
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
94 // Unwrap correct answer |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
95 if let correctCountryKey = correctCountryKey { |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
96 let correctCountryValue = data[correctCountryKey]! |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
97 |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
98 userChoices[correctCountryKey] = correctCountryValue |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
99 dataAsked[correctCountryKey] = correctCountryValue |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
100 |
f51b70c2cccc
randomize country selection
Dennis C. M. <dennis@denniscm.com>
parents:
26
diff
changeset
|
101 let correctAnswer = (key: correctCountryKey, value: correctCountryValue) |
8 | 102 self.correctAnswer = correctAnswer |
103 } else { | |
104 fatalError("Couldn't unwrap optional value") | |
105 } | |
106 | |
107 self.userChoices = userChoices | |
108 } | |
109 } |