Mercurial > public > geoquiz
comparison GeoQuiz/Controllers/CountryGameController.swift @ 34:6ec51a4ca897
fix crash when asking new questions
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sat, 12 Nov 2022 14:23:05 +0100 |
parents | 6d574bd1644f |
children |
comparison
equal
deleted
inserted
replaced
33:6d574bd1644f | 34:6ec51a4ca897 |
---|---|
6 // | 6 // |
7 | 7 |
8 import Foundation | 8 import Foundation |
9 import AVFAudio | 9 import AVFAudio |
10 | 10 |
11 class CountryGameController: Game, ObservableObject { | 11 @MainActor class CountryGameController: Game, ObservableObject { |
12 | 12 |
13 // Define type of generics | 13 // Define generic type |
14 typealias T = CountryModel.Country | 14 typealias T = CountryModel.Country |
15 | 15 |
16 // Game | |
16 var data: [String: T] | 17 var data: [String: T] |
17 var dataAsked = [String: T]() | 18 var dataAsked = [String: T]() |
18 | |
19 @Published var correctAnswer = ( | |
20 key: String(), | |
21 value: T(flag: String(), currency: String(), population: Int(), capital: String()) | |
22 ) | |
23 | 19 |
24 // User | 20 // User |
25 @Published var userChoices = [String: T]() | 21 @Published var userChoices = [String: T]() |
26 @Published var userScore = 0 | 22 @Published var userScore = 0 |
27 @Published var userLives = 3 | 23 @Published var userLives = 3 |
24 | |
25 @Published var correctAnswer = (key: "", value: T(flag: "", currency: "", population: Int(), capital: "")) | |
28 @Published var correctAnswers = [String: T]() | 26 @Published var correctAnswers = [String: T]() |
29 @Published var wrongAnswers = [String: T]() | 27 @Published var wrongAnswers = [String: T]() |
30 | 28 |
31 // Alerts | 29 // Alerts |
32 @Published var alertTitle = String() | 30 @Published var alertTitle = String() |
33 @Published var alertMessage = String() | 31 @Published var alertMessage = String() |
32 | |
34 @Published var showingEndGameAlert = false | 33 @Published var showingEndGameAlert = false |
35 @Published var showingWrongAnswerAlert = false | 34 @Published var showingWrongAnswerAlert = false |
36 @Published var showingExitGameAlert = false | 35 @Published var showingExitGameAlert = false |
37 | 36 |
38 // Animations | 37 // Animations |
42 // Sound effects | 41 // Sound effects |
43 @Published var player: AVAudioPlayer? | 42 @Published var player: AVAudioPlayer? |
44 | 43 |
45 init() { | 44 init() { |
46 let data: CountryModel = Bundle.main.decode("countries.json") | 45 let data: CountryModel = Bundle.main.decode("countries.json") |
47 let shuffledCountries = data.countries.shuffled().prefix(5) | 46 let shuffledCountries = data.countries.shuffled().prefix(100) |
48 var countries = [String: T]() | 47 var countries = [String: T]() |
49 | 48 |
50 for shuffledCountry in shuffledCountries { | 49 for shuffledCountry in shuffledCountries { |
51 countries[shuffledCountry.key] = shuffledCountry.value | 50 countries[shuffledCountry.key] = shuffledCountry.value |
52 } | 51 } |
53 | 52 |
54 self.data = countries | 53 self.data = countries |
55 | |
56 print(countries) | |
57 | 54 |
58 let user = UserController() | 55 let user = UserController() |
59 userLives = user.data.numberOfLives | 56 userLives = user.data.numberOfLives |
60 | 57 |
61 if let userData = UserDefaults.standard.data(forKey: "UserData") { | 58 if let userData = UserDefaults.standard.data(forKey: "UserData") { |