-
Notifications
You must be signed in to change notification settings - Fork 1
/
respin.go
668 lines (513 loc) · 17.9 KB
/
respin.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package lowcode
import (
"fmt"
"log/slog"
"os"
"github.com/bytedance/sonic"
"github.com/bytedance/sonic/ast"
"github.com/zhs007/goutils"
"github.com/zhs007/slotsgamecore7/asciigame"
sgc7game "github.com/zhs007/slotsgamecore7/game"
sgc7plugin "github.com/zhs007/slotsgamecore7/plugin"
"github.com/zhs007/slotsgamecore7/sgc7pb"
"github.com/zhs007/slotsgamecore7/stats2"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"gopkg.in/yaml.v2"
)
const RespinTypeName = "respin"
type RespinData struct {
BasicComponentData
LastRespinNum int
CurRespinNum int
CurAddRespinNum int
RetriggerAddRespinNum int // 再次触发时增加的次数
TotalCoinWin int64
// TotalCashWin int64
LastTriggerNum int // 剩余的触发次数,respin有2种模式,一种是直接增加免费次数,一种是累积整体触发次数
CurTriggerNum int // 当前已经触发次数
Awards []*Award // 当前已经触发次数
TriggerRespinNum []int // 配合LastTriggerNum用的respin次数,-1表示用当前的RetriggerAddRespinNum,否则就是具体值
}
// OnNewGame -
func (respinData *RespinData) OnNewGame(gameProp *GameProperty, component IComponent) {
respinData.BasicComponentData.OnNewGame(gameProp, component)
respinData.LastRespinNum = 0
respinData.CurRespinNum = 0
respinData.CurAddRespinNum = 0
respinData.TotalCoinWin = 0
// respinData.TotalCashWin = 0
respinData.RetriggerAddRespinNum = 0
respinData.LastTriggerNum = 0
respinData.CurTriggerNum = 0
respinData.Awards = nil
}
// onNewStep -
func (respinData *RespinData) onNewStep() {
// respinData.BasicComponentData.OnNewStep(gameProp, component)
respinData.CurAddRespinNum = 0
}
// Clone
func (respinData *RespinData) Clone() IComponentData {
target := &RespinData{
BasicComponentData: respinData.CloneBasicComponentData(),
LastRespinNum: respinData.LastRespinNum,
CurRespinNum: respinData.CurRespinNum,
CurAddRespinNum: respinData.CurAddRespinNum,
TotalCoinWin: respinData.TotalCoinWin,
RetriggerAddRespinNum: respinData.RetriggerAddRespinNum,
LastTriggerNum: respinData.LastTriggerNum,
CurTriggerNum: respinData.CurTriggerNum,
}
target.TriggerRespinNum = make([]int, len(respinData.TriggerRespinNum))
copy(target.TriggerRespinNum, respinData.TriggerRespinNum)
return target
}
// BuildPBComponentData
func (respinData *RespinData) BuildPBComponentData() proto.Message {
pbcd := &sgc7pb.RespinData{
BasicComponentData: respinData.BuildPBBasicComponentData(),
LastRespinNum: int32(respinData.LastRespinNum),
CurRespinNum: int32(respinData.CurRespinNum),
CurAddRespinNum: int32(respinData.CurAddRespinNum),
TotalCoinWin: respinData.TotalCoinWin,
// TotalCashWin: respinData.TotalCashWin,
RetriggerAddRespinNum: int32(respinData.RetriggerAddRespinNum),
LastTriggerNum: int32(respinData.LastTriggerNum),
CurTriggerNum: int32(respinData.CurTriggerNum),
}
for _, v := range respinData.TriggerRespinNum {
pbcd.TriggerRespinNum = append(pbcd.TriggerRespinNum, int32(v))
}
return pbcd
}
// GetVal -
func (respinData *RespinData) GetVal(key string) (int, bool) {
if key == CVCurRespinNum {
return respinData.CurRespinNum, true
}
return 0, false
}
// GetLastRespinNum -
func (respinData *RespinData) GetLastRespinNum() int {
return respinData.LastRespinNum
}
// IsEnding -
func (respinData *RespinData) IsRespinEnding() bool {
return respinData.LastRespinNum == 0 && respinData.LastTriggerNum == 0
}
// IsStarted -
func (respinData *RespinData) IsRespinStarted() bool {
return respinData.CurRespinNum > 0
}
// ChgConfigIntVal -
func (respinData *RespinData) ChgConfigIntVal(key string, off int) {
if key == CCVLastRespinNum {
respinData.AddRespinTimes(off)
} else {
respinData.BasicComponentData.ChgConfigIntVal(key, off)
}
}
// SetConfigIntVal -
func (respinData *RespinData) SetConfigIntVal(key string, val int) {
if key == CCVLastRespinNum {
respinData.AddRespinTimes(val)
} else {
respinData.BasicComponentData.ChgConfigIntVal(key, val)
}
}
// // AddRetriggerRespinNum -
// func (respinData *RespinData) AddRetriggerRespinNum(num int) {
// respinData.RetriggerAddRespinNum += num
// }
// AddTriggerRespinAward -
func (respinData *RespinData) AddTriggerRespinAward(award *Award) {
award.TriggerIndex = respinData.CurTriggerNum + respinData.LastTriggerNum
respinData.Awards = append(respinData.Awards, award)
}
// AddRespinTimes -
func (respinData *RespinData) AddRespinTimes(num int) {
respinData.LastRespinNum += num
respinData.CurAddRespinNum += num
}
// OnTriggerRespin
func (respinData *RespinData) TriggerRespin(gameProp *GameProperty, plugin sgc7plugin.IPlugin, curpr *sgc7game.PlayResult, gp *GameParams) {
n := respinData.TriggerRespinNum[respinData.CurTriggerNum]
if n <= 0 {
n = respinData.RetriggerAddRespinNum
respinData.TriggerRespinNum[respinData.CurTriggerNum] = n
}
respinData.LastRespinNum += n
respinData.CurAddRespinNum += n
respinData.CurTriggerNum++
if respinData.LastTriggerNum > 0 {
respinData.LastTriggerNum--
}
for _, v := range respinData.Awards {
if v.TriggerIndex == respinData.CurTriggerNum {
gameProp.procAward(plugin, v, curpr, gp, true)
}
}
}
// PushTrigger -
func (respinData *RespinData) PushTriggerRespin(gameProp *GameProperty, plugin sgc7plugin.IPlugin, curpr *sgc7game.PlayResult, gp *GameParams, num int) {
respinData.LastTriggerNum++
respinData.TriggerRespinNum = append(respinData.TriggerRespinNum, num)
// 第一次trigger时,需要直接
if respinData.LastRespinNum == 0 && respinData.CurRespinNum == 0 {
respinData.TriggerRespin(gameProp, plugin, curpr, gp)
}
}
// RespinLevelConfig - configuration for Respin Level
type RespinLevelConfig struct {
BasicComponentConfig `yaml:",inline" json:",inline"`
LastRespinNum int `yaml:"lastRespinNum" json:"lastRespinNum"` // 倒数第几局开始
MaxCoinWins int `yaml:"maxCoinWins" json:"maxCoinWins"` // 如果最大获奖低于这个
JumpComponent string `yaml:"jumpComponent" json:"jumpComponent"` // 跳转到这个component
}
// RespinConfig - configuration for Respin
type RespinConfig struct {
BasicComponentConfig `yaml:",inline" json:",inline"`
// InitRespinNum int `yaml:"initRespinNum" json:"initRespinNum"`
MainComponent string `yaml:"mainComponent" json:"mainComponent"`
// IsWinBreak bool `yaml:"isWinBreak" json:"isWinBreak"`
// Levels []*RespinLevelConfig `yaml:"levels" json:"levels"`
}
// SetLinkComponent
func (cfg *RespinConfig) SetLinkComponent(link string, componentName string) {
if link == "next" {
cfg.DefaultNextComponent = componentName
} else if link == "loop" {
cfg.MainComponent = componentName
}
}
type Respin struct {
*BasicComponent `json:"-"`
Config *RespinConfig `json:"config"`
}
// // OnNewGame -
// func (respin *Respin) OnNewGame(gameProp *GameProperty) error {
// cd := gameProp.MapComponentData[respin.Name]
// cd.OnNewGame()
// return nil
// }
// OnPlayGame - on playgame
func (respin *Respin) procLevel(level *RespinLevelConfig, respinData *RespinData, _ *GameProperty) bool {
if respinData.LastRespinNum <= level.LastRespinNum && respinData.CoinWin < level.MaxCoinWins {
return true
}
return false
}
// // OnPlayGame - on playgame
// func (respin *Respin) AddRespinTimes(gameProp *GameProperty, num int) {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// cd.LastRespinNum += num
// cd.CurAddRespinNum += num
// }
// Init -
func (respin *Respin) Init(fn string, pool *GamePropertyPool) error {
data, err := os.ReadFile(fn)
if err != nil {
goutils.Error("Respin.Init:ReadFile",
slog.String("fn", fn),
goutils.Err(err))
return err
}
cfg := &RespinConfig{}
err = yaml.Unmarshal(data, cfg)
if err != nil {
goutils.Error("Respin.Init:Unmarshal",
slog.String("fn", fn),
goutils.Err(err))
return err
}
return respin.InitEx(cfg, pool)
}
// InitEx -
func (respin *Respin) InitEx(cfg any, pool *GamePropertyPool) error {
respin.Config = cfg.(*RespinConfig)
respin.Config.ComponentType = RespinTypeName
respin.onInit(&respin.Config.BasicComponentConfig)
return nil
}
// playgame
func (respin *Respin) OnPlayGame(gameProp *GameProperty, curpr *sgc7game.PlayResult, gp *GameParams, plugin sgc7plugin.IPlugin,
cmd string, param string, ps sgc7game.IPlayerState, stake *sgc7game.Stake, prs []*sgc7game.PlayResult, icd IComponentData) (string, error) {
// respin.onPlayGame(gameProp, curpr, gp, plugin, cmd, param, ps, stake, prs)
cd := icd.(*RespinData)
cd.onNewStep()
// if cd.CurRespinNum == 0 && cd.LastRespinNum == 0 && respin.Config.InitRespinNum > 0 {
// cd.LastRespinNum = respin.Config.InitRespinNum
// }
recheck:
if cd.LastRespinNum == 0 {
if cd.LastTriggerNum > 0 {
cd.TriggerRespin(gameProp, plugin, curpr, gp)
goto recheck
}
if respin.Config.DefaultNextComponent == "" {
nc := respin.onStepEnd(gameProp, curpr, gp, "")
return nc, ErrComponentDoNothing
}
nc := respin.onStepEnd(gameProp, curpr, gp, respin.Config.DefaultNextComponent)
return nc, nil
}
nextComponent := respin.Config.MainComponent
// for _, v := range respin.Config.Levels {
// if respin.procLevel(v, cd, gameProp) {
// nextComponent = v.JumpComponent
// break
// }
// }
if cd.LastRespinNum > 0 {
cd.LastRespinNum--
}
cd.CurRespinNum++
nc := respin.onStepEnd(gameProp, curpr, gp, nextComponent)
// gp.AddComponentData(respin.Name, cd)
return nc, nil
}
// OnAsciiGame - outpur to asciigame
func (respin *Respin) OnAsciiGame(gameProp *GameProperty, pr *sgc7game.PlayResult, lst []*sgc7game.PlayResult, mapSymbolColor *asciigame.SymbolColorMap, icd IComponentData) error {
cd := icd.(*RespinData)
if cd.CurAddRespinNum > 0 {
fmt.Printf("%v last %v, current %v, retrigger %v\n", respin.Name, cd.LastRespinNum, cd.CurRespinNum, cd.CurAddRespinNum)
} else {
fmt.Printf("%v last %v, current %v\n", respin.Name, cd.LastRespinNum, cd.CurRespinNum)
}
return nil
}
// // OnStats
// func (respin *Respin) OnStats(feature *sgc7stats.Feature, stake *sgc7game.Stake, lst []*sgc7game.PlayResult) (bool, int64, int64) {
// if feature != nil && len(lst) > 0 {
// if feature.RespinNumStatus != nil ||
// feature.RespinWinStatus != nil {
// pbcd, lastpr := findLastPBComponentData(lst, respin.Name)
// if pbcd != nil {
// respin.onStatsWithPBEnding(feature, pbcd, lastpr)
// }
// }
// if feature.RespinStartNumStatus != nil {
// pbcd, firstpr := findFirstPBComponentData(lst, respin.Name)
// if pbcd != nil {
// respin.onStatsWithPBStart(feature, pbcd, firstpr)
// }
// }
// }
// return false, 0, 0
// }
// // onStatsWithPBEnding -
// func (respin *Respin) onStatsWithPBEnding(feature *sgc7stats.Feature, pbComponentData proto.Message, pr *sgc7game.PlayResult) error {
// pbcd, isok := pbComponentData.(*sgc7pb.RespinData)
// if !isok {
// goutils.Error("Respin.onStatsWithPBEnding",
// goutils.Err(ErrIvalidProto))
// return ErrIvalidProto
// }
// if feature.RespinNumStatus != nil {
// feature.RespinNumStatus.AddStatus(int(pbcd.CurRespinNum))
// }
// if feature.RespinWinStatus != nil {
// feature.RespinWinStatus.AddStatus(int(pbcd.TotalCoinWin))
// }
// return nil
// }
// // onStatsWithPBEnding -
// func (respin *Respin) onStatsWithPBStart(feature *sgc7stats.Feature, pbComponentData proto.Message, pr *sgc7game.PlayResult) error {
// pbcd, isok := pbComponentData.(*sgc7pb.RespinData)
// if !isok {
// goutils.Error("Respin.onStatsWithPBStart",
// goutils.Err(ErrIvalidProto))
// return ErrIvalidProto
// }
// if feature.RespinStartNumStatus != nil {
// feature.RespinStartNumStatus.AddStatus(int(pbcd.LastRespinNum))
// }
// return nil
// }
// NewComponentData -
func (respin *Respin) NewComponentData() IComponentData {
return &RespinData{}
}
// EachUsedResults -
func (respin *Respin) EachUsedResults(pr *sgc7game.PlayResult, pbComponentData *anypb.Any, oneach FuncOnEachUsedResult) {
pbcd := &sgc7pb.RespinData{}
err := pbComponentData.UnmarshalTo(pbcd)
if err != nil {
goutils.Error("Respin.EachUsedResults:UnmarshalTo",
goutils.Err(err))
return
}
for _, v := range pbcd.BasicComponentData.UsedResults {
oneach(pr.Results[v])
}
}
// ProcRespinOnStepEnd - 现在只有respin需要特殊处理结束,如果多层respin嵌套时,只要新的有next,就不会继续结束respin
func (respin *Respin) ProcRespinOnStepEnd(gameProp *GameProperty, curpr *sgc7game.PlayResult, gp *GameParams, cd IComponentData, canRemove bool) (string, error) {
rcd := cd.(*RespinData)
// rcd.TotalCashWin += curpr.CashWin
rcd.TotalCoinWin += int64(curpr.CoinWin)
if canRemove && rcd.LastRespinNum == 0 && rcd.LastTriggerNum == 0 {
gameProp.removeRespin(respin.Name)
if respin.Config.DefaultNextComponent != "" {
return respin.Config.DefaultNextComponent, nil
}
}
return "", nil
}
// IsRespin -
func (respin *Respin) IsRespin() bool {
return true
}
// NewStats2 -
func (respin *Respin) NewStats2(parent string) *stats2.Feature {
return stats2.NewFeature(parent, stats2.Options{stats2.OptRootTrigger})
}
// OnStats2
func (respin *Respin) OnStats2(icd IComponentData, s2 *stats2.Cache) {
rcd := icd.(*RespinData)
if rcd.LastRespinNum == 0 && rcd.LastTriggerNum == 0 {
s2.ProcStatsRootTrigger(respin.Name, rcd.TotalCoinWin, true)
} else {
s2.ProcStatsRootTrigger(respin.Name, 0, false)
}
}
// // IsTriggerRespin -
// func (respin *Respin) IsTriggerRespin() bool {
// rcd := cd.(*RespinData)
// return false
// }
// // SaveRetriggerRespinNum -
// func (respin *Respin) SaveRetriggerRespinNum(gameProp *GameProperty) {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// cd.RetriggerAddRespinNum = cd.LastRespinNum
// }
// // Retrigger -
// func (respin *Respin) Retrigger(gameProp *GameProperty) {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// cd.LastRespinNum += cd.RetriggerAddRespinNum
// cd.CurAddRespinNum += cd.RetriggerAddRespinNum
// cd.CurTriggerNum++
// if cd.LastTriggerNum > 0 {
// cd.LastTriggerNum--
// }
// }
// // Trigger -
// func (respin *Respin) Trigger(gameProp *GameProperty, plugin sgc7plugin.IPlugin, curpr *sgc7game.PlayResult, gp *GameParams) {
// cd := gameProp.GetGlobalComponentData(respin) //gameProp.MapComponentData[respin.Name].(*RespinData)
// n := cd.TriggerRespinNum[cd.CurTriggerNum]
// if n <= 0 {
// n = cd.RetriggerAddRespinNum
// cd.TriggerRespinNum[cd.CurTriggerNum] = n
// }
// cd.LastRespinNum += n
// cd.CurAddRespinNum += n
// cd.CurTriggerNum++
// if cd.LastTriggerNum > 0 {
// cd.LastTriggerNum--
// }
// for _, v := range cd.Awards {
// if v.TriggerIndex == cd.CurTriggerNum {
// gameProp.procAward(plugin, v, curpr, gp, true)
// }
// }
// }
// // AddRetriggerRespinNum -
// func (respin *Respin) AddRetriggerRespinNum(gameProp *GameProperty, num int) {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// cd.RetriggerAddRespinNum += num
// }
// // AddTriggerAward -
// func (respin *Respin) AddTriggerAward(gameProp *GameProperty, award *Award) {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// award.TriggerIndex = cd.CurTriggerNum + cd.LastTriggerNum
// cd.Awards = append(cd.Awards, award)
// }
// // PushTrigger -
// func (respin *Respin) PushTrigger(gameProp *GameProperty, plugin sgc7plugin.IPlugin, curpr *sgc7game.PlayResult, gp *GameParams, num int) {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// cd.LastTriggerNum++
// cd.TriggerRespinNum = append(cd.TriggerRespinNum, num)
// // 第一次trigger时,需要直接
// if cd.LastRespinNum == 0 && cd.CurRespinNum == 0 {
// respin.Trigger(gameProp, plugin, curpr, gp)
// }
// }
func (respin *Respin) getRetriggerRespinNum(basicCD *BasicComponentData) int {
val, isok := basicCD.GetConfigIntVal(CCVReelSet)
if isok {
return val
}
return 0
}
// // GetLastRespinNum -
// func (respin *Respin) GetLastRespinNum(gameProp *GameProperty) int {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// return cd.LastRespinNum
// }
// // IsEnding -
// func (respin *Respin) IsEnding(gameProp *GameProperty) bool {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// return cd.LastRespinNum == 0 && cd.LastTriggerNum == 0
// }
// // IsStarted -
// func (respin *Respin) IsStarted(gameProp *GameProperty) bool {
// cd := gameProp.MapComponentData[respin.Name].(*RespinData)
// return cd.CurRespinNum > 0
// }
// GetAllLinkComponents - get all link components
func (respin *Respin) GetAllLinkComponents() []string {
return []string{respin.Config.DefaultNextComponent, respin.Config.MainComponent}
}
// GetChildLinkComponents - get child link components
func (respin *Respin) GetChildLinkComponents() []string {
return []string{respin.Config.MainComponent}
}
func NewRespin(name string) IComponent {
return &Respin{
BasicComponent: NewBasicComponent(name, 0),
}
}
// "configuration": {
// "isWinBreak": "false"
// },
type jsonRespin struct {
// IsWinBreak string `json:"isWinBreak"`
}
func (jr *jsonRespin) build() *RespinConfig {
cfg := &RespinConfig{
// IsWinBreak: jr.IsWinBreak == "true",
}
// cfg.UseSceneV3 = true
return cfg
}
func parseRespin(gamecfg *BetConfig, cell *ast.Node) (string, error) {
cfg, label, _, err := getConfigInCell(cell)
if err != nil {
goutils.Error("parseRespin2:getConfigInCell",
goutils.Err(err))
return "", err
}
buf, err := cfg.MarshalJSON()
if err != nil {
goutils.Error("parseRespin2:MarshalJSON",
goutils.Err(err))
return "", err
}
data := &jsonRespin{}
err = sonic.Unmarshal(buf, data)
if err != nil {
goutils.Error("parseRespin2:Unmarshal",
goutils.Err(err))
return "", err
}
cfgd := data.build()
gamecfg.mapConfig[label] = cfgd
gamecfg.mapBasicConfig[label] = &cfgd.BasicComponentConfig
ccfg := &ComponentConfig{
Name: label,
Type: RespinTypeName,
}
gamecfg.Components = append(gamecfg.Components, ccfg)
return label, nil
}