From 2ba33c7601dca833d6fafca5649b86ccf5142fbc Mon Sep 17 00:00:00 2001 From: Riccardo Bovo Date: Mon, 11 Nov 2024 21:00:13 +0000 Subject: [PATCH] Room Ownership logic updated Taking care of few edge cases and cleaning logs --- Assets/Scripts/Multiplayer/MultiplayerManager.cs | 8 ++++---- Assets/Scripts/Multiplayer/Photon/PhotonManager.cs | 10 +++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/Multiplayer/MultiplayerManager.cs b/Assets/Scripts/Multiplayer/MultiplayerManager.cs index e186c48dad..86d548dc55 100644 --- a/Assets/Scripts/Multiplayer/MultiplayerManager.cs +++ b/Assets/Scripts/Multiplayer/MultiplayerManager.cs @@ -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) @@ -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> { m_LocalPlayer }; allPlayers.AddRange(m_RemotePlayers); diff --git a/Assets/Scripts/Multiplayer/Photon/PhotonManager.cs b/Assets/Scripts/Multiplayer/Photon/PhotonManager.cs index 6858ff7bf1..69c6a9c9e1 100644 --- a/Assets/Scripts/Multiplayer/Photon/PhotonManager.cs +++ b/Assets/Scripts/Multiplayer/Photon/PhotonManager.cs @@ -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()) .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 PerformCommand(BaseCommand command)