using asap.core; using Castle.Core.Internal; using cfg; using GameCore; using System; using System.Collections; using System.Collections.Generic; using Unity.Mathematics; using UnityEngine; namespace game { public class EventWashingData { private const string LocalKeySetKey = "ewdlksk"; // EventWashingData LocalKeySetKey, 首字母 public int EventID { get; set; } public int WashingID { get; set; } public int StageID { get; set; } public int StageIndex { get; set; } public int StageCount { get; set; } public int StepID { get; set; } public int StepIndex { get; set; } public int StepCount { get; set; } public List StepItems { get; set; } public int StepEnergyConsume { get; set; } public float Percent { get; set; } // 数值范围:0-1 public float OmitPercent { get; set; } public float Energy { get; set; } // 数值范围:0-1 public float EnergyConsumptionSpeed { get; set; } public bool IsOver { get; set; } public int Token { get; set; } public int ConsumedToken { get; set; } public bool IsAllGameOver { get; set; } public EventWashingData() { EventID = -1; WashingID = -1; StageID = -1; StepID = -1; Percent = 0; IsOver = false; Token = 0; StepItems = new List(); IsAllGameOver = false; Token = 0; } public void Clear() { ClearAllLocalData(); StepItems.Clear(); StepItems = null; } public void AddLocalDataKey(string group, string key) { LocalStepData stepData = null; List list = null; if (PlayerPrefs.HasKey(LocalKeySetKey) == true) { string data = PlayerPrefs.GetString(LocalKeySetKey); try { stepData = Newtonsoft.Json.JsonConvert.DeserializeObject(data); } catch (Exception e) { Debug.LogError(e.Message); stepData = new LocalStepData(); } if (stepData.LocalDatas.TryGetValue(group, out list)) { list.Add(key); } else { list = new List(); list.Add(key); stepData.LocalDatas.Add(group, list); } PlayerPrefs.SetString(LocalKeySetKey, Newtonsoft.Json.JsonConvert.SerializeObject(stepData)); } else { stepData = new LocalStepData(); list = new List(); list.Add(key); stepData.LocalDatas.Add(group, list); PlayerPrefs.SetString(LocalKeySetKey, Newtonsoft.Json.JsonConvert.SerializeObject(stepData)); } PlayerPrefs.Save(); } public void ClearLocalDataExceptGroup(string group) { if (PlayerPrefs.HasKey(LocalKeySetKey) == false) return; string data = PlayerPrefs.GetString(LocalKeySetKey); LocalStepData stepData = null; try { stepData = Newtonsoft.Json.JsonConvert.DeserializeObject(data); } catch (Exception e) { Debug.LogError(e.Message); return; } int count = 0; foreach (var item in stepData.LocalDatas) { if (string.IsNullOrEmpty(item.Key) == false && item.Key.Equals(group) == false) { var list = item.Value; count = list.Count; for (int i = 0; i < count; ++i) { if (PlayerPrefs.HasKey(list[i]) == true) PlayerPrefs.DeleteKey(list[i]); } } } PlayerPrefs.Save(); } public void ClearAllLocalData() { if (PlayerPrefs.HasKey(LocalKeySetKey) == false) return; string data = PlayerPrefs.GetString(LocalKeySetKey); LocalStepData stepData = null; try { stepData = Newtonsoft.Json.JsonConvert.DeserializeObject(data); } catch (Exception e) { Debug.LogError(e.Message); return; } int count = 0; foreach (var item in stepData.LocalDatas) { if (string.IsNullOrEmpty(item.Key) == false) { var list = item.Value; count = list.Count; for (int i = 0; i < count; ++i) { if (PlayerPrefs.HasKey(list[i]) == true) PlayerPrefs.DeleteKey(list[i]); } } } PlayerPrefs.Save(); } } public class LocalStepData { public Dictionary> LocalDatas = new Dictionary>(); } public class EventWashingDataManager { private Tables m_Tables; private TbEventWashing m_TbEventWashing; private TbEventWashingStage m_TbEventWashingStage; private TbEventWashingStep m_TbEventWashingStep; private EventWashing m_EventWashing = null; private EventWashingData m_WashingData = null; #if UNITY_EDITOR private bool m_IsCheckCondition = true; private int m_DefaultToken = 0; #else private bool m_IsCheckCondition = true; private int m_DefaultToken = 0; #endif public EventWashingData WashingData { get => m_WashingData; } private UIType m_InfoPopupPanel = null; public UIType GetCurrentInfoPopupPanel() { if (m_InfoPopupPanel == null) { var data = GetCurrentEventWashing(); m_InfoPopupPanel = new UIType(data.PanelID); } return m_InfoPopupPanel; } #region 数据处理相关 /// /// 初始化 /// public bool Init() { m_Tables = GContext.container.Resolve(); if (m_Tables == null) return false; m_TbEventWashing = m_Tables.TbEventWashing; m_TbEventWashingStage = m_Tables.TbEventWashingStage; m_TbEventWashingStep = m_Tables.TbEventWashingStep; return true; } public bool InitGameData() { if (m_WashingData == null) GetFreshEventStep(); GetCurrentEventStep(); return true; } public void ClearActData() { m_InfoPopupPanel = null; } public EventWashing GetCurrentEventWashing() { if (m_WashingData == null) return null; EventWashing washing = m_TbEventWashing.GetOrDefault(m_WashingData.WashingID); return washing; } public void ActivateEvent(FishingEvent fishingEvent) { if (fishingEvent == null) return; if (!Init()) return; if (m_WashingData == null) GetFreshEventStep(); else FixWashingData(); } /// /// 从服务器获取数据 /// public void GetDataFromServer(string json) { if (json.IsNullOrEmpty()) { GameDebug.LogError("EventWashing get data from server has encountered empty data."); return; } try { m_WashingData = Newtonsoft.Json.JsonConvert.DeserializeObject(json); } catch (Exception e) { GameDebug.LogError("EventWashing deserialize data has failed. Exception: " + e.ToString()); return; } if (!Init()) return; //FixWashingData(); } public void SyncData() { PlayFabMgr.Instance.UpdateUserDataValue(EventWashingConfig.SAVE_DATA_KEY, Newtonsoft.Json.JsonConvert.SerializeObject(m_WashingData)); } public string GetShipName() { if (m_WashingData == null) return ""; int stageID = m_WashingData.StageID; EventWashingStage stage = m_TbEventWashingStage.GetOrDefault(stageID); if (stage == null) return ""; return LocalizationMgr.GetText(stage.Name_l10n_key); } public TimeSpan RemainingTime { get => DateTime.Parse((m_Tables.TbFishingEvent[m_WashingData.EventID].TimeDefinition as LimitedTime).EndTime) - ZZTimeHelper.UtcNow(); } public int FixWashingData() { if (m_WashingData == null) return 0; // 获取当前开放并符合解锁条件的洗游艇活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 4, m_IsCheckCondition); if (fishingEvent == null) return ClearWashingData(); int eventID = fishingEvent.ID; int washingID = fishingEvent.RedirectID; if (washingID < 0) return ClearWashingData(); if (eventID != m_WashingData.EventID || washingID != m_WashingData.WashingID) return ClearWashingData(); return 0; } private int ClearWashingData() { if (m_WashingData == null) return 0; m_WashingData.Clear(); m_WashingData = null; return 0; } public void GoToStep(int stageID, int stepID) { // 获取当前开放并符合解锁条件的洗游艇活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 4, m_IsCheckCondition); if (fishingEvent == null) { Debug.LogError("=== 找不到 洗游艇 对应的 type=4 & subType=4,且符合条件的活动。"); return; } int eventID = fishingEvent.ID; int washingID = fishingEvent.RedirectID; if (washingID < 0) { Debug.LogError("=== 洗游艇ID出错,活动ID为:" + eventID + ", 洗游艇ID:" + washingID); return; } // 获取当前洗游艇活动数据 EventWashing washing = m_TbEventWashing.GetOrDefault(washingID); if (washing == null) { Debug.LogError("=== 洗游艇找不到游艇数据,washingID:" + washingID); return; } // 获取游艇数据 int stageCount = washing.StageList.Count; if (stageCount == 0) { Debug.LogError("=== 洗游艇对应StageList出错,washingID:" + washingID); return; } int stageIndex = -1; for (int i = 0; i < stageCount; i++) { if (washing.StageList[i] == stageID) { stageIndex = i; break; } } if (stageIndex < 0) { Debug.LogError("=== 洗游艇stageID找不到对应数据,stageID:" + stageID); return; } EventWashingStage stage = m_TbEventWashingStage.GetOrDefault(stageID); if (stage == null) { Debug.LogError("=== 洗游艇stepID找不到对应数据,stageID:" + stageID); return; } // 获取步骤数据 int stepCount = stage.StepList.Count; if (stepCount == 0) { Debug.LogError("=== 洗游艇StepList出错,StageID:" + stageID); return; } int stepIndex = 0; for (int i = 0; i < stepCount; i++) { if (stage.StepList[i] == stepID) { stepIndex = i; break; } } if (stepIndex < 0) { Debug.LogError("=== 洗游艇stepID找不到对应数据,stepID:" + stepID); return; } EventWashingStep eventWashingStep = m_TbEventWashingStep.GetOrDefault(stepID); int token = 0; if (m_WashingData != null) token = m_WashingData.Token; m_WashingData = new EventWashingData(); m_WashingData.EventID = eventID; m_WashingData.WashingID = washingID; m_WashingData.StageID = stageID; m_WashingData.StageIndex = stageIndex; m_WashingData.StageCount = stageCount; m_WashingData.StepID = stepID; m_WashingData.StepIndex = stepIndex; m_WashingData.StepCount = stepCount; m_WashingData.StepItems.Clear(); m_WashingData.StepEnergyConsume = eventWashingStep.EnergyConsume; m_WashingData.Percent = 0f; m_WashingData.OmitPercent = 0f; m_WashingData.IsOver = false; m_WashingData.Token = token; // 同步数据 SyncData(); } public void ResetRefreshData() { ClearWashingData(); //if (m_WashingData != null) //{ // m_WashingData.Token = 0; // m_WashingData.ClearAllLocalData(); //} //GetFreshEventStep(); } public bool IsCurrentStepID(int stepID) { if (m_WashingData == null) return false; return m_WashingData.StepID == stepID; } //public string GetBgm() //{ // // 获取当前开放并符合解锁条件的洗游艇活动ID // FishingEventData fishingEventData = GContext.container.Resolve(); // FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 4, m_IsCheckCondition); // if (fishingEvent == null) return null; // int eventID = fishingEvent.ID; // int washingID = fishingEvent.RedirectID; // if (washingID < 0) return null; // // 获取当前洗游艇活动数据 // EventWashing washing = m_TbEventWashing.GetOrDefault(washingID); // return washing.Bgm; //} #endregion #region 红点相关 public bool IsRedDotDisplayed() { if (m_WashingData == null) return false; return m_WashingData.Token > 0 && m_WashingData.IsAllGameOver == false; } #endregion #region 代币相关 /// /// 消费token /// /// /// public bool ConsumeTokens(int count) { bool isEnough = IsEnough(count); if (isEnough == false) return false; m_WashingData.ConsumedToken += count; AddToken(-count); #if AGG using (var e = GEvent.GameEvent("event_washing")) { e.AddContent("step_id", m_WashingData.StepID) .AddContent("independent_id", m_WashingData.ConsumedToken); } #endif return isEnough; } public void AddToken(int count) { if (m_WashingData != null) { m_WashingData.Token += count; } else { //m_WashingData = new EventWashingData(); //m_WashingData.Token = count; GetFreshEventStep(); if (m_WashingData == null) return; m_WashingData.Token += count; } if (m_WashingData.Token > 0) RedPointManager.Instance.SetRedPointState(RedPointName.Home_Washing, true); else RedPointManager.Instance.SetRedPointState(RedPointName.Home_Washing, false); FishingYachtAct.Publish(new EventWashingTokenUpdateData()); GContext.container.Resolve().SaveTransitionData(m_WashingData.EventID, m_WashingData.Token); SyncData(); } public int GetToken() { if (m_WashingData != null ) return m_WashingData.Token; return 0; } public bool IsEnough(int spend) { if (m_WashingData == null) return false; return (m_WashingData.Token - spend) >= 0; } public void SetEnergy(float energy, float speed) { if (m_WashingData == null) return; if (energy < 0) m_WashingData.Energy = 0f; else if (energy > 1f) m_WashingData.Energy = 1f; else m_WashingData.Energy = energy; if (speed < 0f) m_WashingData.EnergyConsumptionSpeed = 0f; else m_WashingData.EnergyConsumptionSpeed = speed; } public void UpdateToken(float deltaTime) { if (m_WashingData != null) { float step = 1f / m_WashingData.StepEnergyConsume; float nextEnergy = 1f - math.frac(m_WashingData.Percent / step); if (m_WashingData.Energy < nextEnergy) m_WashingData.Energy = 0f; if (m_WashingData.Energy > 0) m_WashingData.Energy = nextEnergy; if (m_WashingData.Energy < 0) m_WashingData.Energy = 0; } } public bool IsUpdateTokenOver() { if (m_WashingData == null) return true; float step = 1f / m_WashingData.StepEnergyConsume; int belongStep = (int)math.floor(m_WashingData.Percent / step) + 1; if (belongStep > m_WashingData.StepEnergyConsume) m_WashingData.IsOver = true; if (belongStep > m_WashingData.ConsumedToken && belongStep <= m_WashingData.StepEnergyConsume) return true; return false; } public bool IsGameOver() { if (m_WashingData == null) return true; return m_WashingData.IsOver; } #endregion #region 游戏步骤相关 public string GetPreLoadStepResName() { if (WashingData == null) { GetFreshEventStep(); } else { GetCurrentEventStep(); } string res = string.Format("GC_{0:D}_{1:D}_{2:D}", m_WashingData.WashingID, m_WashingData.StageID, m_WashingData.StepID); return res; } public EventWashingStep GetFreshEventStep(bool isSyncData = true) { // 获取当前开放并符合解锁条件的洗游艇活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 4, m_IsCheckCondition); if (fishingEvent == null) return null; int eventID = fishingEvent.ID; int washingID = fishingEvent.RedirectID; if (washingID < 0) return null; // 获取当前洗游艇活动数据 EventWashing washing = m_TbEventWashing.GetOrDefault(washingID); if (washing == null) return null; // 获取游艇数据 int stageCount = washing.StageList.Count; if (stageCount == 0) return null; int stageIndex = 0; int stageID = washing.StageList[stageIndex]; EventWashingStage stage = m_TbEventWashingStage.GetOrDefault(stageID); if (stage == null) return null; // 获取步骤数据 int stepCount = stage.StepList.Count; if (stepCount == 0) return null; int stepIndex = 0; int stepID = stage.StepList[stepIndex]; // 步骤ID EventWashingStep eventWashingStep = m_TbEventWashingStep.GetOrDefault(stepID); int token = m_DefaultToken; if (m_WashingData != null) m_WashingData.ClearAllLocalData(); token = GContext.container.Resolve().GetInitWelcomeGift(eventID); m_WashingData = new EventWashingData(); m_WashingData.EventID = eventID; m_WashingData.WashingID = washingID; m_WashingData.StageID = stageID; m_WashingData.StageIndex = stageIndex; m_WashingData.StageCount = stageCount; m_WashingData.StepID = stepID; m_WashingData.StepIndex = stepIndex; m_WashingData.StepCount = stepCount; m_WashingData.StepItems.Clear(); m_WashingData.StepEnergyConsume = eventWashingStep.EnergyConsume; m_WashingData.Percent = 0f; m_WashingData.OmitPercent = 0f; m_WashingData.IsOver = false; m_WashingData.Token = token; m_WashingData.ConsumedToken = 0; m_WashingData.IsAllGameOver = false; GContext.container.Resolve().SaveTransitionData(m_WashingData.EventID, m_WashingData.Token); // 同步数据 if (isSyncData == true) SyncData(); return eventWashingStep; } public bool HavePreEventStep() { if (m_WashingData == null) return false; if (m_WashingData.StageIndex > 0) return true; if (m_WashingData.StepIndex > 0) return true; return false; } public bool HaveNextEventStep() { if (m_WashingData == null) return false; if (m_WashingData.StageIndex < m_WashingData.StageCount - 1) return true; if (m_WashingData.StepIndex < m_WashingData.StepCount - 1) return true; return false; } public EventWashingStep GetNextEventStep(bool isSyncData = true) { // 获取当前开放并符合解锁条件的洗游艇活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 4, m_IsCheckCondition); if (fishingEvent == null) return null; int eventID = fishingEvent.ID; int washingID = fishingEvent.RedirectID; if (washingID < 0) return null; if (washingID != m_WashingData.WashingID) return GetFreshEventStep(); // 获取当前洗游艇活动数据 EventWashing eventWashing = m_TbEventWashing.GetOrDefault(washingID); if (eventWashing == null) return null; bool haveNextStep = HaveNextEventStep(); if (haveNextStep == false) return null; // 所有活动结束 if (m_WashingData.StepIndex < m_WashingData.StepCount - 1) { m_WashingData.StepIndex += 1; } else { // 换船 m_WashingData.StageIndex += 1; m_WashingData.StepIndex = 0; } int stageIndex = m_WashingData.StageIndex; int stageCount = eventWashing.StageList.Count; int stageID = eventWashing.StageList[stageIndex]; EventWashingStage eventWashingStage = m_TbEventWashingStage.GetOrDefault(stageID); if (eventWashingStage == null) return null; int stepIndex = m_WashingData.StepIndex; int stepCount = eventWashingStage.StepList.Count; int stepID = eventWashingStage.StepList[stepIndex]; EventWashingStep eventWashingStep = m_TbEventWashingStep.GetOrDefault(stepID); m_WashingData.EventID = eventID; m_WashingData.WashingID = washingID; m_WashingData.StageID = stageID; m_WashingData.StageIndex = stageIndex; m_WashingData.StageCount = stageCount; m_WashingData.StepID = stepID; m_WashingData.StepIndex = stepIndex; m_WashingData.StepCount = stepCount; m_WashingData.StepItems.Clear(); m_WashingData.StepEnergyConsume = eventWashingStep.EnergyConsume; m_WashingData.Percent = 0f; m_WashingData.OmitPercent = 0f; m_WashingData.IsOver = false; m_WashingData.ConsumedToken = 0; // 同步数据 if (isSyncData == true) SyncData(); return eventWashingStep; } public EventWashingStep GetCurrentEventStep() { // 获取当前开放并符合解锁条件的洗游艇活动ID FishingEventData fishingEventData = GContext.container.Resolve(); FishingEvent fishingEvent = fishingEventData.GetEventByTypeAndSubType(4, 4, m_IsCheckCondition); if (fishingEvent == null) return null; int eventID = fishingEvent.ID; int washingID = fishingEvent.RedirectID; if (washingID < 0) return null; if (eventID != m_WashingData.EventID || washingID != m_WashingData.WashingID) return GetFreshEventStep(); return m_TbEventWashingStep.GetOrDefault(m_WashingData.StepID); } #endregion } }