diff --git a/cmd/main/main.go b/cmd/main/main.go index e8760f7..1e9e067 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -3,7 +3,10 @@ package main import ( "fmt" "log" + "math" "os" + "os/signal" + "strings" "time" "github.com/go-vgo/robotgo" @@ -24,9 +27,23 @@ func (p Point) Click() { robotgo.Click("left", false) } +func (p1 *Point) GetDistance(p2 *Point) float64 { + return math.Sqrt(math.Pow(float64(p1.X-p2.X), float64(2)) + math.Pow(float64(p1.Y-p2.Y), float64(2))) +} + +func (p0 *Point) GetDistanceFromClosest(pp ...*Point) float64 { + var minDistance float64 + for index, p := range pp { + if distance := p.GetDistance(p0); index == 0 || distance < minDistance { + minDistance = distance + } + } + return minDistance +} + type PointGroup struct { Name string - Points []Point + Points []*Point } type SavedGroups struct { @@ -42,6 +59,12 @@ func getSavedGroups(yamlFile string) *SavedGroups { log.Fatalf("%v - Exiting", err) } } + for index, g := range s.Groups { + if index == 0 { + fmt.Println("Available groups:") + } + fmt.Printf("%v. %v\n", index+1, g.Name) + } return &s } @@ -70,7 +93,7 @@ func addSavedGroup(s *SavedGroups) { x, y := robotgo.GetMousePos() fmt.Printf("Added point (x=%v, y=%v) to %v\n", x, y, name) p := Point{X: x, Y: y} - g.Points = append(g.Points, p) + g.Points = append(g.Points, &p) } g.Name = name s.Groups = append(s.Groups, g) @@ -86,14 +109,43 @@ func storeSavedGroups(s *SavedGroups, yamlFile string) { if err != nil { log.Fatalf("error: %v", err) } - //fmt.Printf("--- d dump:\n%s\n\n", string(d)) } -func main() { - yamlFile := "SavedGroups.yaml" - s := getSavedGroups(yamlFile) - addSavedGroup(s) - storeSavedGroups(s, yamlFile) +func AbsInt(i int) int { + if i > 0 { + return i + } + return -i +} + +func Oscillator(amplitude int) chan int { + amplitude-- + c := make(chan int, 1) + i := amplitude + go func() { + for { + c <- AbsInt((i%(amplitude*2))-amplitude) + 1 + i++ + if i == amplitude*2 { + i = 0 + } + } + }() + return c +} + +func StringOscillator(s string, amplitude int) chan string { + c := make(chan string) + o := Oscillator(amplitude) + go func() { + for { + c <- strings.Repeat(s, <-o) + } + }() + return c +} + +func ClickRepeteadly(s *SavedGroups) { for index, g := range s.Groups { fmt.Printf("- Press %v to select %v\n", index+1, g.Name) } @@ -113,11 +165,56 @@ func main() { for index, p := range selection.Points { fmt.Printf("%v - %v\n", index+1, p) } + o := StringOscillator(".", 15) + var t int + var minDistance float64 for { for _, p := range selection.Points { p.Click() time.Sleep(100 * time.Millisecond) } + fmt.Println(<-o) time.Sleep(500 * time.Millisecond) + t = 0 + for t = 0; t < 10; t++ { + x, y := robotgo.GetMousePos() + currentPosition := Point{X: x, Y: y} + minDistance = currentPosition.GetDistanceFromClosest(selection.Points...) + if minDistance <= 50 { + break + } + fmt.Printf("%v You moved the mouse (distance %.2f), waiting 2 seconds (%v of 10)\n", strings.Repeat("_", 10-t), minDistance, t+1) + time.Sleep(2 * time.Second) + } + if t >= 10 { + fmt.Println("You stayed away too long, exiting...") + return + } + } +} + +func cleanUp(c chan os.Signal) chan int { + r := make(chan int) + go func() { + signal := <-c + fmt.Printf("Got signal %v, cleaning up...\n", signal) + r <- 1 + }() + return r +} + +func main() { + c := make(chan os.Signal, 1) + quit := cleanUp(c) + signal.Notify(c, os.Interrupt) + yamlFile := "SavedGroups.yaml" + s := getSavedGroups(yamlFile) + addSavedGroup(s) + storeSavedGroups(s, yamlFile) + go ClickRepeteadly(s) + if <-quit == 1 { + fmt.Println("Exiting...") + } else { + fmt.Println("Unknown error...") } }