ResultPanel.cs 6.0 KB

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