Mercurial > public > geoquiz
comparison GeoQuiz/Controllers/CountryGameController.swift @ 33:6d574bd1644f
refactor controllers
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sat, 12 Nov 2022 11:18:30 +0100 |
parents | eb23effeede7 |
children | 6ec51a4ca897 |
comparison
equal
deleted
inserted
replaced
32:c62d6f92709d | 33:6d574bd1644f |
---|---|
14 typealias T = CountryModel.Country | 14 typealias T = CountryModel.Country |
15 | 15 |
16 var data: [String: T] | 16 var data: [String: T] |
17 var dataAsked = [String: T]() | 17 var dataAsked = [String: T]() |
18 | 18 |
19 // Data | |
20 @Published var correctAnswer = ( | 19 @Published var correctAnswer = ( |
21 key: String(), | 20 key: String(), |
22 value: T(flag: String(), currency: String(), population: Int(), capital: String()) | 21 value: T(flag: String(), currency: String(), population: Int(), capital: String()) |
23 ) | 22 ) |
24 | 23 |
43 // Sound effects | 42 // Sound effects |
44 @Published var player: AVAudioPlayer? | 43 @Published var player: AVAudioPlayer? |
45 | 44 |
46 init() { | 45 init() { |
47 let data: CountryModel = Bundle.main.decode("countries.json") | 46 let data: CountryModel = Bundle.main.decode("countries.json") |
48 let shuffledCountries = data.countries.shuffled().prefix(100) | 47 let shuffledCountries = data.countries.shuffled().prefix(5) |
48 var countries = [String: T]() | |
49 | 49 |
50 var countries = [String: T]() | |
51 for shuffledCountry in shuffledCountries { | 50 for shuffledCountry in shuffledCountries { |
52 countries[shuffledCountry.key] = shuffledCountry.value | 51 countries[shuffledCountry.key] = shuffledCountry.value |
53 } | 52 } |
54 | 53 |
55 self.data = countries | 54 self.data = countries |
55 | |
56 print(countries) | |
56 | 57 |
57 let user = UserController() | 58 let user = UserController() |
58 userLives = user.data.numberOfLives | 59 userLives = user.data.numberOfLives |
59 | 60 |
60 if let userData = UserDefaults.standard.data(forKey: "UserData") { | 61 if let userData = UserDefaults.standard.data(forKey: "UserData") { |
61 if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) { | 62 if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) { |
62 userLives = decodedUserData.numberOfLives | 63 userLives = decodedUserData.numberOfLives |
63 } | 64 } |
64 } | 65 } |
65 | 66 |
66 askQuestion { | 67 ask() |
67 selector() | |
68 } | |
69 } | 68 } |
70 } | 69 } |
71 | |
72 extension CountryGameController { | |
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 | |
86 // Get correct answer | |
87 let randomCountryKeys = data.keys.shuffled() | |
88 | |
89 let correctCountryKey = randomCountryKeys.first(where: { | |
90 !userChoices.keys.contains($0) && // Avoid duplicated countries | |
91 !dataAsked.keys.contains($0) // Avoid countries already asked | |
92 }) | |
93 | |
94 // Unwrap correct answer | |
95 if let correctCountryKey = correctCountryKey { | |
96 let correctCountryValue = data[correctCountryKey]! | |
97 | |
98 userChoices[correctCountryKey] = correctCountryValue | |
99 dataAsked[correctCountryKey] = correctCountryValue | |
100 | |
101 let correctAnswer = (key: correctCountryKey, value: correctCountryValue) | |
102 self.correctAnswer = correctAnswer | |
103 } else { | |
104 fatalError("Couldn't unwrap optional value") | |
105 } | |
106 | |
107 self.userChoices = userChoices | |
108 } | |
109 } |