This repository was archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Add custom karma.coffee #1
Open
arthuryeti
wants to merge
1
commit into
parisjs:master
Choose a base branch
from
arthuryeti:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ["redis-brain.coffee", "tweet-content.coffee", "google.coffee"] | ||
| ["redis-brain.coffee", "tweet-content.coffee", "google.coffee", "karma.coffee"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Description: | ||
| # Track arbitrary karma | ||
| # | ||
| # Dependencies: | ||
| # None | ||
| # | ||
| # Configuration: | ||
| # None | ||
| # | ||
| # Commands: | ||
| # <thing>++ - give thing some karma | ||
| # <thing>-- - take away some of thing's karma | ||
| # hubot karma <thing> - check thing's karma (if <thing> is omitted, show the top 5) | ||
| # hubot karma empty <thing> - 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ReferenceError: msg is not defined |
||
| @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." | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed.