Skip to content

Commit 850428c

Browse files
committed
adding rand move
1 parent 69a51a2 commit 850428c

File tree

5 files changed

+53
-67
lines changed

5 files changed

+53
-67
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
with:
1919
go-version: '1.21'
2020
- name: Run GoReleaser
21-
uses: goreleaser/goreleaser-action@v4
21+
uses: goreleaser/goreleaser-action@v5
2222
with:
2323
distribution: goreleaser
2424
version: latest

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
*.iml
2+
*.iml
3+
dist

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Commands
2+
3+
## Mouse
4+
type: mouse
5+
button: [left, center, right, wheelDown, wheelUp, wheelLeft, wheelRight]

main.go

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/go-vgo/robotgo"
6+
"math/rand"
67
"time"
78

89
//"github.com/go-vgo/robotgo"
@@ -11,70 +12,54 @@ import (
1112
"os"
1213
)
1314

14-
// type Config struct {
15-
// Commands []struct {
16-
// Type string `yaml:"type"`
17-
// } `yaml:"commands"`
18-
// }
1915
type Config struct {
2016
Commands []struct {
21-
Type string
22-
X int
23-
Y int
24-
MinX int
25-
MinY int
26-
MaxX string
27-
MaxY string
28-
Button string
29-
Duration int64
17+
Type *string
18+
X *int
19+
Y *int
20+
MinX *int `yaml:"minX"`
21+
MinY *int `yaml:"minY"`
22+
MaxX *int `yaml:"maxX"`
23+
MaxY *int `yaml:"maxY"`
24+
Button *string
25+
Duration *int64
3026
}
3127
}
3228

33-
//type Command struct {
34-
// Type string
35-
// X int
36-
// Y int
37-
// MinX int
38-
// MinY int
39-
// MaxX string
40-
// MaxY string
41-
// Button string
42-
// Duration int
43-
//}
44-
4529
func main() {
46-
4730
file, err := os.Open("script.yaml")
4831
if err != nil {
4932
panic("unable to open configuration file script.yaml")
5033
}
51-
x, err := io.ReadAll(file)
34+
rawConfig, err := io.ReadAll(file)
5235
if err != nil {
53-
panic("failed reading script.yaml")
36+
panic(fmt.Errorf("failed reading script.yaml: %w", err))
5437
}
5538

5639
var config Config
57-
yaml.Unmarshal(x, &config)
58-
for _, command := range config.Commands {
59-
fmt.Println(command)
60-
switch command.Type {
61-
case "move":
62-
robotgo.Move(command.X, command.Y)
63-
case "click":
64-
robotgo.Click(command.Button)
65-
case "sleep":
66-
fmt.Printf("Sleeping for %d milliseconds.", command.Duration)
67-
time.Sleep(time.Duration(command.Duration) * time.Millisecond)
40+
err = yaml.Unmarshal(rawConfig, &config)
41+
if err != nil {
42+
panic(fmt.Errorf("failed to parse script.yaml: %w", err))
43+
}
44+
for {
45+
for _, command := range config.Commands {
46+
switch *command.Type {
47+
case "move":
48+
if command.X != nil && command.Y != nil {
49+
robotgo.MoveSmooth(*command.X, *command.Y)
50+
} else if command.MinX != nil && command.MinY != nil && command.MaxX != nil && command.MaxY != nil {
51+
x := rand.Intn(*command.MaxX-*command.MinX) + *command.MinX
52+
y := rand.Intn(*command.MaxY-*command.MinY) + *command.MinY
53+
robotgo.MoveSmooth(x, y)
54+
} else {
55+
panic(fmt.Sprintf("Invalid move configuration: %+v", command))
56+
}
57+
case "click":
58+
robotgo.Click(command.Button)
59+
case "sleep":
60+
fmt.Printf("Sleeping for %d milliseconds.", command.Duration)
61+
time.Sleep(time.Duration(*command.Duration) * time.Millisecond)
62+
}
6863
}
6964
}
70-
71-
//if ok {
72-
//
73-
// for _, command := range commands {
74-
// if a, ok := command.(map[string]any); ok {
75-
// fmt.Println(a["type"])
76-
// }
77-
//
78-
// }
79-
//}
8065
}

script.yaml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
commands:
22
- type: move
3-
x: 1
4-
y: 1
3+
minX: 200
4+
minY: 200
5+
maxX: 400
6+
maxY: 400
57
- type: sleep
6-
duration: 3000
7-
- type: move
8-
x: 100
9-
y: 100
10-
- type: sleep
11-
duration: 3000
12-
- type: move
13-
x: 300
14-
y: 300
15-
- type: sleep
16-
duration: 3000
17-
- type: click
18-
button: right
8+
duration: 5000
9+
10+
# - type: sleep
11+
# duration: 3000
12+
# - type: click
13+
# button: right

0 commit comments

Comments
 (0)