Skip to content

Commit

Permalink
Update script so that score of only registered users changes and igno…
Browse files Browse the repository at this point in the history
…re c++ string occurence in messages
  • Loading branch information
RohanBh committed Jun 11, 2017
1 parent 23c85fa commit ce5d81b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion scripts/bye.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ ways_to_say_good_night = [
]

module.exports = (robot) ->
robot.hear /(good night|goodnight|cya|bye|nighty night)/i, (msg) ->
robot.hear /(good night|goodnight|cya|bye|nighty night|gn)/i, (msg) ->
msg.send msg.random ways_to_say_good_night
26 changes: 20 additions & 6 deletions scripts/leaderboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ module.exports = (robot) ->
updateScore = (word, field, username) ->
posRegex = /\+\+/
negRegex = /\-\-/
userArray = robot.brain.users()
names = (user.name for user in userArray)

# if there is to be `plus` in score
if word.indexOf("++") >= 0
name = word.replace posRegex, ""
if username.toLowerCase() == name.toLowerCase() or name.toLowerCase() == "c"
unless username.toLowerCase() in names
response = "-2"
else if username.toLowerCase() == name.toLowerCase()
response = "-1"
else
field[name.toLowerCase()] = lastScore(name, field) + 1
Expand All @@ -84,7 +88,9 @@ module.exports = (robot) ->
# if there is to be `minus` in score
else if word.indexOf("--") >= 0
name = word.replace negRegex, ""
if username.toLowerCase() == name.toLowerCase()
unless username.toLowerCase() in names
response = "-2"
else if username.toLowerCase() == name.toLowerCase()
response = "-1"
else
field[name.toLowerCase()] = lastScore(name, field) - 1
Expand Down Expand Up @@ -112,6 +118,7 @@ module.exports = (robot) ->
# index keeping an eye on position, where next replace will be
start = 0
end = 0
shouldSend = true

# for each ++/--
for i in [0...msg.match.length]
Expand All @@ -124,15 +131,22 @@ module.exports = (robot) ->
end = start + testword.length

# generates response message for reply
if result.Response == "-1"
if result.Response == "-2"
if result.Name.toLowerCase() isnt "c"
newmsg = "#{testword} [Sorry, I don't know anything about #{result.Name}.]"
else
shouldSend = false # Do not reply if c++ is encountered
else if result.Response == "-1"
newmsg = "#{testword} [Sorry, You can't give ++ or -- to yourself.]"
else
newmsg = "#{testword} [#{result.Response} #{result.Name} now at #{result.New}] "
oldmsg = oldmsg.substr(0, start) + newmsg + oldmsg.substr(end+1)
start += newmsg.length
if result.Name.toLowerCase() isnt "c"
oldmsg = oldmsg.substr(0, start) + newmsg + oldmsg.substr(end+1)
start += newmsg.length

# reply with updated message
msg.send "#{oldmsg}"
if shouldSend
msg.send "#{oldmsg}"


# response for score status of any <keyword>
Expand Down

0 comments on commit ce5d81b

Please sign in to comment.