-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
401 lines (317 loc) · 7.61 KB
/
utils.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package lowcode
import (
"context"
"log/slog"
"github.com/bytedance/sonic"
"github.com/zhs007/goutils"
sgc7game "github.com/zhs007/slotsgamecore7/game"
sgc7plugin "github.com/zhs007/slotsgamecore7/plugin"
)
// // findLastPBComponentData
// func findLastPBComponentData(lst []*sgc7game.PlayResult, componentName string) (proto.Message, *sgc7game.PlayResult) {
// for i := len(lst) - 1; i >= 0; i-- {
// pr := lst[i]
// gp := pr.CurGameModParams.(*GameParams)
// if gp != nil {
// pbcd := gp.MapComponentMsgs[componentName]
// if pbcd != nil {
// return pbcd, pr
// }
// }
// }
// return nil, nil
// }
// // findLastPBComponentDataEx
// func findLastPBComponentDataEx(lst []*sgc7game.PlayResult, respinComponentName string, componentName string) (proto.Message, *sgc7game.PlayResult) {
// for i := len(lst) - 1; i >= 0; i-- {
// pr := lst[i]
// gp := pr.CurGameModParams.(*GameParams)
// if gp != nil {
// pbRespin := gp.MapComponentMsgs[respinComponentName]
// pbcd := gp.MapComponentMsgs[componentName]
// if pbRespin != nil && pbcd != nil {
// return pbcd, pr
// }
// }
// }
// return nil, nil
// }
// // findFirstPBComponentData
// func findFirstPBComponentData(lst []*sgc7game.PlayResult, componentName string) (proto.Message, *sgc7game.PlayResult) {
// for _, pr := range lst {
// gp := pr.CurGameModParams.(*GameParams)
// if gp != nil {
// pbcd := gp.MapComponentMsgs[componentName]
// if pbcd != nil {
// return pbcd, pr
// }
// }
// }
// return nil, nil
// }
// // findFirstPBComponentDataEx
// func findFirstPBComponentDataEx(lst []*sgc7game.PlayResult, respinComponentName string, componentName string) (proto.Message, *sgc7game.PlayResult) {
// for _, pr := range lst {
// gp := pr.CurGameModParams.(*GameParams)
// if gp != nil {
// pbRespin := gp.MapComponentMsgs[respinComponentName]
// pbcd := gp.MapComponentMsgs[componentName]
// if pbRespin != nil && pbcd != nil {
// return pbcd, pr
// }
// }
// }
// return nil, nil
// }
// // findAllPBComponentDataEx
// func findAllPBComponentDataEx(lst []*sgc7game.PlayResult, respinComponentName string, componentName string) ([]proto.Message, []*sgc7game.PlayResult) {
// pbs := []proto.Message{}
// prs := []*sgc7game.PlayResult{}
// for _, pr := range lst {
// gp := pr.CurGameModParams.(*GameParams)
// if gp != nil {
// pbRespin := gp.MapComponentMsgs[respinComponentName]
// pbcd := gp.MapComponentMsgs[componentName]
// if pbRespin != nil && pbcd != nil {
// pbs = append(pbs, pbcd)
// prs = append(prs, pr)
// }
// }
// }
// if len(pbs) == 0 {
// return nil, nil
// }
// return pbs, prs
// }
// func calcTotalCashWins(lst []*sgc7game.PlayResult) int64 {
// wins := int64(0)
// for _, v := range lst {
// wins += v.CashWin
// }
// return wins
// }
func isSameBoolSlice(src []bool, dest []bool) bool {
if len(src) == len(dest) {
for i, v := range src {
if v != dest[i] {
return false
}
}
return true
}
return false
}
func GetExcludeSymbols(pt *sgc7game.PayTables, symbols []int) []int {
es := []int{}
for s := range pt.MapPay {
if goutils.IndexOfIntSlice(symbols, s, 0) < 0 {
es = append(es, s)
}
}
return es
}
func IsInPosArea(x, y int, posArea []int) bool {
return x >= posArea[0] && x <= posArea[1] && y >= posArea[2] && y <= posArea[3]
}
// ProcCheat -
func ProcCheat(plugin sgc7plugin.IPlugin, cheat string) (*ForceOutcome2, error) {
if cheat != "" {
if sgc7game.IsRngString(cheat) {
str := goutils.AppendString("[", cheat, "]")
rngs := []int{}
err := sonic.Unmarshal([]byte(str), &rngs)
if err != nil {
return nil, err
}
plugin.SetCache(rngs)
} else {
if gAllowForceOutcome {
return NewForceOutcome2(cheat)
// return ParseForceOutcome(cheat), nil
}
}
}
return nil, nil
}
func procSpin(game *Game, ips sgc7game.IPlayerState, plugin sgc7plugin.IPlugin, stake *sgc7game.Stake, cmd string, params string) ([]*sgc7game.PlayResult, error) {
results := []*sgc7game.PlayResult{}
gameData := game.NewGameData(stake)
defer game.DeleteGameData(gameData)
for {
if cmd == "" {
cmd = "SPIN"
}
pr, err := game.Play(plugin, cmd, params, ips, stake, results, gameData)
if err != nil {
goutils.Error("Spin:Play",
slog.Int("results", len(results)),
goutils.Err(err))
return nil, err
}
if pr == nil {
break
}
results = append(results, pr)
if pr.IsFinish {
break
}
if pr.IsWait {
break
}
if len(pr.NextCmds) > 0 {
cmd = pr.NextCmds[0]
} else {
cmd = ""
}
if len(results) >= MaxStepNum {
goutils.Error("procSpin",
slog.Int("steps", len(results)),
goutils.Err(ErrTooManySteps))
return nil, ErrTooManySteps
}
}
return results, nil
}
func Spin(game *Game, ips sgc7game.IPlayerState, plugin sgc7plugin.IPlugin, stake *sgc7game.Stake, cmd string, params string, cheat string) ([]*sgc7game.PlayResult, error) {
fo, err := ProcCheat(plugin, cheat)
if err != nil {
goutils.Error("Spin:ProcCheat",
slog.String("cheat", cheat),
goutils.Err(err))
return nil, err
}
err = game.CheckStake(stake)
if err != nil {
goutils.Error("Spin:CheckStake",
slog.Any("stake", stake),
goutils.Err(err))
return nil, err
}
if fo == nil {
return procSpin(game, ips, plugin, stake, cmd, params)
}
for tryi := 0; tryi < gMaxForceOutcomeTimes; tryi++ {
newips := ips.Clone()
plugin.ClearCache()
plugin.ClearUsedRngs()
lst, err := procSpin(game, newips, plugin, stake, cmd, params)
if err != nil {
goutils.Error("Spin:procSpin",
goutils.Err(err))
return nil, err
}
if fo.IsValid(lst) {
return lst, nil
}
}
goutils.Error("Spin",
goutils.Err(ErrCannotForceOutcome))
return nil, ErrCannotForceOutcome
}
func InsStringSliceNonRep(arr []string, str string) []string {
if goutils.IndexOfStringSlice(arr, str, 0) >= 0 {
return arr
}
return append(arr, str)
}
func InsSliceNonRep(arr []string, src []string) []string {
for _, v := range src {
arr = InsStringSliceNonRep(arr, v)
}
return arr
}
func PowInt(v int, n int) int {
if n < 0 {
return 1
}
if n == 0 {
return 1
}
if n == 1 {
return v
}
if n == 2 {
return v * v
}
if n == 3 {
return v * v * v
}
t := v * v * v
for i := 3; i < n; i++ {
t *= v
}
return t
}
func CmpVal(src int, op string, target int) bool {
if op == "==" {
return src == target
}
if op == ">" {
return src > target
}
if op == ">=" {
return src >= target
}
if op == "<" {
return src < target
}
if op == "<=" {
return src <= target
}
return false
}
func HasSamePos(src []int, target []int) bool {
if len(src) == 0 || len(target) == 0 {
return false
}
for i := 0; i < len(src)/2; i++ {
if goutils.IndexOfInt2Slice(target, src[i*2], src[i*2+1], 0) >= 0 {
return true
}
}
return false
}
func Shuffle(arr []int, plugin sgc7plugin.IPlugin) ([]int, error) {
dst := []int{}
for {
if len(arr) == 0 {
return dst, nil
}
cr, err := plugin.Random(context.Background(), len(arr))
if err != nil {
goutils.Error("Shuffle:Random",
goutils.Err(err))
return nil, err
}
dst = append(dst, arr[cr])
arr = append(arr[:cr], arr[cr+1:]...)
}
}
// IsInitialArr - is the arr [0,1,2...]
func IsInitialArr(arr []int) bool {
for i, v := range arr {
if i != v {
return false
}
}
return true
}
// GenInitialArr - return [0,1,2...]
func GenInitialArr(len int) []int {
arr := []int{}
for i := 0; i < len; i++ {
arr = append(arr, i)
}
return arr
}
func IsSameIntArr(arr []int, arr1 []int) bool {
if len(arr) == len(arr1) {
for i, v := range arr {
if v != arr1[i] {
return false
}
}
return true
}
return false
}