PlayerDetailsController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlayerDetailsController.cs" company="Exit Games GmbH">
  3. // Part of: Pun Cockpit
  4. // </copyright>
  5. // <author>developer@exitgames.com</author>
  6. // --------------------------------------------------------------------------------------------------------------------
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using Photon.Realtime;
  12. using Photon.Pun.UtilityScripts;
  13. using Hashtable = ExitGames.Client.Photon.Hashtable;
  14. namespace Photon.Pun.Demo.Cockpit
  15. {
  16. /// <summary>
  17. /// Controller for the Playerdetails UI view
  18. /// </summary>
  19. public class PlayerDetailsController : MonoBehaviourPunCallbacks
  20. {
  21. public GameObject ContentPanel;
  22. public PropertyCell PropertyCellPrototype;
  23. public Text UpdateStatusText;
  24. public Transform BuiltInPropertiesPanel;
  25. public Transform PlayerNumberingExtensionPanel;
  26. public Transform ScoreExtensionPanel;
  27. public Transform TeamExtensionPanel;
  28. public Transform TurnExtensionPanel;
  29. public Transform CustomPropertiesPanel;
  30. public GameObject MasterClientToolBar;
  31. public GameObject NotInRoomLabel;
  32. Dictionary<string, PropertyCell> builtInPropsCellList = new Dictionary<string, PropertyCell>();
  33. Player _player;
  34. void Awake()
  35. {
  36. this.PropertyCellPrototype.gameObject.SetActive(false);
  37. }
  38. public override void OnEnable()
  39. {
  40. base.OnEnable(); // as this inherits from MonoBehaviourPunCallbacks, we need to call base
  41. UpdateStatusText.text = string.Empty;
  42. NotInRoomLabel.SetActive(false);
  43. PlayerNumbering.OnPlayerNumberingChanged += OnPlayerNumberingChanged;
  44. }
  45. public override void OnDisable()
  46. {
  47. base.OnDisable(); // as this inherits from MonoBehaviourPunCallbacks, we need to call base
  48. PlayerNumbering.OnPlayerNumberingChanged -= OnPlayerNumberingChanged;
  49. }
  50. public void SetPlayerTarget(Player player)
  51. {
  52. //Debug.Log("SetPlayerTarget " + player);
  53. this._player = player;
  54. ContentPanel.SetActive(true);
  55. NotInRoomLabel.SetActive(false);
  56. this.ResetList();
  57. foreach (DictionaryEntry item in this.GetAllPlayerBuiltIntProperties())
  58. {
  59. this.AddProperty(ParseKey(item.Key), item.Value.ToString(), this.BuiltInPropertiesPanel);
  60. }
  61. // PlayerNumbering extension
  62. this.AddProperty("Player Number", "#" + player.GetPlayerNumber().ToString("00"), this.PlayerNumberingExtensionPanel);
  63. // Score extension
  64. this.AddProperty(PunPlayerScores.PlayerScoreProp, player.GetScore().ToString(), this.ScoreExtensionPanel);
  65. foreach (DictionaryEntry item in _player.CustomProperties)
  66. {
  67. this.AddProperty(ParseKey(item.Key), item.Value.ToString(), this.CustomPropertiesPanel);
  68. }
  69. MasterClientToolBar.SetActive(PhotonNetwork.CurrentRoom.PlayerCount>1 && PhotonNetwork.LocalPlayer.IsMasterClient);
  70. }
  71. void AddProperty(string property, string value, Transform parent)
  72. {
  73. PropertyCell _cell = Instantiate(PropertyCellPrototype);
  74. builtInPropsCellList.Add(property, _cell);
  75. _cell.transform.SetParent(parent, false);
  76. _cell.gameObject.SetActive(true);
  77. _cell.AddToList(property, value, false);
  78. }
  79. string ParseKey(object key)
  80. {
  81. if (key.GetType() == typeof(byte))
  82. {
  83. byte _byteKey = (byte)key;
  84. switch (_byteKey)
  85. {
  86. case (byte)255:
  87. return "PlayerName";
  88. case (byte)254:
  89. return "Inactive";
  90. case (byte)253:
  91. return "UserId";
  92. }
  93. }
  94. return key.ToString();
  95. }
  96. public void KickOutPlayer()
  97. {
  98. PhotonNetwork.CloseConnection(_player);
  99. }
  100. public void SetAsMaster()
  101. {
  102. PhotonNetwork.SetMasterClient(_player);
  103. }
  104. #region Photon CallBacks
  105. public override void OnPlayerLeftRoom(Player otherPlayer)
  106. {
  107. NotInRoomLabel.SetActive(otherPlayer == _player);
  108. ContentPanel.SetActive(otherPlayer != _player);
  109. }
  110. public override void OnMasterClientSwitched(Player newMasterClient)
  111. {
  112. MasterClientToolBar.SetActive(_player == newMasterClient);
  113. }
  114. public override void OnPlayerPropertiesUpdate(Player target, ExitGames.Client.Photon.Hashtable changedProps)
  115. {
  116. //Debug.Log("OnPlayerPropertiesUpdate " + target.ActorNumber + " " + target.ToStringFull() + " " + changedProps.ToStringFull());
  117. //Debug.Log("_player.ID " + _player.ActorNumber);
  118. if (_player.ActorNumber == target.ActorNumber)
  119. {
  120. foreach (DictionaryEntry entry in changedProps)
  121. {
  122. string _key = this.ParseKey(entry.Key);
  123. if (this.builtInPropsCellList.ContainsKey(_key))
  124. {
  125. this.builtInPropsCellList[_key].UpdateInfo(entry.Value.ToString());
  126. }
  127. else
  128. {
  129. this.AddProperty(_key, entry.Value.ToString(), this.CustomPropertiesPanel);
  130. }
  131. }
  132. }
  133. StartCoroutine("UpdateUIPing");
  134. }
  135. #endregion
  136. private void OnPlayerNumberingChanged()
  137. {
  138. if (_player != null)
  139. { // we might be called before player is setup
  140. this.builtInPropsCellList["Player Number"].UpdateInfo("#" + _player.GetPlayerNumber().ToString("00"));
  141. }
  142. }
  143. IEnumerator UpdateUIPing()
  144. {
  145. UpdateStatusText.text = "Updated";
  146. yield return new WaitForSeconds(1f);
  147. UpdateStatusText.text = string.Empty;
  148. }
  149. public void ResetList()
  150. {
  151. foreach (KeyValuePair<string, PropertyCell> entry in builtInPropsCellList)
  152. {
  153. if (entry.Value != null)
  154. {
  155. Destroy(entry.Value.gameObject);
  156. }
  157. }
  158. builtInPropsCellList = new Dictionary<string, PropertyCell>();
  159. }
  160. Hashtable GetAllPlayerBuiltIntProperties()
  161. {
  162. Hashtable allProps = new Hashtable();
  163. if (_player != null)
  164. {
  165. allProps["ID"] = _player.ActorNumber;
  166. allProps[ActorProperties.UserId] = _player.UserId != null ? _player.UserId : string.Empty;
  167. allProps["NickName"] = _player.NickName != null ? _player.NickName : string.Empty;
  168. allProps["IsLocal"] = _player.IsLocal;
  169. allProps[ActorProperties.IsInactive] = _player.IsInactive;
  170. allProps["IsMasterClient"] = _player.IsMasterClient;
  171. }
  172. Debug.Log(allProps.ToStringFull());
  173. return allProps;
  174. }
  175. }
  176. }