forked from gregorycerna/hubot-ama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathama.coffee
223 lines (189 loc) · 7.81 KB
/
ama.coffee
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Description:
# daily AMA for hubot
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# ama start - start an AMA, and a new one will be started every 24 hours afterward
# ama stop - stop the AMA
# ama current - current person doing the AMA
# ama add <name> - manually add someone to the candidates list
# ama remove <name> -manually remove someone from the candidates list
# ama list - lists all AMA candidates
#
# Author:
# Gregory Cerna
# Steven Diaz
# Samuel Tallent
class AMAManager
constructor: (@robot) ->
storageLoaded = =>
@storage = @robot.brain.data.ama ||= {
candidates: {}
}
@robot.logger.debug "AMA data loaded: " + JSON.stringify(@storage)
@channels = ["bots", "ama", "Shell"] # channels where AMAs can run
@admins = ["andy", "greg", "stevendiaz", "thomas", "Shell", "samtallent"]
@intervalID = null # if not null, AMAs are currently on
@current = null # current AMA person
@robot.brain.on "loaded", storageLoaded
storageLoaded()
checkPermission: (msg) ->
if @admins.length == 0 || msg.message.user.name in @admins
return true
else
msg.send "You lack permission for this command"
return false
save: ->
@robot.logger.debug "Saving AMA data: " + JSON.stringify(@storage)
@robot.brain.emit 'save'
printHelp: (msg) ->
msg.send "```
ama start - pick someone for an AMA and repeat every 24 hours
ama stop - stop picking AMAs every 24 hours
ama current - display the current AMA celebrity
ama add - add yourself to the AMA list
ama remove - remove yourself from the AMA list
ama list - lists all AMA candidates
ama clear - clears selections (admin only)
ama odds - shows odds of each candidate being chosen
ama pass - stops the ama and returns the selected candidate's weight to the previous value
```"
pass: (msg) ->
user = msg.message.user.name
if user.toLowerCase() == @current.toLowerCase()
@storage.candidates[@current] = @storage.candidates[@current] * 2.0
stopAMA(msg)
else
msg.send('Sorry, you do not have permission for this command')
selectUser = () ->
weightedUserList = @storage.candidates
weightedUserArray = []
resetWeights = true
smallestWeight = 1.0
for user, weight of weightedUserList
if weight < smallestWeight
smallestWeight = weight
for user, weight of @storage.candidates
if weight == 1.0
resetWeights = false
break
if resetWeights
for user, weight of @storage.candidates
@storage.candidates[user] = @storage.candidates[user] * 2.0
weightedUserList = @storage.candidates
smallestWeight = smallestWeight * 2.0
for user, weight of @storage.candidates
normalizedWeight = weight / smallestWeight
weightedUserArray.push(user) while normalizedWeight-- > 0
selectedUser = weightedUserArray[Math.floor(Math.random() * weightedUserArray.length)]
@storage.candidates[selectedUser] = @storage.candidates[selectedUser] / 2.0
return selectedUser
startAMA: (msg) ->
if Object.keys(@storage.candidates).length > 0
if @intervalID
msg.send "sorry, an AMA is already going. try ama stop"
else
firstSelection = selectUser()
msg.send "#{firstSelection} has been selected to be today's AMA celebrity! Ask away, and anything goes :wink:"
@current = firstSelection
@intervalID = setInterval( ->
selected = selectUser()
msg.send "#{selected} has been selected to be today's AMA celebrity! Ask away, and anything goes :wink:"
@current = selected
, 1000 * 60 * 60 * 24) #24 hours
else
msg.send "Unable to start AMA: no candidates."
stopAMA = (msg) ->
if @intervalID
msg.send "the AMA is over! thank you everyone for your time."
clearInterval(@intervalID)
@intervalID = null
@current = null
else
msg.send "there's no AMA going on right now. try \"ama start\""
#helper function for stopAMA because I couldn't call it in pass as it was for whatever reason
stopAMA: (msg) ->
stopAMA(msg)
currentAMA: (msg) ->
if @current
msg.send "#{@current} is the current AMA celebrity! Ask them anything!"
else
msg.send "no one is currently doing an AMA."
addCandidate: (msg) ->
user = msg.message.user.name.toLowerCase()
if hasCandidate(user)
msg.send "You are already an AMA candidate."
else
@storage.candidates[user] = 1.0
msg.send "You have been added as an AMA candidate."
removeCandidate: (msg) ->
user = msg.message.user.name.toLowerCase()
if hasCandidate(user)
delete @storage.candidates[user]
msg.send "You have been removed as an AMA candidate."
else
msg.send "You are not an AMA candidate."
clearCandidates: (msg) ->
if Object.keys(@storage.candidates).length > 0
@storage.candidates = {}
@save
msg.send "Candidates cleared. "
else
msg.send "There are no candidates to clear."
listWeights: (msg) ->
str = "There are #{Object.keys(@storage.candidates).length} candidates for the AMA"
for candidate, weight of @storage.candidates
str = str + "\nCandidate: " + candidate + "\tWeight: " + weight
msg.send str
listCandidates: (msg) ->
str = "There are #{Object.keys(@storage.candidates).length} candidates for the AMA"
for candidate, weight of @storage.candidates
str = str + "\n#{candidate}"
msg.send str
hasCandidate = (passedUser) ->
for user, weight of @storage.candidates
if passedUser == user
return true
return false
validChannel: (msg) ->
if @channels.length == 0 || msg.message.user.room in @channels
return true
else
return false
module.exports = (robot) ->
ama = new AMAManager robot
# super janky way to pass data to methods bc random things are undefined
# for no apparent reason
checkMessage = (msg, cmd, data = null, data2 = null) ->
if ama.validChannel msg
if data
if data2
cmd msg, data, data2
else
cmd msg, data
else
cmd msg
checkRestrictedMessage = (msg, cmd) ->
if ama.checkPermission msg
checkMessage msg, cmd
robot.hear /^\s*ama\s*$/i, (msg) ->
msg.send "Invalid command, say \"ama help\" for help"
robot.hear /^\s*ama (.*)/i, (msg) ->
cmd = msg.match[1]
switch cmd
when "start" then checkMessage msg, ama.startAMA
when "stop" then checkMessage msg, ama.stopAMA
when "add" then checkMessage msg, ama.addCandidate
when "remove" then checkMessage msg, ama.removeCandidate
when "current" then checkMessage msg, ama.currentAMA
when "list" then checkMessage msg, ama.listCandidates
when "odds" then checkMessage msg, ama.listWeights
when "pass" then checkMessage msg, ama.pass
when "help" then ama.printHelp msg
when "clear" then checkRestrictedMessage msg, ama.clearCandidates
else msg.send "Invalid command, say \"ama help\" for help"