You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.5 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. namespace Networking.Server
  7. {
  8. [CreateAssetMenu(menuName = "Major Project/Networking/ClientList", order = 150)]
  9. public class ConnectionHandler : ScriptableObject
  10. {
  11. #region Public Variables
  12. /// <summary>
  13. /// All Clients which are Currently Connected;
  14. /// </summary>
  15. public List<ClientData> ConnectedClients;
  16. /// <summary>
  17. /// Clients which connected at one point but no longer are
  18. /// Usefull if a client needs to reconnect
  19. /// </summary>
  20. public List<ClientData> DisconnectedClients;
  21. #endregion Public Variables
  22. #region Private Variables
  23. public NetworkServerSimple server;
  24. #endregion Private Variables
  25. /// <summary>
  26. /// Clears the lists
  27. /// </summary>
  28. public void Reset()
  29. {
  30. ConnectedClients = new List<ClientData>();
  31. DisconnectedClients = new List<ClientData>();
  32. }
  33. /// <summary>
  34. /// Resets the client list + registers handlers with the server
  35. /// </summary>
  36. /// <param name="serverObject">Server to register handlers with</param>
  37. public void SetUp(ServerObject serverObject)
  38. {
  39. this.server = serverObject.server;
  40. Reset();
  41. server.RegisterHandler(MsgType.Connect, OnClientConnect);
  42. }
  43. public void OnClientConnect(NetworkMessage msg)
  44. {
  45. if (ConnectedClients.Any(p => p.ID == msg.channelId))
  46. {
  47. Debug.LogError("Client[" + msg.channelId + "] already connected");
  48. return;
  49. }
  50. msg.conn.Send(LoginProtocols.RequestLoginDetails,new LoginProtocols.EmptyMsg());
  51. }
  52. }
  53. [System.Serializable]
  54. public class ClientData
  55. {
  56. /// <summary>
  57. /// Client Name
  58. /// </summary>
  59. public string Name;
  60. /// <summary>
  61. /// Color which represents player, picked by player
  62. /// </summary>
  63. public Color Color;
  64. /// <summary>
  65. /// Clients Current Score
  66. /// </summary>
  67. public int Score;
  68. /// <summary>
  69. /// Network connection ID
  70. /// </summary>
  71. public int ID;
  72. /// <summary>
  73. /// Connection to Client
  74. /// </summary>
  75. public NetworkConnection conn;
  76. }
  77. }