From 17d5cdb6742cb17197839f0aae54cf33299a5144 Mon Sep 17 00:00:00 2001 From: JunDao Date: Sat, 4 May 2019 17:34:16 +0800 Subject: [PATCH] autofish can automatic rethrow when timeout --- cmd/autofish/autofish.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cmd/autofish/autofish.go b/cmd/autofish/autofish.go index 6061c1c..cc88c61 100644 --- a/cmd/autofish/autofish.go +++ b/cmd/autofish/autofish.go @@ -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) + } +}