Skip to content

Commit

Permalink
Allow users to give/remove more than 1 point up to configurable limit
Browse files Browse the repository at this point in the history
Maximum defaults to 5 and can be set in HUBOT_PLUSPLUS_MAX_POINTS.
  • Loading branch information
regentgal committed Jan 24, 2019
1 parent 3acd269 commit 573b61d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ Give (or take away) points from people and things, all from the comfort of your
personal Hubot.

What makes (or will make) this different from the original hubot-plusplus?
- Aims to incorporate some of the features of the Hipchat Karma bot

This package allows input closer to that accepted by the Hipchat Bot Karma
+ Flexibility of input (arbitrary text before/after "thing++")
+ Add and remote multiple points at once
- Admin support
+ Only admins can erase things
+ Set arbitrary point values for things

API
---
Expand Down
11 changes: 7 additions & 4 deletions src/mega-plusplus.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = (robot) ->
scoreKeeper = new ScoreKeeper(robot)
scoreKeyword = process.env.HUBOT_PLUSPLUS_KEYWORD or 'score'
reasonsKeyword = process.env.HUBOT_PLUSPLUS_REASONS or 'raisins'
maxPoints = process.env.HUBOT_PLUSPLUS_MAX_POINTS or 5
reasonConjunctions = process.env.HUBOT_PLUSPLUS_CONJUNCTIONS or 'for|because|cause|cuz|as'

# sweet regex bro
Expand All @@ -52,7 +53,7 @@ module.exports = (robot) ->
# allow for spaces after the thing being upvoted (@user ++)
\s*
# the increment/decrement operator ++ or --
(\+\+|--|)
(\+{2,}|-{2,})
# optional reason for the plusplus
(?:\s+(?:#{reasonConjunctions})\s+(.+))?
///i, (msg) ->
Expand All @@ -76,10 +77,12 @@ module.exports = (robot) ->
reason = lastReason if !reason? && lastReason?

# do the {up, down}vote, and figure out what the new score is
[score, reasonScore] = if operator == "++"
scoreKeeper.add(name, from, room, reason)
requestedMagnitude = (operator.length - 1)
magnitude = Math.min(requestedMagnitude, maxPoints)
[score, reasonScore] = if operator.charAt(0) == "+"
scoreKeeper.addX(name, from, magnitude, room, reason)
else
scoreKeeper.subtract(name, from, room, reason)
scoreKeeper.subtractX(name, from, magnitude, room, reason)

# if we got a score, then display all the things and fire off events!
if score?
Expand Down
14 changes: 10 additions & 4 deletions src/scorekeeper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,34 @@ class ScoreKeeper
[@storage.scores[user], @storage.reasons[user][reason] || "none"]

add: (user, from, room, reason) ->
@addX(user, from, 1, room, reason)

addX: (user, from, magnitude, room, reason) ->
if @validate(user, from)
user = @getUser(user)
@storage.scores[user]++
@storage.scores[user] = @storage.scores[user] + magnitude
@storage.reasons[user] ||= {}

if reason
@storage.reasons[user][reason] ||= 0
@storage.reasons[user][reason]++
@storage.reasons[user][reason] += magnitude

@saveUser(user, from, room, reason)
else
[null, null]

subtract: (user, from, room, reason) ->
@subtractX(user, from, 1, room, reason)

subtractX: (user, from, magnitude, room, reason) ->
if @validate(user, from)
user = @getUser(user)
@storage.scores[user]--
@storage.scores[user] = @storage.scores[user] - magnitude
@storage.reasons[user] ||= {}

if reason
@storage.reasons[user][reason] ||= 0
@storage.reasons[user][reason]--
@storage.reasons[user][reason] -= magnitude

@saveUser(user, from, room, reason)
else
Expand Down

0 comments on commit 573b61d

Please sign in to comment.