     return leaderboard_values
 
   list: ->
-    return (key for key of @robot.brain.data.leaderboards)
+    return (key for key of @leaderboards)
 
 module.exports = (robot) ->
   leaderboards = new Leaderboards robot

# Commands:
#   hubot <user> climbs <event> leaderboard - Add a victory for the user
#   hubot set <event> leaderboard <user> <score> - Set the score for a user
#   hubot show <event> leaderboard - Returns the event leaderboard 
#   hubot reset <event> leaderboard - Clears the event leaderboard 
#   hubot list leaderboards - Returns a list of leaderboards 
#
# Author:
#   canadianveggie

class Leaderboards
  constructor: (@robot) ->
    @robot.brain.data.leaderboards = {}

  initialize: (event, user) ->
    @robot.brain.data.leaderboards[event.toLowerCase()] ?= {}
    @robot.brain.data.leaderboards[event.toLowerCase()][user.toLowerCase()] ?= {user: user, score: 0}

  increment: (event, user, i) ->
    this.initialize event, user
    i ?= 1
    @robot.brain.data.leaderboards[event.toLowerCase()][user.toLowerCase()].score += i

  set: (event, user, val) ->
    this.initialize event, user
    @robot.brain.data.leaderboards[event.toLowerCase()][user.toLowerCase()].score = val

  delete: (event) ->
    delete @robot.brain.data.leaderboards[event.toLowerCase()]

  get: (event) ->
    sortBy = (key, a, b, r) ->

      return +1*r if a[key] < b[key]
      return 0

    leaderboard_values = (v for k,v of @leaderboards[event.toLowerCase()] or {})

    leaderboard_values.sort (a,b) ->
      sortBy('score', a, b, true) or
