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.

146 lines
4.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Multiplayer
  6. {
  7. public class LoginManager
  8. {
  9. private ServerManager ServerManager;
  10. private bool isGameRunning = false;
  11. public LoginManager(ServerManager ServerManager)
  12. {
  13. this.ServerManager = ServerManager;
  14. RegisterHandles();
  15. }
  16. public void RegisterHandles()
  17. {
  18. ServerManager.Server.RegisterHandler(LoginMsgID.Name, OnPlayerLogin);
  19. ServerManager.Server.RegisterHandler(MsgType.Connect, OnConnect);
  20. ServerManager.Server.RegisterHandler(MsgType.Disconnect, OnDisconnect);
  21. ServerManager.Server.RegisterHandler(PlayerMsgID.GameStart, ClientStartsGame);
  22. ServerManager.Server.RegisterHandler(PlayerMsgID.Lobby, GoToLobby);
  23. ServerManager.Server.RegisterHandler(PlayerMsgID.GameWin, OnGameWin);
  24. ServerManager.Server.RegisterHandler(PlayerMsgID.GameOver, OnGameOver);
  25. ServerManager.Server.RegisterHandler(LoginMsgID.QueryGameRunning, QueryGameState);
  26. }
  27. //Called when a player logs in
  28. public void OnPlayerLogin(NetworkMessage msg)
  29. {
  30. StringMsg stringMsg;
  31. if (!Utility.ReadMessage<StringMsg>(msg, out stringMsg))
  32. return;
  33. Debug.Log("Player login: " + stringMsg.String);
  34. if (ServerManager.AllPlayers.ContainsKey(stringMsg.ID))
  35. {
  36. Debug.Log(stringMsg.String + " already logged in");
  37. return;
  38. }
  39. Player newPlayer = new Player(msg.conn, stringMsg.String);
  40. ServerManager.AllPlayers.Add(newPlayer.ID, newPlayer);
  41. foreach (Player player in ServerManager.AllPlayers.Values)
  42. {
  43. if (!player.Equals(newPlayer))
  44. {
  45. player.Connection.Send(LoginMsgID.RemotePlayerJoin, new PlayerDataMsg(newPlayer.ID, newPlayer.Name, Color.white));
  46. newPlayer.Connection.Send(LoginMsgID.RemotePlayerJoin, new PlayerDataMsg(player.ID, player.Name, Color.white));
  47. }
  48. }
  49. }
  50. private void OnConnect(NetworkMessage msg)
  51. {
  52. Debug.Log("New Connection: \n Address: " + msg.conn.address + "\n ID: " + msg.conn.connectionId);
  53. msg.conn.Send(LoginMsgID.QueryName, new PlayerMsg((byte)msg.conn.connectionId));
  54. }
  55. private void OnDisconnect(NetworkMessage msg)
  56. {
  57. if (ServerManager.AllPlayers.ContainsKey(msg.conn.connectionId))
  58. {
  59. Player leftPlayer = ServerManager.AllPlayers[msg.conn.connectionId];
  60. Debug.Log(ServerManager.AllPlayers[msg.conn.connectionId].Name + "has disconnected.");
  61. ServerManager.AllPlayers.Remove(msg.conn.connectionId);
  62. foreach(Player player in ServerManager.AllPlayers.Values)
  63. {
  64. player.Connection.Send(LoginMsgID.RemotePlayerExit, new PlayerMsg(leftPlayer.ID));
  65. }
  66. }
  67. else
  68. Debug.Log("Unkown player has disconnected.");
  69. }
  70. private void ClientStartsGame(NetworkMessage msg)
  71. {
  72. StartGame();
  73. }
  74. private void StartGame()
  75. {
  76. isGameRunning = true;
  77. foreach (Player player in ServerManager.AllPlayers.Values)
  78. {
  79. player.Connection.Send(LoginMsgID.Seed, new longMsg(player.ID, ServerManager.Seed));
  80. player.Connection.Send(PlayerMsgID.GameStart, new PlayerMsg(player.ID));
  81. }
  82. }
  83. private void OnGameWin(NetworkMessage msg)
  84. {
  85. ServerManager.Seed = System.DateTime.Now.Ticks;
  86. foreach (Player player in ServerManager.AllPlayers.Values)
  87. {
  88. PlayerMsg winMsg = new PlayerMsg(player.ID);
  89. player.Connection.Send(PlayerMsgID.GameWin, winMsg);
  90. }
  91. }
  92. private void OnGameOver(NetworkMessage msg)
  93. {
  94. foreach (Player player in ServerManager.AllPlayers.Values)
  95. {
  96. PlayerMsg winMsg = new PlayerMsg(player.ID);
  97. player.Connection.Send(PlayerMsgID.GameOver, winMsg);
  98. }
  99. }
  100. private void GoToLobby(NetworkMessage msg)
  101. {
  102. isGameRunning = false;
  103. foreach(Player player in ServerManager.AllPlayers.Values)
  104. player.Connection.Send(PlayerMsgID.Lobby, new PlayerMsg(player.ID));
  105. }
  106. private void QueryGameState(NetworkMessage msg)
  107. {
  108. msg.conn.Send(LoginMsgID.QueryGameRunning, new BoolMsg((byte)msg.conn.connectionId, isGameRunning));
  109. }
  110. }
  111. }