Skip to content

Commit ce5d81b

Browse files
committed
Update script so that score of only registered users changes and ignore c++ string occurence in messages
1 parent 23c85fa commit ce5d81b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

scripts/bye.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ ways_to_say_good_night = [
4848
]
4949

5050
module.exports = (robot) ->
51-
robot.hear /(good night|goodnight|cya|bye|nighty night)/i, (msg) ->
51+
robot.hear /(good night|goodnight|cya|bye|nighty night|gn)/i, (msg) ->
5252
msg.send msg.random ways_to_say_good_night

scripts/leaderboard.coffee

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,15 @@ module.exports = (robot) ->
6969
updateScore = (word, field, username) ->
7070
posRegex = /\+\+/
7171
negRegex = /\-\-/
72+
userArray = robot.brain.users()
73+
names = (user.name for user in userArray)
7274

7375
# if there is to be `plus` in score
7476
if word.indexOf("++") >= 0
7577
name = word.replace posRegex, ""
76-
if username.toLowerCase() == name.toLowerCase() or name.toLowerCase() == "c"
78+
unless username.toLowerCase() in names
79+
response = "-2"
80+
else if username.toLowerCase() == name.toLowerCase()
7781
response = "-1"
7882
else
7983
field[name.toLowerCase()] = lastScore(name, field) + 1
@@ -84,7 +88,9 @@ module.exports = (robot) ->
8488
# if there is to be `minus` in score
8589
else if word.indexOf("--") >= 0
8690
name = word.replace negRegex, ""
87-
if username.toLowerCase() == name.toLowerCase()
91+
unless username.toLowerCase() in names
92+
response = "-2"
93+
else if username.toLowerCase() == name.toLowerCase()
8894
response = "-1"
8995
else
9096
field[name.toLowerCase()] = lastScore(name, field) - 1
@@ -112,6 +118,7 @@ module.exports = (robot) ->
112118
# index keeping an eye on position, where next replace will be
113119
start = 0
114120
end = 0
121+
shouldSend = true
115122

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

126133
# generates response message for reply
127-
if result.Response == "-1"
134+
if result.Response == "-2"
135+
if result.Name.toLowerCase() isnt "c"
136+
newmsg = "#{testword} [Sorry, I don't know anything about #{result.Name}.]"
137+
else
138+
shouldSend = false # Do not reply if c++ is encountered
139+
else if result.Response == "-1"
128140
newmsg = "#{testword} [Sorry, You can't give ++ or -- to yourself.]"
129141
else
130142
newmsg = "#{testword} [#{result.Response} #{result.Name} now at #{result.New}] "
131-
oldmsg = oldmsg.substr(0, start) + newmsg + oldmsg.substr(end+1)
132-
start += newmsg.length
143+
if result.Name.toLowerCase() isnt "c"
144+
oldmsg = oldmsg.substr(0, start) + newmsg + oldmsg.substr(end+1)
145+
start += newmsg.length
133146

134147
# reply with updated message
135-
msg.send "#{oldmsg}"
148+
if shouldSend
149+
msg.send "#{oldmsg}"
136150

137151

138152
# response for score status of any <keyword>

0 commit comments

Comments
 (0)