using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Entities.Data; using UnityEngine; using UnityEngine.UI; public class PlazaRoomMge : Regist { #region Config public static float ItemHeight { get { if (itemHeight == 0) { itemHeight = (Grid.GetChild(0).GetComponent().rect.height + Grid.GetComponent().spacing); } return itemHeight; } } private static float itemHeight; public static float DownLoadTimespan = 30; public static bool DownloadLock; public static int LastIndex; public static int FirstIndex; public static int CustomLastIndex; public static int CustomFirstIndex; public static bool FilterEnabled; public static Transform Grid; public static VirtualScrollRect ScrollRect; public static List PlazaRoomItems = new List(); public static Dictionary RoomDatas = new Dictionary(); public static List CustomPlazaRoomItems = new List(); public static Dictionary CustomRoomDatas = new Dictionary(); #endregion private float RefreshTime = 10; private float RefreshTimer = 0; private void Update() { RefreshTimer += Time.deltaTime; if (RefreshTimer >= RefreshTime) { RefreshTimer = 0; if (FilterEnabled) RefreshRoomStatus(CustomPlazaRoomItems); else RefreshRoomStatus(PlazaRoomItems); } } public override void RegistValueC() { VirtualScrollRect virtualScrollRect = ManaReso.Get("Z_ScrollRect"); virtualScrollRect.OnVerticalLessEqual0 = NextPageGate; virtualScrollRect.OnVerticalGreaterEqual1 = PreviousPageGate; CostFml = ManaData.GetPlazaRoomConfig()[1].Value; ValueFml = ManaData.GetPlazaRoomConfig()[2].Value; } public override void RegistReference() { Grid = ManaReso.Get("Z_Grid"); ScrollRect = ManaReso.Get("Z_ScrollRect"); DurationLab = ManaReso.Get("AAa_ValueLab"); DurationSlider = ManaReso.Get("AAa_Slider"); MaxPlayerLab = ManaReso.Get("AAb_ValueLab"); MaxPlayerSlider = ManaReso.Get("AAb_Slider"); DurationSlider.onValueChanged.AddListener(OnSliderValueChange); DurationSlider.onValueChanged.AddListener(f => ManaAudio.PlayClip(Clip.BtnClip)); MaxPlayerSlider.onValueChanged.AddListener(OnSliderValueChange); MaxPlayerSlider.onValueChanged.AddListener(f => ManaAudio.PlayClip(Clip.BtnClip)); } public static void RefreshRoomStatus(List plazaRoomItems) { List databaseRoomIDs = new List(); foreach (var plazaRoomItem in plazaRoomItems) { databaseRoomIDs.Add(plazaRoomItem.RoomData.ID); } SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetRoomFullStatus(databaseRoomIDs); } public static void DownloadRoomData() { ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "Loading")); ManaReso.Get("Z_Tip").TweenForCG(); DownloadLock = true; SFSManager.GardenSmartFox.ExecuteAfterCheckConection ( () => SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetRoomData(LastIndex), (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } }, (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } } ); } public static void DownloadCustomRoomData() { ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "Loading")); ManaReso.Get("Z_Tip").TweenForCG(); DownloadLock = true; SFSManager.GardenSmartFox.ExecuteAfterCheckConection ( () => SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetCustomRoomData(CustomLastIndex, long.Parse(ManaServer.SerialNumber)), (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } }, (succeed, baseEvent) => { if (!succeed) { ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "LoadFailed")); DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; ManaReso.Get("Z_Tip").TweenBacCG(); }); } } ); } public static void OnReceiveRoomData(ISFSArray sfsArray) { //Debug.LogWarning("receive " + sfsArray.Count); int count = RoomDatas.Count; for (int i = 0; i < sfsArray.Count; i++) { RoomData roomData = new RoomData(sfsArray.GetSFSObject(i)); if (!RoomDatas.Values.Contains(roomData)) { RoomDatas.Add(count + i, roomData); } } if (FilterEnabled) { return; } ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "LoadSucceed")); DelayCall.Call(0.5f, () => { ManaReso.Get("Z_Tip").TweenBacCG(); }); if (sfsArray.Count == 0) { DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; }); } else { int availableItemAmt = MaxRoomItem - PlazaRoomItems.Count; //if (LastIndex == 0) //{ NextPage(availableItemAmt, ref FirstIndex, ref LastIndex, RoomDatas); //} DownloadLock = false; } RefreshRoomStatus(PlazaRoomItems); } public static void OnReceiveCustomRoomData(ISFSArray sfsArray) { //Debug.LogWarning("receive custom " + sfsArray.Count); int count = CustomRoomDatas.Count; for (int i = 0; i < sfsArray.Count; i++) { RoomData roomData = new RoomData(sfsArray.GetSFSObject(i)); if (!CustomRoomDatas.Values.Contains(roomData)) { CustomRoomDatas.Add(count + i, roomData); } } if (!FilterEnabled) { return; } ManaLan.Add(ManaReso.Get("Z_Tip"), new LanStr("UI", "LoadSucceed")); DelayCall.Call(0.5f, () => { ManaReso.Get("Z_Tip").TweenBacCG(); }); if (sfsArray.Count == 0) { DelayCall.Call(DownLoadTimespan, () => { DownloadLock = false; }); } else { int availableItemAmt = MaxRoomItem - PlazaRoomItems.Count; //if (LastIndex == 0) //{ NextPage(availableItemAmt, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas); //} DownloadLock = false; } RefreshRoomStatus(CustomPlazaRoomItems); } public static void OnReceiveRoomFullStatus(List ids, List statuses) { foreach (var plazaRoomItem in PlazaRoomItems) { int index = ids.IndexOf(plazaRoomItem.RoomData.ID); if (index != -1) { plazaRoomItem.RefreshStatus(statuses[index]); } } } private static int PageVolume = 5; public static void NextPageGate() { if (FilterEnabled) { NextPage(PageVolume, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas); } else { NextPage(PageVolume, ref FirstIndex, ref LastIndex, RoomDatas); } } public static void PreviousPageGate() { if (FilterEnabled) { PreviousPage(PageVolume, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas); } else { PreviousPage(PageVolume, ref FirstIndex, ref LastIndex, RoomDatas); } } public static void NextPage(int nextAmt, ref int firstIndex, ref int lastIndex, Dictionary dictionary) { if (lastIndex >= dictionary.Count) { if (!DownloadLock) { //Debug.LogWarning("Download Page"); if (FilterEnabled) { DownloadCustomRoomData(); } else { DownloadRoomData(); } } } else { //Debug.LogWarning("Next Page"); for (int i = 0; i < nextAmt; i++) { RoomData roomData; if (dictionary.TryGetValue(lastIndex, out roomData)) { CreateNextRoomItem(ref firstIndex, ref lastIndex, roomData); } else { break; } } } } public static void PreviousPage(int nextAmt, ref int firstIndex, ref int lastIndex, Dictionary dictionary) { if (firstIndex == 0) { return; } //Debug.LogWarning("Prev Page"); for (int i = 0; i < nextAmt; i++) { RoomData roomData; if (dictionary.TryGetValue(firstIndex - 1, out roomData)) { CreatePreviousRoomItem(ref firstIndex, ref lastIndex, roomData); } else { break; } } } private static int MaxRoomItem = 10; private static void CreateNextRoomItem(ref int firstIndex, ref int lastIndex, RoomData roomData) { if (PlazaRoomItems.Count >= MaxRoomItem) { firstIndex++; lastIndex++; Transform firstChild = Grid.GetChild(0); firstChild.GetComponent().Init(roomData); ScrollRect.Recycle(new Vector2(0, -ItemHeight)); Grid.position += new Vector3(0, -ItemHeight); firstChild.SetAsLastSibling(); } else { lastIndex++; PlazaRoomItems.Add(roomData.CreateItem().Init(roomData)); } } private static void CreatePreviousRoomItem(ref int firstIndex, ref int lastIndex, RoomData roomData) { if (PlazaRoomItems.Count >= MaxRoomItem) { firstIndex--; lastIndex--; Transform lastChild = Grid.GetChild(Grid.childCount - 1); lastChild.GetComponent().Init(roomData); ScrollRect.Recycle(new Vector2(0, ItemHeight)); Grid.position += new Vector3(0, ItemHeight); lastChild.SetAsFirstSibling(); } } public static void OpenPanel() { if (!DownloadLock) { DownloadRoomData(); DownloadCustomRoomData(); } OnSliderValueChange(0); ManaReso.Get("Z_BK").TweenForCG(); } public static void ClosePanel() { ManaReso.Get("Z_BK").TweenBacCG(); } public static void EnableFilter() { FilterEnabled = true; ManaReso.SetActive("Z_InfoImg51", true); ClearGrid(); NextPage(PageVolume, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas); } public static void DisableFilter() { FilterEnabled = false; ManaReso.SetActive("Z_InfoImg51", false); ClearGrid(); NextPage(PageVolume, ref FirstIndex, ref LastIndex, RoomDatas); } private static void ClearGrid() { LastIndex = 0; FirstIndex = 0; CustomLastIndex = 0; CustomFirstIndex = 0; while (Grid.childCount > 0) { ManaReso.Save(Grid.GetChild(0)); } PlazaRoomItems = new List(); ManaReso.Get("Z_Tip").GetTweenCG().InOrigin = true; DownloadLock = false; } public static int Cost; public static int Duration; public static int MaxPlayer; public static string CostFml; public static string ValueFml; private static Text DurationLab; private static Text MaxPlayerLab; private static Slider DurationSlider; private static Slider MaxPlayerSlider; public static void CreateRoom() { string roomName = ManaReso.Get("AA_InputField").text; if (string.IsNullOrEmpty(roomName)) { Bubble.Show(null, Language.GetStr("UI", "AA_AllWhiteSpace")); return; } if (roomName.GetBytes() > 24) { Bubble.Show(null, Language.GetStr("UI", "AA_TooLong")); return; } if (StringFilter.ContainSensitiveWord(roomName)) { Bubble.Show(null, Language.GetStr("Common", "ContainSensitiveWord")); return; } ManaLan.Add(ManaReso.Get("AA_Tip"), new LanStr("UI", "AA_SendRequest")); ManaReso.Get("AA_Tip").TweenForCG(); ManaReso.Get("AA_CreatePlazaRoom").interactable = false; int value = (int) Auxiliary.FmlParse(ValueFml, "c", Cost.ToString(), "p", MaxPlayer.ToString(), "h", Duration.ToString()); SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.CreateRoom(roomName, long.Parse(ManaServer.SerialNumber), Duration, MaxPlayer, value); } public static void OnCreateRoomError() { ManaLan.Add(ManaReso.Get("AA_Tip"), new LanStr("UI", "AA_CreateError")); ManaReso.Get("AA_Tip").TweenBacCG(); ManaReso.Get("AA_CreatePlazaRoom").interactable = true; Debug.Log("create error"); } public static void OnCreateRoomSucceed(RoomData roomData) { if (!RoomDatas.Values.Contains(roomData)) { RoomDatas.Add(RoomDatas.Count, roomData); } if (!CustomRoomDatas.Values.Contains(roomData)) { CustomRoomDatas.Add(CustomRoomDatas.Count, roomData); } int availableItemAmt = MaxRoomItem - PlazaRoomItems.Count; if (FilterEnabled) { NextPage(availableItemAmt, ref CustomFirstIndex, ref CustomLastIndex, CustomRoomDatas); } else { NextPage(availableItemAmt, ref FirstIndex, ref LastIndex, RoomDatas); } ManaCenter.CreateRoomAmt++; ManaLan.Add(ManaReso.Get("AA_Tip"), new LanStr("UI", "AA_CreateSucceed")); ManaReso.Get("AA_Tip").TweenBacCG(); ManaReso.Get("AA_CreatePlazaRoom").interactable = true; DownloadRoomData(); ManaCenter.AddDiamond(-Cost, StaticsManager.ItemID.创建派对, StaticsManager.ConsumeModule.Charge); PlazaRoomItem.OnJoinBtn(roomData); CloseCreatePanel(); } public static void OnSliderValueChange(float value) { Duration = (int) DurationSlider.value; MaxPlayer = (int) MaxPlayerSlider.value; DurationLab.text = Duration + Language.GetStr("UI", "AAa_Value"); MaxPlayerLab.text = MaxPlayer + Language.GetStr("UI", "AAb_Value"); Cost = (int) Auxiliary.FmlParse(CostFml, "h", Duration.ToString(), "p", MaxPlayer.ToString()); ManaReso.SetText("AA_Desc", Language.GetStr("UI", "AA_Cost") + "<(钻石)>" + Cost); if (Cost > ManaCenter.Diamond) { ManaReso.SetText("AA_BtnLab", Language.GetStr("UI", "AA_NoEnoughDiamond")); ManaReso.Get