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 --- score.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'score.go') 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