Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 349fe1f

Browse files
author
Pete Nicholls
committed
Improve best/worst lists
1 parent 63061ca commit 349fe1f

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/scripts/karma.coffee

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#
33
# <thing>++ - give thing some karma
44
# <thing>-- - take away some of thing's karma
5-
# karma <thing> - check thing's karma, if <thing> is omitted get top and bottom 3
5+
# karma <thing> - check thing's karma (if <thing> is omitted, show the top 5)
66
# karma empty <thing> - empty a thing's karma
7+
# karma best - show the top 5
8+
# karma worst - show the bottom 5
79
class Karma
810

911
constructor: (@robot) ->
@@ -45,16 +47,20 @@ class Karma
4547
k = if @cache[thing] then @cache[thing] else 0
4648
return k
4749

48-
summary: ->
50+
sort: ->
4951
s = []
50-
for key,val of @cache
51-
s.push({name: key, karma: val})
52-
s.sort (a,b) -> b.karma - a.karma
53-
if s.length >= 6
54-
return [s[0], s[1], s[2], s[s.length-3], s[s.length-2], s[s.length-1]]
55-
else
56-
return s
57-
52+
for key, val of @cache
53+
s.push({ name: key, karma: val })
54+
s.sort (a, b) -> b.karma - a.karma
55+
56+
top: (n = 5) ->
57+
sorted = @sort()
58+
sorted.slice(0, n)
59+
60+
bottom: (n = 5) ->
61+
sorted = @sort()
62+
sorted.slice(-n).reverse()
63+
5864
module.exports = (robot) ->
5965
karma = new Karma robot
6066
robot.hear /(\S+[^+\s])\+\+(\s|$)/, (msg) ->
@@ -72,18 +78,20 @@ module.exports = (robot) ->
7278
karma.kill subject
7379
msg.send "#{subject} has had its karma scattered to the winds."
7480

75-
robot.respond /karma ?(\S+[^-\s])?$/i, (msg) ->
76-
if msg.match[1]
77-
match = msg.match[1].toLowerCase()
81+
robot.respond /karma( best)?$/i, (msg) ->
82+
verbiage = ["The Best"]
83+
for item, rank in karma.top()
84+
verbiage.push "#{rank + 1}. #{item.name} - #{item.karma}"
85+
msg.send verbiage.join("\n")
86+
87+
robot.respond /karma worst$/i, (msg) ->
88+
verbiage = ["The Worst"]
89+
for item, rank in karma.bottom()
90+
verbiage.push "#{rank + 1}. #{item.name} - #{item.karma}"
91+
msg.send verbiage.join("\n")
92+
93+
robot.respond /karma (\S+[^-\s])$/i, (msg) ->
94+
match = msg.match[1].toLowerCase()
95+
if match != "best" && match != "worst"
7896
msg.send "\"#{match}\" has #{karma.get(match)} karma."
79-
else
80-
s = karma.summary()
81-
if s.length >= 3
82-
msg.send "Highest karma: \"#{s[0].name}\" (#{s[0].karma}), " +
83-
"\"#{s[1].name}\" (#{s[1].karma}), and \"#{s[2].name}\" " +
84-
"(#{s[2].karma}). Lowest karma: \"#{s[s.length-1].name}\" " +
85-
"(#{s[s.length-1].karma}), \"#{s[s.length-2].name}\" " +
86-
"(#{s[s.length-2].karma}), and \"#{s[s.length-3].name}\" " +
87-
"(#{s[s.length-3].karma})."
88-
else
89-
msg.send "There aren't enough items with karma to give a top and bottom 3"
97+

0 commit comments

Comments
 (0)