RechargeGiftManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using UnityEngine;
  6. using Random = UnityEngine.Random;
  7. public class RechargeGiftManager
  8. {
  9. public static string PackID = "packid";
  10. public static string StartTime = "starttime";
  11. public static string EndTime = "endtime";
  12. public static string UnvalidValueStr = "0";
  13. public static Dictionary<RechargeGift.GiftType, string> TypeDictionary = new Dictionary<RechargeGift.GiftType, string>
  14. {
  15. {RechargeGift.GiftType.金币,"g"},
  16. {RechargeGift.GiftType.钻石,"d"},
  17. {RechargeGift.GiftType.礼包,"p"},
  18. {RechargeGift.GiftType.花朵,"f"},
  19. {RechargeGift.GiftType.服装,"c"},
  20. {RechargeGift.GiftType.开垦土地,"s"},
  21. {RechargeGift.GiftType.精灵,"a"},
  22. };
  23. }
  24. public class RechargeGift
  25. {
  26. public enum GiftType
  27. {
  28. 金币 = 0,
  29. 钻石 = 1,
  30. 礼包 = 2,
  31. 花朵 = 3,
  32. 服装 = 4,
  33. 开垦土地 = 5,
  34. 精灵 = 6,
  35. }
  36. public abstract class GiftAward
  37. {
  38. protected float CloseExchangeRatio = 0.25f;
  39. protected float AbilityExchangeRatio = 0.25f;
  40. protected GiftType GiftType;
  41. public GiftAward(GiftType giftType)
  42. {
  43. GiftType = giftType;
  44. }
  45. /// <returns>true-全部领完</returns>
  46. public abstract bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo);
  47. protected void GetGiftAward(GiftType type, int value)
  48. {
  49. if (type == GiftType.开垦土地)
  50. {
  51. for (int i = 0; i < value; i++)
  52. {
  53. GardenManager.UnlockSlot();
  54. int extraSlot = ConfigManager.GetIntFormConfig(PlayerConfigLabel.ExtraSlot);
  55. ConfigManager.SaveIntToConfig(PlayerConfigLabel.ExtraSlot, extraSlot + 1);
  56. Debug.LogWarning($"{type}");
  57. }
  58. }
  59. else if (type == GiftType.服装)
  60. {
  61. CloseItem closeItem = PlayerManager.CloseItemDictionary[value];
  62. if (closeItem.IsBought)
  63. {
  64. ExchangeInfo info =
  65. closeItem.GetExchangeValue(CloseExchangeRatio, StaticsManager.ConsumeModule.Gift);
  66. Debug.LogWarning($"{type} {info.Current} {info.Value}");
  67. }
  68. else
  69. {
  70. closeItem.OnBuySucceed();
  71. Debug.LogWarning($"{type} {value}");
  72. }
  73. }
  74. else if (type == GiftType.礼包)
  75. {
  76. string packID = SkillConfigLabel.GetFullID(SkillType.Pack, value);
  77. (Manager.SkillDictionary[packID] as Pack).OnBuySucceed(false);
  78. Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
  79. }
  80. else if (type == GiftType.精灵)
  81. {
  82. string abilityID = SkillConfigLabel.GetFullID(SkillType.Ability, value);
  83. Ability ability = Manager.SkillDictionary[abilityID] as Ability;
  84. if (ability.ItemStatus == SkillStatus.Lock)
  85. {
  86. ExchangeInfo info =
  87. ability.GetUnlockAheadExchangeValue(AbilityExchangeRatio, StaticsManager.ConsumeModule.Gift);
  88. Debug.LogWarning($"{type} {info.Current} {info.Value}");
  89. }
  90. else if (ability.ItemStatus == SkillStatus.UnLock)
  91. {
  92. ability.UnlockSucceed();
  93. Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
  94. }
  95. else if (ability.ItemStatus == SkillStatus.Upgrade)
  96. {
  97. ability.UpgradeSucceed();
  98. Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
  99. }
  100. }
  101. else if (type == GiftType.花朵)
  102. {
  103. FlowerInfo flowerInfo = GardenManager.FlowerInfoDictionary[value];
  104. flowerInfo.Add();
  105. Debug.LogWarning($"{type} {flowerInfo.Name}");
  106. }
  107. else if (type == GiftType.金币)
  108. {
  109. Manager.AddCoin(value, StaticsManager.ItemID.获得金币, StaticsManager.ConsumeModule.Gift);
  110. Debug.LogWarning($"{type} {value}");
  111. }
  112. else if (type == GiftType.钻石)
  113. {
  114. Manager.AddDiamond(value, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.Gift);
  115. Debug.LogWarning($"{type} {value}");
  116. }
  117. }
  118. }
  119. public class SingleGiftAward : GiftAward
  120. {
  121. protected int Value;
  122. public SingleGiftAward(GiftType giftType, int value) : base(giftType)
  123. {
  124. Value = value;
  125. Debug.Log($"{giftType} {value}");
  126. }
  127. public override bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
  128. {
  129. GetGiftAward(GiftType, Value);
  130. giftAwardInfo.Key = GiftType;
  131. giftAwardInfo.Value = Value;
  132. return true;
  133. }
  134. }
  135. public class MultipleGiftAward : GiftAward
  136. {
  137. protected List<int> Values;
  138. public MultipleGiftAward(GiftType giftType, List<int> values) : base(giftType)
  139. {
  140. Values = values;
  141. Debug.Log($"{giftType} {Auxiliary.IntsToString(Values)}");
  142. }
  143. public override bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
  144. {
  145. GetGiftAward(GiftType, Values[0]);
  146. giftAwardInfo.Key = GiftType;
  147. giftAwardInfo.Value = Values[0];
  148. Values.RemoveAt(0);
  149. if (Values.Count > 0)
  150. {
  151. return false;
  152. }
  153. else
  154. {
  155. return true;
  156. }
  157. }
  158. }
  159. //随机获取区间内的一个
  160. public class RandomGiftAward : GiftAward
  161. {
  162. protected int StartIndex;
  163. protected int EndIndex;
  164. public RandomGiftAward(GiftType giftType, int startIndex, int endIndex) : base(giftType)
  165. {
  166. StartIndex = startIndex;
  167. EndIndex = endIndex;
  168. Debug.Log($"{giftType} {StartIndex} {EndIndex}");
  169. }
  170. public override bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
  171. {
  172. int value = Random.Range(StartIndex, EndIndex + 1);
  173. GetGiftAward(GiftType, value);
  174. giftAwardInfo.Key = GiftType;
  175. giftAwardInfo.Value = value;
  176. return true;
  177. }
  178. }
  179. #region Config
  180. private DateTime StartDate;
  181. private DateTime EndDate;
  182. private List<GiftAward> GiftAwards = new List<GiftAward>();
  183. #endregion
  184. public RechargeGift(JsonData jsonData)
  185. {
  186. //Debug.Log(jsonData.ToJson());
  187. string label = RechargeGiftManager.TypeDictionary[GiftType.金币];
  188. string valueStr = jsonData[label].ToString();
  189. if (valueStr != RechargeGiftManager.UnvalidValueStr)
  190. {
  191. GiftAwards.AddRange(ParseAllGiftAward(GiftType.金币, valueStr));
  192. }
  193. label = RechargeGiftManager.TypeDictionary[GiftType.钻石];
  194. valueStr = jsonData[label].ToString();
  195. if (valueStr != RechargeGiftManager.UnvalidValueStr)
  196. {
  197. GiftAwards.AddRange(ParseAllGiftAward(GiftType.钻石, valueStr));
  198. }
  199. label = RechargeGiftManager.TypeDictionary[GiftType.礼包];
  200. valueStr = jsonData[label].ToString();
  201. if (valueStr != RechargeGiftManager.UnvalidValueStr)
  202. {
  203. GiftAwards.AddRange(ParseAllGiftAward(GiftType.礼包, valueStr));
  204. }
  205. label = RechargeGiftManager.TypeDictionary[GiftType.花朵];
  206. valueStr = jsonData[label].ToString();
  207. if (valueStr != RechargeGiftManager.UnvalidValueStr)
  208. {
  209. GiftAwards.AddRange(ParseAllGiftAward(GiftType.花朵, valueStr));
  210. }
  211. label = RechargeGiftManager.TypeDictionary[GiftType.服装];
  212. valueStr = jsonData[label].ToString();
  213. if (valueStr != RechargeGiftManager.UnvalidValueStr)
  214. {
  215. GiftAwards.AddRange(ParseAllGiftAward(GiftType.服装, valueStr));
  216. }
  217. label = RechargeGiftManager.TypeDictionary[GiftType.开垦土地];
  218. valueStr = jsonData[label].ToString();
  219. if (valueStr != RechargeGiftManager.UnvalidValueStr)
  220. {
  221. GiftAwards.AddRange(ParseAllGiftAward(GiftType.开垦土地, valueStr));
  222. }
  223. label = RechargeGiftManager.TypeDictionary[GiftType.精灵];
  224. valueStr = jsonData[label].ToString();
  225. if (valueStr != RechargeGiftManager.UnvalidValueStr)
  226. {
  227. GiftAwards.AddRange(ParseAllGiftAward(GiftType.精灵, valueStr));
  228. }
  229. StartDate = DateTime.Parse(jsonData[RechargeGiftManager.StartTime].ToString());
  230. EndDate = DateTime.Parse(jsonData[RechargeGiftManager.EndTime].ToString());
  231. //Debug.LogWarning(StartDate);
  232. //Debug.LogWarning(EndDate);
  233. }
  234. private List<GiftAward> ParseAllGiftAward(GiftType giftType, string valueStr)
  235. {
  236. List<GiftAward> giftAwards = new List<GiftAward>();
  237. string[] values = valueStr.Split('|');
  238. for (int i = 0; i < values.Length; i++)
  239. {
  240. giftAwards.Add(ParseGiftAward(giftType, values[i]));
  241. }
  242. return giftAwards;
  243. }
  244. private GiftAward ParseGiftAward(GiftType giftType, string valueStr)
  245. {
  246. if (valueStr.Contains(" "))
  247. {
  248. List<int> values = Auxiliary.StringToInts(' ', valueStr, new List<int>());
  249. return new MultipleGiftAward(giftType, values);
  250. }
  251. else if (valueStr.Contains(","))
  252. {
  253. valueStr = valueStr.TrimStart('[');
  254. valueStr = valueStr.TrimEnd(']');
  255. string[] valueStrs = valueStr.Split(',');
  256. int startIndex = int.Parse(valueStrs[0]);
  257. int endIndex = int.Parse(valueStrs[1]);
  258. return new RandomGiftAward(giftType, startIndex, endIndex);
  259. }
  260. else if (valueStr.Contains("-"))
  261. {
  262. string[] valueStrs = valueStr.Split('-');
  263. int startIndex = int.Parse(valueStrs[0]);
  264. int endIndex = int.Parse(valueStrs[1]);
  265. List<int> values = Auxiliary.StartEndIndexToInts(startIndex, endIndex);
  266. return new MultipleGiftAward(giftType, values);
  267. }
  268. else
  269. {
  270. int value = int.Parse(valueStr);
  271. return new SingleGiftAward(giftType, value);
  272. }
  273. }
  274. public bool IsGiftAvailable()
  275. {
  276. if (HttpManager.CurrentDateTime > EndDate)
  277. {
  278. //Debug.Log("has't start");
  279. return false;
  280. }
  281. if (HttpManager.CurrentDateTime < StartDate)
  282. {
  283. //Debug.Log("over");
  284. return false;
  285. }
  286. if (GiftAwards.Count == 0)
  287. {
  288. return false;
  289. }
  290. return true;
  291. }
  292. public bool GetOneGiftAward(out KV<GiftType, int> giftAwardInfo)
  293. {
  294. if (GiftAwards[0].GetOneGiftAward(out giftAwardInfo))
  295. {
  296. GiftAwards.RemoveAt(0);
  297. }
  298. if (GiftAwards.Count > 0)
  299. {
  300. return false;
  301. }
  302. else
  303. {
  304. return true;
  305. }
  306. }
  307. }
  308. public class ThanksGift
  309. {
  310. #region Config
  311. private static bool Inited;
  312. private static Dictionary<int, RechargeGift> GiftDictionary = new Dictionary<int, RechargeGift>();
  313. #endregion
  314. public static void Init(JsonData jsonData)
  315. {
  316. //Debug.LogWarning("Inited");
  317. Inited = true;
  318. for (int i = 0; i < jsonData.Count; i++)
  319. {
  320. int packID = (int) jsonData[i][RechargeGiftManager.PackID];
  321. RechargeGift rechargeGift = new RechargeGift(jsonData[i]);
  322. GiftDictionary.Add(packID, rechargeGift);
  323. }
  324. }
  325. public static void GetAllGift(int packID)
  326. {
  327. if (!Inited)
  328. {
  329. HttpManager.GetThanksGiftInfo
  330. (
  331. jData =>
  332. {
  333. Init(jData);
  334. getGift(packID);
  335. },
  336. () => Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)
  337. )
  338. );
  339. }
  340. else
  341. {
  342. getGift(packID);
  343. }
  344. }
  345. private static void getGift(int packID)
  346. {
  347. if (GiftDictionary.ContainsKey(packID))
  348. {
  349. //RechargeGift rechargeGift = GiftDictionary[packID];
  350. //rechargeGift.GetOneGiftAward();
  351. }
  352. }
  353. }