From 7c1c2ffad83317bcab14e90f124a64882c99a5e4 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 28 Sep 2015 18:41:59 +0200 Subject: Store last city --- command.go | 14 ++++++++------ weather.go | 27 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/command.go b/command.go index b2bdb59..cd34b7e 100644 --- a/command.go +++ b/command.go @@ -16,7 +16,7 @@ type Commander interface { } type Command struct { - last map[string]time.Time + lastCmd map[string]time.Time } var commands = make(map[string]Commander) @@ -25,13 +25,15 @@ func Register(cmd string, f Commander) { commands[cmd] = f } +const timeOut = 5 * time.Second + func (v *Command) Timeout(nick string) bool { - defer func() { v.last[nick] = time.Now() }() - if v.last == nil { - v.last = make(map[string]time.Time) + defer func() { v.lastCmd[nick] = time.Now() }() + if v.lastCmd == nil { + v.lastCmd = make(map[string]time.Time) } - if last, ok := v.last[nick]; ok { - if to := time.Since(last); to < 5*time.Second { + if last, ok := v.lastCmd[nick]; ok { + if to := time.Since(last); to < timeOut { log.Println(nick, "timeout", to) return true } diff --git a/weather.go b/weather.go index bada901..cf5265d 100644 --- a/weather.go +++ b/weather.go @@ -8,17 +8,26 @@ import ( irc "github.com/fluffle/goirc/client" ) -type Weather struct{ Command } +type Weather struct { + Command + lastCity map[string]string +} func (_ Weather) WithArgs(_ int) bool { return true } -func (_ Weather) Handle(conn *irc.Conn, line *irc.Line) { - if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 { - if c, err := weather.ByCityName(q[1]); err != nil { - conn.Notice(line.Target(), err.Error()) - } else { - conn.Notice(line.Target(), fmt.Sprint(c)) - } +func (w *Weather) Handle(conn *irc.Conn, line *irc.Line) { + var city string + q := strings.SplitN(line.Text(), " ", 2) + if len(q) == 2 { + city = q[1] + } else if l, ok := w.lastCity[line.Nick]; ok { + city = l + } + if c, err := weather.ByCityName(city); err != nil { + conn.Notice(line.Target(), err.Error()) + } else { + conn.Notice(line.Target(), fmt.Sprint(c)) + w.lastCity[line.Nick] = city } } @@ -27,5 +36,5 @@ func (_ Weather) Help() string { } func init() { - Register("weather", &Weather{}) + Register("weather", &Weather{lastCity: make(map[string]string)}) } -- cgit v1.2.3 From 43895243d24b9122758a9edf52a43a373fd61531 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 29 Sep 2015 00:06:35 +0200 Subject: Add error message --- weather.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/weather.go b/weather.go index cf5265d..312b330 100644 --- a/weather.go +++ b/weather.go @@ -22,6 +22,9 @@ func (w *Weather) Handle(conn *irc.Conn, line *irc.Line) { city = q[1] } else if l, ok := w.lastCity[line.Nick]; ok { city = l + } else { + conn.Notice(line.Target(), "set your location first") + return } if c, err := weather.ByCityName(city); err != nil { conn.Notice(line.Target(), err.Error()) -- cgit v1.2.3 From bbbfec1829840f29fc0019f3ff54c948017c27e9 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 5 Oct 2015 00:45:38 +0200 Subject: Use ticker --- main.go | 2 +- score.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 5f99cfe..320347b 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { } }) - go AutoSave() + go AutoSaveScore() for n := 0; n < maxDiscon; n++ { log.Println("Connect to", *server) diff --git a/score.go b/score.go index 20b4cb3..b8243d2 100644 --- a/score.go +++ b/score.go @@ -91,9 +91,8 @@ func Count(nick string) { score[nick]++ } -func AutoSave() { - for { +func AutoSaveScore() { + for range time.Tick(time.Minute) { saveScore(score) - time.Sleep(time.Minute) } } -- cgit v1.2.3