Mercurial > public > geoquiz
annotate GeoQuiz/Controllers/CityGameController.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 // CityGameController.swift |
0 | 3 // GeoQuiz |
4 // | |
6 | 5 // Created by Dennis Concepción Martín on 29/9/22. |
0 | 6 // |
7 | |
8 import Foundation | |
5 | 9 import AVFAudio |
0 | 10 |
19 | 11 class CityGameController: Game, ObservableObject { |
6 | 12 |
3 | 13 // Define type of generics |
19 | 14 typealias T = CityModel.City |
6 | 15 |
16 var data: [String: T] | |
17 var dataAsked = [String: T]() | |
0 | 18 |
8 | 19 // Data |
20 @Published var correctAnswer = ( | |
21 key: String(), | |
22 value: T(country: String(), lat: Double(), lon: Double()) | |
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: CityModel = Bundle.main.decode("cities.json") |
30 | 48 let shuffledCities = data.cities.shuffled().prefix(100) |
49 | |
50 var cities = [String: T]() | |
51 for shuffledCity in shuffledCities { | |
52 cities[shuffledCity.key] = shuffledCity.value | |
53 } | |
54 | |
55 self.data = cities | |
10
a793f33f05fb
refactor code and fix layout
Dennis C. M. <dennis@denniscm.com>
parents:
9
diff
changeset
|
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 |
10
a793f33f05fb
refactor code and fix layout
Dennis C. M. <dennis@denniscm.com>
parents:
9
diff
changeset
|
63 } |
a793f33f05fb
refactor code and fix layout
Dennis C. M. <dennis@denniscm.com>
parents:
9
diff
changeset
|
64 } |
a793f33f05fb
refactor code and fix layout
Dennis C. M. <dennis@denniscm.com>
parents:
9
diff
changeset
|
65 |
8 | 66 askQuestion { |
67 selector() | |
68 } | |
0 | 69 } |
70 } | |
7 | 71 |
19 | 72 extension CityGameController { |
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 let userChoicesCountry = userChoices.map { $0.value.country } | |
81 | |
82 if !userChoicesCountry.contains(choice.value.country) { | |
83 userChoices[choice.key] = choice.value | |
84 } | |
85 } else { | |
86 fatalError("Couldn't get a random value from data") | |
7 | 87 } |
88 } | |
8 | 89 |
30 | 90 // Get correct answer |
91 let randomCityKeys = data.keys.shuffled() | |
8 | 92 let userChoicesCountry = userChoices.map { $0.value.country } |
30 | 93 |
94 let correctCityKey = randomCityKeys.first(where: { | |
95 !userChoices.keys.contains($0) && // Avoid duplicated cities | |
96 !dataAsked.keys.contains($0) && // Avoid cities already asked | |
97 !userChoicesCountry.contains(data[$0]!.country) // Avoid duplicated country names in userChoices | |
8 | 98 }) |
30 | 99 |
8 | 100 // Unwrap optional |
30 | 101 if let correctCityKey = correctCityKey { |
102 let correctCityValue = data[correctCityKey]! | |
103 | |
104 userChoices[correctCityKey] = correctCityValue | |
105 dataAsked[correctCityKey] = correctCityValue | |
106 | |
107 let correctAnswer = (key: correctCityKey, value: correctCityValue) | |
8 | 108 self.correctAnswer = correctAnswer |
109 } else { | |
110 fatalError("Couldn't unwrap optional value") | |
111 } | |
112 | |
113 self.userChoices = userChoices | |
7 | 114 } |
115 } | |
116 |