ResultPanel.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Advertisements;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using LitJson;
  7. public class ResultPanel : PopUpPanel {
  8. public enum Result
  9. {
  10. Lose = 0,
  11. Win = 1,
  12. Draw = 2
  13. }
  14. public enum ChangeColor
  15. {
  16. Up = 0,
  17. Down = 1,
  18. }
  19. public Animator animator;
  20. public Text title;
  21. public Text coinTxt;
  22. public Text rankTxt;
  23. public Text orderTxt;
  24. public Text orderChangeTxt;
  25. public Text btnTxt;
  26. public Button btn;
  27. public ResultDrop resultDrop;
  28. public Color[] changeColorArr;
  29. private ResultData data;
  30. private RewardData rewardData;
  31. private bool isShown;
  32. protected override void BeforeShow ()
  33. {
  34. if(data.result == Result.Win)
  35. title.text = Language.GetStr ("ResultPanel", "victory");
  36. else if(data.result == Result.Lose)
  37. title.text = Language.GetStr ("ResultPanel", "defeat");
  38. else if(data.result == Result.Draw)
  39. title.text = Language.GetStr ("ResultPanel", "draw");
  40. rewardData = null;
  41. isShown = false;
  42. RequestReward ();
  43. }
  44. protected override void Shown ()
  45. {
  46. isShown = true;
  47. if (rewardData != null) {
  48. ShowReward ();
  49. }
  50. }
  51. private void RequestReward()
  52. {
  53. Session.GetInstance ().GetNetworkManager ().Result (data.result.GetHashCode(), data.crystalSpend);
  54. }
  55. private void OnGotReward(RewardData rewardData)
  56. {
  57. this.rewardData = rewardData;
  58. coinTxt.text = "+" + rewardData.coinAdd;
  59. rankTxt.text = (rewardData.rankAdd >= 0 ? "+" + rewardData.rankAdd : rewardData.rankAdd.ToString ());
  60. orderTxt.text = rewardData.rankOrder.ToString ();
  61. int startOrder = Session.GetInstance ().myUserData.rankIndex;
  62. int deltaOrder = rewardData.rankOrder - startOrder;
  63. if (deltaOrder > 0) {
  64. orderChangeTxt.color = changeColorArr [ChangeColor.Down.GetHashCode ()];
  65. orderChangeTxt.text = "[ -"+ Mathf.Abs(deltaOrder) +" ]";
  66. } else {
  67. orderChangeTxt.color = changeColorArr [ChangeColor.Up.GetHashCode ()];
  68. orderChangeTxt.text = "[ +"+ Mathf.Abs(deltaOrder) +" ]";
  69. }
  70. if (isShown)
  71. ShowReward ();
  72. }
  73. private void ShowReward()
  74. {
  75. UserData userData = Session.GetInstance ().myUserData;
  76. userData.coin = rewardData.coin;
  77. userData.rank = rewardData.rank;
  78. userData.rankIndex = rewardData.rankOrder;
  79. animator.Play ("Reward");
  80. }
  81. public void ShowDrop()
  82. {
  83. if (rewardData.HasDrop ()) {
  84. resultDrop.Show (rewardData.dropList, rewardData.extraPrice);
  85. animator.Play ("Drop");
  86. btnTxt.text = Language.GetStr("ResultPanel", "mix");
  87. } else {
  88. animator.Play ("NoDrop");
  89. btnTxt.text = Language.GetStr("Public", "continue");
  90. }
  91. }
  92. public void SelectDrop(int index)
  93. {
  94. BattleRequest.Reward (rewardData.dropId).ResultEvent.AddListener ((bool success, JsonData data)=>{
  95. if(success)
  96. {
  97. int rewardCount = JsonUtil.ToInt(data["reward"]);
  98. int equipId = JsonUtil.ToInt(data["equip_id"]);
  99. int equipItemId = JsonUtil.ToInt(data["equip_item_id"]);
  100. int count = JsonUtil.ToInt(data["count"]);
  101. if(JsonUtil.ContainKey(data, "diamond"))
  102. Session.GetInstance().myUserData.diamond = JsonUtil.ToInt(data["diamond"]);
  103. rewardData.rewardCount = rewardCount;
  104. EquipManager.GetInstance().RewardEquipment(equipId, equipItemId, count);
  105. resultDrop.Reward(index, rewardCount-1);
  106. if(!btn.gameObject.activeSelf)
  107. {
  108. btn.gameObject.SetActive(true);
  109. btnTxt.text = Language.GetStr("Public", "continue");
  110. }
  111. }
  112. else
  113. {
  114. resultDrop.RewardFaild(index);
  115. }
  116. });
  117. }
  118. public void OnClick()
  119. {
  120. if(rewardData != null && rewardData.HasDrop() && rewardData.rewardCount == 0)
  121. {
  122. resultDrop.Mix ();
  123. btn.gameObject.SetActive (false);
  124. return;
  125. }
  126. Session.GetInstance().GetBattleSession().GetMessageManager().ExitBattle();
  127. Close ();
  128. }
  129. override protected void Remove()
  130. {
  131. this.gameObject.SetActive (false);
  132. PopUpManager.UpdateModal();
  133. }
  134. private static ResultPanel currentPanel;
  135. public static ResultPanel Show(ResultData data)
  136. {
  137. if (currentPanel != null) {
  138. currentPanel.gameObject.SetActive (true);
  139. PopUpManager.AddToMainCanvas (currentPanel.gameObject);
  140. }
  141. else
  142. currentPanel = PopUpManager.AddPopUp<ResultPanel> (Resources.Load<GameObject> ("Prefabs/UI/Battle/ResultPanel"), null, true);
  143. currentPanel.data = data;
  144. currentPanel.Open ();
  145. return currentPanel;
  146. }
  147. public static bool IsEnabled()
  148. {
  149. if (currentPanel != null && currentPanel.gameObject.activeSelf)
  150. return true;
  151. return false;
  152. }
  153. public static void SetReward(RewardData rewardData)
  154. {
  155. if (IsEnabled ())
  156. currentPanel.OnGotReward (rewardData);
  157. }
  158. public static void RequestFailed()
  159. {
  160. if (IsEnabled ())
  161. currentPanel.RequestReward ();
  162. }
  163. public class ResultData
  164. {
  165. public Player me;
  166. public Player opp;
  167. public Result result;
  168. public int crystalSpend;
  169. public ResultData(Player me, Player opp, Result result, int crystalSpend)
  170. {
  171. this.me = me;
  172. this.opp = opp;
  173. this.result = result;
  174. this.crystalSpend = crystalSpend;
  175. }
  176. }
  177. public class RewardData
  178. {
  179. public int rank;
  180. public int exp;
  181. public int coin;
  182. public int win;
  183. public int lose;
  184. public int rankAdd;
  185. public int expAdd;
  186. public int coinAdd;
  187. public int winAdd;
  188. public int loseAdd;
  189. public int rankOrder;
  190. public int dropId;
  191. public List<int> dropList;
  192. public int extraPrice;
  193. public int rewardCount;
  194. public RewardData(JsonData data)
  195. {
  196. rank = JsonUtil.ToInt(data["r"]);
  197. exp = JsonUtil.ToInt(data["e"]);
  198. coin = JsonUtil.ToInt(data["c"]);
  199. win = JsonUtil.ToInt(data["w"]);
  200. lose = JsonUtil.ToInt(data["l"]);
  201. rankAdd = JsonUtil.ToInt(data["ra"]);
  202. expAdd = JsonUtil.ToInt(data["ea"]);
  203. coinAdd = JsonUtil.ToInt(data["ca"]);
  204. winAdd = JsonUtil.ToInt(data["wa"]);
  205. loseAdd = JsonUtil.ToInt(data["la"]);
  206. rankOrder = JsonUtil.ToInt(data["o"]);
  207. dropList = new List<int>();
  208. if(JsonUtil.ContainKey(data, "drop_id"))
  209. {
  210. dropId = JsonUtil.ToInt(data["drop_id"]);
  211. extraPrice = JsonUtil.ToInt(data["extra_price"]);
  212. JsonData drop = data["drop"];
  213. for(int i=0; i<drop.Count; i++)
  214. {
  215. int equipId = JsonUtil.ToInt(drop[i]);
  216. dropList.Add(equipId);
  217. }
  218. }
  219. }
  220. public bool HasDrop()
  221. {
  222. return dropId > 0;
  223. }
  224. }
  225. }