/
componentvaltrigger.go
165 lines (132 loc) · 4.69 KB
/
componentvaltrigger.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
package lowcode
import (
"log/slog"
"os"
"github.com/zhs007/goutils"
"github.com/zhs007/slotsgamecore7/asciigame"
sgc7game "github.com/zhs007/slotsgamecore7/game"
sgc7plugin "github.com/zhs007/slotsgamecore7/plugin"
"gopkg.in/yaml.v2"
)
const ComponentValTriggerTypeName = "componentValTrigger"
type OperateType int
const (
OTEqual OperateType = 0 // ==
OTGreaterEqual OperateType = 1 // >=
OTLessEqual OperateType = 2 // <=
OTGreater OperateType = 3 // >
OTLess OperateType = 4 // <
OTNotEqual OperateType = 5 // !=
)
func ParseOperateType(str string) OperateType {
if str == ">=" {
return OTGreaterEqual
} else if str == "<=" {
return OTLessEqual
} else if str == ">" {
return OTGreater
} else if str == "<" {
return OTLess
} else if str == "!=" {
return OTNotEqual
}
return OTEqual
}
// ComponentValTriggerConfig - configuration for ComponentValTrigger
type ComponentValTriggerConfig struct {
BasicComponentConfig `yaml:",inline" json:",inline"`
ComponentVals []string `yaml:"componentVals" json:"componentVals"` // 用来检查的值,bg-wins.wins 这样的命名方式
OperateString []string `yaml:"operate" json:"operate"` // ==/>=/<=/>/</!=
Operate []OperateType `yaml:"-" json:"-"` //
TargetVals []int `yaml:"targetVals" json:"targetVals"` // 目标值
JumpToComponent string `yaml:"jumpToComponent" json:"jumpToComponent"` // jump to
}
type ComponentValTrigger struct {
*BasicComponent `json:"-"`
Config *ComponentValTriggerConfig `json:"config"`
}
// Init -
func (componentValTrigger *ComponentValTrigger) Init(fn string, pool *GamePropertyPool) error {
data, err := os.ReadFile(fn)
if err != nil {
goutils.Error("ComponentValTrigger.Init:ReadFile",
slog.String("fn", fn),
goutils.Err(err))
return err
}
cfg := &ComponentValTriggerConfig{}
err = yaml.Unmarshal(data, cfg)
if err != nil {
goutils.Error("ComponentValTrigger.Init:Unmarshal",
slog.String("fn", fn),
goutils.Err(err))
return err
}
return componentValTrigger.InitEx(cfg, pool)
}
// InitEx -
func (componentValTrigger *ComponentValTrigger) InitEx(cfg any, pool *GamePropertyPool) error {
componentValTrigger.Config = cfg.(*ComponentValTriggerConfig)
componentValTrigger.Config.ComponentType = ComponentValTriggerTypeName
for _, v := range componentValTrigger.Config.OperateString {
componentValTrigger.Config.Operate = append(componentValTrigger.Config.Operate, ParseOperateType(v))
}
componentValTrigger.onInit(&componentValTrigger.Config.BasicComponentConfig)
return nil
}
func (componentValTrigger *ComponentValTrigger) check(gameProp *GameProperty, valname string, op OperateType, target int) bool {
val, err := gameProp.GetComponentVal(valname)
if err != nil {
goutils.Error("ComponentValTrigger.check:GetComponentVal",
slog.String("ComponentVal", valname),
goutils.Err(err))
return false
}
switch op {
case OTEqual:
return (val == target)
case OTNotEqual:
return (val != target)
case OTGreaterEqual:
return (val >= target)
case OTLessEqual:
return (val <= target)
case OTGreater:
return (val > target)
case OTLess:
return (val < target)
}
return false
}
// playgame
func (componentValTrigger *ComponentValTrigger) 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) {
// componentValTrigger.onPlayGame(gameProp, curpr, gp, plugin, cmd, param, ps, stake, prs)
isTrigger := true
for i, valname := range componentValTrigger.Config.ComponentVals {
if !componentValTrigger.check(gameProp, valname, componentValTrigger.Config.Operate[i], componentValTrigger.Config.TargetVals[i]) {
isTrigger = false
break
}
}
nc := ""
if isTrigger {
nc = componentValTrigger.onStepEnd(gameProp, curpr, gp, componentValTrigger.Config.JumpToComponent)
} else {
nc = componentValTrigger.onStepEnd(gameProp, curpr, gp, "")
}
return nc, nil
}
// OnAsciiGame - outpur to asciigame
func (componentTrigger *ComponentValTrigger) OnAsciiGame(gameProp *GameProperty, pr *sgc7game.PlayResult, lst []*sgc7game.PlayResult, mapSymbolColor *asciigame.SymbolColorMap, cd IComponentData) error {
return nil
}
// // OnStats
// func (componentTrigger *ComponentValTrigger) OnStats(feature *sgc7stats.Feature, stake *sgc7game.Stake, lst []*sgc7game.PlayResult) (bool, int64, int64) {
// return false, 0, 0
// }
func NewComponentValTrigger(name string) IComponent {
return &ComponentValTrigger{
BasicComponent: NewBasicComponent(name, 0),
}
}