       $unset:
         enabledPlayerId: ''
 
+  Players.update
+    allEnabled: true
+    roomId: roomId
+  ,
+    $set:
+      allEnabled: false
+  ,
+    multi: true
+
 
 enableAllPlayers = (roomId) ->
   Players.update
-    isEnabled: false
-    isMaster: false
-    roomId: roomId
-  ,
-    $set:
+    roomId: roomId
+  ,
+    $set:
+      allEnabled: true
       isEnabled: true
   ,
     multi: true

#   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.
  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)?

  Players.upsert
    playerId: playerId
  ,
    $set:
      isEnabled: isEnabled
      isMaster: isMaster
      name: generateName()

        isEnabled: true
        singledAt: Date.now()

    # Update the room.
    Rooms.upsert
      roomId: roomId
    ,
