ChestMge.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml;
  8. using Sfs2X.Entities.Data;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using Random = UnityEngine.Random;
  12. public class ChestData
  13. {
  14. #region Config
  15. public int Value;
  16. public int Round;
  17. public int RemainValue;
  18. public int RemainRound;
  19. public int DatabaseRoomID;
  20. public long ID;
  21. public long Owner;
  22. public DateTime ActivatedTime;
  23. public ChestType ChestType;
  24. public Vector3 Position;
  25. public static int SystemRoomDatabaseID = 1;
  26. private static bool Initialized;
  27. //private static float XMin;
  28. //private static float XMax;
  29. //private static float YMin;
  30. //private static float YMax;
  31. private static Vector3 DefaultPosition;
  32. private static Vector3 ChestDepthOffset = new Vector3(0, 0, 0.98f);
  33. #endregion
  34. public ChestData(ISFSObject arg)
  35. {
  36. if (!Initialized)
  37. {
  38. Initialize();
  39. }
  40. ID = arg.GetLong(gd_chest.ID);
  41. Value = arg.GetInt(gd_chest.Value);
  42. Round = arg.GetInt(gd_chest.Round);
  43. RemainValue = arg.GetInt(gd_chest.RemainValue);
  44. RemainRound = arg.GetInt(gd_chest.RemainRound);
  45. DatabaseRoomID = arg.GetInt(gd_chest.DatabaseRoomID);
  46. Owner = arg.GetLong(gd_chest.Owner);
  47. ActivatedTime = DateUtil.GetTimeFromSecends(arg.GetLong(gd_chest.ActivatedTime).ToString());
  48. ChestType = (ChestType) arg.GetInt(gd_chest.Type);
  49. Vector3 defaultPosition = DefaultPosition + ChestDepthOffset;
  50. if (arg.ContainsKey(gd_chest.Position))
  51. {
  52. string vectorStr = arg.GetUtfString(gd_chest.Position);
  53. if (vectorStr == "null")
  54. {
  55. Position = defaultPosition;
  56. }
  57. else
  58. {
  59. Position = arg.GetUtfString(gd_chest.Position).StringToVector() + ChestDepthOffset;
  60. }
  61. }
  62. else
  63. {
  64. Position = defaultPosition;
  65. }
  66. }
  67. private static void Initialize()
  68. {
  69. Initialized = true;
  70. DefaultPosition = ManaReso.Get("PlazaRoomChestPos").position;
  71. //XMin = ManaReso.Get("ChestLowerLeft").position.x;
  72. //YMin = ManaReso.Get("ChestLowerLeft").position.y;
  73. //XMax = ManaReso.Get("ChestUpperRight").position.x;
  74. //YMax = ManaReso.Get("ChestUpperRight").position.y;
  75. }
  76. }
  77. public class ChestOperateData
  78. {
  79. public long ChestID;
  80. public bool Received;
  81. public int DatabaseRoomID;
  82. public int? RemainGuessAmt;
  83. public DateTime? LastActivatedTime; //只有系统宝箱才用
  84. public ChestOperateData(string str)
  85. {
  86. string[] strings = str.Split('|');
  87. ChestID = long.Parse(strings[0]);
  88. DatabaseRoomID = int.Parse(strings[1]);
  89. Received = int.Parse(strings[2]).ToBool();
  90. if (strings.Length > 3)
  91. {
  92. RemainGuessAmt = int.Parse(strings[3]);
  93. }
  94. if (strings.Length > 4)
  95. {
  96. LastActivatedTime = DateTime.Parse(strings[4]);
  97. }
  98. }
  99. public ChestOperateData(bool received, long chestID, int databaseRoomID)
  100. {
  101. Received = received;
  102. ChestID = chestID;
  103. DatabaseRoomID = databaseRoomID;
  104. }
  105. public override string ToString()
  106. {
  107. StringBuilder stringBuilder = new StringBuilder();
  108. stringBuilder.Append(ChestID);
  109. stringBuilder.Append("|" + DatabaseRoomID);
  110. stringBuilder.Append("|" + Received.ToInt());
  111. if (RemainGuessAmt != null)
  112. {
  113. stringBuilder.Append("|" + RemainGuessAmt);
  114. }
  115. if (LastActivatedTime != null)
  116. {
  117. stringBuilder.Append("|" + LastActivatedTime);
  118. }
  119. return stringBuilder.ToString();
  120. }
  121. }
  122. public class ChestMge : Regist
  123. {
  124. #region Config
  125. public static int PlayerMaxGuessAmt = 3;
  126. private static Text Desc;
  127. private static Text BtnLab;
  128. private static Text SliderValueLab;
  129. private static Slider Slider;
  130. private static Button Btn;
  131. private static InputField InputField;
  132. private static string RefundNodeName = "Refund";
  133. private static string ReceivedNodeName = "Received";
  134. public static List<PlazaRoomChest> PlazaRoomChests = new List<PlazaRoomChest>();
  135. public static List<int> RefundRoomIDs = new List<int>();
  136. public static Dictionary<long, ChestOperateData> OperateDataDictionary = new Dictionary<long, ChestOperateData>();
  137. #endregion
  138. public override void RegistValueA()
  139. {
  140. XmlNodeList RefundNodes = ManaData.PlayerNode.SelectNodes(RefundNodeName);
  141. XmlNodeList ReceivedNodes = ManaData.PlayerNode.SelectNodes(ReceivedNodeName);
  142. for (int i = 0; i < RefundNodes.Count; i++)
  143. {
  144. RefundRoomIDs.Add(int.Parse(RefundNodes[i].InnerText));
  145. }
  146. for (int i = 0; i < ReceivedNodes.Count; i++)
  147. {
  148. //Debug.Log(ReceivedNodes[i].InnerText);
  149. ChestOperateData chestOperateData = new ChestOperateData(ReceivedNodes[i].InnerText);
  150. OperateDataDictionary.Add(chestOperateData.ChestID, chestOperateData);
  151. }
  152. //foreach (var VARIABLE in RefundRoomIDs)
  153. //{
  154. // Debug.LogWarning("refund " + VARIABLE);
  155. //}
  156. //foreach (var VARIABLE in ChestRoomDictionary)
  157. //{
  158. // Debug.LogWarning("received " + VARIABLE.Key);
  159. //}
  160. CheckRefund();
  161. GetChestExpireStatus();
  162. }
  163. public override void RegistReference()
  164. {
  165. Btn = ManaReso.Get<Button>("Yb_Btn");
  166. Desc = ManaReso.Get<Text>("Yb_Desc");
  167. Slider = ManaReso.Get<Slider>("Yb_Slider");
  168. BtnLab = ManaReso.Get<Text>("Yb_BtnLab");
  169. InputField = ManaReso.Get<InputField>("Yb_InputField");
  170. SliderValueLab = ManaReso.Get<Text>("Yb_SliderValueLab");
  171. }
  172. public static void OpenCreatePanel()
  173. {
  174. ManaReso.Get("Y_Chest").TweenForCG();
  175. ManaReso.SetActive("Ya_GetAward", false);
  176. ManaReso.SetActive("Y_Guess", false);
  177. ManaReso.SetActive("Yb_CreateChest", true);
  178. OnSliderValueChange(Slider.value);
  179. OnInputValueChange(InputField.text);
  180. Slider.value = 1;
  181. Slider.maxValue = SFSManager.GardenSmartFox.PlazaRoomManager.CurrentRoomData.MaxPlayer;
  182. }
  183. public static void CloseChestPanel()
  184. {
  185. ManaReso.Get("Y_Chest").TweenBacCG();
  186. }
  187. public static void OnLuckyToggle(bool enable)
  188. {
  189. if (enable)
  190. {
  191. ChestType = ChestType.Lucky;
  192. ManaLan.Add(Desc, new LanStr("UI", "Yb_LuckyDesc"));
  193. }
  194. }
  195. public static void OnColorToggle(bool enable)
  196. {
  197. if (enable)
  198. {
  199. ChestType = ChestType.GuessColor;
  200. ManaLan.Add(Desc, new LanStr("UI", "Yb_GuessColorDesc"));
  201. }
  202. }
  203. public static void OnNumberToggle(bool enable)
  204. {
  205. if (enable)
  206. {
  207. ChestType = ChestType.GuessNumber;
  208. ManaLan.Add(Desc, new LanStr("UI", "Yb_GuessNumberDesc"));
  209. }
  210. }
  211. public static void OnInputValueChange(string value)
  212. {
  213. if (string.IsNullOrEmpty(value) || int.Parse(value) < 20)
  214. {
  215. Btn.interactable = false;
  216. Btn.image.material = Lib.GrayMat;
  217. ManaLan.Add(BtnLab, new LanStr("UI", "Yb_Minimum"));
  218. return;
  219. }
  220. Cost = int.Parse(value);
  221. ManaReso.SetText("Yb_CostLab", Language.GetStr("UI", "AA_Cost") + "<(钻石)>" + Cost);
  222. if (ManaCenter.Diamond < Cost)
  223. {
  224. Btn.interactable = false;
  225. Btn.image.material = Lib.GrayMat;
  226. ManaLan.Add(BtnLab, new LanStr("Common", "ShortDiamond"));
  227. }
  228. else
  229. {
  230. Btn.interactable = true;
  231. Btn.image.material = null;
  232. ManaLan.Add(BtnLab, new LanStr("Common", "Confirm"));
  233. }
  234. }
  235. public static void OnSliderValueChange(float value)
  236. {
  237. Round = (int) value;
  238. SliderValueLab.text = Round + Language.GetStr("UI", "Yb_SliderValueLab");
  239. }
  240. private static int Cost;
  241. private static int Round;
  242. private static float ChestXMinOffset = 1;
  243. private static float ChestXMaxOffset = 2;
  244. private static ChestType ChestType = ChestType.Lucky;
  245. public static void CreateChest()
  246. {
  247. ManaLan.Add(ManaReso.Get<Text>("Y_Tip"), new LanStr("UI", "AA_SendRequest"));
  248. ManaReso.Get("Y_Tip").TweenForCG();
  249. int databaseRoomID = SFSManager.GardenSmartFox.PlazaRoomManager.CurrentRoomData.ID;
  250. long serialNumber = long.Parse(ManaServer.SerialNumber);
  251. Vector3 position = SFSManager.GardenSmartFox.PlazaRoomManager.SelfInstance.Player.transform.position + Mathf.Sign(Random.Range(-1f, 1f))*new Vector3(Random.Range(ChestXMinOffset, ChestXMaxOffset), 0, 0);
  252. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.CreateChest(Round, Cost, ChestType, databaseRoomID, serialNumber, position);
  253. ManaReso.Get<CanvasGroup>("Y_Chest").interactable = false;
  254. }
  255. public static void OnCreateChestError()
  256. {
  257. ManaLan.Add(ManaReso.Get<Text>("Y_Tip"), new LanStr("UI", "AA_CreateError"));
  258. ManaReso.Get("Y_Tip").TweenBacCG();
  259. ManaReso.Get<CanvasGroup>("Y_Chest").interactable = true;
  260. }
  261. public static void OnCreateChestSucceed()
  262. {
  263. ManaCenter.CreateChestAmt++;
  264. if (ChestType == ChestType.Lucky)
  265. {
  266. ManaCenter.CreateLuckyChestAmt++;
  267. }
  268. else if (ChestType == ChestType.GuessColor)
  269. {
  270. ManaCenter.CreateGuessColorChestAmt++;
  271. }
  272. else if (ChestType == ChestType.GuessNumber)
  273. {
  274. ManaCenter.CreateGuessNumberChestAmt++;
  275. }
  276. ManaLan.Add(ManaReso.Get<Text>("Y_Tip"), new LanStr("UI", "AA_CreateSucceed"));
  277. ManaReso.Get("Y_Tip").TweenBacCG();
  278. ManaReso.Get<CanvasGroup>("Y_Chest").interactable = true;
  279. ManaCenter.AddDiamond(-Cost, StaticsManager.ItemID.创建宝箱, StaticsManager.ConsumeModule.Charge);
  280. CloseChestPanel();
  281. }
  282. public static void OnReceiveNewChest(ChestData chestData)
  283. {
  284. if (!SFSManager.GardenSmartFox.PlazaRoomManager.JoinedPlazaRoom)
  285. {
  286. return;
  287. }
  288. ManaAudio.PlayClip(Clip.BubbleClip);
  289. InstantiateChest(chestData);
  290. if (chestData.Owner == long.Parse(ManaServer.SerialNumber))
  291. {
  292. if (SFSManager.GardenSmartFox.PlazaRoomManager.CurrentRoomData.ID != ChestData.SystemRoomDatabaseID)
  293. {
  294. RefundRoomIDs.UniqueAdd(SFSManager.GardenSmartFox.PlazaRoomManager.CurrentRoomData.ID);
  295. }
  296. }
  297. }
  298. private static void CheckRefund()
  299. {
  300. if (RefundRoomIDs.Count == 0)
  301. {
  302. return;
  303. }
  304. else
  305. {
  306. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.CheckChestRefund(RefundRoomIDs);
  307. }
  308. }
  309. private static void GetChestExpireStatus()
  310. {
  311. if (OperateDataDictionary.Keys.Count == 0)
  312. {
  313. return;
  314. }
  315. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetChestExpireStatus(OperateDataDictionary.Keys.ToList());
  316. }
  317. public static void OnReceiveChestExpireStatus(List<long> chestIDs)
  318. {
  319. foreach (var chestID in chestIDs)
  320. {
  321. //Debug.LogWarning("expire " + chestID);
  322. OperateDataDictionary.Remove(chestID);
  323. }
  324. }
  325. public static void RetrieveAllChest()
  326. {
  327. foreach (var plazaRoomChest in PlazaRoomChests)
  328. {
  329. plazaRoomChest.RetriveChest();
  330. }
  331. PlazaRoomChests = new List<PlazaRoomChest>();
  332. }
  333. private static void InstantiateChest(ChestData chestData)
  334. {
  335. if (chestData.ChestType != ChestType.System)
  336. {
  337. if (chestData.RemainRound <= 0 || chestData.RemainValue <= 0)
  338. {
  339. return;
  340. }
  341. ChestOperateData chestOperateData;
  342. OperateDataDictionary.TryGetValue(chestData.ID, out chestOperateData);
  343. if (chestOperateData == null || chestOperateData.Received == false)
  344. {
  345. PlazaRoomChest chest = ManaReso.GetPlazaRoomChest(chestData.Position);
  346. chest.Init(chestData);
  347. PlazaRoomChests.Add(chest);
  348. if (chestOperateData == null || chestOperateData.RemainGuessAmt == null || chestOperateData.RemainGuessAmt > 0)
  349. {
  350. chest.Active();
  351. chest.TurnNormalColor();
  352. }
  353. else
  354. {
  355. chest.Deactive();
  356. chest.TurnGray();
  357. }
  358. }
  359. }
  360. else
  361. {
  362. PlazaRoomChest chest = ManaReso.GetPlazaRoomChest(chestData.Position);
  363. chest.SystemChestInit(chestData);
  364. PlazaRoomChests.Add(chest);
  365. }
  366. }
  367. private static int SystemChestActivateTimespan = 600;
  368. public static void ReceiveAllChestData(List<ChestData> chestDatas)
  369. {
  370. ManaAudio.PlayClip(Clip.BubbleClip);
  371. foreach (var chestData in chestDatas)
  372. {
  373. InstantiateChest(chestData);
  374. }
  375. }
  376. public static void ActivateSystemChest(DateTime activatedTime)
  377. {
  378. if (!SFSManager.GardenSmartFox.PlazaRoomManager.JoinedPlazaRoom)
  379. {
  380. return;
  381. }
  382. if (PlazaRoomChest.SystemChest == null)
  383. {
  384. return;
  385. }
  386. if (OperateDataDictionary.Keys.Contains(PlazaRoomChest.SystemChest.ChestData.ID))
  387. {
  388. if (activatedTime.Subtract(OperateDataDictionary[PlazaRoomChest.SystemChest.ChestData.ID].LastActivatedTime.Value).TotalSeconds >= SystemChestActivateTimespan)
  389. {
  390. ReactivateSystemChest(activatedTime);
  391. }
  392. return;
  393. }
  394. PlazaRoomChest.SystemChest.Active();
  395. PlazaRoomChest.SystemChest.TurnNormalColor();
  396. PlazaRoomChest.SystemChest.ChestTimerTransform.TweenBacCG();
  397. }
  398. public static void ReactivateSystemChest(DateTime activatedTime)
  399. {
  400. if (!SFSManager.GardenSmartFox.PlazaRoomManager.JoinedPlazaRoom)
  401. {
  402. return;
  403. }
  404. if (PlazaRoomChest.SystemChest == null)
  405. {
  406. return;
  407. }
  408. OperateDataDictionary.Remove(PlazaRoomChest.SystemChest.ChestData.ID);
  409. PlazaRoomChest.SystemChest.Active();
  410. PlazaRoomChest.SystemChest.TurnNormalColor();
  411. PlazaRoomChest.SystemChest.ChestTimerTransform.TweenBacCG();
  412. PlazaRoomChest.SystemChest.LastActivatedTime = activatedTime;
  413. }
  414. public static void DeactivateSystemChest(long pasttime)
  415. {
  416. if (!SFSManager.GardenSmartFox.PlazaRoomManager.JoinedPlazaRoom)
  417. {
  418. return;
  419. }
  420. if (PlazaRoomChest.SystemChest == null)
  421. {
  422. return;
  423. }
  424. PlazaRoomChest.SystemChest.Deactive();
  425. PlazaRoomChest.SystemChest.TurnGray();
  426. PlazaRoomChest.SystemChest.ChestTimer = PlazaRoomChest.SystemChest.ChestTime - pasttime + 1;
  427. }
  428. public static void ReceiveChestAward(int award, long chestID)
  429. {
  430. ChestOperateData chestOperateData;
  431. if (OperateDataDictionary.TryGetValue(chestID, out chestOperateData))
  432. {
  433. chestOperateData.Received = true;
  434. }
  435. else
  436. {
  437. chestOperateData = new ChestOperateData(true, PlazaRoomChest.CurrentChest.ChestData.ID, PlazaRoomChest.CurrentChest.ChestData.DatabaseRoomID);
  438. OperateDataDictionary.Add(chestOperateData.ChestID, chestOperateData);
  439. }
  440. ManaReso.Get<CanvasGroup>("Y_Chest").interactable = true;
  441. if (award <= 0)
  442. {
  443. ManaReso.Get("Y_Chest").TweenBacCG();
  444. Bubble.Show(null, Language.GetStr("UI", "Y_SoldOut"));
  445. }
  446. else
  447. {
  448. ManaAudio.PlayClip(Clip.CurrentClip);
  449. ManaReso.Get("Y_Chest").TweenForCG();
  450. ManaReso.SetActive("Ya_GetAward", true);
  451. ManaReso.SetActive("Y_Guess", false);
  452. ManaReso.SetActive("Yb_CreateChest", false);
  453. ManaReso.SetText("Ya_Desc", $"x{award}");
  454. ManaCenter.AddDiamond(award, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.RoomChestAward);
  455. ManaCenter.GetChestAwardAmt++;
  456. }
  457. for (int i = 0; i < PlazaRoomChests.Count; i++)
  458. {
  459. if (PlazaRoomChests[i].ChestData.ID == chestID)
  460. {
  461. if (PlazaRoomChests[i].ChestData.ChestType == ChestType.System)
  462. {
  463. PlazaRoomChests[i].ResetChestTimer();
  464. OperateDataDictionary[chestID].RemainGuessAmt = 0;
  465. OperateDataDictionary[chestID].LastActivatedTime = PlazaRoomChests[i].LastActivatedTime;
  466. }
  467. else
  468. {
  469. PlazaRoomChests[i].RetriveChest();
  470. PlazaRoomChests.RemoveAt(i--);
  471. }
  472. }
  473. }
  474. }
  475. public static void ReceiveChestRefund(int refund, List<int> databaseRoomIDs)
  476. {
  477. foreach (var databaseRoomID in databaseRoomIDs)
  478. {
  479. RefundRoomIDs.Remove(databaseRoomID);
  480. }
  481. if (refund > 0)
  482. {
  483. ManaCenter.AddDiamond(refund, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.RoomChestRefund);
  484. ManaInfoBox.Show(InfoCategory.Garden, Language.GetStr("UI", "Y_Refund")+refund, 10, Color.white);
  485. }
  486. }
  487. public static void Save()
  488. {
  489. ClearRefundAndOperateData(ManaData.PlayerNode);
  490. foreach (var id in RefundRoomIDs)
  491. {
  492. XmlNode node = ManaData.PlayerDoc.CreateNode(XmlNodeType.Element, RefundNodeName, null);
  493. node.InnerText = id.ToString();
  494. ManaData.PlayerNode.AppendChild(node);
  495. }
  496. foreach (var kv in OperateDataDictionary)
  497. {
  498. XmlNode node = ManaData.PlayerDoc.CreateNode(XmlNodeType.Element, ReceivedNodeName, null);
  499. node.InnerText = kv.Value.ToString();
  500. ManaData.PlayerNode.AppendChild(node);
  501. //Debug.LogWarning($"{kv.Key}|{kv.Value}");
  502. }
  503. }
  504. public static void ClearRefundAndOperateData(XmlNode playerConfigNode)
  505. {
  506. XmlNode node = playerConfigNode.SelectSingleNode(RefundNodeName);
  507. while (node != null)
  508. {
  509. playerConfigNode.RemoveChild(node);
  510. node = playerConfigNode.SelectSingleNode(RefundNodeName);
  511. }
  512. node = playerConfigNode.SelectSingleNode(ReceivedNodeName);
  513. while (node != null)
  514. {
  515. playerConfigNode.RemoveChild(node);
  516. node = playerConfigNode.SelectSingleNode(ReceivedNodeName);
  517. }
  518. }
  519. }