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 PlazaRoomManager : 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; private float RefreshRoomStatusTime = 10; private float RefreshRoomStatusTimer = 0; public static float DownLoadDataTime = 30; public static bool DownloadDataFlag; public static int NormalLastDisplayIndex; public static int NormalFirstDisplayIndex; public static int CustomLastDisplayIndex; public static int CustomFirstDisplayIndex; public static bool FilterFlag; public static int ScrollRectMaxDisplayAmt = 10; public static int ScrollRectPageVolume = 5; public static Transform Grid; public static VirtualScrollRect ScrollRect; public static List NormalPlazaRoomItems = new List(); public static Dictionary NormalRoomDatas = new Dictionary(); public static List CustomPlazaRoomItems = new List(); public static Dictionary CustomRoomDatas = new Dictionary(); #endregion private void Update() { RefreshRoomStatusTimer += Time.deltaTime; if (RefreshRoomStatusTimer >= RefreshRoomStatusTime) { RefreshRoomStatusTimer = 0; if (FilterFlag) RefreshRoomStatus(CustomPlazaRoomItems); else RefreshRoomStatus(NormalPlazaRoomItems); } } public override void ThirdInit() { VirtualScrollRect virtualScrollRect = ResourceManager.Get(CanvasLabel.Z_ScrollRect); virtualScrollRect.OnVerticalLessEqual0 = NextRoomPage; virtualScrollRect.OnVerticalGreaterEqual1 = PreviousRoomPage; CreateRoom_CostFml = ConfigManager.GetPlazaRoomConfig()[1].Value; DefaultRoomValueFml = ConfigManager.GetPlazaRoomConfig()[2].Value; } public override void RegistReference() { Grid = ResourceManager.Get(CanvasLabel.Z_Grid); ScrollRect = ResourceManager.Get(CanvasLabel.Z_ScrollRect); CreateRoom_DurationText = ResourceManager.Get(CanvasLabel.AAa_ValueLab); CreateRoom_DurationSlider = ResourceManager.Get(CanvasLabel.AAa_Slider); CreateRoom_MaxPlayerText = ResourceManager.Get(CanvasLabel.AAb_ValueLab); CreateRoom_MaxPlayerSlider = ResourceManager.Get(CanvasLabel.AAb_Slider); CreateRoom_DurationSlider.onValueChanged.AddListener(OnSliderValueChange); CreateRoom_DurationSlider.onValueChanged.AddListener(f => AudioManager.PlayClip(AudioLabel.ClickButton)); CreateRoom_MaxPlayerSlider.onValueChanged.AddListener(OnSliderValueChange); CreateRoom_MaxPlayerSlider.onValueChanged.AddListener(f => AudioManager.PlayClip(AudioLabel.ClickButton)); Manager.OnCoinChange += coin => { ResourceManager.SetText(CanvasLabel.X_CoinText, ResourceManager.Get(CanvasLabel.F_CoinLab).text); }; Manager.OnDiamondChange += diamond => { ResourceManager.SetText(CanvasLabel.X_DiamondText, ResourceManager.Get(CanvasLabel.F_DiamondLab).text); }; } 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 DownloadNormalRoomData() { LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__Loading)); ResourceManager.Get(CanvasLabel.Z_Tip).TweenForCG(); DownloadDataFlag = true; SFSManager.GardenSmartFox.ExecuteAfterCheckConection ( () => SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetRoomData(NormalLastDisplayIndex), (succeed, baseEvent) => { if (!succeed) { LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__LoadFailed)); DelayCall.Call(DownLoadDataTime, () => { DownloadDataFlag = false; ResourceManager.Get(CanvasLabel.Z_Tip).TweenBacCG(); }); } }, (succeed, baseEvent) => { if (!succeed) { LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__LoadFailed)); DelayCall.Call(DownLoadDataTime, () => { DownloadDataFlag = false; ResourceManager.Get(CanvasLabel.Z_Tip).TweenBacCG(); }); } } ); } public static void DownloadCustomRoomData() { LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__Loading)); ResourceManager.Get(CanvasLabel.Z_Tip).TweenForCG(); DownloadDataFlag = true; SFSManager.GardenSmartFox.ExecuteAfterCheckConection ( () => SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetCustomRoomData(CustomLastDisplayIndex, long.Parse(HttpManager.SerialNumber)), (succeed, baseEvent) => { if (!succeed) { LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__LoadFailed)); DelayCall.Call(DownLoadDataTime, () => { DownloadDataFlag = false; ResourceManager.Get(CanvasLabel.Z_Tip).TweenBacCG(); }); } }, (succeed, baseEvent) => { if (!succeed) { LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__LoadFailed)); DelayCall.Call(DownLoadDataTime, () => { DownloadDataFlag = false; ResourceManager.Get(CanvasLabel.Z_Tip).TweenBacCG(); }); } } ); } public static void OnReceiveNormalRoomData(ISFSArray sfsArray) { //Debug.LogWarning("receive " + sfsArray.Count); int count = NormalRoomDatas.Count; for (int i = 0; i < sfsArray.Count; i++) { RoomData roomData = new RoomData(sfsArray.GetSFSObject(i)); if (!NormalRoomDatas.Values.Contains(roomData)) { NormalRoomDatas.Add(count + i, roomData); } } if (FilterFlag) { return; } LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__LoadSucceed)); DelayCall.Call(0.5f, () => { ResourceManager.Get(CanvasLabel.Z_Tip).TweenBacCG(); }); if (sfsArray.Count == 0) { DelayCall.Call(DownLoadDataTime, () => { DownloadDataFlag = false; }); } else { int availableItemAmt = ScrollRectMaxDisplayAmt - NormalPlazaRoomItems.Count; NextRoomPage(availableItemAmt, ref NormalFirstDisplayIndex, ref NormalLastDisplayIndex, NormalRoomDatas); DownloadDataFlag = false; } RefreshRoomStatus(NormalPlazaRoomItems); } 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 (!FilterFlag) { return; } LanguageManager.Add(ResourceManager.Get(CanvasLabel.Z_Tip), new MulLanStr(LanguageLabel.UI__LoadSucceed)); DelayCall.Call(0.5f, () => { ResourceManager.Get(CanvasLabel.Z_Tip).TweenBacCG(); }); if (sfsArray.Count == 0) { DelayCall.Call(DownLoadDataTime, () => { DownloadDataFlag = false; }); } else { int availableItemAmt = ScrollRectMaxDisplayAmt - NormalPlazaRoomItems.Count; NextRoomPage(availableItemAmt, ref CustomFirstDisplayIndex, ref CustomLastDisplayIndex, CustomRoomDatas); DownloadDataFlag = false; } RefreshRoomStatus(CustomPlazaRoomItems); } public static void OnReceiveRoomFullStatus(List ids, List statuses) { foreach (var plazaRoomItem in NormalPlazaRoomItems) { int index = ids.IndexOf(plazaRoomItem.RoomData.ID); if (index != -1) { plazaRoomItem.RefreshStatus(statuses[index]); } } } public static void NextRoomPage() { if (FilterFlag) { NextRoomPage(ScrollRectPageVolume, ref CustomFirstDisplayIndex, ref CustomLastDisplayIndex, CustomRoomDatas); } else { NextRoomPage(ScrollRectPageVolume, ref NormalFirstDisplayIndex, ref NormalLastDisplayIndex, NormalRoomDatas); } } public static void PreviousRoomPage() { if (FilterFlag) { PreviousRoomPage(ScrollRectPageVolume, ref CustomFirstDisplayIndex, ref CustomLastDisplayIndex, CustomRoomDatas); } else { PreviousRoomPage(ScrollRectPageVolume, ref NormalFirstDisplayIndex, ref NormalLastDisplayIndex, NormalRoomDatas); } } public static void NextRoomPage(int nextAmt, ref int firstIndex, ref int lastIndex, Dictionary dictionary) { if (lastIndex >= dictionary.Count) { if (!DownloadDataFlag) { //Debug.LogWarning("Download Page"); if (FilterFlag) { DownloadCustomRoomData(); } else { DownloadNormalRoomData(); } } } 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 PreviousRoomPage(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 void CreateNextRoomItem(ref int firstIndex, ref int lastIndex, RoomData roomData) { if (NormalPlazaRoomItems.Count >= ScrollRectMaxDisplayAmt) { 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++; NormalPlazaRoomItems.Add(roomData.CreateItem().Init(roomData)); } } private static void CreatePreviousRoomItem(ref int firstIndex, ref int lastIndex, RoomData roomData) { if (NormalPlazaRoomItems.Count >= ScrollRectMaxDisplayAmt) { 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 OpenJoinRoomPanel() { if (!DownloadDataFlag) { DownloadNormalRoomData(); DownloadCustomRoomData(); } OnSliderValueChange(0); ResourceManager.Get(CanvasLabel.Z_BK).TweenForCG(); } public static void CloseJoinRoomPanel() { ResourceManager.Get(CanvasLabel.Z_BK).TweenBacCG(); } public static void EnableCreateByMeFilter() { FilterFlag = true; ResourceManager.SetActive(CanvasLabel.Z_InfoImg51, true); ClearScrollRect(); NextRoomPage(ScrollRectPageVolume, ref CustomFirstDisplayIndex, ref CustomLastDisplayIndex, CustomRoomDatas); } public static void DisableCreateByMeFilter() { FilterFlag = false; ResourceManager.SetActive(CanvasLabel.Z_InfoImg51, false); ClearScrollRect(); NextRoomPage(ScrollRectPageVolume, ref NormalFirstDisplayIndex, ref NormalLastDisplayIndex, NormalRoomDatas); } private static void ClearScrollRect() { NormalLastDisplayIndex = 0; NormalFirstDisplayIndex = 0; CustomLastDisplayIndex = 0; CustomFirstDisplayIndex = 0; while (Grid.childCount > 0) { ResourceManager.Save(Grid.GetChild(0)); } NormalPlazaRoomItems = new List(); ResourceManager.Get(CanvasLabel.Z_Tip).GetTweenCG().InOrigin = true; DownloadDataFlag = false; } public static int CreateRoom_Cost; public static int CreateRoom_Duration; public static int CreateRoom_MaxPlayer; public static int MaxRoomNameChar = 24; public static string CreateRoom_CostFml; public static string DefaultRoomValueFml; private static Text CreateRoom_DurationText; private static Text CreateRoom_MaxPlayerText; private static Slider CreateRoom_DurationSlider; private static Slider CreateRoom_MaxPlayerSlider; public static void CreateRoom() { string roomName = ResourceManager.Get(CanvasLabel.AA_InputField).text; if (string.IsNullOrEmpty(roomName)) { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__AA_AllWhiteSpace)); return; } if (roomName.GetBytes() > MaxRoomNameChar) { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__AA_TooLong)); return; } if (StringFilter.ContainSensitiveWord(roomName)) { Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ContainSensitiveWord)); return; } LanguageManager.Add(ResourceManager.Get(CanvasLabel.AA_Tip), new MulLanStr(LanguageLabel.UI__AA_SendRequest)); ResourceManager.Get(CanvasLabel.AA_Tip).TweenForCG(); ResourceManager.Get(CanvasLabel.AA_CreatePlazaRoom).interactable = false; int value = (int) Auxiliary.FmlParse(DefaultRoomValueFml, "c", CreateRoom_Cost.ToString(), "p", CreateRoom_MaxPlayer.ToString(), "h", CreateRoom_Duration.ToString()); SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.CreateRoom(roomName, long.Parse(HttpManager.SerialNumber), CreateRoom_Duration, CreateRoom_MaxPlayer, value); } public static void OnCreateRoomError() { LanguageManager.Add(ResourceManager.Get(CanvasLabel.AA_Tip), new MulLanStr(LanguageLabel.UI__AA_CreateError)); ResourceManager.Get(CanvasLabel.AA_Tip).TweenBacCG(); ResourceManager.Get(CanvasLabel.AA_CreatePlazaRoom).interactable = true; Debug.Log("create error"); } public static void OnCreateRoomSucceed(RoomData roomData) { if (!NormalRoomDatas.Values.Contains(roomData)) { NormalRoomDatas.Add(NormalRoomDatas.Count, roomData); } if (!CustomRoomDatas.Values.Contains(roomData)) { CustomRoomDatas.Add(CustomRoomDatas.Count, roomData); } int availableItemAmt = ScrollRectMaxDisplayAmt - NormalPlazaRoomItems.Count; if (FilterFlag) { NextRoomPage(availableItemAmt, ref CustomFirstDisplayIndex, ref CustomLastDisplayIndex, CustomRoomDatas); } else { NextRoomPage(availableItemAmt, ref NormalFirstDisplayIndex, ref NormalLastDisplayIndex, NormalRoomDatas); } Manager.CreateRoomAmt++; LanguageManager.Add(ResourceManager.Get(CanvasLabel.AA_Tip), new MulLanStr(LanguageLabel.UI__AA_CreateSucceed)); ResourceManager.Get(CanvasLabel.AA_Tip).TweenBacCG(); ResourceManager.Get(CanvasLabel.AA_CreatePlazaRoom).interactable = true; DownloadNormalRoomData(); Manager.AddDiamond(-CreateRoom_Cost, StaticsManager.ItemID.创建派对, StaticsManager.ConsumeModule.Charge); PlazaRoomItem.OnJoinBtnClick(roomData); CloseCreateRoomPanel(); } public static void OnSliderValueChange(float value) { CreateRoom_Duration = (int) CreateRoom_DurationSlider.value; CreateRoom_MaxPlayer = (int) CreateRoom_MaxPlayerSlider.value; CreateRoom_DurationText.text = CreateRoom_Duration + Language.GetStr(LanguageLabel.UI__AAa_Value); CreateRoom_MaxPlayerText.text = CreateRoom_MaxPlayer + Language.GetStr(LanguageLabel.UI__AAb_Value); CreateRoom_Cost = (int) Auxiliary.FmlParse(CreateRoom_CostFml, "h", CreateRoom_Duration.ToString(), "p", CreateRoom_MaxPlayer.ToString()); ResourceManager.SetText(CanvasLabel.AA_Desc, Language.GetStr(LanguageLabel.UI__AA_Cost) + TransferLabel.DiamondSprite + CreateRoom_Cost); if (CreateRoom_Cost > Manager.Diamond) { ResourceManager.SetText(CanvasLabel.AA_BtnLab, Language.GetStr(LanguageLabel.UI__AA_NoEnoughDiamond)); ResourceManager.Get