Mercurial > public > geoquiz
annotate 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 |
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 |
34
6ec51a4ca897
fix crash when asking new questions
Dennis C. M. <dennis@denniscm.com>
parents:
33
diff
changeset
|
11 @MainActor class CountryGameController: Game, ObservableObject { |
6 | 12 |
34
6ec51a4ca897
fix crash when asking new questions
Dennis C. M. <dennis@denniscm.com>
parents:
33
diff
changeset
|
13 // Define generic type |
19 | 14 typealias T = CountryModel.Country |
6 | 15 |
34
6ec51a4ca897
fix crash when asking new questions
Dennis C. M. <dennis@denniscm.com>
parents:
33
diff
changeset
|
16 // Game |
6 | 17 var data: [String: T] |
18 var dataAsked = [String: T]() | |
0 | 19 |
3 | 20 // User |
6 | 21 @Published var userChoices = [String: T]() |
0 | 22 @Published var userScore = 0 |
23 @Published var userLives = 3 | |
34
6ec51a4ca897
fix crash when asking new questions
Dennis C. M. <dennis@denniscm.com>
parents:
33
diff
changeset
|
24 |
6ec51a4ca897
fix crash when asking new questions
Dennis C. M. <dennis@denniscm.com>
parents:
33
diff
changeset
|
25 @Published var correctAnswer = (key: "", value: T(flag: "", currency: "", population: Int(), capital: "")) |
6 | 26 @Published var correctAnswers = [String: T]() |
27 @Published var wrongAnswers = [String: T]() | |
3 | 28 |
29 // Alerts | |
30 @Published var alertTitle = String() | |
31 @Published var alertMessage = String() | |
34
6ec51a4ca897
fix crash when asking new questions
Dennis C. M. <dennis@denniscm.com>
parents:
33
diff
changeset
|
32 |
3 | 33 @Published var showingEndGameAlert = false |
0 | 34 @Published var showingWrongAnswerAlert = false |
4 | 35 @Published var showingExitGameAlert = false |
0 | 36 |
3 | 37 // Animations |
38 @Published var scoreScaleAmount = 1.0 | |
39 @Published var livesScaleAmount = 1.0 | |
40 | |
5 | 41 // Sound effects |
42 @Published var player: AVAudioPlayer? | |
43 | |
0 | 44 init() { |
26 | 45 let data: CountryModel = Bundle.main.decode("countries.json") |
34
6ec51a4ca897
fix crash when asking new questions
Dennis C. M. <dennis@denniscm.com>
parents:
33
diff
changeset
|
46 let shuffledCountries = data.countries.shuffled().prefix(100) |
33 | 47 var countries = [String: T]() |
30 | 48 |
49 for shuffledCountry in shuffledCountries { | |
50 countries[shuffledCountry.key] = shuffledCountry.value | |
51 } | |
52 | |
53 self.data = countries | |
9 | 54 |
19 | 55 let user = UserController() |
14 | 56 userLives = user.data.numberOfLives |
10
a793f33f05fb
refactor code and fix layout
Dennis C. M. <dennis@denniscm.com>
parents:
9
diff
changeset
|
57 |
14 | 58 if let userData = UserDefaults.standard.data(forKey: "UserData") { |
19 | 59 if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) { |
14 | 60 userLives = decodedUserData.numberOfLives |
9 | 61 } |
62 } | |
63 | |
33 | 64 ask() |
0 | 65 } |
66 } |