Skip to content

Commit e09e1c2

Browse files
authored
Merge pull request #35 from mdgspace/fix_scorefield
bfix: add constraints to reply
2 parents 8854256 + f064da7 commit e09e1c2

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

scripts/leaderboard.coffee

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module.exports = (robot) ->
8888
name = word.replace posRegex, ""
8989
if username.toLowerCase() == name.toLowerCase()
9090
response = "-1"
91-
else if doesExist(name, slackIds)
91+
else if name in slackIds
9292
field[name.toLowerCase()] = lastScore(name, field) + 1
9393
userfield = userFieldPlus(name.toLowerCase())
9494
updateDetailedScore(userfield, username, "plus")
@@ -101,7 +101,7 @@ module.exports = (robot) ->
101101
name = word.replace negRegex, ""
102102
if username.toLowerCase() == name.toLowerCase()
103103
response = "-1"
104-
else if doesExist(name, slackIds)
104+
else if name in slackIds
105105
field[name.toLowerCase()] = lastScore(name, field) - 1
106106
userfield = userFieldMinus(name.toLowerCase())
107107
updateDetailedScore(userfield, username, "minus")
@@ -134,14 +134,6 @@ module.exports = (robot) ->
134134
else
135135
false
136136

137-
doesExist = (key, list) ->
138-
isFound = false
139-
for item in list
140-
if item == key
141-
isFound = true
142-
break
143-
isFound
144-
145137
# listen for any [word](++/--) in chat and react/update score
146138
robot.hear /[a-zA-Z0-9\-_]+(\-\-|\+\+)/gi, (msg) ->
147139

@@ -157,6 +149,10 @@ module.exports = (robot) ->
157149
# index keeping an eye on position, where next replace will be
158150
start = 0
159151
end = 0
152+
153+
# reply only when there exist altest one testword which
154+
# is neither skipped nor its length greater than 30
155+
reply = false
160156

161157
getSlackIds (slackIds) ->
162158
# for each ++/--
@@ -166,10 +162,11 @@ module.exports = (robot) ->
166162
end = start + testword.length
167163

168164
# check if testword is already skipped or it is too lengthy
169-
if testword.slice(0, -2) in SkippedList or testword.length > 40
165+
if testword.slice(0, -2) in SkippedList or testword.length > 30
170166
newmsg = ""
171167

172168
else
169+
reply = true
173170
# updates Scoring for words, accordingly and returns result string
174171
result = updateScore(testword, ScoreField, msg.message.user.name, slackIds)
175172

@@ -184,8 +181,9 @@ module.exports = (robot) ->
184181
oldmsg = oldmsg.substr(0, start) + newmsg + oldmsg.substr(end + 1)
185182
start += newmsg.length
186183

187-
# reply with updated message
188-
msg.send "#{oldmsg}"
184+
if reply
185+
# reply with updated message
186+
msg.send "#{oldmsg}"
189187

190188

191189
# response for score status of any <keyword>

0 commit comments

Comments
 (0)