using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Multiplayer { [HideMonoScript] public class ClientList : MonoBehaviour { public ServerBehaviour server; public List clients; private void OnEnable() { NetMsgEvents.StartListening($"{server.eventStreamName}_AcceptingHandshake", OnServerAcceptingHandshakeEvent); NetMsgEvents.StartListening($"{server.eventStreamName}_ClientDisconnect", OnServerClientDisconnectEvent); } private void OnDisable() { NetMsgEvents.StopListening($"{server.eventStreamName}_AcceptingHandshake", OnServerAcceptingHandshakeEvent); NetMsgEvents.StartListening($"{server.eventStreamName}_ClientDisconnect", OnServerClientDisconnectEvent); } private void OnServerAcceptingHandshakeEvent(string text, int clientId, uint code, NetMsg netMsg) { NetAcceptHandshakeMsg handshake = (NetAcceptHandshakeMsg)netMsg; bool registered = false; for (int i = 0; i < clients.Count; i++) { if(clients[i].playerName == handshake.playerName) { clients[i].connectionId = clientId; clients[i].isConnected = true; registered = true; } } if(!registered) clients.Add(new ClientData(clientId, handshake.playerName, true)); } private void OnServerClientDisconnectEvent(string text, int clientId, uint code, NetMsg netMsg) { NetDisconnectMsg disconnectMsg = (NetDisconnectMsg)netMsg; for (int i = 0; i < clients.Count; i++) { if(clients[i].connectionId == clientId) { clients[i].connectionId = -1; clients[i].isConnected = false; } } } } }