123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Advertisements;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- public class ResultPanel : PopUpPanel {
- public enum Result
- {
- Lose = 0,
- Win = 1,
- Draw = 2
- }
- public enum ChangeColor
- {
- Up = 0,
- Down = 1,
- }
- public Animator animator;
- public Text title;
- public Text coinTxt;
- public Text rankTxt;
- public Text orderTxt;
- public Text orderChangeTxt;
- public Text btnTxt;
- public Button btn;
- public ResultDrop resultDrop;
- public Color[] changeColorArr;
- private ResultData data;
- private RewardData rewardData;
- private bool isShown;
- protected override void BeforeShow ()
- {
- if(data.result == Result.Win)
- title.text = Language.GetStr ("ResultPanel", "victory");
- else if(data.result == Result.Lose)
- title.text = Language.GetStr ("ResultPanel", "defeat");
- else if(data.result == Result.Draw)
- title.text = Language.GetStr ("ResultPanel", "draw");
- rewardData = null;
- isShown = false;
- RequestReward ();
- }
- protected override void Shown ()
- {
- isShown = true;
- if (rewardData != null) {
- ShowReward ();
- }
- }
- private void RequestReward()
- {
- Session.GetInstance ().GetNetworkManager ().Result (data.result.GetHashCode(), data.crystalSpend);
- }
- private void OnGotReward(RewardData rewardData)
- {
- this.rewardData = rewardData;
- coinTxt.text = "+" + rewardData.coinAdd;
- rankTxt.text = (rewardData.rankAdd >= 0 ? "+" + rewardData.rankAdd : rewardData.rankAdd.ToString ());
- orderTxt.text = rewardData.rankOrder.ToString ();
- int startOrder = Session.GetInstance ().myUserData.rankIndex;
- int deltaOrder = rewardData.rankOrder - startOrder;
- if (deltaOrder > 0) {
- orderChangeTxt.color = changeColorArr [ChangeColor.Down.GetHashCode ()];
- orderChangeTxt.text = "[ -"+ Mathf.Abs(deltaOrder) +" ]";
- } else {
- orderChangeTxt.color = changeColorArr [ChangeColor.Up.GetHashCode ()];
- orderChangeTxt.text = "[ +"+ Mathf.Abs(deltaOrder) +" ]";
- }
- if (isShown)
- ShowReward ();
- }
- private void ShowReward()
- {
- UserData userData = Session.GetInstance ().myUserData;
- userData.coin = rewardData.coin;
- userData.rank = rewardData.rank;
- userData.rankIndex = rewardData.rankOrder;
- animator.Play ("Reward");
- }
- public void ShowDrop()
- {
- if (rewardData.HasDrop ()) {
- resultDrop.Show (rewardData.dropList, rewardData.extraPrice);
- animator.Play ("Drop");
- btnTxt.text = Language.GetStr("ResultPanel", "mix");
- } else {
- animator.Play ("NoDrop");
- btnTxt.text = Language.GetStr("Public", "continue");
- }
- }
- public void SelectDrop(int index)
- {
- BattleRequest.Reward (rewardData.dropId).ResultEvent.AddListener ((bool success, JsonData data)=>{
- if(success)
- {
- int rewardCount = JsonUtil.ToInt(data["reward"]);
- int equipId = JsonUtil.ToInt(data["equip_id"]);
- int equipItemId = JsonUtil.ToInt(data["equip_item_id"]);
- int count = JsonUtil.ToInt(data["count"]);
- if(JsonUtil.ContainKey(data, "diamond"))
- Session.GetInstance().myUserData.diamond = JsonUtil.ToInt(data["diamond"]);
- rewardData.rewardCount = rewardCount;
- EquipManager.GetInstance().RewardEquipment(equipId, equipItemId, count);
- resultDrop.Reward(index, rewardCount-1);
- if(!btn.gameObject.activeSelf)
- {
- btn.gameObject.SetActive(true);
- btnTxt.text = Language.GetStr("Public", "continue");
- }
- }
- else
- {
- resultDrop.RewardFaild(index);
- }
- });
- }
- public void OnClick()
- {
- if(rewardData != null && rewardData.HasDrop() && rewardData.rewardCount == 0)
- {
- resultDrop.Mix ();
- btn.gameObject.SetActive (false);
- return;
- }
- Session.GetInstance().GetBattleSession().GetMessageManager().ExitBattle();
- Close ();
- }
- override protected void Remove()
- {
- this.gameObject.SetActive (false);
- PopUpManager.UpdateModal();
- }
- private static ResultPanel currentPanel;
- public static ResultPanel Show(ResultData data)
- {
- if (currentPanel != null) {
- currentPanel.gameObject.SetActive (true);
- PopUpManager.AddToMainCanvas (currentPanel.gameObject);
- }
- else
- currentPanel = PopUpManager.AddPopUp<ResultPanel> (Resources.Load<GameObject> ("Prefabs/UI/Battle/ResultPanel"), null, true);
- currentPanel.data = data;
- currentPanel.Open ();
- return currentPanel;
- }
- public static bool IsEnabled()
- {
- if (currentPanel != null && currentPanel.gameObject.activeSelf)
- return true;
- return false;
- }
- public static void SetReward(RewardData rewardData)
- {
- if (IsEnabled ())
- currentPanel.OnGotReward (rewardData);
- }
- public static void RequestFailed()
- {
- if (IsEnabled ())
- currentPanel.RequestReward ();
- }
- public class ResultData
- {
- public Player me;
- public Player opp;
- public Result result;
- public int crystalSpend;
- public ResultData(Player me, Player opp, Result result, int crystalSpend)
- {
- this.me = me;
- this.opp = opp;
- this.result = result;
- this.crystalSpend = crystalSpend;
- }
- }
- public class RewardData
- {
- public int rank;
- public int exp;
- public int coin;
- public int win;
- public int lose;
- public int rankAdd;
- public int expAdd;
- public int coinAdd;
- public int winAdd;
- public int loseAdd;
- public int rankOrder;
- public int dropId;
- public List<int> dropList;
- public int extraPrice;
- public int rewardCount;
- public RewardData(JsonData data)
- {
- rank = JsonUtil.ToInt(data["r"]);
- exp = JsonUtil.ToInt(data["e"]);
- coin = JsonUtil.ToInt(data["c"]);
- win = JsonUtil.ToInt(data["w"]);
- lose = JsonUtil.ToInt(data["l"]);
- rankAdd = JsonUtil.ToInt(data["ra"]);
- expAdd = JsonUtil.ToInt(data["ea"]);
- coinAdd = JsonUtil.ToInt(data["ca"]);
- winAdd = JsonUtil.ToInt(data["wa"]);
- loseAdd = JsonUtil.ToInt(data["la"]);
- rankOrder = JsonUtil.ToInt(data["o"]);
- dropList = new List<int>();
- if(JsonUtil.ContainKey(data, "drop_id"))
- {
- dropId = JsonUtil.ToInt(data["drop_id"]);
- extraPrice = JsonUtil.ToInt(data["extra_price"]);
- JsonData drop = data["drop"];
- for(int i=0; i<drop.Count; i++)
- {
- int equipId = JsonUtil.ToInt(drop[i]);
- dropList.Add(equipId);
- }
- }
- }
- public bool HasDrop()
- {
- return dropId > 0;
- }
- }
- }
|