Skip to content

Commit

Permalink
Room Ownership logic updated
Browse files Browse the repository at this point in the history
  • Loading branch information
sbanca committed Nov 11, 2024
1 parent bdcea97 commit 00033e2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
4 changes: 3 additions & 1 deletion Assets/Editor/MultiplayerManagerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public override void OnInspectorGUI()
{
remoteUsersRegistered = "UserIds:[ ";
foreach (var remotePlayer in multiplayerManager.m_RemotePlayers)
remoteUsersRegistered += remotePlayer.PlayerId.ToString()+",";
{
remoteUsersRegistered += remotePlayer.PlayerId.ToString() + ",";
}
remoteUsersRegistered += "]";
}
else remoteUsersRegistered = "Not Assigned";
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Multiplayer/MultiplayerInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public interface IDataConnectionHandler : IConnectionHandler
void Update();

int GetPlayerCount();
bool GetPlayerRoomOwnershipStatus(int playerId);
Task<bool> PerformCommand(BaseCommand command);
Task<bool> UndoCommand(BaseCommand command);
Task<bool> RedoCommand(BaseCommand command);
Expand Down
27 changes: 25 additions & 2 deletions Assets/Scripts/Multiplayer/MultiplayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,31 @@ void OnPlayerLeft(int id)
}
}

// TODO extend to more than two users case
if (m_RemotePlayers.Count == 0) isUserRoomOwner = true; // If there are no other players left, the local player becomes the room owner
// Ownership logic
// 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));
// 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)
{
isUserRoomOwner = true;
return;
}

// 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);

// Find the player with the lowest PlayerId
var newOwner = allPlayers.OrderBy(player => player.PlayerId).First();

// If the new owner is the local player, set the flag
if (m_LocalPlayer.PlayerId == newOwner.PlayerId) isUserRoomOwner = true;

}

Expand Down
42 changes: 27 additions & 15 deletions Assets/Scripts/Multiplayer/Photon/PhotonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public void Update()
}
}

public void CheckExistingUsers()
{
foreach (PlayerRef player in m_Runner.ActivePlayers)
{
if (player != m_Runner.LocalPlayer)
{
m_PlayersSpawning.Add(player);
}
}

}

#region IConnectionHandler Methods

public async Task<bool> Connect()
Expand Down Expand Up @@ -161,20 +173,6 @@ public async Task<bool> JoinRoom(RoomCreateData roomCreateData)

}

public void CheckExistingUsers()
{
int playercount = m_Runner.SessionInfo.PlayerCount;

foreach (PlayerRef player in m_Runner.ActivePlayers)
{
if (player != m_Runner.LocalPlayer)
{
m_PlayersSpawning.Add(player);
}
}

}

public async Task<bool> Disconnect()
{
State = ConnectionState.DISCONNECTING;
Expand Down Expand Up @@ -238,6 +236,21 @@ public int GetPlayerCount()
return 0;
}

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;
}

public async Task<bool> PerformCommand(BaseCommand command)
{
await Task.Yield();
Expand Down Expand Up @@ -305,7 +318,6 @@ private bool ProcessCommand(BaseCommand command)
return success;
}


private bool CommandBrushStroke(BrushStrokeCommand command)
{
var stroke = command.m_Stroke;
Expand Down

0 comments on commit 00033e2

Please sign in to comment.