-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiweightawards.go
288 lines (224 loc) · 8.44 KB
/
multiweightawards.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
package lowcode
import (
"fmt"
"log/slog"
"os"
"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"
"google.golang.org/protobuf/proto"
"gopkg.in/yaml.v2"
)
const MultiWeightAwardsTypeName = "multiWeightAwards"
type MultiWeightAwardsData struct {
BasicComponentData
HasGot []bool
}
// OnNewGame -
func (multiWeightAwardsData *MultiWeightAwardsData) OnNewGame(gameProp *GameProperty, component IComponent) {
multiWeightAwardsData.BasicComponentData.OnNewGame(gameProp, component)
multiWeightAwardsData.HasGot = nil
}
// // OnNewStep -
// func (multiWeightAwardsData *MultiWeightAwardsData) OnNewStep(gameProp *GameProperty, component IComponent) {
// multiWeightAwardsData.BasicComponentData.OnNewStep(gameProp, component)
// }
// Clone
func (multiWeightAwardsData *MultiWeightAwardsData) Clone() IComponentData {
target := &MultiWeightAwardsData{
BasicComponentData: multiWeightAwardsData.CloneBasicComponentData(),
}
target.HasGot = make([]bool, len(multiWeightAwardsData.HasGot))
copy(target.HasGot, multiWeightAwardsData.HasGot)
return target
}
// BuildPBComponentData
func (multiWeightAwardsData *MultiWeightAwardsData) BuildPBComponentData() proto.Message {
pbcd := &sgc7pb.MultiWeightAwardsData{
BasicComponentData: multiWeightAwardsData.BuildPBBasicComponentData(),
}
pbcd.HasGot = append(pbcd.HasGot, multiWeightAwardsData.HasGot...)
// for _, s := range multiWeightAwardsData.HasGot {
// pbcd.HasGot = append(pbcd.HasGot, s)
// }
return pbcd
}
type MultiWeightAwardsNode struct {
Awards []*Award `yaml:"awards" json:"awards"` // 新的奖励系统
Weight string `yaml:"weight" json:"weight"` //
VW *sgc7game.ValWeights2 `yaml:"-" json:"-"` //
}
// MultiWeightAwardsConfig - configuration for MultiWeightAwards feature
type MultiWeightAwardsConfig struct {
BasicComponentConfig `yaml:",inline" json:",inline"`
Nodes []*MultiWeightAwardsNode `yaml:"nodes" json:"nodes"`
InitMask string `yaml:"initMask" json:"initMask"` // 用这个来初始化,true表示需要开奖
ReverseInitMask bool `yaml:"reverseInitMask" json:"reverseInitMask"` // reverse the target mask
TargetMask string `yaml:"targetMask" json:"targetMask"` // 用这个来初始化,true表示需要开奖
ReverseTargetMask bool `yaml:"reverseTargetMask" json:"reverseTargetMask"` // reverse the target mask
}
type MultiWeightAwards struct {
*BasicComponent `json:"-"`
Config *MultiWeightAwardsConfig `json:"config"`
}
// Init -
func (multiWeightAwards *MultiWeightAwards) Init(fn string, pool *GamePropertyPool) error {
data, err := os.ReadFile(fn)
if err != nil {
goutils.Error("MultiWeightAwards.Init:ReadFile",
slog.String("fn", fn),
goutils.Err(err))
return err
}
cfg := &MultiWeightAwardsConfig{}
err = yaml.Unmarshal(data, cfg)
if err != nil {
goutils.Error("MultiWeightAwards.Init:Unmarshal",
slog.String("fn", fn),
goutils.Err(err))
return err
}
return multiWeightAwards.InitEx(cfg, pool)
}
// InitEx -
func (multiWeightAwards *MultiWeightAwards) InitEx(cfg any, pool *GamePropertyPool) error {
multiWeightAwards.Config = cfg.(*MultiWeightAwardsConfig)
multiWeightAwards.Config.ComponentType = MultiWeightAwardsTypeName
for _, v := range multiWeightAwards.Config.Nodes {
vw2, err := pool.LoadIntWeights(v.Weight, multiWeightAwards.Config.UseFileMapping)
if err != nil {
goutils.Error("MultiWeightAwards.Init:LoadIntWeights",
slog.String("Weight", v.Weight),
goutils.Err(err))
return err
}
v.VW = vw2
for _, award := range v.Awards {
award.Init()
}
}
multiWeightAwards.onInit(&multiWeightAwards.Config.BasicComponentConfig)
return nil
}
// InitEx -
func (multiWeightAwards *MultiWeightAwards) buildMask(plugin sgc7plugin.IPlugin, gameProp *GameProperty, curpr *sgc7game.PlayResult, gp *GameParams, cd *MultiWeightAwardsData) error {
if multiWeightAwards.Config.ReverseTargetMask {
mask := make([]bool, len(cd.HasGot))
for i := 0; i < len(cd.HasGot); i++ {
mask[i] = !cd.HasGot[i]
}
return gameProp.Pool.SetMask(plugin, gameProp, curpr, gp, multiWeightAwards.Config.TargetMask, mask, true)
}
return gameProp.Pool.SetMask(plugin, gameProp, curpr, gp, multiWeightAwards.Config.TargetMask, cd.HasGot, true)
}
// playgame
func (multiWeightAwards *MultiWeightAwards) OnPlayGame(gameProp *GameProperty, curpr *sgc7game.PlayResult, gp *GameParams, plugin sgc7plugin.IPlugin,
cmd string, param string, ps sgc7game.IPlayerState, stake *sgc7game.Stake, prs []*sgc7game.PlayResult, cd IComponentData) (string, error) {
// multiWeightAwards.onPlayGame(gameProp, curpr, gp, plugin, cmd, param, ps, stake, prs)
mwad := cd.(*MultiWeightAwardsData)
mwad.HasGot = nil
if multiWeightAwards.Config.InitMask != "" {
mask, err := gameProp.Pool.GetMask(multiWeightAwards.Config.InitMask, gameProp)
if err != nil {
goutils.Error("MultiWeightAwards.OnPlayGame:GetMask",
slog.String("mask", multiWeightAwards.Config.InitMask),
goutils.Err(err))
return "", err
}
for i, maskv := range mask {
if maskv == !multiWeightAwards.Config.ReverseInitMask {
v := multiWeightAwards.Config.Nodes[i]
cv, err := v.VW.RandVal(plugin)
if err != nil {
goutils.Error("MultiWeightAwards.OnPlayGame:RandVal",
goutils.Err(err))
return "", err
}
if cv.Int() != 0 {
if len(v.Awards) > 0 {
gameProp.procAwards(plugin, v.Awards, curpr, gp)
}
mwad.HasGot = append(mwad.HasGot, true)
} else {
mwad.HasGot = append(mwad.HasGot, false)
}
} else {
mwad.HasGot = append(mwad.HasGot, false)
}
}
} else {
for _, v := range multiWeightAwards.Config.Nodes {
cv, err := v.VW.RandVal(plugin)
if err != nil {
goutils.Error("MultiWeightAwards.OnPlayGame:RandVal",
goutils.Err(err))
return "", err
}
if cv.Int() != 0 {
if len(v.Awards) > 0 {
gameProp.procAwards(plugin, v.Awards, curpr, gp)
}
mwad.HasGot = append(mwad.HasGot, true)
} else {
mwad.HasGot = append(mwad.HasGot, false)
}
}
}
if multiWeightAwards.Config.TargetMask != "" {
err := multiWeightAwards.buildMask(plugin, gameProp, curpr, gp, mwad)
if err != nil {
goutils.Error("MultiWeightAwards.OnPlayGame:buildMask",
slog.String("mask", multiWeightAwards.Config.TargetMask),
goutils.Err(err))
return "", err
}
}
nc := multiWeightAwards.onStepEnd(gameProp, curpr, gp, "")
return nc, nil
}
// OnAsciiGame - outpur to asciigame
func (multiWeightAwards *MultiWeightAwards) OnAsciiGame(gameProp *GameProperty, pr *sgc7game.PlayResult, lst []*sgc7game.PlayResult, mapSymbolColor *asciigame.SymbolColorMap, cd IComponentData) error {
mcd := cd.(*MultiWeightAwardsData)
if len(mcd.HasGot) > 0 {
fmt.Printf("MultiWeightAwards result is %v\n", mcd.HasGot)
}
return nil
}
// // OnStats
// func (multiWeightAwards *MultiWeightAwards) OnStats(feature *sgc7stats.Feature, stake *sgc7game.Stake, lst []*sgc7game.PlayResult) (bool, int64, int64) {
// return false, 0, 0
// }
// // OnStatsWithPB -
// func (multiWeightAwards *MultiWeightAwards) OnStatsWithPB(feature *sgc7stats.Feature, pbComponentData proto.Message, pr *sgc7game.PlayResult) (int64, error) {
// pbcd, isok := pbComponentData.(*sgc7pb.MultiWeightAwardsData)
// if !isok {
// goutils.Error("MultiWeightAwards.OnStatsWithPB",
// goutils.Err(ErrIvalidProto))
// return 0, ErrIvalidProto
// }
// return multiWeightAwards.OnStatsWithPBBasicComponentData(feature, pbcd.BasicComponentData, pr), nil
// }
// NewComponentData -
func (multiWeightAwards *MultiWeightAwards) NewComponentData() IComponentData {
return &MultiWeightAwardsData{}
}
// // EachUsedResults -
// func (multiWeightAwards *MultiWeightAwards) EachUsedResults(pr *sgc7game.PlayResult, pbComponentData *anypb.Any, oneach FuncOnEachUsedResult) {
// pbcd := &sgc7pb.SymbolCollectionData{}
// err := pbComponentData.UnmarshalTo(pbcd)
// if err != nil {
// goutils.Error("MultiWeightAwards.EachUsedResults:UnmarshalTo",
// goutils.Err(err))
// return
// }
// for _, v := range pbcd.BasicComponentData.UsedResults {
// oneach(pr.Results[v])
// }
// }
func NewMultiWeightAwards(name string) IComponent {
return &MultiWeightAwards{
BasicComponent: NewBasicComponent(name, 0),
}
}