diff --git a/hubot-scripts.json b/hubot-scripts.json index 9431e34..7a0d60e 100644 --- a/hubot-scripts.json +++ b/hubot-scripts.json @@ -1 +1 @@ -["redis-brain.coffee", "tweet-content.coffee", "google.coffee"] +["redis-brain.coffee", "tweet-content.coffee", "google.coffee", "karma.coffee"] diff --git a/scripts/karma.coffee b/scripts/karma.coffee new file mode 100644 index 0000000..bb2ec61 --- /dev/null +++ b/scripts/karma.coffee @@ -0,0 +1,111 @@ +# Description: +# Track arbitrary karma +# +# Dependencies: +# None +# +# Configuration: +# None +# +# Commands: +# ++ - give thing some karma +# -- - take away some of thing's karma +# hubot karma - check thing's karma (if is omitted, show the top 5) +# hubot karma empty - empty a thing's karma +# hubot karma best - show the top 5 +# hubot karma worst - show the bottom 5 +# +# Author: +# stuartf + +class Karma + + constructor: (@robot) -> + @cache = {} + + @increment_responses = [ + "+1!", "gained a level!", "is on the rise!", "leveled up!" + ] + + @decrement_responses = [ + "took a hit! Ouch.", "took a dive.", "lost a life.", "lost a level." + ] + + @robot.brain.on 'loaded', => + if @robot.brain.data.karma + @cache = @robot.brain.data.karma + + kill: (thing) -> + delete @cache[thing] + @robot.brain.data.karma = @cache + + increment: (thing) -> + unless thing is msg.message.user.name + @cache[thing] ?= 0 + @cache[thing] += 1 + @robot.brain.data.karma = @cache + + decrement: (thing) -> + unless thing is msg.message.user.name + @cache[thing] ?= 0 + @cache[thing] -= 1 + @robot.brain.data.karma = @cache + + 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 @cache[thing] else 0 + return k + + sort: -> + s = [] + for key, val of @cache + 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) -> + subject = msg.match[1].toLowerCase() + karma.increment subject + msg.send "#{subject} #{karma.incrementResponse()} (Karma: #{karma.get(subject)})" + + robot.hear /(\S+[^-\s])--(\s|$)/, (msg) -> + subject = msg.match[1].toLowerCase() + karma.decrement subject + 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() + 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." +