-
Notifications
You must be signed in to change notification settings - Fork 3
/
clicker.go
46 lines (37 loc) · 1.31 KB
/
clicker.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
package helios
import (
"github.com/go-vgo/robotgo"
"math"
"math/rand"
"time"
)
type Clicker struct {
}
// Click clicks at a random X and Y coordinate within the given location.
// This doesn't use the standard Click() function of any library which is easy
// to detect as an automated click - rather it adds entropy throughout the mouse events.
func (c *Clicker) Click(match *Match) {
c.sleepRandomly(0.2, 0.5)
c.moveMouseRandomlyWithinBox(match.topLeft.x, match.topLeft.y, float64(match.width), float64(match.height))
c.sleepRandomly(0.2, 0.5)
c.performRandomisedClick()
}
func (c *Clicker) performRandomisedClick() {
robotgo.Toggle("left", "down")
c.sleepRandomly(0.2, 0.5)
robotgo.Toggle("left", "up")
}
func (c *Clicker) sleepRandomly(min, max float64) {
time.Sleep(time.Duration(c.generateRandomNumber(min, max) * float64(time.Second)))
}
func (c *Clicker) moveMouseRandomlyWithinBox(x, y, w, h float64) {
randomX := c.generateRandomNumber(x, x+w)
randomY := c.generateRandomNumber(y, y+h)
robotgo.Move(int(math.Round(randomX)), int(math.Round(randomY)))
}
func (c *Clicker) generateRandomNumber(min float64, max float64) float64 {
rand.Seed(time.Now().UnixNano())
randNum := (rand.Float64() * (max - min)) + min
// Trims to two decimal places. Doesn't need to be perfect.
return math.Floor(randNum*100) / 100
}