-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamelogic.go
259 lines (227 loc) · 7.1 KB
/
gamelogic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"math/rand"
"github.com/sushovande/qr-mixer-game/qrpb"
proto "google.golang.org/protobuf/proto"
)
// Step returns the GameState resulting from the action at the current GameState
func (env *Env) Step(old *qrpb.GameState, answer string) (StepResponse, error) {
qrm, err := env.cgo.GetQRMappings()
if err != nil {
return StepResponse{}, err
}
// Set up defaults
result := NewStepResponse()
result.actionString = "Lost a Life!"
result.actionResult = *qrpb.ActionLog_RESULT_LOST_LIFE.Enum()
result.scannedClue = qrm.LookupByQrCode(answer).GetUsername()
result.newState = proto.Clone(old).(*qrpb.GameState)
// ENDGAME logic
if old.GetUserLevel() == 22 {
result.actionString = "Already Victorious!"
result.actionResult = *qrpb.ActionLog_RESULT_ALREADY_VICTORIOUS.Enum()
return result, nil
}
// 21 will be handled by regular list_username logic below, with username set to cauldron
if old.GetUserLevel() == 20 {
// You scanned someone and tried to get their metals
gs, err := GetUserStateByUsername(env.db, result.scannedClue)
if err != nil {
return StepResponse{}, err
}
if gs == nil {
// scanned someone who is not yet registered?
return StepResponse{}, fmt.Errorf("you tried to get metals from someone who is not yet registered in the game")
}
if GrabMetalFromSomeone(&result, gs.State) {
result.actionString = "Grabbed Metal!"
result.actionResult = *qrpb.ActionLog_RESULT_GRABBED_METAL.Enum()
if result.newState.GetHasAl() && result.newState.GetHasCu() && result.newState.GetHasSn() && result.newState.GetHasZn() {
result.newState.UserLevel = proto.Int64(old.GetUserLevel() + 1)
}
} else {
result.actionString = "Nothing Found!"
result.actionResult = *qrpb.ActionLog_RESULT_NO_GRABBED_METAL.Enum()
}
return result, nil
}
// Regular questions
sqs, err := env.cgo.GetGameQSet()
if err != nil {
return StepResponse{}, err
}
sq := GetQuestionByIndex(sqs, old.GetUserLevel())
if *sq.Type == qrpb.GQType_USERNAME_LIST {
if ListHasString(sq.AnsUsernames, result.scannedClue) {
// TODO: check if this level arithmetic is desirable
result.newState.UserLevel = proto.Int64(old.GetUserLevel() + 1)
result.actionString = "Correct!"
result.actionResult = *qrpb.ActionLog_RESULT_PROGRESS.Enum()
} else {
result.newState.Life = proto.Int64(old.GetLife() - 1)
}
} else if *sq.Type == qrpb.GQType_SURVEY_ANS {
gu, err := GetUserInfoByUsername(env.db, result.scannedClue)
if err != nil {
return StepResponse{}, err
}
if gu == nil {
// scanned someone who is not yet registered?
return StepResponse{}, fmt.Errorf("you scanned someone who is not yet registered in the game")
}
if getSurveyResponse(gu, sq.GetSurveyId()) == sq.GetSurveyTrueIsCorrect() {
result.newState.UserLevel = proto.Int64(old.GetUserLevel() + 1)
result.actionString = "Correct!"
result.actionResult = *qrpb.ActionLog_RESULT_PROGRESS.Enum()
} else {
result.newState.Life = proto.Int64(old.GetLife() - 1)
}
} else if *sq.Type == qrpb.GQType_ANY_PERSON {
// TODO: if at all needed, remove the ability to scan inanimate objects at this time.
result.newState.UserLevel = proto.Int64(old.GetUserLevel() + 1)
result.actionString = "Correct!"
result.actionResult = *qrpb.ActionLog_RESULT_PROGRESS.Enum()
}
MaybeGrantMetal(&result)
if result.newState.GetLife() <= 0 {
result.actionString = "Dead!"
result.actionResult = *qrpb.ActionLog_RESULT_ALREADY_DEAD.Enum()
result.newState.UserLevel = proto.Int64(-1)
}
result.levelClue = GetQuestionByIndex(sqs, *result.newState.UserLevel).GetQuestionHtml()
return result, nil
}
func MaybeGrantMetal(result *StepResponse) {
if result.actionResult != *qrpb.ActionLog_RESULT_PROGRESS.Enum() {
return
}
// Check stage-1 granting.
if *result.newState.UserLevel >= 8 && *result.newState.UserLevel <= 10 {
if result.newState.GetHasAl() || result.newState.GetHasCu() {
return
}
// choose a metal to grant
metal := "al"
if rand.Float32() > 0.5 {
metal = "cu"
}
if *result.newState.UserLevel == 8 {
if rand.Float32() > 0.4 {
GrantMetal(result, metal)
}
} else if *result.newState.UserLevel == 9 {
if rand.Float32() > 0.7 {
GrantMetal(result, metal)
}
} else if *result.newState.UserLevel == 10 {
GrantMetal(result, metal)
}
}
// Check stage-2 granting.
if *result.newState.UserLevel >= 18 && *result.newState.UserLevel <= 20 {
if result.newState.GetHasSn() || result.newState.GetHasZn() {
return
}
// choose a metal to grant
metal := "sn"
if rand.Float32() > 0.5 {
metal = "zn"
}
if *result.newState.UserLevel == 18 {
if rand.Float32() > 0.4 {
GrantMetal(result, metal)
}
} else if *result.newState.UserLevel == 19 {
if rand.Float32() > 0.7 {
GrantMetal(result, metal)
}
} else if *result.newState.UserLevel == 20 {
GrantMetal(result, metal)
}
}
}
// GrabMetalFromSomeone grabs a shared metal from another user in the endgame.
// Returns true if a metal was grabbed.
func GrabMetalFromSomeone(result *StepResponse, gs *qrpb.GameState) bool {
grabbable := make([]string, 0)
if gs.GetHasAl() && !result.newState.GetHasAl() {
grabbable = append(grabbable, "al")
}
if gs.GetHasCu() && !result.newState.GetHasCu() {
grabbable = append(grabbable, "cu")
}
if gs.GetHasSn() && !result.newState.GetHasSn() {
grabbable = append(grabbable, "sn")
}
if gs.GetHasZn() && !result.newState.GetHasZn() {
grabbable = append(grabbable, "zn")
}
if len(grabbable) == 0 {
return false
}
w := rand.Intn(len(grabbable))
GrantMetal(result, grabbable[w])
return true
}
func GrantMetal(result *StepResponse, metal string) {
switch metal {
case "al":
result.newState.HasAl = proto.Bool(true)
return
case "cu":
result.newState.HasCu = proto.Bool(true)
return
case "sn":
result.newState.HasSn = proto.Bool(true)
return
case "zn":
result.newState.HasZn = proto.Bool(true)
return
}
}
func GetQuestionByIndex(sqs *qrpb.GameQSet, n int64) *qrpb.GameQuestion {
for _, q := range sqs.GameQuestions {
if q.GetQuestionId() == n {
return q
}
}
return nil
}
func GetSurveyQuestionByIndex(gopt *qrpb.SurveySet, n int64) *qrpb.SurveyQuestion {
for _, q := range gopt.SurveyQuestions {
if q.GetQuestionId() == n {
return q
}
}
return nil
}
func getSurveyResponse(gu *qrpb.GUser, qid int64) bool {
for _, sa := range gu.SurveyAnswers {
if *sa.QuestionId == qid {
return sa.GetIsTrue()
}
}
return false
}
func ListHasString(list []string, needle string) bool {
for _, s := range list {
if s == needle {
return true
}
}
return false
}