123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- using UnityEngine;
- using Random = UnityEngine.Random;
- public class RechargeGiftManager
- {
- 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)
- {
- if (type == GiftType.开垦土地)
- {
- for (int i = 0; i < value; i++)
- {
- GardenManager.UnlockSlot();
- int extraSlot = ConfigManager.GetIntFormConfig(PlayerConfigLabel.ExtraSlot);
- ConfigManager.SaveIntToConfig(PlayerConfigLabel.ExtraSlot, extraSlot + 1);
- Debug.LogWarning($"{type}");
- }
- }
- else if (type == GiftType.服装)
- {
- CloseItem closeItem = PlayerManager.CloseItemDictionary[value];
- if (closeItem.IsBought)
- {
- ExchangeInfo info =
- closeItem.GetExchangeValue(CloseExchangeRatio, StaticsManager.ConsumeModule.Gift);
- Debug.LogWarning($"{type} {info.Current} {info.Value}");
- }
- else
- {
- closeItem.OnBuySucceed();
- Debug.LogWarning($"{type} {value}");
- }
- }
- else if (type == GiftType.礼包)
- {
- string packID = SkillConfigLabel.GetFullID(SkillType.Pack, value);
- (Manager.SkillDictionary[packID] as Pack).OnBuySucceed(false);
- Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
- }
- else if (type == GiftType.精灵)
- {
- string abilityID = SkillConfigLabel.GetFullID(SkillType.Ability, value);
- Ability ability = Manager.SkillDictionary[abilityID] as Ability;
- if (ability.ItemStatus == SkillStatus.Lock)
- {
- ExchangeInfo info =
- ability.GetUnlockAheadExchangeValue(AbilityExchangeRatio, StaticsManager.ConsumeModule.Gift);
- Debug.LogWarning($"{type} {info.Current} {info.Value}");
- }
- else if (ability.ItemStatus == SkillStatus.UnLock)
- {
- ability.UnlockSucceed();
- Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
- }
- else if (ability.ItemStatus == SkillStatus.Upgrade)
- {
- ability.UpgradeSucceed();
- Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
- }
- }
- else if (type == GiftType.花朵)
- {
- FlowerInfo flowerInfo = GardenManager.FlowerInfoDictionary[value];
- flowerInfo.Add();
- Debug.LogWarning($"{type} {flowerInfo.Name}");
- }
- else if (type == GiftType.金币)
- {
- Manager.AddCoin(value, StaticsManager.ItemID.获得金币, StaticsManager.ConsumeModule.Gift);
- Debug.LogWarning($"{type} {value}");
- }
- else if (type == GiftType.钻石)
- {
- Manager.AddDiamond(value, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.Gift);
- Debug.LogWarning($"{type} {value}");
- }
- }
- }
- 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);
- giftAwardInfo.Key = GiftType;
- giftAwardInfo.Value = Value;
- 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]);
- giftAwardInfo.Key = GiftType;
- giftAwardInfo.Value = Values[0];
- 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);
- giftAwardInfo.Key = GiftType;
- giftAwardInfo.Value = value;
- return true;
- }
- }
- #region Config
- private DateTime StartDate;
- private DateTime EndDate;
- private List<GiftAward> GiftAwards = new List<GiftAward>();
- #endregion
- public RechargeGift(JsonData jsonData)
- {
- //Debug.Log(jsonData.ToJson());
- string label = RechargeGiftManager.TypeDictionary[GiftType.金币];
- string valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftManager.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.金币, valueStr));
- }
- label = RechargeGiftManager.TypeDictionary[GiftType.钻石];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftManager.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.钻石, valueStr));
- }
- label = RechargeGiftManager.TypeDictionary[GiftType.礼包];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftManager.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.礼包, valueStr));
- }
- label = RechargeGiftManager.TypeDictionary[GiftType.花朵];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftManager.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.花朵, valueStr));
- }
- label = RechargeGiftManager.TypeDictionary[GiftType.服装];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftManager.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.服装, valueStr));
- }
- label = RechargeGiftManager.TypeDictionary[GiftType.开垦土地];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftManager.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.开垦土地, valueStr));
- }
- label = RechargeGiftManager.TypeDictionary[GiftType.精灵];
- valueStr = jsonData[label].ToString();
- if (valueStr != RechargeGiftManager.UnvalidValueStr)
- {
- GiftAwards.AddRange(ParseAllGiftAward(GiftType.精灵, valueStr));
- }
- StartDate = DateTime.Parse(jsonData[RechargeGiftManager.StartTime].ToString());
- EndDate = DateTime.Parse(jsonData[RechargeGiftManager.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 IsGiftAvailable()
- {
- 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 ThanksGift
- {
- #region Config
- private static bool Inited;
- private static Dictionary<int, RechargeGift> GiftDictionary = new Dictionary<int, RechargeGift>();
- #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][RechargeGiftManager.PackID];
- RechargeGift rechargeGift = new RechargeGift(jsonData[i]);
- GiftDictionary.Add(packID, rechargeGift);
- }
- }
- public static void GetAllGift(int packID)
- {
- if (!Inited)
- {
- HttpManager.GetThanksGiftInfo
- (
- jData =>
- {
- Init(jData);
- getGift(packID);
- },
- () => Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)
- )
- );
- }
- else
- {
- getGift(packID);
- }
- }
- private static void getGift(int packID)
- {
- if (GiftDictionary.ContainsKey(packID))
- {
- //RechargeGift rechargeGift = GiftDictionary[packID];
- //rechargeGift.GetOneGiftAward();
- }
- }
- }
|