forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobot.go
63 lines (55 loc) · 976 Bytes
/
gobot.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
package gobot
import (
"time"
"math/rand"
"net"
)
func Every(t string, f func()) {
dur := parseDuration(t)
go func(){
for{
time.Sleep(dur)
go f()
}
}()
}
func After(t string, f func()) {
dur := parseDuration(t)
go func(){
time.Sleep(dur)
f()
}()
}
func parseDuration(t string) time.Duration {
return ParseDuration(t)
}
func ParseDuration(t string) time.Duration {
dur, err := time.ParseDuration(t)
if err != nil {
panic(err)
}
return dur
}
func Random(min int, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max - min) + min
}
func On(cs chan interface{}) interface{}{
for s := range cs {
return s
}
return nil
}
func Work(robots []Robot) {
for s := range robots {
go robots[s].Start()
}
for{time.Sleep(10 * time.Millisecond)}
}
func ConnectTo(port string) net.Conn {
tcpPort, err := net.Dial("tcp", port)
if err != nil {
panic(err)
}
return tcpPort
}