 
 # Notifies the server about achievements to add to the user
 # All achievements are separated by server result and passed to next
-notifyServer = (playerName, achievements, next) ->
+# Note: In the current implementation, playerId is actually a name
+# If playerId is refactored to a numerical id, the server will need to be updated
+notifyServer = (playerId, achievements, next) ->
   achievementsByStatus =
     success: []
     duplicate: []


# Checks what achievements a player is eligible for
# The achievements are then awarded to the player
@checkAndAwardAchievements = (server, player, next = ->) ->
  checkAchievements player.ratingKey, (err, achievements) ->
    return next(err) if err
    return next() if achievements.length == 0

    filterEarned player.id, achievements, (err, achievements) ->
      return next(err) if err
      return next() if achievements.length == 0

      notifyServer player.name, achievements, (err, byStatus) ->
        return next(err) if err

        # TODO: Handle errors, probably add them to some queue to retry

    do (achievement) -> (callback) ->
      request.post {
        url: "https://www.pokebattle.com/api/v1/achievements/"
        json: { user: playerId, achievement: achievement.name }
      }, (err, res, data) ->
        status = "success"
        status = "error"  if err
