Skip to content

Commit

Permalink
Room Ownership logic updated
Browse files Browse the repository at this point in the history
Taking care of few edge cases and cleaning logs
  • Loading branch information
sbanca committed Nov 11, 2024
1 parent 5ca5b49 commit 2ba33c7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Assets/Scripts/Multiplayer/MultiplayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,13 @@ void OnPlayerLeft(int id)
}
}

// Ownership logic
// Reassign Ownership if needed
// Check if any remaining player is the room owner
bool anyRoomOwner = m_RemotePlayers.Any(player => m_Manager.GetPlayerRoomOwnershipStatus(player.PlayerId))
|| (m_LocalPlayer != null && m_Manager.GetPlayerRoomOwnershipStatus(m_LocalPlayer.PlayerId));
|| isUserRoomOwner;

// If there's still a room owner, no reassignment is needed
if (anyRoomOwner) return;
Debug.LogError("No room owner left after a player left, reassigning ownership.");

// If there are no other players left, the local player becomes the room owner
if (m_RemotePlayers.Count == 0)
Expand All @@ -392,7 +392,7 @@ void OnPlayerLeft(int id)
return;
}

// There are other players left
// Since There are other players left
// Determine the new room owner by the lowest PlayerId
var allPlayers = new List<ITransientData<PlayerRigData>> { m_LocalPlayer };
allPlayers.AddRange(m_RemotePlayers);
Expand Down
10 changes: 3 additions & 7 deletions Assets/Scripts/Multiplayer/Photon/PhotonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,13 @@ public int GetPlayerCount()

public bool GetPlayerRoomOwnershipStatus(int playerId)
{
// Check local player
if (m_LocalPlayer != null && m_LocalPlayer.PlayerId == playerId) return m_LocalPlayer.IsRoomOwner;

// Check among remote players
var remotePlayer = m_PlayersSpawning
.Select(playerRef => m_Runner.GetPlayerObject(playerRef)?.GetComponent<PhotonPlayerRig>())
.FirstOrDefault(playerRig => playerRig != null && playerRig.PlayerId == playerId);

Debug.LogError($"Remote Player: {remotePlayer.PlayerId} is Room Owner: {remotePlayer.IsRoomOwner}");

return remotePlayer != null && remotePlayer.IsRoomOwner;
if (remotePlayer != null && remotePlayer.Object != null && remotePlayer.Object.IsValid)
return remotePlayer.IsRoomOwner;
else return false;
}

public async Task<bool> PerformCommand(BaseCommand command)
Expand Down

0 comments on commit 2ba33c7

Please sign in to comment.