-
Notifications
You must be signed in to change notification settings - Fork 927
/
cachedruleset.go
114 lines (93 loc) · 2.44 KB
/
cachedruleset.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
package automod
import (
"encoding/json"
"emperror.dev/errors"
"github.com/jonas747/yagpdb/automod/models"
)
type ParsedRuleset struct {
RSModel *models.AutomodRuleset
ParsedConditions []*ParsedPart
Rules []*ParsedRule
}
type ParsedRule struct {
Model *models.AutomodRule
Triggers []*ParsedPart
Conditions []*ParsedPart
Effects []*ParsedPart
}
type ParsedPart struct {
// Parts are either children directly of the ruleset, ad ruleset conditions or as children of individual rules
ParentRule *ParsedRule
ParentRS *ParsedRuleset
RuleModel *models.AutomodRuleDatum
RSConditionModel *models.AutomodRulesetCondition
Part RulePart
ParsedSettings interface{}
}
func ParseRuleset(rs *models.AutomodRuleset) (*ParsedRuleset, error) {
result := &ParsedRuleset{
RSModel: rs,
}
result.ParsedConditions = make([]*ParsedPart, len(rs.R.RulesetAutomodRulesetConditions))
result.Rules = make([]*ParsedRule, len(rs.R.RulesetAutomodRules))
for i, v := range rs.R.RulesetAutomodRulesetConditions {
partType := RulePartMap[v.TypeID]
dst := partType.DataType()
if dst != nil {
err := json.Unmarshal(v.Settings, dst)
if err != nil {
return nil, errors.WithMessage(err, "rsconditions, json")
}
}
result.ParsedConditions[i] = &ParsedPart{
ParentRS: result,
RSConditionModel: v,
Part: partType,
ParsedSettings: dst,
}
}
for i, v := range rs.R.RulesetAutomodRules {
parsed, err := ParseRuleData(v)
if err != nil {
return nil, err
}
result.Rules[i] = parsed
}
return result, nil
}
func ParseRuleData(rule *models.AutomodRule) (*ParsedRule, error) {
var triggers []*ParsedPart
var conditions []*ParsedPart
var effects []*ParsedPart
pr := &ParsedRule{
Model: rule,
}
for _, v := range rule.R.RuleAutomodRuleData {
partType := RulePartMap[v.TypeID]
dst := partType.DataType()
if dst != nil {
err := json.Unmarshal(v.Settings, dst)
if err != nil {
return nil, errors.WithMessage(err, "rule, json")
}
}
p := &ParsedPart{
ParentRule: pr,
RuleModel: v,
Part: partType,
ParsedSettings: dst,
}
switch RulePartType(v.Kind) {
case RulePartTrigger:
triggers = append(triggers, p)
case RulePartCondition:
conditions = append(conditions, p)
case RulePartEffect:
effects = append(effects, p)
}
}
pr.Triggers = triggers
pr.Conditions = conditions
pr.Effects = effects
return pr, nil
}