PlazaRoomMge.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Sfs2X.Entities.Data;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class PlazaRoomMge : Regist
  9. {
  10. #region Config
  11. public static int CurrentRoomPage = 0;
  12. public static int CurrentRoomCount = 1;
  13. public static int DownloadVolume = 20;
  14. public static bool FilterEnabled;
  15. public static bool DownloadLock;
  16. public static Transform Grid;
  17. public static CounterAction DownloadCallback;
  18. public static List<PlazaRoomItem> PlazaRoomItems = new List<PlazaRoomItem>();
  19. public static Dictionary<int, RoomData> RoomDatas = new Dictionary<int, RoomData>();
  20. #endregion
  21. public override void RegistValueC()
  22. {
  23. DatabaseManager.OnReceiveRoomData += OnReceiveRoomData;
  24. Download(DownloadVolume);
  25. VirtualScrollRect virtualScrollRect = ManaReso.Get<VirtualScrollRect>("Z_ScrollRect");
  26. virtualScrollRect.OnVerticalLessEqual0 = NextPage;
  27. virtualScrollRect.OnVerticalGreaterEqual1 = PreviousPage;
  28. CostFml = ManaData.GetPlazaRoomConfig()[1].Value;
  29. OnSliderValueChange(0);
  30. }
  31. public override void RegistReference()
  32. {
  33. Grid = ManaReso.Get("Z_Grid");
  34. DurationLab = ManaReso.Get<Text>("AAa_ValueLab");
  35. DurationSlider = ManaReso.Get<Slider>("AAa_Slider");
  36. MaxPlayerLab = ManaReso.Get<Text>("AAb_ValueLab");
  37. MaxPlayerSlider = ManaReso.Get<Slider>("AAb_Slider");
  38. DurationSlider.onValueChanged.AddListener(OnSliderValueChange);
  39. MaxPlayerSlider.onValueChanged.AddListener(OnSliderValueChange);
  40. }
  41. public static void OnReceiveRoomData(ISFSArray sfsArray)
  42. {
  43. Debug.Log(sfsArray.Size());
  44. CurrentRoomCount = sfsArray.Size();
  45. RoomDatas = new Dictionary<int, RoomData>();
  46. for (int i = 0; i < sfsArray.Count; i++)
  47. {
  48. RoomData roomData = new RoomData(sfsArray.GetSFSObject(i));
  49. RoomDatas.Add(i, roomData);
  50. }
  51. UpdateRoomItem();
  52. if (DownloadCallback != null)
  53. {
  54. DownloadCallback.Invoke();
  55. }
  56. }
  57. public static void NextPage()
  58. {
  59. if (RoomDatas.Count <= CurrentRoomPage*PageAmt)
  60. {
  61. if (!DownloadLock)
  62. {
  63. Debug.LogWarning("Download Page");
  64. ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "Loading"));
  65. ManaReso.Get("Z_Tip").TweenForCG();
  66. DownloadLock = true;
  67. DownloadNext
  68. (
  69. () =>
  70. {
  71. ManaLan.Add(ManaReso.Get<Text>("Z_Tip"), new LanStr("UI", "LoadSucceed"));
  72. DelayCall.Call(0.5f, () => { ManaReso.Get("Z_Tip").TweenBacCG(); });
  73. }
  74. );
  75. }
  76. }
  77. else
  78. {
  79. Debug.LogWarning("Next Page");
  80. List<RoomData> roomDatas = GetRoomDatas(++CurrentRoomPage);
  81. for (int i = 0; i < roomDatas.Count; i++)
  82. {
  83. CreateNextRoomItem(roomDatas[i]);
  84. }
  85. }
  86. }
  87. public static void PreviousPage()
  88. {
  89. if (CurrentRoomPage < 1)
  90. {
  91. return;
  92. }
  93. Debug.LogWarning("Prev Page");
  94. List<RoomData> roomDatas = GetRoomDatas(--CurrentRoomPage);
  95. for (int i = 0; i < roomDatas.Count; i++)
  96. {
  97. CreatePreviousRoomItem(roomDatas[i]);
  98. }
  99. }
  100. private static int MaxRoomItem = 10;
  101. private static void CreateNextRoomItem(RoomData roomData)
  102. {
  103. if (PlazaRoomItems.Count >= MaxRoomItem)
  104. {
  105. Transform firstChild = Grid.GetChild(0);
  106. firstChild.GetComponent<PlazaRoomItem>().Init(roomData);
  107. firstChild.SetAsLastSibling();
  108. }
  109. else
  110. {
  111. PlazaRoomItems.Add(roomData.CreateItem().Init(roomData));
  112. }
  113. }
  114. private static void CreatePreviousRoomItem(RoomData roomData)
  115. {
  116. if (PlazaRoomItems.Count >= MaxRoomItem)
  117. {
  118. Transform lastChild = Grid.GetChild(Grid.childCount - 1);
  119. lastChild.GetComponent<PlazaRoomItem>().Init(roomData);
  120. lastChild.SetAsFirstSibling();
  121. }
  122. else
  123. {
  124. return;
  125. }
  126. }
  127. private static void UpdateRoomItem()
  128. {
  129. Debug.LogWarning("Update Page");
  130. if (CurrentRoomPage == 0)
  131. {
  132. return;
  133. }
  134. int lastPage = (RoomDatas.Count/PageAmt) + 1;
  135. if (CurrentRoomPage > lastPage)
  136. {
  137. CurrentRoomPage = lastPage;
  138. }
  139. if (CurrentRoomPage == 1)
  140. {
  141. for (int i = PageAmt; i < Grid.childCount; i++)
  142. {
  143. ManaReso.Save(Grid.GetChild(i--));
  144. }
  145. for (int i = 0; i < PageAmt; i++)
  146. {
  147. if (RoomDatas.Count > i)
  148. {
  149. if (Grid.childCount > i + 1)
  150. {
  151. Grid.GetChild(i).GetComponent<PlazaRoomItem>().Init(RoomDatas[i]);
  152. }
  153. else
  154. {
  155. RoomDatas[i].CreateItem().Init(RoomDatas[i]);
  156. }
  157. }
  158. else
  159. {
  160. if (Grid.childCount > i + 1)
  161. {
  162. ManaReso.Save(Grid.GetChild(i--));
  163. }
  164. }
  165. }
  166. }
  167. else
  168. {
  169. int startIndex = (CurrentRoomPage - 1)*5;
  170. for (int i = startIndex; i < startIndex + 10; i++)
  171. {
  172. if (RoomDatas.Count > i)
  173. {
  174. if (Grid.childCount > i + 1)
  175. {
  176. Grid.GetChild(i).GetComponent<PlazaRoomItem>().Init(RoomDatas[i]);
  177. }
  178. else
  179. {
  180. RoomDatas[i].CreateItem().Init(RoomDatas[i]);
  181. }
  182. }
  183. else
  184. {
  185. if (Grid.childCount > i + 1)
  186. {
  187. ManaReso.Save(Grid.GetChild(i--));
  188. }
  189. }
  190. }
  191. }
  192. }
  193. private static void Download(int limit)
  194. {
  195. GardenSmartFoxManager.GardenSmartFox.ExecuteAfterCheckConection
  196. (
  197. () =>
  198. {
  199. DatabaseManager.GetRoomList(limit);
  200. }
  201. );
  202. }
  203. private static void DownloadNext(Action action)
  204. {
  205. int limit = (CurrentRoomCount/DownloadVolume + 1)*DownloadVolume;
  206. DownloadCallback = new CounterAction(action);
  207. Download(limit);
  208. }
  209. private static int PageAmt = 5;
  210. private static List<RoomData> GetRoomDatas(int page)
  211. {
  212. List<RoomData> roomDatas = new List<RoomData>();
  213. if (RoomDatas.Count >= page*PageAmt)
  214. {
  215. for (int i = 0; i < PageAmt; i++)
  216. {
  217. roomDatas.Add(RoomDatas[(page - 1)*PageAmt + i]);
  218. }
  219. }
  220. else
  221. {
  222. for (int i = 0; i < (PageAmt - page*PageAmt - RoomDatas.Count); i++)
  223. {
  224. roomDatas.Add(RoomDatas[(page - 1)*PageAmt + i]);
  225. }
  226. }
  227. return roomDatas;
  228. }
  229. public static void OpenPanel()
  230. {
  231. ManaReso.Get("Z_BK").TweenForCG();
  232. }
  233. public static void ClosePanel()
  234. {
  235. ManaReso.Get("Z_BK").TweenBacCG();
  236. }
  237. public static void EnableFilter()
  238. {
  239. FilterEnabled = true;
  240. ManaReso.SetActive("Z_InfoImg51", true);
  241. }
  242. public static void DisableFilter()
  243. {
  244. FilterEnabled = false;
  245. ManaReso.SetActive("Z_InfoImg51", false);
  246. }
  247. public static int Duration;
  248. public static int MaxPlayer;
  249. public static string CostFml;
  250. private static Text DurationLab;
  251. private static Text MaxPlayerLab;
  252. private static Slider DurationSlider;
  253. private static Slider MaxPlayerSlider;
  254. public static void CreateRoom()
  255. {
  256. string rawName = ManaReso.Get<InputField>("AA_InputField").text;
  257. if (string.IsNullOrEmpty(rawName))
  258. {
  259. Bubble.Show(null, Language.GetStr("UI", "AA_AllWhiteSpace"));
  260. return;
  261. }
  262. if (StringFilter.ContainSensitiveWord(rawName))
  263. {
  264. Bubble.Show(null, Language.GetStr("Common", "ContainSensitiveWord"));
  265. return;
  266. }
  267. //if (ManaCenter.Pay())
  268. //{
  269. //}
  270. }
  271. public static void OnSliderValueChange(float value)
  272. {
  273. //if (!DurationSlider.gameObject.activeInHierarchy || !MaxPlayerSlider.gameObject.activeInHierarchy)
  274. //{
  275. // return;
  276. //}
  277. Duration = (int) DurationSlider.value;
  278. MaxPlayer = (int) MaxPlayerSlider.value;
  279. DurationLab.text = Duration + Language.GetStr("UI", "AAa_Value");
  280. MaxPlayerLab.text = MaxPlayer + Language.GetStr("UI", "AAb_Value");
  281. int cost = (int) Auxiliary.FmlParse(CostFml, "h", Duration.ToString(), "p", MaxPlayer.ToString());
  282. ManaReso.SetText("AA_Desc", Language.GetStr("UI", "AA_Cost") + "<(钻石)>" + cost);
  283. if (cost > ManaCenter.Diamond)
  284. {
  285. ManaReso.SetText("AA_BtnLab", Language.GetStr("UI", "AA_NoEnoughDiamond"));
  286. ManaReso.Get<Button>("AA_Btn").image.material = Lib.GrayMat;
  287. ManaReso.Get<Button>("AA_Btn").interactable = false;
  288. }
  289. else
  290. {
  291. ManaReso.SetText("AA_BtnLab", Language.GetStr("UI", "AA_Create"));
  292. ManaReso.Get<Button>("AA_Btn").image.material = null;
  293. ManaReso.Get<Button>("AA_Btn").interactable = true;
  294. }
  295. }
  296. public static void OpenCreatePanel()
  297. {
  298. ManaReso.Get("AA_CreatePlazaRoom").TweenForCG();
  299. }
  300. public static void CloseCreatePanel()
  301. {
  302. ManaReso.Get("AA_CreatePlazaRoom").TweenBacCG();
  303. }
  304. }