Mercurial > public > geoquiz
comparison GeoQuiz/Logic/CountryGameClass.swift @ 10:a793f33f05fb
refactor code and fix layout
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sat, 08 Oct 2022 21:36:40 +0200 |
parents | GeoQuiz/Logic/CountryGame.swift@3540c7efc216 |
children | 136928bae534 |
comparison
equal
deleted
inserted
replaced
9:3540c7efc216 | 10:a793f33f05fb |
---|---|
1 // | |
2 // CountryGameClass.swift | |
3 // GeoQuiz | |
4 // | |
5 // Created by Dennis Concepción Martín on 20/9/22. | |
6 // | |
7 | |
8 import Foundation | |
9 import AVFAudio | |
10 | |
11 class CountryGame: Game, ObservableObject { | |
12 | |
13 // Define type of generics | |
14 typealias T = CountryData.Country | |
15 | |
16 var data: [String: T] | |
17 var dataAsked = [String: T]() | |
18 | |
19 // Data | |
20 @Published var correctAnswer = ( | |
21 key: String(), | |
22 value: T(flag: String(), currency: String(), population: Int(), capital: String()) | |
23 ) | |
24 | |
25 // User | |
26 @Published var userChoices = [String: T]() | |
27 @Published var userScore = 0 | |
28 @Published var userLives = 3 | |
29 @Published var correctAnswers = [String: T]() | |
30 @Published var wrongAnswers = [String: T]() | |
31 | |
32 // Alerts | |
33 @Published var alertTitle = String() | |
34 @Published var alertMessage = String() | |
35 @Published var showingGameOverAlert = false | |
36 @Published var showingEndGameAlert = false | |
37 @Published var showingWrongAnswerAlert = false | |
38 @Published var showingExitGameAlert = false | |
39 | |
40 // Animations | |
41 @Published var scoreScaleAmount = 1.0 | |
42 @Published var livesScaleAmount = 1.0 | |
43 | |
44 // Sound effects | |
45 @Published var player: AVAudioPlayer? | |
46 | |
47 init() { | |
48 let data: CountryData = load("countries.json") | |
49 self.data = data.countries | |
50 | |
51 let user = User() | |
52 userLives = user.settings.numberOfLives | |
53 | |
54 if let userSettings = UserDefaults.standard.data(forKey: "UserSettings") { | |
55 if let decodedUserSettings = try? JSONDecoder().decode(UserSettings.self, from: userSettings) { | |
56 userLives = decodedUserSettings.numberOfLives | |
57 } | |
58 } | |
59 | |
60 askQuestion { | |
61 selector() | |
62 } | |
63 } | |
64 } | |
65 | |
66 extension CountryGame { | |
67 func selector() { | |
68 | |
69 // Get random choices | |
70 var userChoices = [String: T]() | |
71 | |
72 while userChoices.count < 2 { | |
73 if let choice = data.randomElement() { | |
74 userChoices[choice.key] = choice.value | |
75 } else { | |
76 fatalError("Couldn't get a random value from data") | |
77 } | |
78 } | |
79 | |
80 // Get question asked (correct answer) | |
81 let correctAnswer = data.first(where: { | |
82 !userChoices.keys.contains($0.key) && // Avoid duplicated countries | |
83 !dataAsked.keys.contains($0.key) // Avoid countries already asked | |
84 }) | |
85 | |
86 // Unwrap optional | |
87 if let correctAnswer = correctAnswer { | |
88 userChoices[correctAnswer.key] = correctAnswer.value | |
89 dataAsked[correctAnswer.key] = correctAnswer.value | |
90 self.correctAnswer = correctAnswer | |
91 } else { | |
92 fatalError("Couldn't unwrap optional value") | |
93 } | |
94 | |
95 self.userChoices = userChoices | |
96 } | |
97 } |