 #   roomId {String} The name of the room.
 
 
-
 joinRoom = (roomId, playerId, isMaster) ->
   # Enable the player if they are a master or that is the default
   # state.
+  room = Rooms.findOne
+    roomId: roomId
+  ,
+    fields:
+      allEnabled: true
+  allEnabled = room?.allEnabled ? false
+
   isEnabled = if isMaster or ServerSettings.players.enableOnJoin
     true
   else
     # The joining player should be enabled if all players in the room
     # are enabled.
-    Rooms.findOne(roomId: roomId, allEnabled: true)?
+    allEnabled
 
   Players.upsert
     playerId: playerId
   ,
     $set:
+      allEnabled: allEnabled
       isEnabled: isEnabled
       isMaster: isMaster
       name: generateName()

      $unset:
        enabledPlayerId: ''


enableAllPlayers = (roomId) ->
  Players.update
    isEnabled: false
    isMaster: false
    roomId: roomId
  ,
    $set:
      isEnabled: true
  ,
    multi: true


# Player documents
#
#   allEnabled {Boolean} True if everyone in the room is enabled.
#
#   isEnabled {Boolean} If true, the player can use their controller.
#
#   isMaster {Boolean} If true, the player can always use their
