using asap.core; using Castle.Core.Internal; using cfg; using GameCore; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.U2D; namespace game { public class EventPotionDataManager { #if UNITY_EDITOR private bool m_IsCheckCondition = true; private bool m_IsSyncData = true; private int m_DefaultToken = 100; #else private bool m_IsCheckCondition = true; private bool m_IsSyncData = true; private int m_DefaultToken = 0; #endif #region 生命周期 public bool Init() { InitTables(); return true; } #endregion #region 图集等 private SpriteAtlas m_SpriteAtlas; public SpriteAtlas SpriteAtlas { get { return m_SpriteAtlas; } set { m_SpriteAtlas = value; } } #endregion #region Table Data private Tables m_Tables; private TbEventPotionMain m_TbEventPotionMain; private TbEventPotionStage m_TbEventPotionStage; private TbEventPotionBlock m_TbEventPotionBlock; private TbEventPotionBottle m_TbEventPotionBottle; private void InitTables() { m_Tables = GContext.container.Resolve(); if (m_Tables == null) return; m_TbEventPotionMain = m_Tables.TbEventPotionMain; m_TbEventPotionStage = m_Tables.TbEventPotionStage; m_TbEventPotionBlock = m_Tables.TbEventPotionBlock; m_TbEventPotionBottle = m_Tables.TbEventPotionBottle; } public EventPotionBottle GetEventPotionBottle(int num) { return m_TbEventPotionBottle.GetOrDefault(num); } public EventPotionBlock GetEventPotionBlock(int num) { EventPotionMain potionMain = GetEventPotionMain(m_PotionData.PotionID); foreach (var item in potionMain.BlockResourceID) { var potionBlock = m_TbEventPotionBlock.GetOrDefault(item); if (potionBlock.Count == num) return potionBlock; } return null; } public EventPotionMain GetEventPotionMain(int id) { return m_TbEventPotionMain.GetOrDefault(id); } public EventPotionStage GetEventPotionStage(int id) { return m_TbEventPotionStage.GetOrDefault(id); } #endregion #region 数据相关 private EventPotionData m_PotionData; public EventPotionData PotionData { get { return m_PotionData; } } public void InitLevel() { EventPotionStage potionLevel = null; if (m_PotionData == null) { potionLevel = GetFreshPotionLevel(); } else { potionLevel = GetCurrentPotionLevel(); } if (potionLevel == null) return; m_PotionResDict.Clear(); EventPotionMain potion = m_TbEventPotionMain.GetOrDefault(m_PotionData.PotionID); if (potion == null) return; int count = potion.ResourceID.Count; for (int i = 0; i < count; i++) { m_PotionResDict.Add(potion.ResourceID[i], new EventPotionResource { TypeIndex = i, TypeCount = count, ItemResName = potion.ItemResource[i], PaintResName = potion.PaintResource[i] }); } InitUIData(); } private void ProcessSorting(EventPotionStage potionLevel) { int count = potionLevel.ResourceList.Count; bool isSkip = false; int[] arr = potionLevel.ResourceList.ToArray(); // display数据洗牌 m_PotionData.LevelDisplayData.isOver = false; m_PotionData.LevelDisplayData.ColorTypeList.Clear(); // target数据洗牌 m_PotionData.LevelTargetData.ColorTypeList.Clear(); int sortingType = potionLevel.SortingType; if (sortingType == 0) { // 随机排序 m_PotionData.LevelDisplayData.ColorTypeList.AddRange(arr); ShuffleList(m_PotionData.LevelDisplayData.ColorTypeList, isSkip); m_PotionData.LevelTargetData.ColorTypeList.AddRange(m_PotionData.LevelDisplayData.ColorTypeList.ToArray()); if (isSkip == false) { ShuffleList(m_PotionData.LevelTargetData.ColorTypeList); } // target 权重数据处理 int totalWeight = potionLevel.WeightRandom + potionLevel.WeightLimit; int random = UnityEngine.Random.Range(0, totalWeight); //if (random >= potionLevel.WeightRandom && isSkip == false) //{ // int[] validPermutation = ConstructValidPermutation(arr, potionLevel.WeightLimit); // if (validPermutation != null) // { // m_PotionData.LevelTargetData.ColorTypeList.AddRange(validPermutation); // //Debug.LogError("--- ProcessSorting Valid permutation: " + string.Join(", ", validPermutation)); // } // else // { // m_PotionData.LevelTargetData.ColorTypeList.AddRange(arr); // Debug.LogError("--- ProcessSorting No valid permutation found."); // return; // } //} if (random >= potionLevel.WeightRandom && isSkip == false) { m_PotionData.Rule = 2; List tmpList = new List(); int tmpCount = 0; // 处理LimitParam // 逆向检测 tmpList.Clear(); int tmpLast = count - 1; for (int i = 0; i < count; i++) { if (m_PotionData.LevelDisplayData.ColorTypeList[tmpLast - i] == m_PotionData.LevelTargetData.ColorTypeList[i]) { tmpList.Add(i); } } // 逆向处理 tmpCount = tmpList.Count; if (tmpCount >= potionLevel.LimitParam) { for (int i = 0; i < tmpCount; i += 2) { if (i + 1 < tmpCount) { int tmp = m_PotionData.LevelTargetData.ColorTypeList[tmpList[i]]; m_PotionData.LevelTargetData.ColorTypeList[tmpList[i]] = m_PotionData.LevelTargetData.ColorTypeList[tmpList[i + 1]]; m_PotionData.LevelTargetData.ColorTypeList[tmpList[i + 1]] = tmp; } } } // 正向检测 tmpList.Clear(); for (int i = 0; i < count; i++) { if (m_PotionData.LevelTargetData.ColorTypeList[i] == m_PotionData.LevelDisplayData.ColorTypeList[i]) { tmpList.Add(i); } } // 正向处理 tmpCount = tmpList.Count; if (tmpCount >= potionLevel.LimitParam) { for (int i = 0; i < tmpCount; i += 2) { if (i + 1 < tmpCount) { int tmp = m_PotionData.LevelTargetData.ColorTypeList[tmpList[i]]; m_PotionData.LevelTargetData.ColorTypeList[tmpList[i]] = m_PotionData.LevelTargetData.ColorTypeList[tmpList[i + 1]]; m_PotionData.LevelTargetData.ColorTypeList[tmpList[i + 1]] = tmp; } } } } else { m_PotionData.Rule = 1; } } else { // 指定排序 m_PotionData.Rule = 3; m_PotionData.LevelTargetData.ColorTypeList.AddRange(arr); m_PotionData.LevelDisplayData.ColorTypeList.AddRange(potionLevel.DisplayList.ToArray()); } for (int i = 0; i < count; ++i) { m_PotionData.LevelDisplayData.ShelfTypeList.Add(EventPotionShelfType.Shelf_Default); m_PotionData.LevelDisplayData.GridStateList.Add(EventPotionGridState.GridState_Empty); m_PotionData.LevelDisplayData.FromDisplayAreaIndexList.Add(-1); } // 处理processed数据 m_PotionData.LevelProcessedList.Clear(); PotionItemData processedData = new PotionItemData(); processedData.Clear(); processedData.DataType = EventPotionDataType.Data_ItemAndOperation; processedData.name = "ProcessSorting_" + DateTime.Now.Ticks; processedData.index = m_PotionData.LevelProcessedList.Count; processedData.isOver = false; for (int i = 0; i < count; i++) { processedData.ColorTypeList.Add(-1); processedData.ShelfTypeList.Add(EventPotionShelfType.Shelf_Default); processedData.GridStateList.Add(EventPotionGridState.GridState_Empty); processedData.FromDisplayAreaIndexList.Add(-1); } m_PotionData.LevelProcessedList.Add(processedData); } // 构造一个满足条件的排列 private int[] ConstructValidPermutation(int[] numbers, int param) { int n = numbers.Length; // 处理长度为 3 的情况 if (n == 3) { // 直接交换第一个和最后一个元素 int[] permutation = new int[] { numbers[2], numbers[1], numbers[0] }; return permutation; } // 处理长度为 4-7 的情况 for (int shift = 1; shift < n; shift++) { // 循环右移 shift 位 int[] permutation = new int[n]; for (int i = 0; i < n; i++) { permutation[(i + shift) % n] = numbers[i]; } // 检查是否满足条件 if (CountMatches(permutation, numbers) <= param && CountMatches(permutation, numbers.Reverse().ToArray()) <= param) { return permutation; } } // 如果循环右移不满足条件,尝试交换某些元素 int[] fallbackPermutation = (int[])numbers.Clone(); Swap(fallbackPermutation, 0, n - 1); // 交换第一个和最后一个元素 if (CountMatches(fallbackPermutation, numbers) <= param && CountMatches(fallbackPermutation, numbers.Reverse().ToArray()) <= param) { return fallbackPermutation; } // 如果仍未找到,返回逆序排列(理论上不会走到这里) return numbers.Reverse().ToArray(); } // 交换数组中的两个元素 private void Swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } // 计算两个数组中相同位置相同值的数量 private int CountMatches(int[] arr1, int[] arr2) { int count = 0; for (int i = 0; i < arr1.Length; i++) { if (arr1[i] == arr2[i]) { count++; } } return count; } private Dictionary m_PotionResDict = new Dictionary(); public EventPotionResource GetPotionResource(int id) { m_PotionResDict.TryGetValue(id, out EventPotionResource potionRes); return potionRes; } /// /// 获取的是 m_PotionData.LevelDisplayData.ItemTypeList 的索引列表 /// /// public List GetValidDisplayDataList() { List list = new List(); int count = m_PotionData.LevelDisplayData.ColorTypeList.Count; for (int i = 0; i < count; ++i) { if (m_PotionData.LevelDisplayData.ColorTypeList[i] > EventPotionConfig.PotionItemType_Empty && m_PotionData.LevelDisplayData.GridStateList[i] != EventPotionGridState.GridState_Over) { list.Add(i); } } return list; } public int GetValidDisplayDataListCount() { int num = 0; int count = m_PotionData.LevelDisplayData.ColorTypeList.Count; for (int i = 0; i < count; ++i) { if (m_PotionData.LevelDisplayData.ColorTypeList[i] > EventPotionConfig.PotionItemType_Empty && m_PotionData.LevelDisplayData.GridStateList[i] != EventPotionGridState.GridState_Over) { num++; } } return num; } private List m_WallsBefore = new List(); private List m_WallsAfter = new List(); public int WallBeforeCount { get { return m_WallsBefore.Count; } } public void InitWalls(int totalNums) { int wallNum = totalNums - 2; m_WallsBefore.Clear(); for (int i = 0; i < wallNum; ++i) { PotionItemData tmpWallDataBefore = new PotionItemData() { DataType = EventPotionDataType.Data_Wall }; tmpWallDataBefore.name = "InitWalls_Before_" + i + "_" + DateTime.Now.Ticks; tmpWallDataBefore.index = i; tmpWallDataBefore.isOver = true; m_WallsBefore.Add(tmpWallDataBefore); } wallNum = 1; m_WallsAfter.Clear(); PotionItemData tmpWallDataAfter = new PotionItemData() { DataType = EventPotionDataType.Data_Wall }; tmpWallDataAfter.name = "InitWalls_After_" + DateTime.Now.Ticks; tmpWallDataAfter.index = 0; tmpWallDataAfter.isOver = true; m_WallsAfter.Add(tmpWallDataAfter); } public void ClearData() { m_WallsBefore.Clear(); m_WallsAfter.Clear(); m_ScrollerDataList.Clear(); ClearUIData(); m_SpriteAtlas = null; } public void ActivateEvent(FishingEvent fishingEvent) { if (fishingEvent == null) return; if (!Init()) return; if (m_PotionData == null) GetFreshPotionLevel(); else FixPotionData(); //if (m_PotionData != null) { int potionID = fishingEvent.RedirectID; // 获取当前魔药活动数据 EventPotionMain potion = m_TbEventPotionMain.GetOrDefault(potionID); var data = GContext.container.Resolve().ChainPackWithProgressMigrationData; if (fishingEvent.ID != data.EventId) data.UpdateData(fishingEvent.ID, potion.PackId); } } public void GetDataFromServer(string json) { if (json.IsNullOrEmpty()) { GameDebug.LogWarning("EventPotion get data from server has encountered empty data."); return; } try { m_PotionData = Newtonsoft.Json.JsonConvert.DeserializeObject(json); } catch (Exception e) { GameDebug.LogWarning("EventPotion deserialize data has failed. Exception: " + e.ToString()); return; } if (!Init()) return; } public TimeSpan RemainingTime { get => DateTime.Parse((m_Tables.TbFishingEvent[m_PotionData.EventID].TimeDefinition as LimitedTime).EndTime) - ZZTimeHelper.UtcNow(); } public int FixPotionData() { if (m_PotionData == null) return 0; // 获取当前开放并符合解锁条件的魔药活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 5, m_IsCheckCondition); if (fishingEvent == null) return ClearPotionData(); int eventID = fishingEvent.ID; int potionID = fishingEvent.RedirectID; if (potionID < 0) return ClearPotionData(); if (eventID != m_PotionData.EventID || potionID != m_PotionData.PotionID) return ClearPotionData(); return 0; } public int ClearPotionData() { if (m_PotionData == null) return 0; m_PotionData.Clear(); m_PotionData = null; return 0; } public void SyncData() { if (!m_IsSyncData) return; PlayFabMgr.Instance.UpdateUserDataValue(EventPotionConfig.SAVE_DATA_KEY, Newtonsoft.Json.JsonConvert.SerializeObject(m_PotionData)); } public PotionItemData GetOperationItemData() { foreach (var potionItemData in m_PotionData.LevelProcessedList) { if (potionItemData.DataType == EventPotionDataType.Data_ItemAndOperation) { return potionItemData; } } return null; } #endregion #region UI 设置相关 private UIType m_UIPanel = null, m_InfoPanel = null; public void ClearUIData() { m_UIPanel = null; m_InfoPanel = null; } public void InitUIData() { ClearUIData(); GetUIPanel(); GetInfoPanel(); } public UIType GetUIPanel() { if (m_UIPanel == null) { var data = GetEventPotionMain(m_PotionData.PotionID); m_UIPanel = new UIType(data.UIPanel); } return m_UIPanel; } public UIType GetInfoPanel() { if (m_InfoPanel == null) { var data = GetEventPotionMain(m_PotionData.PotionID); m_InfoPanel = new UIType(data.InfoPanel); } return m_InfoPanel; } #endregion #region 本地存储数据相关 public string GetPotionIDSaveKey(string key) { if (m_PotionData == null) return key; return m_PotionData.EventID + "-" + m_PotionData.PotionID + "-" + key; } public bool HasPotionIDSaveKey(string key) { return PlayerPrefs.HasKey(GetPotionIDSaveKey(key)); } public void SavePotionIDData(string key, string value) { PlayerPrefs.SetString(GetPotionIDSaveKey(key), value); PlayerPrefs.Save(); } public void ClearPostionIDData(string key) { PlayerPrefs.DeleteKey(GetPotionIDSaveKey(key)); } #endregion #region 展示区相关 public void ResetDisplayAreaGridState(EventPotionGridState gridState) { if (m_PotionData == null || m_PotionData.LevelDisplayData == null) return; int count = m_PotionData.LevelDisplayData.GridStateList.Count; for (int i = 0; i < count; ++i) { m_PotionData.LevelDisplayData.GridStateList[i] = gridState; } } #endregion #region 操作数据相关 private List m_ScrollerDataList = new List(); public List ScrollerDataList { get { return m_ScrollerDataList; } } public void InitScrollerDataList() { m_ScrollerDataList.Clear(); RefreshScrollerDataList(); } private void RefreshScrollerDataList() { m_ScrollerDataList.Clear(); // 上方墙壁 int count = m_WallsBefore.Count; foreach (var item in m_WallsBefore) { m_ScrollerDataList.Add(item); } // 操作数据 count = m_PotionData.LevelProcessedList.Count; foreach (var item in m_PotionData.LevelProcessedList) { m_ScrollerDataList.Add(item); } // 下方墙壁 count = m_WallsAfter.Count; foreach (var item in m_WallsAfter) { m_ScrollerDataList.Add(item); } } #endregion #region 关卡相关 public int GetPreLevelCountInCurRound() { if (m_PotionData.PrePotionID < 0) return -1; EventPotionMain potion = m_TbEventPotionMain.GetOrDefault(m_PotionData.PrePotionID); if (m_PotionData.PreStageID == 1) { return potion.StageList1.Count; } return potion.StageList2.Count; } public int GetLevelCountInCurRound() { if (m_PotionData.StageID == 1) return m_PotionData.Stage1TotalLevel; return m_PotionData.Stage2TotalLevel; } public EventPotionStage GetLastStageInCurRound() { if (m_PotionData == null) return null; EventPotionMain potion = m_TbEventPotionMain.GetOrDefault(m_PotionData.PotionID); if (potion == null) return null; int levelID = 0; if (m_PotionData.StageID == 1) levelID = potion.StageList1[m_PotionData.Stage1TotalLevel - 1]; else levelID = potion.StageList2[m_PotionData.Stage2TotalLevel - 1]; return m_TbEventPotionStage.GetOrDefault(levelID); } public EventPotionStage GetFreshPotionLevel() { // 获取当前开放并符合解锁条件的魔药活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 5, m_IsCheckCondition); if (fishingEvent == null) return null; int eventID = fishingEvent.ID; int potionID = fishingEvent.RedirectID; if (potionID < 0) return null; // 获取当前魔药活动数据 EventPotionMain potion = m_TbEventPotionMain.GetOrDefault(potionID); if (potion == null) return null; // 获取魔药数据 int stage1Count = potion.StageList1.Count; if (stage1Count == 0) return null; int stage2Count = potion.StageList2.Count; int stageID = 1; int levelIndex = 0; int levelID = potion.StageList1[levelIndex]; EventPotionStage level = m_TbEventPotionStage.GetOrDefault(levelID); if (level == null) return null; int token = m_DefaultToken; if (m_PotionData != null) m_PotionData.ClearAllLocalData(); token = GContext.container.Resolve().GetInitWelcomeGift(eventID); m_PotionData = new EventPotionData(); m_PotionData.EventID = eventID; m_PotionData.PotionID = potionID; m_PotionData.StageID = stageID; m_PotionData.Stage1TotalLevel = stage1Count; m_PotionData.Stage2TotalLevel = stage2Count; m_PotionData.LevelID = levelID; m_PotionData.LevelIndex = levelIndex; m_PotionData.Round = stageID; m_PotionData.LevelTargetData.Clear(); m_PotionData.LevelProcessedList.Clear(); m_PotionData.LevelDisplayData.Clear(); m_PotionData.IsOver = false; m_PotionData.Token = token; m_PotionData.ConsumedToken = 0; m_PotionData.CorrectNum = 0; m_PotionData.SubmitNum = 0; m_PotionData.Rule = 0; m_PotionData.PreEventID = -1; m_PotionData.PrePotionID = -1; m_PotionData.PreStageID = -1; m_PotionData.PreLevelID = -1; m_PotionData.Name = "GetFreshPotionLevel"; m_PotionData.LevelTargetData.name = m_PotionData.Name + "_Target_" + DateTime.Now.Ticks; m_PotionData.LevelDisplayData.name = m_PotionData.Name + "_Display_" + DateTime.Now.Ticks; GContext.container.Resolve().SaveTransitionData(m_PotionData.EventID, m_PotionData.Token); ProcessSorting(level); foreach (var item in potion.GuidanceGroupList) { GContext.container.Resolve().ClearGuide(item); } ClearPostionIDData(EventPotionConfig.key_first_check_wrong); ClearPostionIDData(EventPotionConfig.key_first_check_combination_wrong); // 同步数据 SyncData(); return level; } public void GoToPotionLevel(int levelID) { // 获取当前开放并符合解锁条件的魔药活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 5, m_IsCheckCondition); if (fishingEvent == null) { Debug.LogWarning("=== 找不到 魔药 对应的 type=4 & subType=5,且符合条件的活动。"); return; } int eventID = fishingEvent.ID; int potionID = fishingEvent.RedirectID; if (potionID < 0) { Debug.LogWarning("=== 魔药ID出错,活动ID为:" + eventID + ", 魔药ID:" + potionID); return; } // 获取当前魔药活动数据 EventPotionMain potion = m_TbEventPotionMain.GetOrDefault(potionID); if (potion == null) { Debug.LogWarning("=== 魔药找不到数据,potionID:" + potionID); return; } int stageID = 0; if (potion.StageList1.IndexOf(levelID) >= 0) { stageID = 1; } else if (potion.StageList2.IndexOf(levelID) >= 0) { stageID = 2; } else { Debug.LogWarning("=== 魔药在 EventPotionMain StageList1 和 StageList2 里找不到数据,potionID:" + potionID); return; } List levelList = null; if (stageID == 1) levelList = potion.StageList1; else levelList = potion.StageList2; int levelIndex = 0; int count = levelList.Count; for (int i = 0; i < count; i++) { if (levelList[i] == levelID) { levelIndex = i; break; } } int token = m_DefaultToken; if (m_PotionData != null) m_PotionData.ClearAllLocalData(); token = GContext.container.Resolve().GetInitWelcomeGift(eventID); m_PotionData = new EventPotionData(); m_PotionData.EventID = eventID; m_PotionData.PotionID = potionID; m_PotionData.StageID = stageID; m_PotionData.Stage1TotalLevel = potion.StageList1.Count; m_PotionData.Stage2TotalLevel = potion.StageList2.Count; m_PotionData.LevelID = levelID; m_PotionData.LevelIndex = levelIndex; m_PotionData.Round = stageID; m_PotionData.LevelTargetData.Clear(); m_PotionData.LevelProcessedList.Clear(); m_PotionData.LevelDisplayData.Clear(); m_PotionData.IsOver = false; m_PotionData.Token = token; m_PotionData.ConsumedToken = 0; m_PotionData.CorrectNum = 0; m_PotionData.SubmitNum = 0; m_PotionData.Rule = 0; m_PotionData.PreEventID = -1; m_PotionData.PrePotionID = -1; m_PotionData.PreStageID = -1; m_PotionData.PreLevelID = -1; m_PotionData.Name = "GoToPotionLevel"; m_PotionData.LevelTargetData.name = m_PotionData.Name + "_Target_" + DateTime.Now.Ticks; m_PotionData.LevelDisplayData.name = m_PotionData.Name + "_Display_" + DateTime.Now.Ticks; EventPotionStage level = m_TbEventPotionStage.GetOrDefault(m_PotionData.LevelID); ProcessSorting(level); // 同步数据 SyncData(); } public bool HaveNextPotionLevel() { //if (m_PotionData == null) return false; //if (m_PotionData.StageID == 1) return true; //if (m_PotionData.LevelIndex < m_PotionData.Stage2TotalLevel - 1) return true; return true; } public EventPotionStage GetNextPotionLevel() { // 获取当前开放并符合解锁条件的魔药活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 5, m_IsCheckCondition); if (fishingEvent == null) return null; int eventID = fishingEvent.ID; int potionID = fishingEvent.RedirectID; if (potionID < 0) return null; if (potionID != m_PotionData.PotionID) return GetFreshPotionLevel(); // 获取当前魔药活动数据 EventPotionMain potion = m_TbEventPotionMain.GetOrDefault(potionID); if (potion == null) return null; bool haveNextPotionLevel = HaveNextPotionLevel(); if (haveNextPotionLevel == false) return null; // 所有活动结束 if (m_PotionData.StageID == 1) { if (m_PotionData.LevelIndex < m_PotionData.Stage1TotalLevel - 1) { m_PotionData.LevelIndex += 1; } else { m_PotionData.StageID = 2; m_PotionData.LevelIndex = 0; m_PotionData.Round++; } } else { if (m_PotionData.LevelIndex < m_PotionData.Stage2TotalLevel - 1) { m_PotionData.LevelIndex += 1; } else { m_PotionData.StageID = 2; m_PotionData.LevelIndex = 0; m_PotionData.Round++; } } int stageID = m_PotionData.StageID; int levelIndex = m_PotionData.LevelIndex; // 获取魔药数据 int stage1Count = potion.StageList1.Count; if (stage1Count == 0) return null; int stage2Count = potion.StageList2.Count; if (stage2Count == 0) return null; int levelID = 0; if (stageID == 1) { levelID = potion.StageList1[levelIndex]; } else { levelID = potion.StageList2[levelIndex]; } EventPotionStage level = m_TbEventPotionStage.GetOrDefault(levelID); if (level == null) return null; m_PotionData.PreEventID = m_PotionData.EventID; m_PotionData.PrePotionID = m_PotionData.PotionID; m_PotionData.PreStageID = m_PotionData.StageID; m_PotionData.PreLevelID = m_PotionData.LevelID; m_PotionData.EventID = eventID; m_PotionData.PotionID = potionID; m_PotionData.StageID = stageID; m_PotionData.Stage1TotalLevel = stage1Count; m_PotionData.Stage2TotalLevel = stage2Count; m_PotionData.LevelID = levelID; m_PotionData.LevelIndex = levelIndex; m_PotionData.LevelTargetData.Clear(); m_PotionData.LevelProcessedList.Clear(); m_PotionData.LevelDisplayData.Clear(); m_PotionData.IsOver = false; m_PotionData.ConsumedToken = 0; m_PotionData.CorrectNum = 0; m_PotionData.SubmitNum = 0; m_PotionData.Rule = 0; ProcessSorting(level); // 同步数据 SyncData(); return level; } public EventPotionStage GetCurrentPotionLevel() { // 获取当前开放并符合解锁条件的魔药活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 5, m_IsCheckCondition); if (fishingEvent == null) return null; int eventID = fishingEvent.ID; int potionID = fishingEvent.RedirectID; if (potionID < 0) return null; if (eventID != m_PotionData.EventID || potionID != m_PotionData.PotionID) return GetFreshPotionLevel(); return m_TbEventPotionStage.GetOrDefault(m_PotionData.LevelID); } #endregion #region 红点相关 public bool IsRedDotDisplayed() { if (m_PotionData == null) return false; return m_PotionData.Token > 0; } #endregion #region 代币相关 public void AddToken(int count) { if (m_PotionData != null) { m_PotionData.Token += count; } else { GetFreshPotionLevel(); if (m_PotionData == null) return; m_PotionData.Token += count; m_PotionData.Name = "AddToken"; m_PotionData.LevelTargetData.name = m_PotionData.Name + "_Target_" + DateTime.Now.Ticks; m_PotionData.LevelDisplayData.name = m_PotionData.Name + "_Display_" + DateTime.Now.Ticks; } if (m_PotionData.Token > 0) RedPointManager.Instance.SetRedPointState(RedPointName.Home_Potion, true); else RedPointManager.Instance.SetRedPointState(RedPointName.Home_Potion, false); FishingPotionAct.Publish(new EventPotionTokenUpdateData()); GContext.container.Resolve().SaveTransitionData(m_PotionData.EventID, m_PotionData.Token); SyncData(); } public int GetToken() { if (m_PotionData != null) return m_PotionData.Token; return 0; } public bool ConsumeTokens(int count) { bool isEnough = IsEnough(count); if (isEnough == false) return false; m_PotionData.ConsumedToken += count; AddToken(-count); return isEnough; } public bool IsEnough(int spend) { if (m_PotionData == null) return false; return (m_PotionData.Token - spend) >= 0; } #endregion #region 工具方法 public void ShuffleList(List list, bool isSkip = false) { if (isSkip == true) return; int count = list.Count; for (int i = count - 1; i >= 0; i--) { int randomIndex = UnityEngine.Random.Range(0, i + 1); int temp = list[randomIndex]; list[randomIndex] = list[i]; list[i] = temp; } } #endregion } public class EventPotionData { private const string LocalKey = "epdlk"; // EventPotionData LocalKey, 首字母 public int EventID { get; set; } public int PotionID { get; set; } public int StageID { get; set; } public int Stage1TotalLevel { get; set; } public int Stage2TotalLevel { get; set; } public int LevelID { get; set; } public int LevelIndex { get; set; } public int CorrectNum { get; set; } public int SubmitNum { get; set; } public PotionItemData LevelTargetData { get; set; } // 每一关上方的目标颜色排序 public List LevelProcessedList { get; set; } // 每一关的操作记录 public PotionItemData LevelDisplayData { get; set; } // 每一关下方的花瓶颜色排序 public bool IsOver { get; set; } public int Token { get; set; } public int ConsumedToken { get; set; } public string Name { get; set; } public int Rule { get; set; } public int PreEventID { get; set; } public int PrePotionID { get; set; } public int PreStageID { get; set; } public int PreLevelID { get; set; } public int Round { get; set; } public EventPotionData() { EventID = -1; PotionID = -1; StageID = -1; Stage1TotalLevel = -1; Stage2TotalLevel = -1; LevelID = -1; LevelIndex = -1; Round = -1; LevelTargetData = new PotionItemData(); LevelProcessedList = new List(); LevelDisplayData = new PotionItemData(); IsOver = false; Token = 0; ConsumedToken = 0; CorrectNum = 0; SubmitNum = 0; Rule = 0; PreEventID = -1; PrePotionID = -1; PreStageID = -1; PreLevelID = -1; } public void Clear() { LevelTargetData.Clear(); LevelProcessedList.Clear(); LevelDisplayData.Clear(); } public void ClearAllLocalData() { //if (PlayerPrefs.HasKey(LocalKey) == false) return; //PlayerPrefs.DeleteKey(LocalKey); } } public struct EventPotionResource { public int TypeIndex { get; set; } public int TypeCount { get; set; } public string ItemResName { get; set; } public string PaintResName { get; set; } } public class PotionItemData { public string name; public int index; public bool isOver = true; public EventPotionDataType DataType = EventPotionDataType.Data_Wall; public List ColorTypeList = new List(); // -1:已使用; 0:空; <100:sp_potion_1001; >100:sp_paint_1001 public List ShelfTypeList = new List(); public List GridStateList = new List(); public List FromDisplayAreaIndexList = new List(); public int GetColorType(int index) { return ColorTypeList[index]; } public void SetColorType(int index, int type) { ColorTypeList[index] = type; } public EventPotionGridState GetGridState(int index) { return GridStateList[index]; } public void SetGridState(int index, EventPotionGridState state) { GridStateList[index] = state; } public int GetFromDisplayAreaIndex(int index) { return FromDisplayAreaIndexList[index]; } public void SetFromDisplayAreaIndex(int index, int value) { FromDisplayAreaIndexList[index] = value; } public void ResetFromDisplayAreaIndexList(int value) { int count = FromDisplayAreaIndexList.Count; for (int i = 0; i < count; ++i) { FromDisplayAreaIndexList[i] = value; } } public int GetValidColorTypeNum() { int num = 0; int count = ColorTypeList.Count; for (int i = 0; i < count; ++i) { if (ColorTypeList[i] > 0) { num++; } } return num; } public void Log(string pre) { //Debug.LogError("--- PotionItemData " + pre + ", type:"+ DataType // +", colorList:"+ Newtonsoft.Json.JsonConvert.SerializeObject(ColorTypeList) // + ", fromList:"+ Newtonsoft.Json.JsonConvert.SerializeObject(FromDisplayAreaIndexList) // + ", stateList:"+ Newtonsoft.Json.JsonConvert.SerializeObject(GridStateList)); } public void Clear() { index = -1; isOver = true; ColorTypeList.Clear(); ShelfTypeList.Clear(); GridStateList.Clear(); FromDisplayAreaIndexList.Clear(); } } public class EventPotionConfig { public const string AtlasName = "event_potion_atlas"; public const string SAVE_DATA_KEY = "epdk"; // event potion data key public const int PotionItemType_Used = -1; // 已使用 public const int PotionItemType_Empty = 0; // 空 public const string spine_potion_idle = "potion_idle"; // spine 动画 public const string spine_potion_shake_01_2 = "potion_shake_01_2"; // spine 动画 public const string spine_potion_shake_01_1 = "potion_shake_01_1"; // spine 动画 public const string spine_potion_shake_02 = "potion_shake_02"; // spine 动画 public const string spine_potion_shake_03 = "potion_shake_03"; // spine 动画 public const string spine_potion_pour_L = "potion_pour_L"; // spine 动画 public const string spine_potion_pour_R = "potion_pour_R"; // spine 动画 public const string spine_potion_fly = "potion_fly"; // spine 动画 public const string anim_gild_show = "gild_show"; // animation 动画 public const string anim_gild_jump = "gild_jump"; // animation 动画 public const string anim_gild_jump_down = "gild_jump_down"; // animation 动画 public const string anim_gild_success = "gild_success"; // animation 动画 public const string anim_gild_wrong = "gild_wrong"; // animation 动画 public const string anim_gild_settlement = "gild_settlement"; // animation 动画 public const string anim_gild_gap = "gild_gap"; // animation 动画 public const int Token_Consume_Num = 1; // 每次消耗代币数量 public const int Token_Return_Num = -1; // 每次返还代币数量 public const string key_first_check_wrong = "potion_kfcw"; // 第一次检测错误,本地存储数据key public const string key_first_check_combination_wrong = "potion_kfccw"; // 第一次组合检测错误,本地存储数据key } public enum EventPotionDataType { Data_Wall, // 墙壁 Data_Item, // 数据 Data_ItemAndOperation // 数据并添加到操作区域 } public enum EventPotionShelfType { Shelf_Default, // 置物架默认状态 Shelf_Success // 置物架成功状态 } public enum EventPotionGridState { GridState_Invalid, // 无效 GridState_Empty, // 空 GridState_Occupied, // 占用中 GridState_Ok, // 对比成功 GridState_Wrong, // 对比失败 GridState_Over, // 已结束 } public class EventPotionTokenUpdateData { } public class EventPotionCloseCameraData { } }