using System.Collections.Generic;
using UnityEngine;
namespace LobbyRelaySample.UI
{
///
/// Watches for changes in the Lobby's player List
///
[RequireComponent(typeof(LobbyDataObserver))]
public class LobbyUsersUI : ObserverPanel
{
[SerializeField]
List m_PlayerCardSlots = new List();
List m_CurrentUsers = new List(); // Just for keeping track more easily of which users are already displayed.
///
/// When the observed data updates, we need to detect changes to the list of players.
///
public override void ObservedUpdated(LobbyData observed)
{
for (int id = m_CurrentUsers.Count - 1; id >= 0; id--) // We might remove users if they aren't in the new data, so iterate backwards.
{
string userId = m_CurrentUsers[id];
if (!observed.LobbyUsers.ContainsKey(userId))
{
foreach (var card in m_PlayerCardSlots)
{
if (card.UserId == userId)
{
card.OnUserLeft();
OnUserLeft(userId);
}
}
}
}
foreach (var lobbyUserKvp in observed.LobbyUsers) // If there are new players, we need to hook them into the UI.
{
if (m_CurrentUsers.Contains(lobbyUserKvp.Key))
continue;
m_CurrentUsers.Add(lobbyUserKvp.Key);
foreach (var pcu in m_PlayerCardSlots)
{
if (pcu.IsAssigned)
continue;
pcu.SetUser(lobbyUserKvp.Value);
break;
}
}
}
void OnUserLeft(string userID)
{
if (!m_CurrentUsers.Contains(userID))
return;
m_CurrentUsers.Remove(userID);
}
}
}