This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
karma.go
173 lines (142 loc) · 4.33 KB
/
karma.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2014-2015 Chadev. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/danryan/hal"
)
// DevKarma contains fields for user karma information.
type DevKarma struct {
Name string `json:"name"`
Karma int `json:"karma"`
}
func (d DevKarma) String() string {
return fmt.Sprintf("%s: %d\n", d.Name, d.Karma)
}
// ByKarma implements sort.Interface for []DevKarma based on the Karma field.
type ByKarma []DevKarma
func (a ByKarma) Len() int { return len(a) }
func (a ByKarma) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByKarma) Less(i, j int) bool { return a[i].Karma < a[j].Karma }
var karmaHandler = hear(`(.+)(\+\+|\-\-)`, "(nickname)++ or (nickname)--", "Increases or Decreases a persons karma", func(res *hal.Response) error {
nick := res.Match[1]
sign := res.Match[2]
var dev DevKarma
key := strings.ToUpper(nick)
val, _ := res.Robot.Store.Get("Karma::" + key)
json.Unmarshal(val, &dev)
// check to see if we got something back
if dev.Name == "" {
dev.Name = nick
}
if sign == "++" {
dev.Karma++
} else if sign == "--" {
dev.Karma--
} else {
hal.Logger.Errorf("invalid sign '%v' given", sign)
err := res.Reply("that is not a valid option, please try again with either ++ or --")
return err
}
val, _ = json.Marshal(dev)
err := res.Robot.Store.Set("Karma::"+key, val)
if err != nil {
hal.Logger.Errorf("could not update the user's karma: %v", err)
err = res.Reply("I was unable to update the users karma, please try again later")
return err
}
updateDevsWithKarma(key, res)
err = res.Send(fmt.Sprintf("Karma for %s has been updated", nick))
return err
})
var karmaStatsHandler = hear(`karma stats(.*)`, "karma stats (username)", "Shows the current karma stats for a user, defaults to the user that ran the cammand", func(res *hal.Response) error {
nick := strings.TrimSpace(res.Match[1])
if nick == "" {
nick = res.UserName()
}
var dev DevKarma
key := strings.ToUpper(nick)
val, err := res.Robot.Store.Get("Karma::" + key)
if err != nil {
hal.Logger.Infof("user %s has no karma", nick)
err = res.Reply(fmt.Sprintf("User %s currently has no karma", nick))
return err
}
json.Unmarshal(val, &dev)
if dev.Name == "" {
hal.Logger.Infof("user %s has no karma", nick)
err = res.Reply(fmt.Sprintf("User %s currently has no karma", nick))
return err
}
err = res.Send(fmt.Sprintf("User %s currently has %d karma", nick, dev.Karma))
return err
})
var karmaRankingHandler = hear(`karma ranking(\s(top|lowest)\s\d*)?`, "karma ranking", "Shows the current karma standings, with optional top/lowest standings", func(res *hal.Response) error {
var devs []DevKarma
names := getDevKarmaList(res)
hal.Logger.Debugf("Value of names: %#v", names)
for _, v := range names {
var dev DevKarma
val, err := res.Robot.Store.Get("Karma::" + v)
if err != nil {
hal.Logger.Errorf("couldn't fetch karma details: %v", err)
err = res.Reply("Sorry, I couldn't fetch karma ratings")
return err
}
json.Unmarshal(val, &dev)
devs = append(devs, dev)
}
hal.Logger.Debugf("Value of devs: %#v", devs)
if len(devs) == 0 {
}
sort.Sort(sort.Reverse(ByKarma(devs)))
hal.Logger.Debugf("Sorted devs list: %#v", devs)
ranking := []string{
"Current rankings:\n",
}
for _, val := range devs {
ranking = append(ranking, val.String())
}
var msg string
for _, m := range ranking {
msg = msg + m
}
msg, err := sprungeSend(msg)
if err != nil {
hal.Logger.Error(err)
err = res.Reply("Sorry, I had some unexpected difficulties posting the rankings")
return err
}
err = res.Reply(fmt.Sprintf("The current rankings can be found here %s", msg))
return err
})
func updateDevsWithKarma(name string, r *hal.Response) {
names := getDevKarmaList(r)
if !inSlice(names, name) {
if len(names) == 0 || names[0] == "" {
names[0] = name
} else {
names = append(names, name)
}
}
list := strings.Join(names, ",")
r.Robot.Store.Set("KarmaList", []byte(list))
}
func getDevKarmaList(r *hal.Response) []string {
val, _ := r.Robot.Store.Get("KarmaList")
list := string(val)
names := strings.Split(list, ",")
return names
}
func inSlice(n []string, name string) bool {
for _, val := range n {
if val == name {
return true
}
}
return false
}