2
2
#
3
3
# <thing>++ - give thing some karma
4
4
# <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)
6
6
# karma empty <thing> - empty a thing's karma
7
+ # karma best - show the top 5
8
+ # karma worst - show the bottom 5
7
9
class Karma
8
10
9
11
constructor : (@robot ) ->
@@ -45,16 +47,20 @@ class Karma
45
47
k = if @cache [thing] then @cache [thing] else 0
46
48
return k
47
49
48
- summary : ->
50
+ sort : ->
49
51
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
+
58
64
module .exports = (robot ) ->
59
65
karma = new Karma robot
60
66
robot .hear / (\S + [^ +\s ] )\+\+ (\s | $ )/ , (msg ) ->
@@ -72,18 +78,20 @@ module.exports = (robot) ->
72
78
karma .kill subject
73
79
msg .send " #{ subject} has had its karma scattered to the winds."
74
80
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"
78
96
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