PlazaRoomMge.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Sfs2X.Core;
  6. using Sfs2X.Entities;
  7. using Sfs2X.Entities.Data;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. public class PlazaRoomMge : Regist
  11. {
  12. #region Config
  13. public static float ItemHeight
  14. {
  15. get
  16. {
  17. if (itemHeight == 0)
  18. {
  19. itemHeight = (Grid.GetChild(0).GetComponent<RectTransform>().rect.height + Grid.GetComponent<VerticalLayoutGroup>().spacing);
  20. }
  21. return itemHeight;
  22. }
  23. }
  24. private static float itemHeight;
  25. public static float DownLoadTimespan = 30;
  26. public static bool DownloadLock;
  27. public static int LastIndex;
  28. public static int FirstIndex;
  29. public static int CustomLastIndex;
  30. public static int CustomFirstIndex;
  31. public static bool FilterEnabled;
  32. public static Transform Grid;
  33. public static VirtualScrollRect ScrollRect;
  34. public static List<PlazaRoomItem> PlazaRoomItems = new List<PlazaRoomItem>();
  35. public static Dictionary<int, RoomData> RoomDatas = new Dictionary<int, RoomData>();
  36. public static List<PlazaRoomItem> CustomPlazaRoomItems = new List<PlazaRoomItem>();
  37. public static Dictionary<int, RoomData> CustomRoomDatas = new Dictionary<int, RoomData>();
  38. #endregion
  39. private float RefreshTime = 10;
  40. private float RefreshTimer = 0;
  41. private void Update()
  42. {
  43. RefreshTimer += Time.deltaTime;
  44. if (RefreshTimer >= RefreshTime)
  45. {
  46. RefreshTimer = 0;
  47. if (FilterEnabled)
  48. RefreshRoomStatus(CustomPlazaRoomItems);
  49. else
  50. RefreshRoomStatus(PlazaRoomItems);
  51. }
  52. }
  53. public override void RegistValueC()
  54. {
  55. VirtualScrollRect virtualScrollRect = ManaReso.Get<VirtualScrollRect>("Z_ScrollRect");
  56. virtualScrollRect.OnVerticalLessEqual0 = NextPageGate;
  57. virtualScrollRect.OnVerticalGreaterEqual1 = PreviousPageGate;
  58. CostFml = ManaData.GetPlazaRoomConfig()[1].Value;
  59. ValueFml = ManaData.GetPlazaRoomConfig()[2].Value;
  60. }
  61. public override void RegistReference()
  62. {
  63. Grid = ManaReso.Get("Z_Grid");
  64. ScrollRect = ManaReso.Get<VirtualScrollRect>("Z_ScrollRect");
  65. DurationLab = ManaReso.Get<Text>("AAa_ValueLab");
  66. DurationSlider = ManaReso.Get<Slider>("AAa_Slider");
  67. MaxPlayerLab = ManaReso.Get<Text>("AAb_ValueLab");
  68. MaxPlayerSlider = ManaReso.Get<Slider>("AAb_Slider");
  69. DurationSlider.onValueChanged.AddListener(OnSliderValueChange);
  70. DurationSlider.onValueChanged.AddListener(f => ManaAudio.PlayClip(Clip.BtnClip));
  71. MaxPlayerSlider.onValueChanged.AddListener(OnSliderValueChange);
  72. MaxPlayerSlider.onValueChanged.AddListener(f => ManaAudio.PlayClip(Clip.BtnClip));
  73. }
  74. public static void RefreshRoomStatus(List<PlazaRoomItem> plazaRoomItems)
  75. {
  76. List<int> databaseRoomIDs = new List<int>();
  77. foreach (var plazaRoomItem in plazaRoomItems)
  78. {
  79. databaseRoomIDs.Add(plazaRoomItem.RoomData.ID);
  80. }
  81. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetRoomFullStatus(databaseRoomIDs);
  82. }
  83. public static void DownloadRoomData()
  84. {
  85. ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "Loading"));
  86. ManaReso.Get("Z_Tip").TweenForCG();
  87. DownloadLock = true;
  88. SFSManager.GardenSmartFox.ExecuteAfterCheckConection
  89. (
  90. () => SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetRoomData(LastIndex),
  91. (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } },
  92. (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } }
  93. );
  94. }
  95. public static void DownloadCustomRoomData()
  96. {
  97. ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "Loading"));
  98. ManaReso.Get("Z_Tip").TweenForCG();
  99. DownloadLock = true;
  100. SFSManager.GardenSmartFox.ExecuteAfterCheckConection
  101. (
  102. () => SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetCustomRoomData(CustomLastIndex, long.Parse(ManaServer.SerialNumber)),
  103. (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } },
  104. (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } }
  105. );
  106. }
  107. public static void OnReceiveRoomData(ISFSArray sfsArray)
  108. {
  109. //Debug.LogWarning("receive " + sfsArray.Count);
  110. int count = RoomDatas.Count;
  111. for (int i = 0; i < sfsArray.Count; i++)
  112. {
  113. RoomData roomData = new RoomData(sfsArray.GetSFSObject(i));
  114. if (!RoomDatas.Values.Contains(roomData))
  115. {
  116. RoomDatas.Add(count + i, roomData);
  117. }
  118. }
  119. if (FilterEnabled)
  120. {
  121. return;
  122. }
  123. ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "LoadSucceed"));
  124. DelayCall.Call(0.5f, () => { ManaReso.Get("Z_Tip").TweenBacCG(); });
  125. if (sfsArray.Count == 0)
  126. {
  127. DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; });
  128. }
  129. else
  130. {
  131. int availableItemAmt = MaxRoomItem - PlazaRoomItems.Count;
  132. //if (LastIndex == 0)
  133. //{
  134. NextPage(availableItemAmt, ref FirstIndex, ref LastIndex, RoomDatas);
  135. //}
  136. DownloadLock = false;
  137. }
  138. RefreshRoomStatus(PlazaRoomItems);
  139. }
  140. public static void OnReceiveCustomRoomData(ISFSArray sfsArray)
  141. {
  142. //Debug.LogWarning("receive custom " + sfsArray.Count);
  143. int count = CustomRoomDatas.Count;
  144. for (int i = 0; i < sfsArray.Count; i++)
  145. {
  146. RoomData roomData = new RoomData(sfsArray.GetSFSObject(i));
  147. if (!CustomRoomDatas.Values.Contains(roomData))
  148. {
  149. CustomRoomDatas.Add(count + i, roomData);
  150. }
  151. }
  152. if (!FilterEnabled)
  153. {
  154. return;
  155. }
  156. ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "LoadSucceed"));
  157. DelayCall.Call(0.5f, () => { ManaReso.Get("Z_Tip").TweenBacCG(); });
  158. if (sfsArray.Count == 0)
  159. {
  160. DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; });
  161. }
  162. else
  163. {
  164. int availableItemAmt = MaxRoomItem - PlazaRoomItems.Count;
  165. //if (LastIndex == 0)
  166. //{
  167. NextPage(availableItemAmt, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas);
  168. //}
  169. DownloadLock = false;
  170. }
  171. RefreshRoomStatus(CustomPlazaRoomItems);
  172. }
  173. public static void OnReceiveRoomFullStatus(List<int> ids, List<bool> statuses)
  174. {
  175. foreach (var plazaRoomItem in PlazaRoomItems)
  176. {
  177. int index = ids.IndexOf(plazaRoomItem.RoomData.ID);
  178. if (index != -1)
  179. {
  180. plazaRoomItem.RefreshStatus(statuses[index]);
  181. }
  182. }
  183. }
  184. private static int PageVolume = 5;
  185. public static void NextPageGate()
  186. {
  187. if (FilterEnabled)
  188. {
  189. NextPage(PageVolume, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas);
  190. }
  191. else
  192. {
  193. NextPage(PageVolume, ref FirstIndex, ref LastIndex, RoomDatas);
  194. }
  195. }
  196. public static void PreviousPageGate()
  197. {
  198. if (FilterEnabled)
  199. {
  200. PreviousPage(PageVolume, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas);
  201. }
  202. else
  203. {
  204. PreviousPage(PageVolume, ref FirstIndex, ref LastIndex, RoomDatas);
  205. }
  206. }
  207. public static void NextPage(int nextAmt, ref int firstIndex, ref int lastIndex, Dictionary<int, RoomData> dictionary)
  208. {
  209. if (lastIndex >= dictionary.Count)
  210. {
  211. if (!DownloadLock)
  212. {
  213. //Debug.LogWarning("Download Page");
  214. if (FilterEnabled)
  215. {
  216. DownloadCustomRoomData();
  217. }
  218. else
  219. {
  220. DownloadRoomData();
  221. }
  222. }
  223. }
  224. else
  225. {
  226. //Debug.LogWarning("Next Page");
  227. for (int i = 0; i < nextAmt; i++)
  228. {
  229. RoomData roomData;
  230. if (dictionary.TryGetValue(lastIndex, out roomData))
  231. {
  232. CreateNextRoomItem(ref firstIndex, ref lastIndex, roomData);
  233. }
  234. else
  235. {
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. public static void PreviousPage(int nextAmt, ref int firstIndex, ref int lastIndex, Dictionary<int, RoomData> dictionary)
  242. {
  243. if (firstIndex == 0)
  244. {
  245. return;
  246. }
  247. //Debug.LogWarning("Prev Page");
  248. for (int i = 0; i < nextAmt; i++)
  249. {
  250. RoomData roomData;
  251. if (dictionary.TryGetValue(firstIndex - 1, out roomData))
  252. {
  253. CreatePreviousRoomItem(ref firstIndex, ref lastIndex, roomData);
  254. }
  255. else
  256. {
  257. break;
  258. }
  259. }
  260. }
  261. private static int MaxRoomItem = 10;
  262. private static void CreateNextRoomItem(ref int firstIndex, ref int lastIndex, RoomData roomData)
  263. {
  264. if (PlazaRoomItems.Count >= MaxRoomItem)
  265. {
  266. firstIndex++;
  267. lastIndex++;
  268. Transform firstChild = Grid.GetChild(0);
  269. firstChild.GetComponent<PlazaRoomItem>().Init(roomData);
  270. ScrollRect.Recycle(new Vector2(0, -ItemHeight));
  271. Grid.position += new Vector3(0, -ItemHeight);
  272. firstChild.SetAsLastSibling();
  273. }
  274. else
  275. {
  276. lastIndex++;
  277. PlazaRoomItems.Add(roomData.CreateItem().Init(roomData));
  278. }
  279. }
  280. private static void CreatePreviousRoomItem(ref int firstIndex, ref int lastIndex, RoomData roomData)
  281. {
  282. if (PlazaRoomItems.Count >= MaxRoomItem)
  283. {
  284. firstIndex--;
  285. lastIndex--;
  286. Transform lastChild = Grid.GetChild(Grid.childCount - 1);
  287. lastChild.GetComponent<PlazaRoomItem>().Init(roomData);
  288. ScrollRect.Recycle(new Vector2(0, ItemHeight));
  289. Grid.position += new Vector3(0, ItemHeight);
  290. lastChild.SetAsFirstSibling();
  291. }
  292. }
  293. public static void OpenPanel()
  294. {
  295. if (!DownloadLock)
  296. {
  297. DownloadRoomData();
  298. DownloadCustomRoomData();
  299. }
  300. OnSliderValueChange(0);
  301. ManaReso.Get("Z_BK").TweenForCG();
  302. }
  303. public static void ClosePanel()
  304. {
  305. ManaReso.Get("Z_BK").TweenBacCG();
  306. }
  307. public static void EnableFilter()
  308. {
  309. FilterEnabled = true;
  310. ManaReso.SetActive("Z_InfoImg51", true);
  311. ClearGrid();
  312. NextPage(PageVolume, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas);
  313. }
  314. public static void DisableFilter()
  315. {
  316. FilterEnabled = false;
  317. ManaReso.SetActive("Z_InfoImg51", false);
  318. ClearGrid();
  319. NextPage(PageVolume, ref FirstIndex, ref LastIndex, RoomDatas);
  320. }
  321. private static void ClearGrid()
  322. {
  323. LastIndex = 0;
  324. FirstIndex = 0;
  325. CustomLastIndex = 0;
  326. CustomFirstIndex = 0;
  327. while (Grid.childCount > 0)
  328. {
  329. ManaReso.Save(Grid.GetChild(0));
  330. }
  331. PlazaRoomItems = new List<PlazaRoomItem>();
  332. ManaReso.Get("Z_Tip").GetTweenCG().InOrigin = true;
  333. DownloadLock = false;
  334. }
  335. public static int Cost;
  336. public static int Duration;
  337. public static int MaxPlayer;
  338. public static string CostFml;
  339. public static string ValueFml;
  340. private static Text DurationLab;
  341. private static Text MaxPlayerLab;
  342. private static Slider DurationSlider;
  343. private static Slider MaxPlayerSlider;
  344. public static void CreateRoom()
  345. {
  346. string roomName = ManaReso.Get<InputField>("AA_InputField").text;
  347. if (string.IsNullOrEmpty(roomName))
  348. {
  349. Bubble.Show(null, Language.GetStr("UI", "AA_AllWhiteSpace"));
  350. return;
  351. }
  352. if (roomName.GetBytes() > 24)
  353. {
  354. Bubble.Show(null, Language.GetStr("UI", "AA_TooLong"));
  355. return;
  356. }
  357. if (StringFilter.ContainSensitiveWord(roomName))
  358. {
  359. Bubble.Show(null, Language.GetStr("Common", "ContainSensitiveWord"));
  360. return;
  361. }
  362. ManaLan.Add(ManaReso.Get<Text>("AA_Tip"), new LanStr("UI", "AA_SendRequest"));
  363. ManaReso.Get("AA_Tip").TweenForCG();
  364. ManaReso.Get<CanvasGroup>("AA_CreatePlazaRoom").interactable = false;
  365. int value = (int) Auxiliary.FmlParse(ValueFml, "c", Cost.ToString(), "p", MaxPlayer.ToString(), "h", Duration.ToString());
  366. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.CreateRoom(roomName, long.Parse(ManaServer.SerialNumber), Duration, MaxPlayer, value);
  367. }
  368. public static void OnCreateRoomError()
  369. {
  370. ManaLan.Add(ManaReso.Get<Text>("AA_Tip"), new LanStr("UI", "AA_CreateError"));
  371. ManaReso.Get("AA_Tip").TweenBacCG();
  372. ManaReso.Get<CanvasGroup>("AA_CreatePlazaRoom").interactable = true;
  373. Debug.Log("create error");
  374. }
  375. public static void OnCreateRoomSucceed(RoomData roomData)
  376. {
  377. if (!RoomDatas.Values.Contains(roomData))
  378. {
  379. RoomDatas.Add(RoomDatas.Count, roomData);
  380. }
  381. if (!CustomRoomDatas.Values.Contains(roomData))
  382. {
  383. CustomRoomDatas.Add(CustomRoomDatas.Count, roomData);
  384. }
  385. int availableItemAmt = MaxRoomItem - PlazaRoomItems.Count;
  386. if (FilterEnabled)
  387. {
  388. NextPage(availableItemAmt, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas);
  389. }
  390. else
  391. {
  392. NextPage(availableItemAmt, ref FirstIndex, ref LastIndex, RoomDatas);
  393. }
  394. ManaCenter.CreateRoomAmt++;
  395. ManaLan.Add(ManaReso.Get<Text>("AA_Tip"), new LanStr("UI", "AA_CreateSucceed"));
  396. ManaReso.Get("AA_Tip").TweenBacCG();
  397. ManaReso.Get<CanvasGroup>("AA_CreatePlazaRoom").interactable = true;
  398. DownloadRoomData();
  399. ManaCenter.AddDiamond(-Cost, StaticsManager.ItemID.创建派对, StaticsManager.ConsumeModule.Charge);
  400. PlazaRoomItem.OnJoinBtn(roomData);
  401. CloseCreatePanel();
  402. }
  403. public static void OnSliderValueChange(float value)
  404. {
  405. Duration = (int) DurationSlider.value;
  406. MaxPlayer = (int) MaxPlayerSlider.value;
  407. DurationLab.text = Duration + Language.GetStr("UI", "AAa_Value");
  408. MaxPlayerLab.text = MaxPlayer + Language.GetStr("UI", "AAb_Value");
  409. Cost = (int) Auxiliary.FmlParse(CostFml, "h", Duration.ToString(), "p", MaxPlayer.ToString());
  410. ManaReso.SetText("AA_Desc", Language.GetStr("UI", "AA_Cost") + "<(钻石)>" + Cost);
  411. if (Cost > ManaCenter.Diamond)
  412. {
  413. ManaReso.SetText("AA_BtnLab", Language.GetStr("UI", "AA_NoEnoughDiamond"));
  414. ManaReso.Get<Button>("AA_Btn").image.material = Lib.GrayMat;
  415. ManaReso.Get<Button>("AA_Btn").interactable = false;
  416. }
  417. else
  418. {
  419. ManaReso.SetText("AA_BtnLab", Language.GetStr("UI", "AA_Create"));
  420. ManaReso.Get<Button>("AA_Btn").image.material = null;
  421. ManaReso.Get<Button>("AA_Btn").interactable = true;
  422. }
  423. }
  424. public static void OpenCreatePanel()
  425. {
  426. ManaReso.Get("AA_CreatePlazaRoom").TweenForCG();
  427. string defaultName = Language.GetStr("UI", "AA_DefaultName").Replace("[&value&]", ManaNickName.NickName);
  428. ManaReso.Get<InputField>("AA_InputField").text = defaultName;
  429. }
  430. public static void CloseCreatePanel()
  431. {
  432. ManaReso.Get("AA_CreatePlazaRoom").TweenBacCG();
  433. }
  434. }