class Bot

Attributes

debug[RW]
speak[RW]

Public Class Methods

new(auth, userId, roomId) click to toggle source
# File lib/bot.rb, line 11
def initialize(auth, userId, roomId)
   @auth          = auth
   @userId        = userId
   @roomId        = roomId
   @debug         = false
   @callback      = nil
   @currentDjId   = nil
   @currentSongId = nil
   @lastHeartbeat = Time.now
   @lastActivity  = Time.now
   @clientId      = '%s-0.59633534294921572' % Time.now.to_i
   @_msgId        = 0
   @_cmds         = []
   @_isConnected  = false
   @fanOf         = Set.new
   @currentStatus = "available"
   @signals       = {}

   connect(@roomId)
end

Public Instance Methods

_heartbeat(msg) click to toggle source
# File lib/bot.rb, line 178
def _heartbeat(msg)
   @ws.send('~m~%s~m~%s' % [msg.length, msg])
   @_msgId += 1
end
_send(rq, callback=nil) click to toggle source
# File lib/bot.rb, line 184
def _send(rq, callback=nil)
   rq["msgid"] = @_msgId
   rq["clientid"] = @clientId
   if not rq["userid"]
      rq["userid"] = @userId
   end
   rq["userauth"] = @auth

   msg = JSON.generate(rq)

   if @debug
      puts "< %s" % msg
   end

   @ws.send('~m~%s~m~%s' % [msg.length, msg])
   @_cmds.push([@_msgId, rq, callback])
   @_msgId += 1
end
addDj(callback=nil) click to toggle source
# File lib/bot.rb, line 313
def addDj(callback=nil)
   rq = { "api" => "room.add_dj", "roomid" => @roomId }
   _send(rq, callback)
end
addFavorite(roomId, callback=nil) click to toggle source
# File lib/bot.rb, line 242
def addFavorite(roomId, callback=nil)
   rq = { "api" => "room.add_favorite", "roomid" => roomId }
   _send(rq, callback)
end
addModerator(userId, callback=nil) click to toggle source
# File lib/bot.rb, line 301
def addModerator(userId, callback=nil)
   rq = { "api" => "room.add_moderator", "roomid" => @roomId, "target_userid" => userId }
   _send(rq, callback)
end
becomeFan(userId, callback=nil) click to toggle source
# File lib/bot.rb, line 396
def becomeFan(userId, callback=nil)
   rq = { "api" => "user.become_fan", "djid" => userId }
   _send(rq, callback)
end
boot(userId, reason="", callback=nil) click to toggle source
# File lib/bot.rb, line 296
def boot(userId, reason="", callback=nil)
   bootUser(userId, reason, callback)
end
bootUser(userId, reason="", callback=nil) click to toggle source
# File lib/bot.rb, line 290
def bootUser(userId, reason="", callback=nil)
   rq = { "api" => "room.boot_user", "roomid" => @roomId, "target_userid" => userId, "reason" => reason }
   _send(rq, callback)
end
bop() click to toggle source

TODO

# File lib/bot.rb, line 346
def bop
end
clb() click to toggle source
# File lib/bot.rb, line 41
def clb
   rq = { "api" => "room.register", "roomid" => @roomId }
   _send(rq, nil)
end
connect(roomId) click to toggle source
# File lib/bot.rb, line 33
def connect(roomId)
   uri = URI.parse("http://turntable.fm:80/api/room.which_chatserver?roomid=%s" % @roomId)
   response = Net::HTTP.get_response(uri)
   data = JSON.parse(response.body)
   host, port = data[1]["chatserver"][0], data[1]["chatserver"][1]
   url = "ws://%s:%s/socket.io/websocket" % [host, port]
   @ws = WebSocket.new(url)
   if @roomId
      def clb
         rq = { "api" => "room.register", "roomid" => @roomId }
         _send(rq, nil)
      end
      @callback = method(:clb)
   end
end
directoryGraph(callback=nil) click to toggle source
# File lib/bot.rb, line 225
def directoryGraph(callback=nil)
   rq = { "api" => "room.directory_graph" }
   _send(rq, callback)
end
emit(signal, data=nil) click to toggle source
# File lib/bot.rb, line 437
def emit(signal, data=nil)
   callbacks = @signals[signal]
   callbacks = [] if not callbacks
   for clb in callbacks
      clb.call(data)
   end
end
fanof(data) click to toggle source
# File lib/bot.rb, line 71
def fanof(data)
   @fanOf |= Set.new(data["fanof"])
   updatePresence()
   # TODO: setInterval ????
   emit("ready")
end
getFanOf(callback=nil) click to toggle source
# File lib/bot.rb, line 362
def getFanOf(callback=nil)
   rq = { "api" => "user.get_fan_of" }
   _send(rq, callback)
end
getFavorites(callback=nil) click to toggle source
# File lib/bot.rb, line 236
def getFavorites(callback=nil)
   rq = { "api" => "room.get_favorites" }
   _send(rq, callback)
end
getProfile() click to toggle source

TODO

# File lib/bot.rb, line 369
def getProfile
end
info_clb(data) click to toggle source
# File lib/bot.rb, line 107
def info_clb(data)
   setTmpSong(data)
   emit("roomChanged", data)
end
listRooms(skip=nil, callback=nil) click to toggle source
# File lib/bot.rb, line 216
def listRooms(skip=nil, callback=nil)
   if not skip
      skip = 0
   end
   rq = { "api" => "room.list_rooms", "skip" => skip }
   _sned(rq, callback)
end
modifyLaptop(laptop="linux", callback=nil) click to toggle source
# File lib/bot.rb, line 378
def modifyLaptop(laptop="linux", callback=nil)
   rq = { "api" => "user.modify", "laptop" => laptop }
   _send(rq, callback)
end
modifyName(name, callback=nil) click to toggle source
# File lib/bot.rb, line 384
def modifyName(name, callback=nil)
   rq = { "api" => "user.modify", "name" => name }
   _send(rq, callback)
end
modifyProfile() click to toggle source

TODO

# File lib/bot.rb, line 374
def modifyProfile
end
on(signal, callback) click to toggle source
# File lib/bot.rb, line 446
def on(signal, callback)
   if not @signals[signal]
      @signals[signal] = []
   end
   @signals[signal].push(callback)
end
on_message(msg) click to toggle source
# File lib/bot.rb, line 55
def on_message(msg)
   heartbeat_rgx = /~m~[0-9]+~m~(~h~[0-9]+)/
   if heartbeat_rgx.match(msg)
      _heartbeat(heartbeat_rgx.match(msg)[1])
      @lastHeartbeat = Time.now
      updatePresence()
      return
   end

   if @debug
      puts "> %s" % msg
   end

   if msg == "~m~10~m~no_session"
      def clb(obj)
         if not @isConnected
            def fanof(data)
               @fanOf |= Set.new(data["fanof"])
               updatePresence()
               # TODO: setInterval ????
               emit("ready")
            end
            getFanOf(method(:fanof))
         end
         @callback.call()
         @isConnected = true
      end
      userAuthenticate(method(:clb))
      return
   end

   @lastActivity = Time.now
   len_rgx = /~m~([0-9]+)~m~/
   len = len_rgx.match(msg)[1]
   obj = JSON.parse(msg[msg.index("{"), msg.length])
   for id, rq, clb in @_cmds
      if id == obj["msgid"]
         if rq["api"] == "room.info"
            if obj["success"]
               currentDj = obj["room"]["metadata"]["current_dj"]
               currentSong = obj["room"]["metadata"]["current_song"]
               if currentDj
                  @currentDj = currentDj
               end
               if currentSong
                  @currentSongId = currentSong["_id"]
               end
            end

         elsif rq["api"] == "room.register"
            if obj["success"]
               @roomId = rq["roomid"]
               def info_clb(data)
                  setTmpSong(data)
                  emit("roomChanged", data)
               end
               roomInfo(method(:info_clb))
            else
               emit("roomChanged", obj)
            end
            clb = nil

         elsif rq["api"] == "room.deregister"
            if obj["success"]
               @roomId = nil
            end
         end

         if clb
            clb.call(obj)
         end

         @_cmds.delete([id, rq, clb])
         break
      end
   end

   if obj["command"] == "registered"
      emit("registered", obj)
   elsif obj["command"] == "deregistered"
      emit("deregistered", obj)
   elsif obj["command"] == "speak"
      emit("speak", obj)
   elsif obj["command"] == "pmmed"
      emit("pmmed", obj)
   elsif obj["command"] == "nosong"
      @currentDjId = nil
      @currentSongId = nil
      emit("endsong", @tmpSong)
      emit("nosong", obj)
   elsif obj["command"] == "newsong"
      if @currentSongId
         emit("endsong", @tmpSong)
      end
      @currentDjId = obj["room"]["metadata"]["current_dj"]
      @currentSongId = obj["room"]["metadata"]["current_song"]["_id"]
      setTmpSong(obj)
      emit("newsong", obj)
   elsif obj["command"] == "update_votes"
      if @tmpSong
         @tmpSong["room"]['metadata']['upvotes']   = obj['room']['metadata']['upvotes']
         @tmpSong['room']['metadata']['downvotes'] = obj['room']['metadata']['downvotes']
         @tmpSong['room']['metadata']['listeners'] = obj['room']['metadata']['listeners']
      end
      emit("update_votes", obj)
   elsif obj["command"] == "booted_user"
      emit('booted_user', obj)
   elsif obj["command"] == "update_user"
      emit('update_user', obj)
   elsif obj["command"] == "add_dj"
      emit('add_dj', obj)
   elsif obj["command"] == "rem_dj"
      emit('rem_dj', obj)
   elsif obj["command"] == "new_moderator"
      emit('new_moderator', obj)
   elsif obj["command"] == "rem_moderator"
      emit('rem_moderator', obj)
   elsif obj["command"] == "snagged"
      emit('snagged', obj)
   end
end
playlistAdd() click to toggle source

TODO

# File lib/bot.rb, line 414
def playlistAdd
end
playlistAll() click to toggle source

TODO

# File lib/bot.rb, line 409
def playlistAll
end
playlistRemove() click to toggle source

TODO

# File lib/bot.rb, line 419
def playlistRemove
end
playlistReorder() click to toggle source

TODO

# File lib/bot.rb, line 424
def playlistReorder
end
pm(msg, userid, callback=nil) click to toggle source
# File lib/bot.rb, line 278
def pm(msg, userid, callback=nil)
   rq = { "api" => "pm.send", "receiverid" => userid, "text" => msg.to_s }
   _send(rq, callback)
end
pmHistory(userid, callback=nil) click to toggle source
# File lib/bot.rb, line 284
def pmHistory(userid, callback=nil)
   rq = { "api" => "pm.history", "receiverid" => userid }
   _send(rq, callback)
end
remDj(*args) click to toggle source

TODO

# File lib/bot.rb, line 320
def remDj(*args)
end
remFavorite(roomId, callback=nil) click to toggle source
# File lib/bot.rb, line 248
def remFavorite(roomId, callback=nil)
   rq = { "api" => "room.rem_favorite", "roomid" => roomId }
   _send(rq, callback)
end
remModerator(userId, callback=nil) click to toggle source
# File lib/bot.rb, line 307
def remModerator(userId, callback=nil)
   rq = { "api" => "room.rem_moderator", "roomid" => @roomId, "target_userid" => userId }
   _send(rq, callback)
end
removeFan(userId, callback=nil) click to toggle source
# File lib/bot.rb, line 402
def removeFan(userId, callback=nil)
   rq = { "api" => "user.remove_fan", "djid" => userId }
   _send(rq, callback)
end
roomDeregister(callback=nil) click to toggle source
# File lib/bot.rb, line 259
def roomDeregister(callback=nil)
   rq = { "api" => "room.deregister", "roomid" => @roomId }
   _send(rq, callback)
end
roomInfo(*args) click to toggle source
# File lib/bot.rb, line 265
def roomInfo(*args)
   rq = { "api" => "room.info", "roomid" => @roomId }
   callback = args[0]
   _send(rq, callback)
end
roomNow(callback=nil) click to toggle source
# File lib/bot.rb, line 204
def roomNow(callback=nil)
   rq = { "api" => "room.now" }
   _send(rq, callback)
end
roomRegister(callback=nil) click to toggle source

TODO

# File lib/bot.rb, line 255
def roomRegister(callback=nil)
end
setAvatar(avatarId, callback=nil) click to toggle source
# File lib/bot.rb, line 390
def setAvatar(avatarId, callback=nil)
   rq = { "api" => "user.set_avatar", "avatarid" => avatarId }
   _send(rq, callback)
end
setStatus(st, callback) click to toggle source
# File lib/bot.rb, line 428
def setStatus(st, callback)
   @currentStatus = st
   updatePresence()
   if callback
      callback({ "success" => true })
   end
end
setTmpSong(data) click to toggle source
# File lib/bot.rb, line 50
def setTmpSong(data)
   tmpSong = { "command" => "endsong", "room" => data["room"], "success" => true }
end
skip(callback=nil) click to toggle source
# File lib/bot.rb, line 330
def skip(callback=nil)
   stopSong(callback)
end
snag(callback=nil) click to toggle source

TODO

# File lib/bot.rb, line 336
def snag(callback=nil)
end
stalk(*args) click to toggle source

TODO

# File lib/bot.rb, line 232
def stalk(*args)
end
start() click to toggle source
# File lib/bot.rb, line 454
def start
   while data = @ws.receive()
      on_message(data)
   end
end
stopSong(callback=nil) click to toggle source
# File lib/bot.rb, line 324
def stopSong(callback=nil)
   rq = { "api" => "room.stop_song", "roomid" => @roomId }
   _send(rq, callback)
end
updatePresence(callback=nil) click to toggle source
# File lib/bot.rb, line 210
def updatePresence(callback=nil)
   rq = { "api": "presence.update", "status": @currentStatus }
   _send(rq, callback)
end
userAuthenticate(callback) click to toggle source
# File lib/bot.rb, line 350
def userAuthenticate(callback)
   rq = { "api" => "user.authenticate" }
   _send(rq, callback)
end
userInfo(callback=nil) click to toggle source
# File lib/bot.rb, line 356
def userInfo(callback=nil)
   rq = { "api" => "user.info" }
   _send(rq, callback)
end
vote() click to toggle source

TODO

# File lib/bot.rb, line 341
def vote
end