djgaven588

MultiplayerServer

Apr 16th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MultiplayerServer : MonoBehaviour {
  6.  
  7. public List<ClientInfo> clientList;
  8. public List<ClientInfo> deadClients;
  9. public int updateRate = 20;
  10. private float currentTimeBetweenUpdates;
  11.  
  12. private PhotonView view;
  13. void Awake ()
  14. {
  15. if(PhotonNetwork.isMasterClient)
  16. {
  17. clientList = new List<ClientInfo>();
  18. view = GetComponent<PhotonView>();
  19. }
  20. else
  21. {
  22. this.enabled = false;
  23. }
  24. }
  25.  
  26. void FixedUpdate ()
  27. {
  28. currentTimeBetweenUpdates -= Time.fixedDeltaTime;
  29. if(currentTimeBetweenUpdates <= 0)
  30. {
  31. MultiplayerUpdate();
  32. currentTimeBetweenUpdates = (float)updateRate / 1000f;
  33. }
  34. }
  35.  
  36. private void MultiplayerUpdate()
  37. {
  38. RemoveDeadClients();
  39. string[] _playerNames = GetPlayerNames();
  40. int[] _playerKills = GetPlayerKills();
  41. int[] _playerDeaths = GetPlayerDeaths();
  42. view.RPC("UpdateLeaderBoard", PhotonTargets.All, _playerNames, _playerKills, _playerDeaths);
  43. }
  44.  
  45. private void RemoveDeadClients()
  46. {
  47. for(int i=clientList.Count-1; i >= 0; i--)
  48. {
  49. if(clientList[i].ClientDead())
  50. {
  51. Debug.Log("A user went missing! Moving player " + clientList[i].client.clientName + " to dead clients!");
  52. deadClients.Add(clientList[i]);
  53. clientList.RemoveAt(i);
  54. }
  55. }
  56. }
  57.  
  58. private void RegisterNewClients()
  59. {
  60. MultiplayerClient[] allClients = GameObject.FindObjectsOfType<MultiplayerClient>();
  61. foreach(MultiplayerClient selectedClient in allClients)
  62. {
  63. if(selectedClient.setup == false)
  64. {
  65. clientList.Add(new ClientInfo(selectedClient.clientName, selectedClient));
  66. selectedClient.GetComponent<PhotonView>().RPC("SyncClientInfo", PhotonTargets.AllBuffered, true, selectedClient.clientName);
  67. Debug.Log("Found new user! Registered player " + selectedClient.clientName + " as new client!");
  68. }
  69. }
  70. }
  71.  
  72. private void SpawnPlayer(Character selectedCharacter, TeamId team)
  73. {
  74.  
  75. }
  76.  
  77. private string[] GetPlayerNames()
  78. {
  79. List<string> names = new List<string>();
  80. for(int i=0; i<clientList.Count; i++)
  81. {
  82. names.Add(clientList[i].client.name);
  83. }
  84. return names.ToArray();
  85. }
  86.  
  87. private int[] GetPlayerKills()
  88. {
  89. List<int> kills = new List<int>();
  90. for(int i=0; i<clientList.Count; i++)
  91. {
  92. kills.Add(clientList[i].kills);
  93. }
  94. return kills.ToArray();
  95. }
  96.  
  97. private int[] GetPlayerDeaths()
  98. {
  99. List<int> deaths = new List<int>();
  100. for(int i=0; i<clientList.Count; i++)
  101. {
  102. deaths.Add(clientList[i].deaths);
  103. }
  104. return deaths.ToArray();
  105. }
  106.  
  107.  
  108. }
  109.  
  110. public class ClientInfo
  111. {
  112. public string name;
  113. public MultiplayerClient client;
  114. public int kills;
  115. public int deaths;
  116.  
  117. public PlayerInfo playerInfo;
  118.  
  119. public ClientInfo(string n, MultiplayerClient c)
  120. {
  121. this.name = n;
  122. this.client = c;
  123. this.kills = 0;
  124. this.deaths = 0;
  125. this.playerInfo = new PlayerInfo();
  126. }
  127.  
  128. ///<summary>
  129. ///This checks to see if the connection is still "living" aka the connection object is still instantiated.
  130. ///</summary>
  131. public bool ClientDead()
  132. {
  133. if(client == null)
  134. {
  135. return true;
  136. }
  137.  
  138. return false;
  139. }
  140.  
  141. ///<summary>
  142. ///Adds a kill to the client.
  143. ///</summary>
  144. public void AddKill()
  145. {
  146. kills++;
  147. }
  148.  
  149. ///<summary>
  150. ///Adds a death to the client.
  151. ///</summary>
  152. public void AddDeath()
  153. {
  154. deaths++;
  155. }
  156.  
  157. ///<summary>
  158. ///Calculates and returns KD ratio.
  159. ///</summary>
  160. public float KdRatio()
  161. {
  162. int killCount = kills;
  163. int deathCount = deaths;
  164. if(kills < 1)
  165. {
  166. killCount = 1;
  167. }
  168. if(deaths < 1)
  169. {
  170. deathCount = 1;
  171. }
  172. return ((float)killCount/(float)deathCount);
  173. }
  174. }
  175.  
  176. public class PlayerInfo
  177. {
  178. public float health = 100f;
  179. public float maxHealth = 100f;
  180. public TeamId currentTeam = TeamId.None;
  181. public GameObject player = null;
  182.  
  183. ///<summary>
  184. ///Adds change to health.
  185. ///Returns if the player is alive.
  186. ///</summary>
  187. public bool ChangeHealth(float change)
  188. {
  189. health += change;
  190. if(health > 0)
  191. {
  192. if(health > maxHealth)
  193. {
  194. health = maxHealth;
  195. }
  196. }
  197. else
  198. {
  199. health = 0;
  200. if(player != null)
  201. {
  202. PhotonNetwork.Destroy(player);
  203. }
  204. return false;
  205. }
  206.  
  207. return true;
  208. }
  209.  
  210. public void SetValues(float maxH)
  211. {
  212. health = maxH;
  213. maxHealth = maxH;
  214. }
  215. }
  216.  
  217. public enum TeamId {
  218. Red,
  219. Blue,
  220. None
  221. }
  222. public enum Characters {
  223. EDE,
  224. Time_Master
  225. }
Add Comment
Please, Sign In to add comment