React to Client changes and show them in Lobby.razor.
I tested this on Multiplayer and realized pretty much everything is broken and I haven't been thinking about networking enough. Will start fixing tomorrow.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace LuckerParty;
|
||||
|
||||
/// <summary>
|
||||
@@ -10,11 +12,15 @@ public sealed class NetworkManager : Component, Component.INetworkListener
|
||||
/// </summary>
|
||||
private readonly Dictionary<Connection, GameObject> _clientMap = new();
|
||||
|
||||
private readonly List<Client> _clients = new();
|
||||
|
||||
/// <summary>
|
||||
/// A GameObject used for organizational grouping of Clients
|
||||
/// </summary>
|
||||
private GameObject _clientGroup;
|
||||
|
||||
public ImmutableList<Client> Clients => _clients.ToImmutableList();
|
||||
|
||||
public void OnActive( Connection channel )
|
||||
{
|
||||
// Set up the Client GameObject
|
||||
@@ -25,6 +31,9 @@ public sealed class NetworkManager : Component, Component.INetworkListener
|
||||
|
||||
// Spawn it on remote clients
|
||||
gameObject.NetworkSpawn( channel );
|
||||
|
||||
_clients.Add( client );
|
||||
IClientEvent.Post( e => e.OnConnected( client ) );
|
||||
}
|
||||
|
||||
public void OnDisconnected( Connection channel )
|
||||
@@ -35,8 +44,12 @@ public sealed class NetworkManager : Component, Component.INetworkListener
|
||||
return;
|
||||
}
|
||||
|
||||
var client = clientGameObject.GetComponent<Client>();
|
||||
IClientEvent.Post( e => e.OnDisconnected( client ) );
|
||||
|
||||
clientGameObject.Destroy();
|
||||
_clientMap.Remove( channel );
|
||||
_clients.Remove( client );
|
||||
}
|
||||
|
||||
protected override void OnAwake()
|
||||
@@ -48,4 +61,13 @@ public sealed class NetworkManager : Component, Component.INetworkListener
|
||||
{
|
||||
_clientGroup.Destroy();
|
||||
}
|
||||
|
||||
public interface IClientEvent : ISceneEvent<IClientEvent>
|
||||
{
|
||||
void OnConnected( Client client )
|
||||
{
|
||||
}
|
||||
|
||||
void OnDisconnected( Client client ) { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,49 @@
|
||||
@namespace LuckerParty.UI
|
||||
@using System
|
||||
@inherits PanelComponent
|
||||
@using System
|
||||
@using System.Collections.Immutable
|
||||
@implements NetworkManager.IClientEvent
|
||||
|
||||
<root>
|
||||
<div class="title">This is the Lobby</div>
|
||||
@foreach ( var client in _clients )
|
||||
{
|
||||
<div>@client.Name</div>
|
||||
}
|
||||
</root>
|
||||
|
||||
@code
|
||||
{
|
||||
public List<Client> Clients { get; set; }
|
||||
private ImmutableList<Client> _clients;
|
||||
private NetworkManager _networkManager;
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
_networkManager = Scene.GetComponentInChildren<NetworkManager>();
|
||||
UpdateClients();
|
||||
}
|
||||
|
||||
void NetworkManager.IClientEvent.OnConnected( Client client )
|
||||
{
|
||||
UpdateClients();
|
||||
}
|
||||
|
||||
void NetworkManager.IClientEvent.OnDisconnected( Client client )
|
||||
{
|
||||
UpdateClients();
|
||||
}
|
||||
|
||||
private void UpdateClients()
|
||||
{
|
||||
_clients = _networkManager.Clients.Sort( ( clientA, clientB ) => DateTimeOffset.Compare( clientA.ConnectionTime, clientB.ConnectionTime ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// the hash determines if the system should be rebuilt. If it changes, it will be rebuilt
|
||||
/// </summary>
|
||||
protected override int BuildHash()
|
||||
{
|
||||
return HashCode.Combine( Clients );
|
||||
return HashCode.Combine( _clients );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user