autofish can automatic rethrow when timeout

This commit is contained in:
JunDao
2019-05-04 17:34:16 +08:00
parent 120bf7e244
commit 17d5cdb674

View File

@ -7,7 +7,12 @@ import (
"time"
)
var c *bot.Client
const timeout = 45
var (
c *bot.Client
watch chan time.Time
)
func main() {
c = bot.NewClient()
@ -33,6 +38,10 @@ func main() {
func onGameStart() error {
log.Println("Game start")
watch = make(chan time.Time)
go watchDog()
return c.UseItem(0)
}
@ -46,6 +55,7 @@ func onSound(name string, category int, x, y, z float64, volume, pitch float32)
if err := c.UseItem(0); err != nil { //throw
return err
}
watch <- time.Now()
}
return nil
}
@ -59,3 +69,18 @@ func onDisconnect(c chat.Message) error {
log.Println("Disconnect:", c)
return nil
}
func watchDog() {
to := time.NewTimer(time.Second * timeout)
for {
select {
case <-watch:
case <-to.C:
log.Println("rethrow")
if err := c.UseItem(0); err != nil {
panic(err)
}
}
to.Reset(time.Second * timeout)
}
}