Skip to content

Instantly share code, notes, and snippets.

@rix501
Created July 29, 2013 14:12
Show Gist options
  • Save rix501/6104567 to your computer and use it in GitHub Desktop.
Save rix501/6104567 to your computer and use it in GitHub Desktop.
Hubot plugin fro tracking arbitrary karma, with extra info
# Track arbitrary karma
#
# Description:
# Track arbitrary karma, with extra info
#
# Dependencies:
#
# Configuration:
#
# Commands:
# <thing>++ - give thing some karma
# <thing>-- - take away some of thing's karma
# karma <thing> - check thing's karma (if <thing> is omitted, show the top 5)
# karma empty <thing> - empty a thing's karma
# karma best - show the top 5
# karma worst - show the bottom 5
class Karma
increment_responses: [
"+1!",
"gained a level!",
"is on the rise!",
"leveled up!",
"doing work!",
"gettin' it!",
"is it the shoes?!",
"he's on fire!"
]
decrement_responses: [
"took a hit! Ouch.",
"took a dive.",
"lost a life.",
"lost a level.",
"went down like a sack of potatoes!",
"like the first mouse.",
":("
]
constructor: (@robot) ->
@cache = {}
@robot.brain.on 'loaded', =>
if @robot.brain.data.karma
@cache = @robot.brain.data.karma
@updateCache()
updateCache: ->
return if @cache.__version is 2
createNewKarma = (val) ->
karma = []
increment = if val > 0 then yes else no
while val -= 1
karma.push
user: null
increment: increment
timestamp: null
karma
for key, val of @cache
@cache[key] =
name: key
karma: createNewKarma(val)
@cache.__version = 2
changeKarma: (thingName, user, increment = true) ->
@cache[thingName] ?=
name: thingName
karma: []
thing = @cache[thingName]
thing.karma.push
user: user
increment: increment
timestamp: Date.now()
kill: (thing) ->
delete @cache[thing]
@robot.brain.data.karma = @cache
getKarma: (thing) ->
total = 0
for karma in thing.karma
if karma.increment
total++
else
total--
total
incrementResponse: ->
@increment_responses[Math.floor(Math.random() * @increment_responses.length)]
decrementResponse: ->
@decrement_responses[Math.floor(Math.random() * @decrement_responses.length)]
get: (thing) ->
k = if @cache[thing] then @getKarma(@cache[thing]) else 0
return k
sort: ->
s = []
for key, thing of @cache
if key isnt '__version'
val = if thing.name == "beng"
-1 * @getKarma(thing)
else
@getKarma(thing)
s.push({ name: key, karma: val })
s.sort (a, b) -> b.karma - a.karma
top: (n = 5) ->
sorted = @sort()
sorted.slice(0, n)
bottom: (n = 5) ->
sorted = @sort()
sorted.slice(-n).reverse()
module.exports = (robot) ->
karma = new Karma robot
robot.hear /(\S+[^+\s])\+\+(\s|$)/, (msg) ->
user = msg.message.user
subject = msg.match[1].toLowerCase()
return if subject is '__version'
# With the BenG hook:
if subject == "beng"
karma.changeKarma subject, user, no
else
karma.changeKarma subject, user
msg.send "#{subject} #{karma.incrementResponse()} (Karma: #{karma.get(subject)})"
robot.hear /(\S+[^-\s])--(\s|$)/, (msg) ->
user = msg.message.user
subject = msg.match[1].toLowerCase()
return if subject is '__version'
karma.changeKarma subject, user, no
msg.send "#{subject} #{karma.decrementResponse()} (Karma: #{karma.get(subject)})"
robot.respond /karma empty ?(\S+[^-\s])$/i, (msg) ->
subject = msg.match[1].toLowerCase()
karma.kill subject
msg.send "#{subject} has had its karma scattered to the winds."
robot.respond /karma( best)?$/i, (msg) ->
verbiage = ["The Best"]
for item, rank in karma.top()
if item.name == "beng"
item.karma = -1 * item.karma
verbiage.push "#{rank + 1}. #{item.name} (#{item.karma})"
msg.send verbiage.join("\n")
robot.respond /karma worst$/i, (msg) ->
verbiage = ["The Worst"]
for item, rank in karma.bottom()
verbiage.push "#{rank + 1}. #{item.name} (#{item.karma})"
msg.send verbiage.join("\n")
robot.respond /karma (\S+[^-\s])$/i, (msg) ->
match = msg.match[1].toLowerCase()
if match != "best" && match != "worst"
msg.send "\"#{match}\" has #{karma.get(match)} karma."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment