ThanksGift.cs 8.6 KB

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