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 TypeDictionary = new Dictionary { {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; } /// true-全部领完 public abstract bool GetOneGiftAward(out KV giftAwardInfo); protected void GetGiftAward(GiftType type, int value, out KV 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 giftAwardInfo) { GetGiftAward(GiftType, Value, out giftAwardInfo); return true; } } public class MultipleGiftAward : GiftAward { protected List Values; public MultipleGiftAward(GiftType giftType, List values) : base(giftType) { Values = values; //Debug.Log($"{giftType} {Auxiliary.IntsToString(Values)}"); } public override bool GetOneGiftAward(out KV 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 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 GiftAwards = new List(); #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 ParseAllGiftAward(GiftType giftType, string valueStr) { List giftAwards = new List(); 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 values = Auxiliary.StringToInts(' ', valueStr, new List()); 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 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 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 GiftDictionary = new Dictionary(); 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 AwardImages = new List(); #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(CanvasLabel.AB_Title); Description = ResourceManager.Get(CanvasLabel.AB_Description); ConfirmButtonText = ResourceManager.Get(CanvasLabel.AB_ConfirmText); AwardImage0 = ResourceManager.Get(CanvasLabel.AB_AwardImage0); AwardImage1 = ResourceManager.Get(CanvasLabel.AB_AwardImage1); AwardImage2 = ResourceManager.Get(CanvasLabel.AB_AwardImage2); FlowerAwardImage = ResourceManager.Get(CanvasLabel.AB_FlowerAwardImage); ConfirmButton = ResourceManager.Get