annotate GeoQuiz/Logic/Game.swift @ 4:de54f05adb78

add prototype game stats
author Dennis C. M. <dennis@denniscm.com>
date Thu, 22 Sep 2022 11:38:42 +0200
parents 4dbe0cd9dadc
children f31a61462e7a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
1 //
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
2 // GameProtocol.swift
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
3 // GeoQuiz
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
4 //
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
5 // Created by Dennis Concepción Martín on 18/9/22.
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
6 //
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
7
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
8 import Foundation
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
9 import SwiftUI
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
10
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
11 protocol Game: ObservableObject {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
12
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
13 // Define generic type
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
14 associatedtype T: Equatable
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
15
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
16 // Game
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
17 var data: [String: T] { get set}
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
18 var dataAsked: [String] { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
19 var correctAnswer: (key: String, value: T) { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
20
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
21 // User
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
22 var userChoices: [String: T] { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
23 var userScore: Int { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
24 var userLives: Int { get set }
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
25 var correctAnswers: [String: T] { get set }
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
26 var wrongAnswers: [String: T] { get set }
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
27
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
28 // Alerts
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
29 var alertTitle: String { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
30 var alertMessage: String { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
31 var showingNoLivesAlert: Bool { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
32 var showingEndGameAlert: Bool { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
33 var showingWrongAnswerAlert: Bool { get set }
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
34 var showingExitGameAlert: Bool { get set }
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
35
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
36 // Animations
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
37 var scoreScaleAmount: Double { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
38 var livesScaleAmount: Double { get set }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
39
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
40 // Modal views
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
41 var showingBuyLivesView: Bool { get set }
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
42 var showingGameStatsView: Bool { get set }
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
43 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
44
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
45 extension Game {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
46 var questionCounter: Int {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
47 dataAsked.count
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
48 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
49
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
50 func askQuestion() {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
51 guard questionCounter < data.count else {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
52 alertTitle = "Amazing!"
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
53 alertMessage = "You've completed the game."
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
54 showingEndGameAlert = true
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
55
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
56 return
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
57 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
58
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
59 // Get random choices
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
60 var userChoices = [String: T]()
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
61
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
62 while userChoices.count < 2 {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
63 if let choice = data.randomElement() {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
64 userChoices[choice.key] = choice.value
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
65 } else {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
66 fatalError("Couldn't get a random value from data")
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
67 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
68 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
69
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
70 // Get question asked (correct answer)
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
71 let correctAnswer = data.first(where: {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
72 !userChoices.keys.contains($0.key) && !dataAsked.contains($0.key)
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
73 })
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
74
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
75 // Unwrap optional
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
76 if let correctAnswer = correctAnswer {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
77 userChoices[correctAnswer.key] = correctAnswer.value
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
78 dataAsked.append(correctAnswer.key)
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
79 self.correctAnswer = correctAnswer
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
80 } else {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
81 fatalError("Couldn't unwrap optional value")
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
82 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
83
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
84 self.userChoices = userChoices
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
85 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
86
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
87 func answer(_ choice: (key: String, value: T)) {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
88 guard userLives > 0 else {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
89 alertTitle = "Not enough lives!"
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
90 alertMessage = "Please buy more lives to keep playing"
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
91 showingNoLivesAlert = true
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
92
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
93 return
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
94 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
95
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
96 if correctAnswer == choice {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
97 hapticSuccess()
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
98
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
99 withAnimation(.easeIn(duration: 0.5)) {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
100 scoreScaleAmount += 1
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
101 userScore += 1
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
102 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
103
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
104 correctAnswers[correctAnswer.key] = correctAnswer.value
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
105 askQuestion()
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
106 } else {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
107 hapticError()
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
108
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
109
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
110 withAnimation(.easeIn(duration: 0.5)) {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
111 livesScaleAmount += 1
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
112 userLives -= 1
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
113 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
114
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
115 alertTitle = "Wrong!"
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
116 alertMessage = "You have \(userLives) lives left"
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
117 showingWrongAnswerAlert = true
4
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
118
de54f05adb78 add prototype game stats
Dennis C. M. <dennis@denniscm.com>
parents: 3
diff changeset
119 wrongAnswers[choice.key] = choice.value
3
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
120 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
121
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
122 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
123 withAnimation(.easeIn(duration: 0.5)) {
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
124 scoreScaleAmount = 1
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
125 livesScaleAmount = 1
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
126 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
127 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
128 }
4dbe0cd9dadc first game prototype
Dennis C. M. <dennis@denniscm.com>
parents:
diff changeset
129 }