-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.go
More file actions
342 lines (293 loc) · 9.99 KB
/
node.go
File metadata and controls
342 lines (293 loc) · 9.99 KB
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
package maa
import (
"maps"
"slices"
"time"
)
// Node represents a single task node in the pipeline.
type Node struct {
Name string `json:"-"`
// Anchor maps anchor name to target node name. This matches GetNodeData output format.
Anchor map[string]string `json:"anchor,omitempty"`
// Recognition defines how this node recognizes targets on screen.
Recognition *Recognition `json:"recognition,omitempty"`
// Action defines what action to perform when recognition succeeds.
Action *Action `json:"action,omitempty"`
// Next specifies the list of possible next nodes to execute.
Next []NextItem `json:"next,omitempty"`
// RateLimit sets the minimum interval between recognition attempts in milliseconds. Default: 1000.
RateLimit *int64 `json:"rate_limit,omitempty"`
// Timeout sets the maximum time to wait for recognition in milliseconds. Default: 20000.
Timeout *int64 `json:"timeout,omitempty"`
// OnError specifies nodes to execute when recognition times out or action execution fails.
OnError []NextItem `json:"on_error,omitempty"`
// Inverse inverts the recognition result. Default: false.
Inverse bool `json:"inverse,omitempty"`
// Enabled determines whether this node is active. Default: true.
Enabled *bool `json:"enabled,omitempty"`
// MaxHit sets the maximum hit count of the node. Default: unlimited.
MaxHit *uint64 `json:"max_hit,omitempty"`
// PreDelay sets the delay before action execution in milliseconds. Default: 200.
PreDelay *int64 `json:"pre_delay,omitempty"`
// PostDelay sets the delay after action execution in milliseconds. Default: 200.
PostDelay *int64 `json:"post_delay,omitempty"`
// PreWaitFreezes waits for screen to stabilize before action execution.
PreWaitFreezes *WaitFreezesParam `json:"pre_wait_freezes,omitempty"`
// PostWaitFreezes waits for screen to stabilize after action.
PostWaitFreezes *WaitFreezesParam `json:"post_wait_freezes,omitempty"`
// Repeat specifies the number of times to repeat the node. Default: 1.
Repeat *uint64 `json:"repeat,omitempty"`
// RepeatDelay sets the delay between repetitions in milliseconds. Default: 0.
RepeatDelay *int64 `json:"repeat_delay,omitempty"`
// RepeatWaitFreezes waits for screen to stabilize between repetitions.
RepeatWaitFreezes *WaitFreezesParam `json:"repeat_wait_freezes,omitempty"`
// Focus specifies custom focus data.
Focus any `json:"focus,omitempty"`
// Attach provides additional custom data for the node.
Attach map[string]any `json:"attach,omitempty"`
}
// NewNode creates a new Node with the given name.
func NewNode(name string) *Node {
return &Node{
Name: name,
Attach: make(map[string]any),
}
}
// SetAnchor sets the anchor for the node and returns the node for chaining.
func (n *Node) SetAnchor(anchor map[string]string) *Node {
n.Anchor = maps.Clone(anchor)
return n
}
// SetAnchorTarget sets an anchor to a specific target node and returns the node for chaining.
func (n *Node) SetAnchorTarget(anchor, nodeName string) *Node {
if anchor == "" {
return n
}
if n.Anchor == nil {
n.Anchor = make(map[string]string)
}
n.Anchor[anchor] = nodeName
return n
}
// SetRecognition sets the recognition for the node and returns the node for chaining.
func (n *Node) SetRecognition(rec *Recognition) *Node {
n.Recognition = rec
return n
}
// SetAction sets the action for the node and returns the node for chaining.
func (n *Node) SetAction(act *Action) *Node {
n.Action = act
return n
}
// SetNext sets the next nodes list for the node and returns the node for chaining.
func (n *Node) SetNext(next []NextItem) *Node {
n.Next = slices.Clone(next)
return n
}
// SetRateLimit sets the rate limit for the node and returns the node for chaining.
func (n *Node) SetRateLimit(rateLimit time.Duration) *Node {
d := rateLimit.Milliseconds()
n.RateLimit = &d
return n
}
// SetTimeout sets the timeout for the node and returns the node for chaining.
func (n *Node) SetTimeout(timeout time.Duration) *Node {
d := timeout.Milliseconds()
n.Timeout = &d
return n
}
// SetOnError sets the error handling nodes for the node and returns the node for chaining.
func (n *Node) SetOnError(onError []NextItem) *Node {
n.OnError = slices.Clone(onError)
return n
}
// SetInverse sets whether to invert the recognition result and returns the node for chaining.
func (n *Node) SetInverse(inverse bool) *Node {
n.Inverse = inverse
return n
}
// SetEnabled sets whether the node is enabled and returns the node for chaining.
func (n *Node) SetEnabled(enabled bool) *Node {
n.Enabled = &enabled
return n
}
// SetMaxHit sets the maximum hit count of the node and returns the node for chaining.
func (n *Node) SetMaxHit(maxHit uint64) *Node {
n.MaxHit = &maxHit
return n
}
// SetPreDelay sets the delay before action execution and returns the node for chaining.
func (n *Node) SetPreDelay(preDelay time.Duration) *Node {
d := preDelay.Milliseconds()
n.PreDelay = &d
return n
}
// SetPostDelay sets the delay after action execution and returns the node for chaining.
func (n *Node) SetPostDelay(postDelay time.Duration) *Node {
d := postDelay.Milliseconds()
n.PostDelay = &d
return n
}
// SetPreWaitFreezes sets the pre-action wait freezes configuration and returns the node for chaining.
func (n *Node) SetPreWaitFreezes(preWaitFreezes *WaitFreezesParam) *Node {
n.PreWaitFreezes = preWaitFreezes
return n
}
// SetPostWaitFreezes sets the post-action wait freezes configuration and returns the node for chaining.
func (n *Node) SetPostWaitFreezes(postWaitFreezes *WaitFreezesParam) *Node {
n.PostWaitFreezes = postWaitFreezes
return n
}
// SetRepeat sets the number of times to repeat the node and returns the node for chaining.
func (n *Node) SetRepeat(repeat uint64) *Node {
n.Repeat = &repeat
return n
}
// SetRepeatDelay sets the delay between repetitions and returns the node for chaining.
func (n *Node) SetRepeatDelay(repeatDelay time.Duration) *Node {
d := repeatDelay.Milliseconds()
n.RepeatDelay = &d
return n
}
// SetRepeatWaitFreezes sets the wait freezes configuration between repetitions and returns the node for chaining.
func (n *Node) SetRepeatWaitFreezes(repeatWaitFreezes *WaitFreezesParam) *Node {
n.RepeatWaitFreezes = repeatWaitFreezes
return n
}
// SetFocus sets the focus data for the node and returns the node for chaining.
func (n *Node) SetFocus(focus any) *Node {
n.Focus = focus
return n
}
// SetAttach sets the attached custom data for the node and returns the node for chaining.
// The map is copied so the node does not share state with the caller.
// A nil attach is stored as an empty map so that Attach is never nil.
func (n *Node) SetAttach(attach map[string]any) *Node {
if attach == nil {
n.Attach = make(map[string]any)
} else {
n.Attach = maps.Clone(attach)
}
return n
}
// NextItem is one item in the list of nodes to run next.
// It is used in Node.Next (on success) and Node.OnError (on failure).
type NextItem struct {
// Name is the name of the target node.
Name string `json:"name"`
// JumpBack indicates whether to jump back to the parent node after this node's chain completes.
JumpBack bool `json:"jump_back"`
// Anchor indicates whether this node should be set as the anchor.
Anchor bool `json:"anchor"`
}
// FormatName returns the name with attribute prefixes, e.g. [JumpBack]NodeA.
func (i NextItem) FormatName() string {
name := i.Name
if i.JumpBack {
name = "[JumpBack]" + name
}
if i.Anchor {
name = "[Anchor]" + name
}
return name
}
// NodeAttributeOption is a functional option for configuring NextItem attributes.
type NodeAttributeOption func(*NextItem)
// WithJumpBack enables the jump-back mechanism. When this node matches, the system returns
// to the parent node after completing this node's chain, and continues recognizing from the start of next list.
func WithJumpBack() NodeAttributeOption {
return func(i *NextItem) {
i.JumpBack = true
}
}
// WithAnchor enables anchor reference. The name field will be treated as an anchor name
// and resolved to the last node that set this anchor at runtime.
func WithAnchor() NodeAttributeOption {
return func(i *NextItem) {
i.Anchor = true
}
}
// AddAnchor sets an anchor to the current node and returns the node for chaining.
func (n *Node) AddAnchor(anchor string) *Node {
return n.SetAnchorTarget(anchor, n.Name)
}
// ClearAnchor marks an anchor as cleared and returns the node for chaining.
func (n *Node) ClearAnchor(anchor string) *Node {
return n.SetAnchorTarget(anchor, "")
}
// RemoveAnchor removes an anchor from the node and returns the node for chaining.
func (n *Node) RemoveAnchor(anchor string) *Node {
if anchor == "" {
return n
}
delete(n.Anchor, anchor)
return n
}
// AddNext appends a node to the next list and returns the node for chaining.
func (n *Node) AddNext(name string, opts ...NodeAttributeOption) *Node {
if name == "" {
return n
}
newItem := NextItem{
Name: name,
}
for _, opt := range opts {
opt(&newItem)
}
found := false
for i, item := range n.Next {
if item.Name == name {
n.Next[i] = newItem
found = true
break
}
}
if !found {
n.Next = append(n.Next, newItem)
}
return n
}
// RemoveNext removes a node from the next list and returns the node for chaining.
func (n *Node) RemoveNext(name string) *Node {
if name == "" {
return n
}
n.Next = slices.DeleteFunc(n.Next, func(item NextItem) bool {
return item.Name == name
})
return n
}
// AddOnError appends a node to the on_error list and returns the node for chaining.
func (n *Node) AddOnError(name string, opts ...NodeAttributeOption) *Node {
if name == "" {
return n
}
newItem := NextItem{
Name: name,
}
for _, opt := range opts {
opt(&newItem)
}
found := false
for i, item := range n.OnError {
if item.Name == name {
n.OnError[i] = newItem
found = true
break
}
}
if !found {
n.OnError = append(n.OnError, newItem)
}
return n
}
// RemoveOnError removes a node from the on_error list and returns the node for chaining.
func (n *Node) RemoveOnError(name string) *Node {
if name == "" {
return n
}
n.OnError = slices.DeleteFunc(n.OnError, func(item NextItem) bool {
return item.Name == name
})
return n
}