-
Notifications
You must be signed in to change notification settings - Fork 1
/
callstack.go
284 lines (217 loc) · 7.25 KB
/
callstack.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
package lowcode
import (
"fmt"
"log/slog"
"github.com/zhs007/goutils"
sgc7game "github.com/zhs007/slotsgamecore7/game"
)
// 关于 onNewGame 和 onNewStep
// 1. callstack不会跨game的保留数据,所以每次新游戏,callstack都会是一个空的
// 2. callstack只会保留主调用堆栈的数据,且当componentData第一次被获取时,执行OnNewStep
// 关于 callstack 层级
// 1. 至少有一个 global,所有component都会有一个global对象
// 2. 可能会有 foreach 层,一个循环体,同时只会有一层(i = 0; i < 10;)这种,同时也只会有一层,这个 foreach 层,只会有当前子组件对象(只可能是连线对象)
// 这些对象在一开始构造,其它的component对象在这一层是找不到的
// 3. respin 不能出现在 foreach 层
// FuncOnEachHistoryComponent -
type FuncOnEachHistoryComponent func(tag string, gameProp *GameProperty, ic IComponent, cd IComponentData) error
type callStackNode struct {
CoreComponent IComponent
Name string
MapComponentData map[string]IComponentData
mapHistory map[string]IComponentData
SymbolCode int
CurIndex int
isNoAutoNew bool
cacheSceneIndex int
cacheOtherSceneIndex int
}
func (csn *callStackNode) IsSame(component IComponent, symbolCode int, i int) bool {
return csn.CoreComponent == component && csn.SymbolCode == symbolCode && csn.CurIndex == i
}
func (csn *callStackNode) IsInCallStack(componentName string) bool {
_, isok := csn.mapHistory[componentName]
return isok
}
func (csn *callStackNode) addComponentData(ic IComponent) {
cd := ic.NewComponentData()
csn.MapComponentData[ic.GetName()] = cd
}
func (csn *callStackNode) onCallEnd(ic IComponent, cd IComponentData) {
csn.mapHistory[ic.GetName()] = cd
}
func (csn *callStackNode) genTag(ic IComponent) string {
if csn.Name == "" {
return ic.GetName()
}
return fmt.Sprintf("%v/%v", csn.Name, ic.GetName())
}
func (csn *callStackNode) GetComponentData(gameProp *GameProperty, ic IComponent) IComponentData {
name := ic.GetName()
cd, isok := csn.MapComponentData[name]
if !isok {
if csn.isNoAutoNew {
return nil
}
cd = ic.NewComponentData()
cd.OnNewGame(gameProp, ic)
csn.MapComponentData[name] = cd
csn.mapHistory[name] = cd
}
// else {
// _, isok := csn.mapHistory[name]
// if !isok {
// cd.OnNewStep(gameProp, ic)
// csn.mapHistory[name] = cd
// }
// }
return cd
}
func (csn *callStackNode) OnNewStep() {
csn.mapHistory = make(map[string]IComponentData)
}
func newGlobalCallStackNode() *callStackNode {
return &callStackNode{
mapHistory: make(map[string]IComponentData),
MapComponentData: make(map[string]IComponentData),
}
}
func newEachSymbolCallStackNode(component IComponent, symbolCode int, i int, cacheSceneIndex int, cacheOtherSceneIndex int, pt *sgc7game.PayTables) *callStackNode {
return &callStackNode{
CoreComponent: component,
Name: fmt.Sprintf("%v:%v>%v", component.GetName(), i, pt.GetStringFromInt(symbolCode)),
mapHistory: make(map[string]IComponentData),
MapComponentData: make(map[string]IComponentData),
SymbolCode: symbolCode,
CurIndex: i,
isNoAutoNew: true,
cacheSceneIndex: cacheSceneIndex,
cacheOtherSceneIndex: cacheOtherSceneIndex,
}
}
type callStackHistoryNode struct {
tag string
component IComponent
cd IComponentData
}
type CallStack struct {
nodes []*callStackNode
historyNodes []*callStackHistoryNode
}
// GetGlobalComponentData -
func (cs *CallStack) GetGlobalComponentData(gameProp *GameProperty, ic IComponent) IComponentData {
return cs.nodes[0].GetComponentData(gameProp, ic)
}
func (cs *CallStack) GetCurComponentData(gameProp *GameProperty, ic IComponent) IComponentData {
return cs.nodes[len(cs.nodes)-1].GetComponentData(gameProp, ic)
}
func (cs *CallStack) GetComponentData(gameProp *GameProperty, ic IComponent) IComponentData {
if len(cs.nodes) == 1 {
return cs.nodes[0].GetComponentData(gameProp, ic)
}
cd := cs.nodes[len(cs.nodes)-1].GetComponentData(gameProp, ic)
if cd == nil {
return cs.nodes[0].GetComponentData(gameProp, ic)
}
return cd
}
func (cs *CallStack) GetComponentNum() int {
return len(cs.historyNodes)
}
func (cs *CallStack) OnNewGame() {
cs.nodes = cs.nodes[:0]
cs.nodes = append(cs.nodes, newGlobalCallStackNode())
cs.historyNodes = nil
}
func (cs *CallStack) OnNewStep() {
cs.nodes = cs.nodes[0:1]
cs.nodes[0].OnNewStep()
cs.historyNodes = nil
}
func (cs *CallStack) Each(gameProp *GameProperty, onEach FuncOnEachHistoryComponent) error {
for i, node := range cs.historyNodes {
err := onEach(node.tag, gameProp, node.component, node.cd)
if err != nil {
goutils.Error("CallStack.Each",
slog.Int("i", i),
slog.String("tag", node.tag),
goutils.Err(err))
return err
}
}
return nil
}
func (cs *CallStack) genTag(component IComponent) string {
if len(cs.nodes) == 1 {
return component.GetName()
}
tag := ""
for i, node := range cs.nodes {
if i != 0 {
tag += "/"
}
tag += node.Name
}
tag += "/"
tag += component.GetName()
return tag
}
func (cs *CallStack) ComponentDone(gameProp *GameProperty, component IComponent, cd IComponentData) {
cs.historyNodes = append(cs.historyNodes, &callStackHistoryNode{
tag: cs.genTag(component),
component: component,
cd: cd,
})
}
func (cs *CallStack) IsInCurCallStack(componentName string) bool {
return cs.nodes[len(cs.nodes)-1].IsInCallStack(componentName)
}
func (cs *CallStack) GetCurCallStackSymbol() int {
if len(cs.nodes) == 1 {
return -1
}
return cs.nodes[len(cs.nodes)-1].SymbolCode
}
func (cs *CallStack) OnCallEnd(ic IComponent, cd IComponentData) string {
cs.nodes[len(cs.nodes)-1].onCallEnd(ic, cd)
tag := cs.nodes[len(cs.nodes)-1].genTag(ic)
cs.historyNodes = append(cs.historyNodes, &callStackHistoryNode{
tag: tag,
component: ic,
cd: cd,
})
return tag
}
func (cs *CallStack) StartEachSymbols(gameProp *GameProperty, component IComponent, children []string, symbolCode int, i int) error {
node := newEachSymbolCallStackNode(component, symbolCode, i, len(gameProp.SceneStack.Scenes), len(gameProp.OtherSceneStack.Scenes), gameProp.Pool.DefaultPaytables)
components := gameProp.Components
for _, v := range children {
ic, isok := components.MapComponents[v]
if !isok {
goutils.Error("CallStack.StartEachSymbols:children",
goutils.Err(ErrInvalidComponentName))
return ErrInvalidComponentName
}
node.addComponentData(ic)
}
cs.nodes = append(cs.nodes, node)
return nil
}
func (cs *CallStack) onEachSymbolsEnd(gameProp *GameProperty, component IComponent, symbolCode int, i int) error {
if len(cs.nodes) > 1 && cs.nodes[len(cs.nodes)-1].IsSame(component, symbolCode, i) {
gameProp.SceneStack.PopEx(cs.nodes[len(cs.nodes)-1].cacheSceneIndex)
gameProp.OtherSceneStack.PopEx(cs.nodes[len(cs.nodes)-1].cacheOtherSceneIndex)
cs.nodes = cs.nodes[:len(cs.nodes)-1]
return nil
}
goutils.Error("CallStack.onEachSymbolsEnd",
slog.String("component", component.GetName()),
slog.Int("symbolCode", symbolCode),
slog.Int("i", i),
goutils.Err(ErrInvalidCallStackNode))
return ErrInvalidCallStackNode
}
func NewCallStack() *CallStack {
cs := &CallStack{}
return cs
}