Mercurial > public > geoquiz
comparison GeoQuiz/Logic/CityGame.swift @ 8:e09959b4e4a8
fix bugs
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 06 Oct 2022 11:14:34 +0200 |
parents | d945e52b0704 |
children | 3540c7efc216 |
comparison
equal
deleted
inserted
replaced
7:d945e52b0704 | 8:e09959b4e4a8 |
---|---|
6 // | 6 // |
7 | 7 |
8 import Foundation | 8 import Foundation |
9 import AVFAudio | 9 import AVFAudio |
10 import SwiftUI | 10 import SwiftUI |
11 import MapKit | |
12 | 11 |
13 class CityGame: Game, ObservableObject { | 12 class CityGame: Game, ObservableObject { |
14 | 13 |
15 // Define type of generics | 14 // Define type of generics |
16 typealias T = CityModel.CityData | 15 typealias T = CityModel.CityData |
17 | 16 |
18 var data: [String: T] | 17 var data: [String: T] |
19 var dataAsked = [String: T]() | 18 var dataAsked = [String: T]() |
20 | 19 |
21 @Published var mapImage: UIImage? = nil | 20 // Data |
22 @Published var correctAnswer = (key: String(), value: T(country: String(), lat: Double(), lon: Double())) { | 21 @Published var correctAnswer = ( |
23 willSet { | 22 key: String(), |
24 getMapImage(lat: newValue.value.lat, lon: newValue.value.lon) | 23 value: T(country: String(), lat: Double(), lon: Double()) |
25 } | 24 ) |
26 } | |
27 | 25 |
28 // User | 26 // User |
29 @Published var userChoices = [String: T]() | 27 @Published var userChoices = [String: T]() |
30 @Published var userScore = 0 | 28 @Published var userScore = 0 |
31 @Published var userLives = 3 | 29 @Published var userLives = 3 |
48 @Published var player: AVAudioPlayer? | 46 @Published var player: AVAudioPlayer? |
49 | 47 |
50 init() { | 48 init() { |
51 let data: CityModel = load("cities.json") | 49 let data: CityModel = load("cities.json") |
52 self.data = data.cities | 50 self.data = data.cities |
53 askQuestion() | 51 askQuestion { |
52 selector() | |
53 } | |
54 } | 54 } |
55 } | 55 } |
56 | 56 |
57 extension CityGame { | 57 extension CityGame { |
58 func getMapImage(lat: Double, lon: Double) { | 58 func selector() { |
59 let region = MKCoordinateRegion( | 59 |
60 center: CLLocationCoordinate2D( | 60 // Get random choices |
61 latitude: lat, | 61 var userChoices = [String: T]() |
62 longitude: lon | 62 |
63 ), | 63 while userChoices.count < 2 { |
64 span: MKCoordinateSpan( | 64 if let choice = data.randomElement() { |
65 latitudeDelta: 1.0, | 65 let userChoicesCountry = userChoices.map { $0.value.country } |
66 longitudeDelta: 1.0 | 66 |
67 ) | 67 if !userChoicesCountry.contains(choice.value.country) { |
68 ) | 68 userChoices[choice.key] = choice.value |
69 | 69 } |
70 // Map options | 70 } else { |
71 let mapOptions = MKMapSnapshotter.Options() | 71 fatalError("Couldn't get a random value from data") |
72 mapOptions.region = region | |
73 mapOptions.size = CGSize(width: 600, height: 600) | |
74 mapOptions.showsBuildings = true | |
75 | |
76 // Create the snapshotter and run it | |
77 let snapshotter = MKMapSnapshotter(options: mapOptions) | |
78 snapshotter.start { (snapshot, error) in | |
79 if let snapshot = snapshot { | |
80 self.mapImage = snapshot.image | |
81 } else if let error = error { | |
82 print(error.localizedDescription) | |
83 } | 72 } |
84 } | 73 } |
74 | |
75 // Get question asked (correct answer) | |
76 let userChoicesCountry = userChoices.map { $0.value.country } | |
77 let correctAnswer = data.first(where: { | |
78 !userChoices.keys.contains($0.key) && // Avoid duplicated cities | |
79 !dataAsked.keys.contains($0.key) && // Avoid cities already asked | |
80 !userChoicesCountry.contains($0.value.country) // Avoid duplicated country names in userChoices | |
81 }) | |
82 | |
83 // Unwrap optional | |
84 if let correctAnswer = correctAnswer { | |
85 userChoices[correctAnswer.key] = correctAnswer.value | |
86 dataAsked[correctAnswer.key] = correctAnswer.value | |
87 self.correctAnswer = correctAnswer | |
88 } else { | |
89 fatalError("Couldn't unwrap optional value") | |
90 } | |
91 | |
92 self.userChoices = userChoices | |
85 } | 93 } |
86 } | 94 } |
87 | 95 |