RankingUIView.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using ProjectBase.UI;
  4. using ShotSimulator.Tool;
  5. using ShotSimulator.User;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.Localization.Components;
  11. using UnityEngine.UI;
  12. namespace ShotSimulator.UI
  13. {
  14. [System.Serializable]
  15. public struct RankingData
  16. {
  17. public string userName;
  18. public int avatarId;
  19. public string avatarUrl;
  20. public int rank;
  21. public int score;
  22. public bool isSelf;
  23. }
  24. public class RankingUIView : BaseUIView
  25. {
  26. public Button backButton;
  27. public Transform rankViewContent;
  28. public RankingItem selfRankingItem;
  29. public LocalizeStringEvent TimeLocalize;
  30. public LocalizeStringEvent TrainTaskLocalize;
  31. public LocalizeStringEvent DifficultyLocalize;
  32. public LocalizeStringEvent modeLocalize;
  33. public LocalizeStringEvent deviceLocalize;
  34. protected override void InitUIObjects()
  35. {
  36. base.InitUIObjects();
  37. backButton.onClick.AddListener(OnBackButtonClick);
  38. InitTimeFilterPanel();
  39. InitTrainTaskFilterPanel();
  40. InitDifficultyTypeFilterPanel();
  41. InitModeTypeFilterPanel();
  42. InitDeviceTypeFilterPanel();
  43. }
  44. protected override void OnShowCallBack()
  45. {
  46. base.OnShowCallBack();
  47. GetRankingByCurrentFilter();
  48. }
  49. private void OnBackButtonClick()
  50. {
  51. UIManager.GetInstance().HideUIView(this);
  52. UIManager.GetInstance().ShowUIView("TrainTaskInfoUIView");
  53. }
  54. #region Time
  55. private TimeFilterType current_TimeFilterType = TimeFilterType.Week;
  56. private TimeFilterType Current_TimeFilterType
  57. {
  58. get { return current_TimeFilterType; }
  59. set
  60. {
  61. current_TimeFilterType = value;
  62. GetRankingByCurrentFilter();
  63. }
  64. }
  65. public List<Toggle> m_TimeFilterToggles = new List<Toggle>();
  66. public Text timeFilterTypeText;
  67. private void InitTimeFilterPanel()
  68. {
  69. for (int i = 0; i < m_TimeFilterToggles.Count; i++)
  70. {
  71. int index = i;
  72. TimeFilterType type = m_TimeFilterToggles[index].GetComponent<TimeTypeEnumTag>().m_TimeFilterType;
  73. m_TimeFilterToggles[index].onValueChanged.AddListener((isOn) =>
  74. {
  75. if (isOn)
  76. {
  77. Current_TimeFilterType = type;
  78. TimeLocalize.StringReference = m_TimeFilterToggles[index].GetComponentInChildren<LocalizeStringEvent>().StringReference;
  79. }
  80. });
  81. }
  82. }
  83. #endregion
  84. #region TrainTask
  85. private TrainTaskType current_TrainTaskType = TrainTaskType.JudgeShot;
  86. private TrainTaskType Current_TrainTaskType
  87. {
  88. get { return current_TrainTaskType; }
  89. set
  90. {
  91. current_TrainTaskType = value;
  92. GetRankingByCurrentFilter();
  93. }
  94. }
  95. public List<Toggle> m_TrainTaskToggles = new List<Toggle>();
  96. public Text trainTaskTypeText;
  97. private void InitTrainTaskFilterPanel()
  98. {
  99. for(int i = 0; i < m_TrainTaskToggles.Count; i++)
  100. {
  101. int index = i;
  102. TrainTaskType type = m_TrainTaskToggles[index].GetComponent<TrainTaskTypeEnumTag>().m_TrainTaskType;
  103. m_TrainTaskToggles[index].onValueChanged.AddListener((isOn) =>
  104. {
  105. if (isOn)
  106. {
  107. Current_TrainTaskType = type;
  108. TrainTaskLocalize.StringReference = m_TrainTaskToggles[index].GetComponentInChildren<LocalizeStringEvent>().StringReference;
  109. }
  110. });
  111. }
  112. }
  113. #endregion
  114. #region DifficultyType
  115. private DifficultyType current_DifficultyType = DifficultyType.Standard;
  116. private DifficultyType Current_DifficultyType
  117. {
  118. get { return current_DifficultyType; }
  119. set
  120. {
  121. current_DifficultyType = value;
  122. GetRankingByCurrentFilter();
  123. }
  124. }
  125. public List<Toggle> m_DifficultyTypeToggles = new List<Toggle>();
  126. public Text difficultyTypeText;
  127. private void InitDifficultyTypeFilterPanel()
  128. {
  129. for (int i = 0; i < m_DifficultyTypeToggles.Count; i++)
  130. {
  131. int index = i;
  132. DifficultyType type = m_DifficultyTypeToggles[index].GetComponent<DifficultyTypeEnumTag>().m_DifficultyType;
  133. m_DifficultyTypeToggles[index].onValueChanged.AddListener((isOn) =>
  134. {
  135. if (isOn)
  136. {
  137. Current_DifficultyType = type;
  138. DifficultyLocalize.StringReference = m_DifficultyTypeToggles[index].GetComponentInChildren<LocalizeStringEvent>().StringReference;
  139. }
  140. });
  141. }
  142. }
  143. #endregion
  144. #region Mode
  145. private ModeType current_ModeType = ModeType.NonTactical;
  146. private ModeType Current_ModeType
  147. {
  148. get { return current_ModeType; }
  149. set
  150. {
  151. current_ModeType = value;
  152. GetRankingByCurrentFilter();
  153. }
  154. }
  155. public List<Toggle> m_ModeTypeToggles = new List<Toggle>();
  156. public Text modeTypeText;
  157. private void InitModeTypeFilterPanel()
  158. {
  159. for (int i = 0; i < m_ModeTypeToggles.Count; i++)
  160. {
  161. int index = i;
  162. ModeType type = m_ModeTypeToggles[index].GetComponent<ModeTypeEnumTag>().m_ModeType;
  163. m_ModeTypeToggles[index].onValueChanged.AddListener((isOn) =>
  164. {
  165. if (isOn)
  166. {
  167. Current_ModeType = type;
  168. modeLocalize.StringReference = m_ModeTypeToggles[index].GetComponentInChildren<LocalizeStringEvent>().StringReference;
  169. }
  170. });
  171. }
  172. }
  173. #endregion
  174. #region Device
  175. private FirearmDeviceType current_FirearmDeviceType = FirearmDeviceType.M9;
  176. private FirearmDeviceType Current_FirearmDeviceType
  177. {
  178. get { return current_FirearmDeviceType; }
  179. set
  180. {
  181. current_FirearmDeviceType = value;
  182. GetRankingByCurrentFilter();
  183. }
  184. }
  185. public List<Toggle> m_DeviceTypeToggles = new List<Toggle>();
  186. public Text deviceTypeTypeText;
  187. private void InitDeviceTypeFilterPanel()
  188. {
  189. for (int i = 0; i < m_DeviceTypeToggles.Count; i++)
  190. {
  191. int index = i;
  192. FirearmDeviceType type = m_DeviceTypeToggles[index].GetComponent<DeviceTypeEnumTag>().m_DeviceType;
  193. m_DeviceTypeToggles[index].onValueChanged.AddListener((isOn) =>
  194. {
  195. if (isOn)
  196. {
  197. Current_FirearmDeviceType = type;
  198. deviceLocalize.StringReference = m_DeviceTypeToggles[index].GetComponentInChildren<LocalizeStringEvent>().StringReference;
  199. }
  200. });
  201. }
  202. }
  203. #endregion
  204. private void GetRankingByCurrentFilter()
  205. {
  206. RankingFilter filter = new RankingFilter()
  207. {
  208. trainTaskType = Current_TrainTaskType,
  209. difficultyType = Current_DifficultyType,
  210. modeType = Current_ModeType,
  211. firearmDeviceType = Current_FirearmDeviceType,
  212. timeFilterType = Current_TimeFilterType
  213. };
  214. UserManager.GetInstance().GetCustomLeaderboard(filter, (result) =>
  215. {
  216. List<RankingData> datas = JsonConvert.DeserializeObject<List<RankingData>>(JsonConvert.SerializeObject(result.data, Formatting.Indented));
  217. UpdateRankingView(datas);
  218. });
  219. }
  220. private void UpdateRankingView(List<RankingData> datas)
  221. {
  222. foreach(Transform child in rankViewContent)
  223. {
  224. Destroy(child.gameObject);
  225. }
  226. selfRankingItem.Reset();
  227. if (datas != null)
  228. {
  229. for (int i = 0; i < datas.Count; i++)
  230. {
  231. GameObject obj = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/UI/Ranking/RankingItem"));
  232. RankingItem item = obj.GetComponent<RankingItem>();
  233. item.transform.SetParent(rankViewContent, false);
  234. item.SetRankData(datas[i]);
  235. if (datas[i].isSelf)
  236. {
  237. selfRankingItem.SetRankData(datas[i]);
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }