Class: Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth, userId, roomId) ⇒ Bot

Returns a new instance of Bot.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



9
10
11
# File 'lib/bot.rb', line 9

def debug
  @debug
end

#speak(msg, callback = nil) ⇒ Object

Returns the value of attribute speak.



9
10
11
# File 'lib/bot.rb', line 9

def speak
  @speak
end

Instance Method Details

#_heartbeat(msg) ⇒ Object



178
179
180
181
# 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) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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) ⇒ Object



313
314
315
316
# 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) ⇒ Object



242
243
244
245
# 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) ⇒ Object



301
302
303
304
# 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) ⇒ Object



396
397
398
399
# 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) ⇒ Object



296
297
298
# File 'lib/bot.rb', line 296

def boot(userId, reason="", callback=nil)
   bootUser(userId, reason, callback)
end

#bootUser(userId, reason = "", callback = nil) ⇒ Object



290
291
292
293
# 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

#bopObject

TODO



346
347
# File 'lib/bot.rb', line 346

def bop
end

#connect(roomId) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



225
226
227
228
# File 'lib/bot.rb', line 225

def directoryGraph(callback=nil)
   rq = { "api" => "room.directory_graph" }
   _send(rq, callback)
end

#emit(signal, data = nil) ⇒ Object



437
438
439
440
441
442
443
# 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

#getFanOf(callback = nil) ⇒ Object



362
363
364
365
# File 'lib/bot.rb', line 362

def getFanOf(callback=nil)
   rq = { "api" => "user.get_fan_of" }
   _send(rq, callback)
end

#getFavorites(callback = nil) ⇒ Object



236
237
238
239
# File 'lib/bot.rb', line 236

def getFavorites(callback=nil)
   rq = { "api" => "room.get_favorites" }
   _send(rq, callback)
end

#getProfileObject

TODO



369
370
# File 'lib/bot.rb', line 369

def getProfile
end

#listRooms(skip = nil, callback = nil) ⇒ Object



216
217
218
219
220
221
222
# 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) ⇒ Object



378
379
380
381
# 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) ⇒ Object



384
385
386
387
# File 'lib/bot.rb', line 384

def modifyName(name, callback=nil)
   rq = { "api" => "user.modify", "name" => name }
   _send(rq, callback)
end

#modifyProfileObject

TODO



374
375
# File 'lib/bot.rb', line 374

def modifyProfile
end

#on(signal, callback) ⇒ Object



446
447
448
449
450
451
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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

#playlistAddObject

TODO



414
415
# File 'lib/bot.rb', line 414

def playlistAdd
end

#playlistAllObject

TODO



409
410
# File 'lib/bot.rb', line 409

def playlistAll
end

#playlistRemoveObject

TODO



419
420
# File 'lib/bot.rb', line 419

def playlistRemove
end

#playlistReorderObject

TODO



424
425
# File 'lib/bot.rb', line 424

def playlistReorder
end

#pm(msg, userid, callback = nil) ⇒ Object



278
279
280
281
# 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) ⇒ Object



284
285
286
287
# File 'lib/bot.rb', line 284

def pmHistory(userid, callback=nil)
   rq = { "api" => "pm.history", "receiverid" => userid }
   _send(rq, callback)
end

#remDj(*args) ⇒ Object

TODO



320
321
# File 'lib/bot.rb', line 320

def remDj(*args)
end

#remFavorite(roomId, callback = nil) ⇒ Object



248
249
250
251
# 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) ⇒ Object



307
308
309
310
# 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) ⇒ Object



402
403
404
405
# 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) ⇒ Object



259
260
261
262
# File 'lib/bot.rb', line 259

def roomDeregister(callback=nil)
   rq = { "api" => "room.deregister", "roomid" => @roomId }
   _send(rq, callback)
end

#roomInfo(*args) ⇒ Object



265
266
267
268
269
# 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) ⇒ Object



204
205
206
207
# File 'lib/bot.rb', line 204

def roomNow(callback=nil)
   rq = { "api" => "room.now" }
   _send(rq, callback)
end

#roomRegister(callback = nil) ⇒ Object

TODO



255
256
# File 'lib/bot.rb', line 255

def roomRegister(callback=nil)
end

#setAvatar(avatarId, callback = nil) ⇒ Object



390
391
392
393
# 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) ⇒ Object



428
429
430
431
432
433
434
# File 'lib/bot.rb', line 428

def setStatus(st, callback)
   @currentStatus = st
   updatePresence()
   if callback
      callback({ "success" => true })
   end
end

#setTmpSong(data) ⇒ Object



50
51
52
# File 'lib/bot.rb', line 50

def setTmpSong(data)
   tmpSong = { "command" => "endsong", "room" => data["room"], "success" => true }
end

#skip(callback = nil) ⇒ Object



330
331
332
# File 'lib/bot.rb', line 330

def skip(callback=nil)
   stopSong(callback)
end

#snag(callback = nil) ⇒ Object

TODO



336
337
# File 'lib/bot.rb', line 336

def snag(callback=nil)
end

#stalk(*args) ⇒ Object

TODO



232
233
# File 'lib/bot.rb', line 232

def stalk(*args)
end

#startObject



454
455
456
457
458
# File 'lib/bot.rb', line 454

def start
   while data = @ws.receive()
      on_message(data)
   end
end

#stopSong(callback = nil) ⇒ Object



324
325
326
327
# File 'lib/bot.rb', line 324

def stopSong(callback=nil)
   rq = { "api" => "room.stop_song", "roomid" => @roomId }
   _send(rq, callback)
end

#updatePresence(callback = nil) ⇒ Object



210
211
212
213
# File 'lib/bot.rb', line 210

def updatePresence(callback=nil)
   rq = { "api": "presence.update", "status": @currentStatus }
   _send(rq, callback)
end

#userAuthenticate(callback) ⇒ Object



350
351
352
353
# File 'lib/bot.rb', line 350

def userAuthenticate(callback)
   rq = { "api" => "user.authenticate" }
   _send(rq, callback)
end

#userInfo(callback = nil) ⇒ Object



356
357
358
359
# File 'lib/bot.rb', line 356

def userInfo(callback=nil)
   rq = { "api" => "user.info" }
   _send(rq, callback)
end

#voteObject

TODO



341
342
# File 'lib/bot.rb', line 341

def vote
end