123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- using UnityEngine;
- using UnityEngine.UI;
- using Random = UnityEngine.Random;
- public class RechargeGiftJsonLabel
- {
- public static string PackID = "packid";
- public static string StartTime = "starttime";
- public static string EndTime = "endtime";
- public static string UnvalidValueStr = "0";
- public static Dictionary<RechargeGift.GiftType, string> TypeDictionary = new Dictionary<RechargeGift.GiftType, string>
- {
- {RechargeGift.GiftType.金币,"g"},
- {RechargeGift.GiftType.钻石,"d"},
- {RechargeGift.GiftType.礼包,"p"},
- {RechargeGift.GiftType.花朵,"f"},
- {RechargeGift.GiftType.服装,"c"},
- {RechargeGift.GiftType.开垦土地,"s"},
- {RechargeGift.GiftType.精灵,"a"},
- };
- }
- public class RechargeGift
- {
- public enum GiftType
- {
- 金币 = 0,
- 钻石 = 1,
- 礼包 = 2,
- 花朵 = 3,
- 服装 = 4,
- 开垦土地 = 5,
- 精灵 = 6,
- }
- public abstract class GiftAward
- {
- protected float CloseExchangeRatio = 0.25f;
- protected float AbilityExchangeRatio = 0.25f;
- protected GiftType GiftType;
- public GiftAward(GiftType giftType)
- {
- GiftType = giftType;
- }
- /// <returns>true-全部领完</returns>
- public abstract bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo);
- protected void GetGiftAward(GiftType type, int value, out KV<GiftType, int> giftAwardInfo)
- {
- if (type == GiftType.开垦土地)
- {
- AudioManager.PlayClip(ResourceLabel.CurrentClip);
- for (int i = 0; i < value; i++)
- {
- GardenManager.UnlockSlot();
- int extraSlot = ConfigManager.GetIntFormConfig(PlayerConfigLabel.ExtraSlot);
- ConfigManager.SaveIntToConfig(PlayerConfigLabel.ExtraSlot, extraSlot + 1);
- }
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- Debug.LogWarning($"{giftAwardInfo.Key} {giftAwardInfo.Value}");
- }
- else if (type == GiftType.服装)
- {
- CloseItem closeItem = PlayerManager.CloseItemDictionary[value];
- if (closeItem.IsBought)
- {
- AudioManager.PlayClip(ResourceLabel.CurrentClip);
- ExchangeInfo info = closeItem.GetExchangeValue(CloseExchangeRatio, StaticsManager.ConsumeModule.Gift);
- giftAwardInfo.Key = CurrentToGiftType(info.Current);
- giftAwardInfo.Value = (int) info.Value;
- }
- else
- {
- closeItem.OnBuySucceed();
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- }
- Debug.LogWarning($"{giftAwardInfo.Key} {giftAwardInfo.Value}");
- }
- else if (type == GiftType.礼包)
- {
- string packID = SkillConfigLabel.GetFullID(SkillType.Pack, value);
- (Manager.SkillDictionary[packID] as Pack).OnBuySucceed(false);
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- Debug.LogWarning($"{giftAwardInfo.Key} {giftAwardInfo.Value}");
- }
- else if (type == GiftType.精灵)
- {
- string abilityID = SkillConfigLabel.GetFullID(SkillType.Ability, value);
- Ability ability = Manager.SkillDictionary[abilityID] as Ability;
- if (ability.ItemStatus == SkillStatus.Lock)
- {
- AudioManager.PlayClip(ResourceLabel.CurrentClip);
- ExchangeInfo info = ability.GetUnlockAheadExchangeValue(AbilityExchangeRatio, StaticsManager.ConsumeModule.Gift);
- giftAwardInfo.Key = CurrentToGiftType(info.Current);
- giftAwardInfo.Value = (int)info.Value;
- }
- else if (ability.ItemStatus == SkillStatus.UnLock)
- {
- ability.UnlockSucceed();
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- }
- else if (ability.ItemStatus == SkillStatus.Upgrade)
- {
- ability.UpgradeSucceed();
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- }
- else
- {
- throw new Exception();
- }
- Debug.LogWarning($"{giftAwardInfo.Key} {giftAwardInfo.Value}");
- }
- else if (type == GiftType.花朵)
- {
- FlowerInfo flowerInfo = GardenManager.FlowerInfoDictionary[value];
- flowerInfo.Add();
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- Debug.LogWarning($"{giftAwardInfo.Key} {giftAwardInfo.Value}");
- }
- else if (type == GiftType.金币)
- {
- AudioManager.PlayClip(ResourceLabel.CurrentClip);
- Manager.AddCoin(value, StaticsManager.ItemID.获得金币, StaticsManager.ConsumeModule.Gift);
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- Debug.LogWarning($"{giftAwardInfo.Key} {giftAwardInfo.Value}");
- }
- else if (type == GiftType.钻石)
- {
- AudioManager.PlayClip(ResourceLabel.CurrentClip);
- Manager.AddDiamond(value, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.Gift);
- giftAwardInfo.Key = type;
- giftAwardInfo.Value = value;
- Debug.LogWarning($"{giftAwardInfo.Key} {giftAwardInfo.Value}");
- }
- else
- {
- throw new Exception();
- }
- }
- protected GiftType CurrentToGiftType(Current current)
- {
- if (current == Current.Coin)
- {
- return GiftType.金币;
- }
- else if (current == Current.Diamond)
- {
- return GiftType.钻石;
- }
- else
- {
- throw new Exception();
- }
- }
- }
- public class SingleGiftAward : GiftAward
- {
- protected int Value;
- public SingleGiftAward(GiftType giftType, int value) : base(giftType)
- {
- Value = value;
- //Debug.Log($"{giftType} {value}");
- }
- public override bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
- {
- GetGiftAward(GiftType, Value, out giftAwardInfo);
- return true;
- }
- }
- public class MultipleGiftAward : GiftAward
- {
- protected List<int> Values;
- public MultipleGiftAward(GiftType giftType, List<int> values) : base(giftType)
- {
- Values = values;
- //Debug.Log($"{giftType} {Auxiliary.IntsToString(Values)}");
- }
- public override bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
- {
- GetGiftAward(GiftType, Values[0], out giftAwardInfo);
- Values.RemoveAt(0);
- if (Values.Count > 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- //随机获取区间内的一个
- public class RandomGiftAward : GiftAward
- {
- protected int StartIndex;
- protected int EndIndex;
- public RandomGiftAward(GiftType giftType, int startIndex, int endIndex) : base(giftType)
- {
- StartIndex = startIndex;
- EndIndex = endIndex;
- //Debug.Log($"{giftType} {StartIndex} {EndIndex}");
- }
- public override bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
- {
- int value = Random.Range(StartIndex, EndIndex + 1);
- GetGiftAward(GiftType, value, out giftAwardInfo);
- return true;
- }
- }
- #region Config
- public int RemainGiftAward
- {
- get { return GiftAwards.Count; }
- }
- private DateTime StartDate;
- private DateTime EndDate;
- private List<GiftAward> GiftAwards = new List<GiftAward>();
- #endregion
- public RechargeGift(JsonData jsonData)
- {
- //Debug.Log(jsonData.ToJson());
- string label = RechargeGiftJsonLabel.TypeDictionary[GiftType.金币];
- string valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftJsonLabel.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.金币, valueStr));
- }
- label = RechargeGiftJsonLabel.TypeDictionary[GiftType.钻石];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftJsonLabel.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.钻石, valueStr));
- }
- label = RechargeGiftJsonLabel.TypeDictionary[GiftType.礼包];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftJsonLabel.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.礼包, valueStr));
- }
- label = RechargeGiftJsonLabel.TypeDictionary[GiftType.花朵];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftJsonLabel.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.花朵, valueStr));
- }
- label = RechargeGiftJsonLabel.TypeDictionary[GiftType.服装];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftJsonLabel.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.服装, valueStr));
- }
- label = RechargeGiftJsonLabel.TypeDictionary[GiftType.开垦土地];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftJsonLabel.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.开垦土地, valueStr));
- }
- label = RechargeGiftJsonLabel.TypeDictionary[GiftType.精灵];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftJsonLabel.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.精灵, valueStr));
- }
- StartDate = DateTime.Parse(jsonData[RechargeGiftJsonLabel.StartTime].ToString());
- EndDate = DateTime.Parse(jsonData[RechargeGiftJsonLabel.EndTime].ToString());
- //Debug.LogWarning(StartDate);
- //Debug.LogWarning(EndDate);
- }
- private List<GiftAward> ParseAllGiftAward(GiftType giftType, string valueStr)
- {
- List<GiftAward> giftAwards = new List<GiftAward>();
- string[] values = valueStr.Split('|');
- for (int i = 0; i < values.Length; i++)
- {
- giftAwards.Add(ParseGiftAward(giftType, values[i]));
- }
- return giftAwards;
- }
- private GiftAward ParseGiftAward(GiftType giftType, string valueStr)
- {
- if (valueStr.Contains(" "))
- {
- List<int> values = Auxiliary.StringToInts(' ', valueStr, new List<int>());
- return new MultipleGiftAward(giftType, values);
- }
- else if (valueStr.Contains(","))
- {
- valueStr = valueStr.TrimStart('[');
- valueStr = valueStr.TrimEnd(']');
- string[] valueStrs = valueStr.Split(',');
- int startIndex = int.Parse(valueStrs[0]);
- int endIndex = int.Parse(valueStrs[1]);
- return new RandomGiftAward(giftType, startIndex, endIndex);
- }
- else if (valueStr.Contains("-"))
- {
- string[] valueStrs = valueStr.Split('-');
- int startIndex = int.Parse(valueStrs[0]);
- int endIndex = int.Parse(valueStrs[1]);
- List<int> values = Auxiliary.StartEndIndexToInts(startIndex, endIndex);
- return new MultipleGiftAward(giftType, values);
- }
- else
- {
- int value = int.Parse(valueStr);
- return new SingleGiftAward(giftType, value);
- }
- }
- public bool IsAwardAvailable()
- {
- if (HttpManager.CurrentDateTime > EndDate)
- {
- //Debug.Log("has't start");
- return false;
- }
- if (HttpManager.CurrentDateTime < StartDate)
- {
- //Debug.Log("over");
- return false;
- }
- if (GiftAwards.Count == 0)
- {
- return false;
- }
- return true;
- }
- public bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
- {
- if (GiftAwards[0].GetOneGiftAward(out giftAwardInfo))
- {
- GiftAwards.RemoveAt(0);
- }
- if (GiftAwards.Count > 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- public class RechargeGiftManager
- {
- #region Config
- public static bool Inited;
- private static Dictionary<int, RechargeGift> GiftDictionary = new Dictionary<int, RechargeGift>();
- private static string AmountTextPrefix = "x";
- private static RechargeGift CurrentRechargeGift;
- private static Text Title;
- private static Text Description;
- private static Text ConfirmButtonText;
- private static Image AwardImage0;
- private static Image AwardImage1;
- private static Image AwardImage2;
- private static Image FlowerAwardImage;
- private static Button ConfirmButton;
- private static Transform PanelBK;
- private static List<Image> AwardImages = new List<Image>();
- #endregion
- public static void Init(JsonData jsonData)
- {
- //Debug.LogWarning("Inited");
- Inited = true;
- for (int i = 0; i < jsonData.Count; i++)
- {
- int packID = (int) jsonData[i][RechargeGiftJsonLabel.PackID];
- RechargeGift rechargeGift = new RechargeGift(jsonData[i]);
- GiftDictionary.Add(packID, rechargeGift);
- }
- Title = ResourceManager.Get<Text>(CanvasLabel.AB_Title);
- Description = ResourceManager.Get<Text>(CanvasLabel.AB_Description);
- ConfirmButtonText = ResourceManager.Get<Text>(CanvasLabel.AB_ConfirmText);
- AwardImage0 = ResourceManager.Get<Image>(CanvasLabel.AB_AwardImage0);
- AwardImage1 = ResourceManager.Get<Image>(CanvasLabel.AB_AwardImage1);
- AwardImage2 = ResourceManager.Get<Image>(CanvasLabel.AB_AwardImage2);
- FlowerAwardImage = ResourceManager.Get<Image>(CanvasLabel.AB_FlowerAwardImage);
- ConfirmButton = ResourceManager.Get<Button>(CanvasLabel.AB_Confirm);
- PanelBK = ResourceManager.Get(CanvasLabel.AB_PanelBK);
- AwardImages.Add(AwardImage0);
- AwardImages.Add(AwardImage1);
- AwardImages.Add(AwardImage2);
- PanelBK.CreateTweenCG(0, 1, 0.25f, false, true, Curve.EaseOutQuad);
- ConfirmButton.onClick.AddListener(() => GetNextRechargeGiftAward(CurrentRechargeGift));
- LanguageManager.Add(Title, new MulLanStr(LanguageLabel.UI__AB_RechargeGiftTitle));
- LanguageManager.Add(ConfirmButtonText, new MulLanStr(LanguageLabel.Common__Confirm));
- }
- public static void GetAllRechargeGiftAward(int packID)
- {
- if (!GiftDictionary.ContainsKey(packID))
- {
- return;
- }
- CurrentRechargeGift = GiftDictionary[packID];
- if (!CurrentRechargeGift.IsAwardAvailable())
- {
- return;
- }
- PanelBK.TweenForCG();
- GetNextRechargeGiftAward(CurrentRechargeGift);
- }
- private static void GetNextRechargeGiftAward(RechargeGift rechargeGift)
- {
- if (rechargeGift.RemainGiftAward == 0)
- {
- PanelBK.TweenBacCG();
- }
- else
- {
- KV<RechargeGift.GiftType, int> awardInfo;
- rechargeGift.GetOneGiftAward(out awardInfo);
- SetupRechargeGiftAwardUI(awardInfo);
- }
- }
- private static void SetupRechargeGiftAwardUI(KV<RechargeGift.GiftType, int> awardInfo)
- {
- if (awardInfo.Key == RechargeGift.GiftType.花朵)
- {
- AwardImages.Remove(FlowerAwardImage);
- AwardImages.Insert(0 ,FlowerAwardImage);
- }
- else
- {
- AwardImages.Remove(FlowerAwardImage);
- AwardImages.Add(FlowerAwardImage);
- }
- if (awardInfo.Key == RechargeGift.GiftType.金币)
- {
- SetupRechargeGiftAwardUI(awardInfo.Key, awardInfo.Value, AwardImages, 0.75f, 0.75f);
- Description.text = AmountTextPrefix + awardInfo.Value;
- }
- else if (awardInfo.Key == RechargeGift.GiftType.钻石)
- {
- SetupRechargeGiftAwardUI(awardInfo.Key, awardInfo.Value, AwardImages, 0.75f, 0.75f);
- Description.text = AmountTextPrefix + awardInfo.Value;
- }
- else if (awardInfo.Key == RechargeGift.GiftType.花朵)
- {
- FlowerInfo flowerInfo = (FlowerInfo) SetupRechargeGiftAwardUI(awardInfo.Key, awardInfo.Value, AwardImages, 0.3f, 0.3f);
- Description.text = flowerInfo.Name;
- }
- else if (awardInfo.Key == RechargeGift.GiftType.精灵)
- {
- SkillRoot skillRoot = (SkillRoot) SetupRechargeGiftAwardUI(awardInfo.Key, awardInfo.Value, AwardImages, 0.4f, 0.4f);
- Description.text = skillRoot.Name;
- }
- else if (awardInfo.Key == RechargeGift.GiftType.服装)
- {
- CloseItem closeItem = (CloseItem) SetupRechargeGiftAwardUI(awardInfo.Key, awardInfo.Value, AwardImages, 1f, 1f);
- Description.text = closeItem.Name;
- }
- else if (awardInfo.Key == RechargeGift.GiftType.礼包)
- {
- SkillRoot skillRoot = (SkillRoot) SetupRechargeGiftAwardUI(awardInfo.Key, awardInfo.Value, AwardImages, 0.4f, 0.4f);
- Description.text = skillRoot.Name;
- }
- else if (awardInfo.Key == RechargeGift.GiftType.开垦土地)
- {
- SetupRechargeGiftAwardUI(awardInfo.Key, awardInfo.Value, AwardImages, 0.4f, 0.4f);
- Description.text = AmountTextPrefix + awardInfo.Value;
- }
- else
- {
- throw new Exception();
- }
- }
- private static object SetupRechargeGiftAwardUI(RechargeGift.GiftType type, int id, List<Image> images, float deltaSizeX, float deltaSizeY)
- {
- if (type == RechargeGift.GiftType.花朵)
- {
- for (int i = 1; i < images.Count; i++)
- {
- images[i].transform.localPosition = new Vector3();
- }
- }
- else
- {
- for (int i = 0; i < images.Count - 1; i++)
- {
- images[i].transform.localPosition = new Vector3();
- }
- }
- for (int i = 0; i < images.Count; i++)
- {
- images[i].transform.SetActive(false);
- }
- if (type == RechargeGift.GiftType.金币)
- {
- images[0].transform.SetActive(true);
- images[0].sprite = ResourceManager.LoadSprite(ResourceLabel.Gold, Folder.Atlas);
- images[0].SetNativeSize();
- images[0].rectTransform.Resize(true, deltaSizeX, deltaSizeY);
- return null;
- }
- else if (type == RechargeGift.GiftType.钻石)
- {
- images[0].transform.SetActive(true);
- images[0].sprite = ResourceManager.LoadSprite(ResourceLabel.Diamond, Folder.Atlas);
- images[0].SetNativeSize();
- images[0].rectTransform.Resize(true, deltaSizeX, deltaSizeY);
- return null;
- }
- else if (type == RechargeGift.GiftType.花朵)
- {
- FlowerInfo flowerInfo = GardenManager.FlowerInfoDictionary[id];
- images[0].transform.SetActive(true);
- images[0].sprite = flowerInfo.Icon;
- images[0].SetNativeSize();
- images[0].rectTransform.Resize(true, deltaSizeX, deltaSizeY);
- return flowerInfo;
- }
- else if (type == RechargeGift.GiftType.精灵)
- {
- string abilityFullId = SkillRoot.AbilityFullIDPrefix + id;
- SkillRoot skillRoot = Manager.SkillDictionary[abilityFullId];
- images[0].transform.SetActive(true);
- images[0].sprite = skillRoot.Icon;
- images[0].SetNativeSize();
- images[0].rectTransform.Resize(true, deltaSizeX, deltaSizeY);
- return skillRoot;
- }
- else if (type == RechargeGift.GiftType.服装)
- {
- images[0].SetNativeSize();
- images[1].SetNativeSize();
- images[2].SetNativeSize();
- CloseItem closeItem = PlayerManager.CloseItemDictionary[id];
- float newSize = closeItem.PixelSize / closeItem.Sprites[0].rect.width * deltaSizeX;
- closeItem.SetupUI(newSize, new Vector2(), images[0], images[1], images[2]);
- return closeItem;
- }
- else if (type == RechargeGift.GiftType.礼包)
- {
- string packFullId = SkillRoot.PackFullIDPrefix + id;
- SkillRoot skillRoot = Manager.SkillDictionary[packFullId];
- images[0].transform.SetActive(true);
- images[0].sprite = skillRoot.Icon;
- images[0].SetNativeSize();
- images[0].rectTransform.Resize(true, deltaSizeX, deltaSizeY);
- return skillRoot;
- }
- else if (type == RechargeGift.GiftType.开垦土地)
- {
- images[0].transform.SetActive(true);
- images[0].sprite = ResourceManager.LoadSprite(ResourceLabel.Slot, Folder.Scene);
- images[0].SetNativeSize();
- images[0].rectTransform.Resize(true, deltaSizeX, deltaSizeY);
- return null;
- }
- else
- {
- throw new Exception();
- }
- }
- }
|