ThanksGift.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using UnityEngine;
  6. public class Gift
  7. {
  8. private enum GiftType
  9. {
  10. 金币 = 0,
  11. 钻石 = 1,
  12. 礼包 = 2,
  13. 花朵 = 3,
  14. 服装 = 4,
  15. 开垦土地 = 5,
  16. 精灵 = 6,
  17. }
  18. private static Dictionary<GiftType, string> TypeLabelDictionary = new Dictionary<GiftType, string>
  19. {
  20. {GiftType.金币,"g"},
  21. {GiftType.钻石,"d"},
  22. {GiftType.礼包,"p"},
  23. {GiftType.花朵,"f"},
  24. {GiftType.服装,"c"},
  25. {GiftType.开垦土地,"s"},
  26. {GiftType.精灵,"a"},
  27. };
  28. #region Config
  29. private static int UnvalidValue = 0;
  30. private static string UnvalidValues = "0";
  31. private static string StartTimeLabel = "starttime";
  32. private static string EndTimeLabel = "endtime";
  33. private List<List<int>> Values = new List<List<int>>();
  34. private List<GiftType> Types = new List<GiftType>();
  35. private DateTime StartDate;
  36. private DateTime EndDate;
  37. #endregion
  38. public Gift(JsonData jsonData)
  39. {
  40. //Debug.Log(jsonData.ToJson());
  41. int value = (int) jsonData[TypeLabelDictionary[GiftType.金币]];
  42. if (value != UnvalidValue)
  43. {
  44. Types.Add(GiftType.金币);
  45. Values.Add(new List<int> {value});
  46. }
  47. value = (int)jsonData[TypeLabelDictionary[GiftType.钻石]];
  48. if (value != UnvalidValue)
  49. {
  50. Types.Add(GiftType.钻石);
  51. Values.Add(new List<int> { value });
  52. }
  53. string values = (string)jsonData[TypeLabelDictionary[GiftType.礼包]];
  54. if (values != UnvalidValues)
  55. {
  56. Types.Add(GiftType.礼包);
  57. Values.Add(Auxiliary.StringToInts(' ', values, new List<int>()));
  58. }
  59. values = (string)jsonData[TypeLabelDictionary[GiftType.花朵]];
  60. if (values != UnvalidValues)
  61. {
  62. Types.Add(GiftType.花朵);
  63. Values.Add(Auxiliary.StringToInts(' ', values, new List<int>()));
  64. }
  65. values = (string)jsonData[TypeLabelDictionary[GiftType.服装]];
  66. if (values != UnvalidValues)
  67. {
  68. Types.Add(GiftType.服装);
  69. Values.Add(Auxiliary.StringToInts(' ', values, new List<int>()));
  70. }
  71. value = (int)jsonData[TypeLabelDictionary[GiftType.开垦土地]];
  72. if (value != UnvalidValue)
  73. {
  74. Types.Add(GiftType.开垦土地);
  75. Values.Add(new List<int> { value });
  76. }
  77. values = (string)jsonData[TypeLabelDictionary[GiftType.精灵]];
  78. if (values != UnvalidValues)
  79. {
  80. Types.Add(GiftType.精灵);
  81. Values.Add(Auxiliary.StringToInts(' ', values, new List<int>()));
  82. }
  83. StartDate = DateTime.Parse(jsonData[StartTimeLabel].ToString());
  84. EndDate = DateTime.Parse(jsonData[EndTimeLabel].ToString());
  85. //for (int i = 0; i < Types.Count; i++)
  86. //{
  87. // Debug.LogWarning(Types[i]);
  88. // for (int j = 0; j < Values[i].Count; j++)
  89. // {
  90. // Debug.Log(Values[i][j]);
  91. // }
  92. //}
  93. //Debug.LogWarning(StartDate);
  94. //Debug.LogWarning(EndDate);
  95. }
  96. public void GetAllGift()
  97. {
  98. if (HttpManager.Time > EndDate)
  99. {
  100. //Debug.Log("has't start");
  101. return;
  102. }
  103. if (HttpManager.Time < StartDate)
  104. {
  105. //Debug.Log("over");
  106. return;
  107. }
  108. for (int i = 0; i < Types.Count; i++)
  109. {
  110. for (int j = 0; j < Values[i].Count; j++)
  111. {
  112. GetGift(Types[i], Values[i][j]);
  113. }
  114. }
  115. }
  116. private float CloseExchangeRatio = 0.25f;
  117. private float AbilityExchangeRatio = 0.25f;
  118. private void GetGift(GiftType type, int value)
  119. {
  120. if (type == GiftType.开垦土地)
  121. {
  122. for (int i = 0; i < value; i++)
  123. {
  124. GardenManager.UnlockSlot();
  125. int extraSlot = ConfigManager.GetPlayerInt(PlayerConfigLabel.ExtraSlot);
  126. ConfigManager.SavePlayerInt(PlayerConfigLabel.ExtraSlot, extraSlot + 1);
  127. //Debug.LogWarning($"{type}");
  128. }
  129. }
  130. else if (type == GiftType.服装)
  131. {
  132. CloseItem closeItem = PlayerManager.CloseItemDic[value];
  133. if (closeItem.Possess)
  134. {
  135. /*ExchangeInfo info = */closeItem.GetExchangeValue(CloseExchangeRatio, StaticsManager.ConsumeModule.Gift);
  136. //Debug.LogWarning($"{type} {info.Current} {info.Value}");
  137. }
  138. else
  139. {
  140. closeItem.OnBuySucceed();
  141. //Debug.LogWarning($"{type} {value}");
  142. }
  143. }
  144. else if (type == GiftType.礼包)
  145. {
  146. string packID = SkillConfigLabel.GetFullID(SkillType.Pack, value);
  147. (Manager.SkillDic[packID] as Pack).OnBuySucceed(false);
  148. //Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
  149. }
  150. else if (type == GiftType.精灵)
  151. {
  152. string abilityID = SkillConfigLabel.GetFullID(SkillType.Ability, value);
  153. Ability ability = Manager.SkillDic[abilityID] as Ability;
  154. if (ability.ItemStatus == SkillStatus.Lock)
  155. {
  156. /*ExchangeInfo info = */ability.GetUnlockAheadExchangeValue(AbilityExchangeRatio, StaticsManager.ConsumeModule.Gift);
  157. //Debug.LogWarning($"{type} {info.Current} {info.Value}");
  158. }
  159. else if (ability.ItemStatus == SkillStatus.UnLock)
  160. {
  161. ability.UnlockSucceed();
  162. //Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
  163. }
  164. else if (ability.ItemStatus == SkillStatus.Upgrade)
  165. {
  166. ability.UpgradeSucceed();
  167. //Debug.LogWarning($"{type} {SkillConfigLabel.Pack + value}");
  168. }
  169. }
  170. else if (type == GiftType.花朵)
  171. {
  172. FlowerInfo flowerInfo = GardenManager.FlowerInfoDic[value];
  173. flowerInfo.Add();
  174. //if (!flowerInfo.Unlock)
  175. //{
  176. // flowerInfo.Unlock = true;
  177. // //Debug.LogWarning($"{type} {flowerInfo.Name}");
  178. //}
  179. //else
  180. //{
  181. // flowerInfo.Amount++;
  182. // //Debug.LogWarning($"{type} {flowerInfo.Name}");
  183. //}
  184. }
  185. else if (type == GiftType.金币)
  186. {
  187. Manager.AddCoin(value, StaticsManager.ItemID.获得金币, StaticsManager.ConsumeModule.Gift);
  188. //Debug.LogWarning($"{type} {value}");
  189. }
  190. else if (type == GiftType.钻石)
  191. {
  192. Manager.AddDiamond(value, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.Gift);
  193. //Debug.LogWarning($"{type} {value}");
  194. }
  195. }
  196. }
  197. public class ThanksGift
  198. {
  199. #region Config
  200. private static bool Inited;
  201. private static Dictionary<int, Gift> GiftDictionary = new Dictionary<int, Gift>();
  202. #endregion
  203. public static void Init(JsonData jsonData)
  204. {
  205. //Debug.LogWarning("Inited");
  206. Inited = true;
  207. for (int i = 0; i < jsonData.Count; i++)
  208. {
  209. int packID = (int) jsonData[i]["packid"];
  210. Gift gift = new Gift(jsonData[i]);
  211. GiftDictionary.Add(packID, gift);
  212. }
  213. }
  214. public static void GetAllGift(int packID)
  215. {
  216. if (!Inited)
  217. {
  218. HttpManager.GetThanksGiftInfo
  219. (
  220. jData =>
  221. {
  222. Init(jData);
  223. getGift(packID);
  224. },
  225. () => Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)
  226. )
  227. );
  228. }
  229. else
  230. {
  231. getGift(packID);
  232. }
  233. }
  234. private static void getGift(int packID)
  235. {
  236. if (GiftDictionary.ContainsKey(packID))
  237. {
  238. //Debug.Log("have gift");
  239. Gift gift = GiftDictionary[packID];
  240. gift.GetAllGift();
  241. }
  242. else
  243. {
  244. //Debug.Log("no gift");
  245. }
  246. }
  247. }