From 9478c76ea54a69f09676df6d3c86479723f363d9 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 17 Jul 2015 14:18:34 +0200 Subject: Add cmd timeout and score percent --- main.go | 42 +++++++++++++++++++++++++++--------------- score.go | 12 +++++++++--- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 7424398..1a2cdaa 100644 --- a/main.go +++ b/main.go @@ -23,19 +23,21 @@ var ( type Commander interface { irc.Handler fmt.Stringer + Timeout() bool } type Command struct { Help string Arg string + Last time.Time } type ( - Last struct{ Command } - RSS struct{ Command } - Theo struct{ Command } - Help struct{ Command } - Top struct{ Command } + Last struct{ Command } + RSS struct{ Command } + Theo struct{ Command } + Help struct{ Command } + Top struct{ Command } ) var ( @@ -48,6 +50,14 @@ func Register(cmd string, f Commander) { } func (v Command) String() string { return v.Help } +func (v *Command) Timeout() bool { + log.Println("timeout:", time.Since(v.Last)) + if time.Since(v.Last) > time.Minute { + v.Last = time.Now() + return false + } + return true +} func (_ Last) Handle(conn *irc.Conn, line *irc.Line) { buffer.Do(func(v interface{}) { @@ -109,8 +119,10 @@ func privmsg(conn *irc.Conn, line *irc.Line) { if len(f) == 1 { cmd := strings.ToLower(f[0]) if c, ok := commands[cmd]; ok { - log.Println(line.Nick, f) - go c.Handle(conn, line) + if !line.Public() || !c.Timeout() { + log.Println(line.Nick, f) + go c.Handle(conn, line) + } } } @@ -133,46 +145,46 @@ func privmsg(conn *irc.Conn, line *irc.Line) { func init() { flag.Parse() - Register("last", Last{ + Register("last", &Last{ Command{ Help: "Return last 10 messages", }, }) - Register("theo", Theo{ + Register("theo", &Theo{ Command{ Help: "Quote Theo De Raadt", }, }) - Register("news", RSS{ + Register("news", &RSS{ Command{ Help: "LOR news (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=1`, }, }) - Register("forum", RSS{ + Register("forum", &RSS{ Command{ Help: "LOR forum (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=2`, }, }) - Register("gallery", RSS{ + Register("gallery", &RSS{ Command{ Help: "LOR gallery (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=3`, }, }) - Register("bsd", RSS{ + Register("bsd", &RSS{ Command{ Help: "Undeadly news (msg private to see all)", Arg: `http://undeadly.org/cgi?action=rss`, }, }) - Register("help", Help{ + Register("help", &Help{ Command{ Help: "This help", }, }) - Register("top", Top{ + Register("top", &Top{ Command{ Help: "Top 10 flooder", }, diff --git a/score.go b/score.go index c449c9a..f38cccb 100644 --- a/score.go +++ b/score.go @@ -14,8 +14,9 @@ const gobfile = `score.gob` var score map[string]int type Score struct { - Nick string - Count int + Nick string + Count int + Percent float64 } type Scores []Score @@ -25,16 +26,21 @@ func (s Scores) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s Scores) Less(i, j int) bool { return s[i].Count < s[j].Count } func NewScores() (s Scores) { + var total int for k, v := range score { s = append(s, Score{ Nick: k, Count: v, }) + total += v } sort.Sort(sort.Reverse(s)) if len(s) > 10 { s = s[:10] } + for i := range s { + s[i].Percent = 100.0 * float64(s[i].Count) / float64(total) + } return } @@ -65,7 +71,7 @@ func saveScore(m map[string]int) { } func (s Score) String() string { - return fmt.Sprintf("%v (%v)", s.Nick, s.Count) + return fmt.Sprintf("%v (%v/%.1f%%)", s.Nick, s.Count, s.Percent) } func (s Scores) String() (ret string) { -- cgit v1.2.3