codecombat/app/lib/Bus.coffee

130 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
CocoClass = require 'lib/CocoClass'
{me} = require 'lib/auth'
CHAT_SIZE_LIMIT = 500 # no more than 500 messages
module.exports = Bus = class Bus extends CocoClass
joined: null
players: null
@get: (docName) -> return @getFromCache or new Bus docName
@getFromCache: (docName) -> return Bus.activeBuses[docName]
@activeBuses: {}
@fireHost: 'https://codecombat.firebaseio.com'
constructor: (@docName) ->
super()
@players = {}
Bus.activeBuses[@docName] = @
subscriptions:
'auth:me-synced': 'onMeSynced'
2014-01-03 13:32:13 -05:00
connect: ->
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish 'bus:connecting', {bus: @}
2014-01-03 13:32:13 -05:00
Firebase.goOnline()
2014-06-30 22:16:26 -04:00
@fireRef = new Firebase(Bus.fireHost + '/' + @docName)
2014-01-03 13:32:13 -05:00
@fireRef.once 'value', @onFireOpen
onFireOpen: (snapshot) =>
if @destroyed
console.log("Leaving '#{@docName}' because class has been destroyed.")
return
@init()
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish 'bus:connected', {bus: @}
2014-01-03 13:32:13 -05:00
disconnect: ->
@fireRef?.off()
@fireRef = null
@fireChatRef?.off()
@fireChatRef = null
@firePlayersRef?.off()
@firePlayersRef = null
@myConnection?.off()
@myConnection = null
@joined = false
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish 'bus:disconnected', {bus: @}
2014-01-03 13:32:13 -05:00
init: ->
"""
Init happens when we're connected.
"""
@fireChatRef = @fireRef.child('chat')
@firePlayersRef = @fireRef.child('players')
@join()
@listenForChanges()
2014-06-30 22:16:26 -04:00
@sendMessage('/me joined.', true)
2014-01-03 13:32:13 -05:00
join: ->
@joined = true
@myConnection = @firePlayersRef.child(me.id)
@myConnection.set({id: me.id, name: me.get('name'), connected: true})
@onDisconnect = @myConnection.child('connected').onDisconnect()
@onDisconnect.set(false)
listenForChanges: ->
@fireChatRef.limit(CHAT_SIZE_LIMIT).on 'child_added', @onChatAdded
@firePlayersRef.on 'child_added', @onPlayerJoined
@firePlayersRef.on 'child_removed', @onPlayerLeft
@firePlayersRef.on 'child_changed', @onPlayerChanged
onChatAdded: (snapshot) =>
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish('bus:new-message', {message: snapshot.val(), bus: @})
2014-01-03 13:32:13 -05:00
onPlayerJoined: (snapshot) =>
player = snapshot.val()
return unless player.connected
@players[player.id] = player
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish('bus:player-joined', {player: player, bus: @})
2014-01-03 13:32:13 -05:00
onPlayerLeft: (snapshot) =>
val = snapshot.val()
return unless val
player = @players[val.id]
return unless player
delete @players[player.id]
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish('bus:player-left', {player: player, bus: @})
2014-01-03 13:32:13 -05:00
onPlayerChanged: (snapshot) =>
player = snapshot.val()
wasConnected = @players[player.id]?.connected
@players[player.id] = player
@onPlayerLeft(snapshot) if wasConnected and not player.connected
@onPlayerJoined(snapshot) if player.connected and not wasConnected
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish('bus:player-states-changed', {states: @players, bus: @})
2014-01-03 13:32:13 -05:00
onMeSynced: ->
2014-01-03 13:32:13 -05:00
@myConnection?.child('name').set(me.get('name'))
countPlayers: -> _.size(@players)
onPoint: ->
# simple way to elect somone to do jobs that don't need to be done by each player
ids = _.keys(@players)
ids.sort()
return ids[0] is me.id
# MESSAGING
sendSystemMessage: (content) ->
@sendMessage(content, true)
sendMessage: (content, system=false) ->
MAX_MESSAGE_LENGTH = 400
message =
2014-06-30 22:16:26 -04:00
content: content[... MAX_MESSAGE_LENGTH]
authorName: me.displayName()
authorID: me.id
2014-01-03 13:32:13 -05:00
dateMade: new Date()
message.system = system if system
@fireChatRef.push(message)
# TEARDOWN
destroy: ->
2014-06-30 22:16:26 -04:00
@sendMessage('/me left.', true) if @joined
2014-01-03 13:32:13 -05:00
delete Bus.activeBuses[@docName] if @docName of Bus.activeBuses
@disconnect()
super()