using System; using System.Collections.Generic; using System.Linq; using asap.core; using cfg; using game; using LitJson; using UnityEngine; namespace GameCore { public class CampData { private cfg.Tables _tables; MMTService _MMService; public CampData(cfg.Tables tables, MMTService mmService) { this._tables = tables; _MMService = mmService; } #region SERVER_DATA int _id = 101; public int PreID; public int Id { set { _id = value; } get { return _id; } } public int AreaIndex { get; private set; } = 0; public int AreaStep { get; private set; } = 0; Dictionary CampDataDic = new Dictionary(); #endregion public int AreaId { get; private set; } = 0; public int AreaStepCount { get; private set; } public int TimelineId { get; private set; } public string Name { get { return _tables.TbConstruction.DataMap[Id].Name_l10n_key; } } public string AreaIcon { get; private set; } public List AllPrefabs { get { return _tables.TbConstruction.DataMap[Id].AreaList .Select(stepid => _tables.TbConstructionArea.DataMap[stepid].BuildingPrefeb) .Distinct() .ToList(); } } public string BasePrefab => _tables.TbConstruction.DataMap[Id].Prefab; public string CurrentPrefab { get { int index = AreaIndex; if (index >= AreaList.Count) index = AreaList.Count - 1; return _tables.TbConstructionArea.DataMap[AreaList[index]].BuildingPrefeb; } } public cfg.ConstructionArea GetAreaByIndex(int index) { if (index >= AreaList.Count) index = AreaList.Count - 1; return _tables.TbConstructionArea.DataMap[AreaList[index]]; } public string BGM { get { return _tables.TbConstruction.DataMap[Id].BGM; } } public int NextMapID { get { return _tables.TbConstruction.DataMap[Id].NextID; } } public cfg.ConstructionArea constructionArea { get; private set; } public cfg.ConstructionStep StepInfo { get; private set; } public List AreaList { get { return _tables.TbConstruction.DataMap[Id].AreaList; } } public List AreaStepList { get; private set; } public bool IsUpdataArea = false; string _isOpenSkyscraper = "0"; public bool IsOpenSkyscraper => _isOpenSkyscraper == "1" || IsCanSkyscraper; public bool IsCanSkyscraper { get { var dataMap = _tables.TbMapData.DataList[^1]; return GContext.container.Resolve().lv >= dataMap.LevelRequired; } } //public int curQRewardCount = 0; //是否需要修复 public bool IsFix => _isFix && AreaIndex < AreaList.Count; bool _isFix; public void InitData(Dictionary userData) { string value; if (userData.TryGetValue("IsFix", out value)) { SetIsFix(bool.Parse(value), false); } if (userData.TryGetValue("IsOpenSkyscraper", out value)) { _isOpenSkyscraper = value; } if (userData.TryGetValue("CampDataDic", out value)) { CampDataDic = JsonMapper.ToObject>(value); } } public void SetIsFix(bool isFix, bool isSave = true) { _isFix = isFix; if (isSave) { PlayFabMgr.Instance.UpdateUserDataValue("IsFix", _isFix.ToString()); } } public void SetCampAreaIndex(string id_string) { AreaIndex = GlobalUtils.TryParseInt(id_string); } public void SetCampAreaStep(string step_string) { AreaStep = GlobalUtils.TryParseInt(step_string); } void SetCampByLevel() { List _dataList = _tables.TbConstruction.DataList; if (!_tables.TbConstruction.DataMap.ContainsKey(Id)) { Debug.LogError($"[CampData]UpdateCurrentData: Not found data of id({Id})!"); Id = _dataList[^1].ID; } int level = GContext.container.Resolve().lv; int id = _dataList[0].ID; for (int i = _dataList.Count - 1; i >= 0; i--) { if (_dataList[i].InitialLevel <= level) { id = _dataList[i].ID; level -= _dataList[i].InitialLevel; break; } } AreaIndex = AreaList.Count; ConstructionArea _area = _tables.TbConstructionArea.DataMap[AreaList[^1]]; AreaStep = _area.StepCount; if (id % 1000 <= Id % 1000) { Id = id; AreaIndex = AreaList.Count; for (int i = 0; i < AreaList.Count; i++) { _area = _tables.TbConstructionArea.DataMap[AreaList[i]]; if (_area.StepCount > level) { AreaIndex = i; AreaStep = level; break; } level -= _area.StepCount; } } } public void Init() { if (CampDataDic.Count > 0) { Id = CampDataDic["CampId"]; } SetCampByLevel(); PreID = Id; if (_tables.TbConstruction.DataMap.ContainsKey(Id)) { if (AreaIndex >= AreaList.Count) { CompleteInit(); } else { UpdateCurrentData(); } } else { Debug.LogError($"[CampData]UpdateCurrentData: Not found data of id({Id})!"); } } public void ChangeNextCampID() { OnChangeCamp(NextMapID); } public void ChangeCampId(int id) { OnChangeCamp(id); } void OnChangeCamp(int id) { PreID = Id; if (_tables.TbConstruction.DataMap.TryGetValue(id, out var camp_data)) { constructionArea = null; Id = id; SetCampByLevel(); if (AreaIndex >= AreaList.Count) { CompleteInit(); } else { UpdateCurrentData(); } CampDataDic["CampId"] = Id; PlayFabMgr.Instance.UpdateUserDataValue("CampDataDic", JsonMapper.ToJson(CampDataDic)); ; } else { Debug.LogError($"[CampData]UpdateCurrentData: Not found data of id({id})!"); } } public void SaveChangeCamp() { SetCampID(); } async void SetCampID() { IUserService userService = GContext.container.Resolve(); SetCampIDRequest setCampIDRequest = new SetCampIDRequest() { playerID = userService.UserId, campID = Id }; var customServerMgr = GContext.container.Resolve(); string json = await customServerMgr.BombRequest("SetCampID", setCampIDRequest); if (!string.IsNullOrEmpty(json) && json != "[]") { SetCampIDResponse setCampIDResp = Newtonsoft.Json.JsonConvert.DeserializeObject(json); } } public void ShowReward() { int lv = GContext.container.Resolve().lv; if (_tables.TbGlobalConfig.AutoClaimUnlock > 0 && lv > _tables.TbGlobalConfig.AutoClaimUnlock) GContext.Publish(new ShowData(RewardType.AutoOpen)); else GContext.Publish(new ShowData()); } public void UpdateCurrentData() { if (AreaList != null) { if (AreaIndex < 0 || AreaIndex >= AreaList.Count) { if (NextMapID == -1 || !_tables.TbConstruction.DataMap.ContainsKey(NextMapID)) { _isOpenSkyscraper = "1"; PlayFabMgr.Instance.UpdateUserDataValue("IsOpenSkyscraper", _isOpenSkyscraper); return; } Debug.LogError( $"[CampData]UpdateCurrentData: AreaIndex({AreaIndex}) is out of range[0-{AreaList.Count}]! Will reset to 0"); AreaIndex = 0; } IsUpdataArea = AreaId != AreaList[AreaIndex]; if (IsUpdataArea) { AreaId = AreaList[AreaIndex]; if (_tables.TbConstructionArea.DataMap.TryGetValue(AreaId, out var area_data)) { constructionArea = area_data; AreaIcon = area_data.Icon; TimelineId = area_data.TimelineID; AreaStepCount = area_data.StepCount; AreaStepList = area_data.StepList; } else { Debug.LogError($"[CampData]UpdateCurrentData: Not found data of area id({AreaId})!"); } } if (AreaStepList != null) { if (AreaStep < 0 || AreaStep >= AreaStepList.Count) { AreaStep = 0; Debug.LogError( $"[CampData]UpdateCurrentData: AreaStep({AreaStep}) is out of range[0-{AreaStepList.Count}]! Reset it to 0."); } var step_id = AreaStepList[AreaStep]; if (_tables.TbConstructionStep.DataMap.TryGetValue(step_id, out var step_data)) { StepInfo = step_data; } else { Debug.LogError($"[CampData]UpdateCurrentData: Not found step data of id({step_id})!"); } } else { Debug.LogError($"[CampData]UpdateCurrentData: AreaStepList is empty!"); } } else { Debug.LogError($"[CampData]UpdateCurrentData: AreaList is empty!"); } } void CompleteInit() { if (AreaList != null) { int curAreaIndex = AreaList.Count - 1; AreaId = AreaList[curAreaIndex]; if (_tables.TbConstructionArea.DataMap.TryGetValue(AreaId, out var area_data)) { constructionArea = area_data; AreaIcon = area_data.Icon; TimelineId = area_data.TimelineID; AreaStepCount = area_data.StepCount; AreaStepList = area_data.StepList; } if (AreaStepList != null) { var step_id = AreaStepList[^1]; if (_tables.TbConstructionStep.DataMap.TryGetValue(step_id, out var step_data)) { StepInfo = step_data; } } if (NextMapID == -1 || !_tables.TbConstruction.DataMap.ContainsKey(NextMapID)) { _isOpenSkyscraper = "1"; PlayFabMgr.Instance.UpdateUserDataValue("IsOpenSkyscraper", _isOpenSkyscraper); } } } void SetHome_Skyscraper() { if (IsOpenSkyscraper) { if (IsCanSkyscraper) { ulong curGold = GContext.container.Resolve().gold; ulong basePrice = (ulong)GContext.container.Resolve().TbMapData.DataList[^1].ConstrutionCost; var multiplierTable = GContext.container.Resolve().TbConstructionInfiniteConfig.CashMagList; var ibData = new InfiniteBuildingData(); ibData.Deserialize(GContext.container.Resolve().InfiniteBuildingData); int step = ibData.Step; ulong price = (ulong)(basePrice * multiplierTable[step]); int count = 0; while (curGold >= price) { curGold -= price; step++; step %= multiplierTable.Count; price = (ulong)(basePrice * multiplierTable[step]); count++; if (count >= 99) break; } RedPointManager.Instance.SetRedPointState(RedPointName.Home_Skyscraper, count > 0, (int)count); } else { RedPointManager.Instance.SetRedPointState(RedPointName.Home_Skyscraper, false); } } } public bool GoBuilding() { if (AreaStep >= AreaStepList.Count) { return false; } var step_id = AreaStepList[AreaStep]; int start = _tables.TbConstructionStep.DataList.IndexOf(_tables.TbConstructionStep.DataMap[step_id]); var camp_data = _tables.TbConstruction.DataMap[Id]; List _areaList = camp_data.AreaList; List _areaStepList = AreaStepList; if (_tables.TbConstructionArea.DataMap.TryGetValue(_areaList[^1], out var area_data)) { _areaStepList = area_data.StepList; } int endIndex = _tables.TbConstructionStep.DataList.IndexOf(_tables.TbConstructionStep.DataMap[_areaStepList[^1]]); List DataList = _tables.TbConstructionStep.DataList; long gold = (long)(ulong)GContext.container.Resolve().gold; var buffTimeData = GContext.container.Resolve().GetWeelyBuffTimeData(); bool isConstructionCost = buffTimeData != null; long _upgradeCost; for (int i = start; i <= endIndex; i++) { if (isConstructionCost) { _upgradeCost = (long)(DataList[i].BuildRequired * (1 - buffTimeData.Param)); } else { _upgradeCost = DataList[i].BuildRequired; } gold -= _upgradeCost; if (gold < 0) { return false; } } return true; } public bool GoBuilding02(int endId) { var step_id = AreaStepList[AreaStep]; if (step_id < 101003 || step_id >= endId) { return false; } int start = _tables.TbConstructionStep.DataList.IndexOf(_tables.TbConstructionStep.DataMap[step_id]); int endIndex = _tables.TbConstructionStep.DataList.IndexOf(_tables.TbConstructionStep.DataMap[endId]); List DataList = _tables.TbConstructionStep.DataList; long gold = (long)(ulong)GContext.container.Resolve().gold; long _upgradeCost; for (int i = start; i < endIndex; i++) { _upgradeCost = DataList[i].BuildRequired; gold -= _upgradeCost; if (gold < 0) { return false; } } return true; } public void SetRedPoint() { SetHome_Skyscraper(); int count = 0; bool redPoint = false; if (GContext.container.Resolve().GetIsFinish(_tables.TbGlobalConfig.BuildRedPointUnlock) && GContext.container.Resolve().gold >= (ulong)StepInfo.BuildRequired) { if (AreaStep >= AreaStepList.Count) { RedPointManager.Instance.SetRedPointState(RedPointName.Home_Build, redPoint, count); return; } var step_id = AreaStepList[AreaStep]; int start = _tables.TbConstructionStep.DataList.IndexOf(_tables.TbConstructionStep.DataMap[step_id]); int nextId = NextMapID; var camp_data = _tables.TbConstruction.DataMap[Id]; while (_tables.TbConstruction.DataMap.TryGetValue(nextId, out var _camp_data)) { camp_data = _camp_data; nextId = camp_data.NextID; } List _areaList = camp_data.AreaList; List _areaStepList = AreaStepList; if (_tables.TbConstructionArea.DataMap.TryGetValue(_areaList[^1], out var area_data)) { _areaStepList = area_data.StepList; } int endIndex = _tables.TbConstructionStep.DataList.IndexOf(_tables.TbConstructionStep.DataMap[_areaStepList[^1]]); int length = start + 100; List DataList = _tables.TbConstructionStep.DataList; length = Math.Min(length, endIndex + 1); long gold = (long)(ulong)GContext.container.Resolve().gold; var buffTimeData = GContext.container.Resolve().GetWeelyBuffTimeData(); bool isConstructionCost = buffTimeData != null; long _upgradeCost; for (int i = start; i < length; i++) { if (isConstructionCost) { _upgradeCost = (long)(DataList[i].BuildRequired * (1 - buffTimeData.Param)); } else { _upgradeCost = DataList[i].BuildRequired; } gold -= _upgradeCost; if (gold < 0) { break; } else { count++; } } redPoint = true; } RedPointManager.Instance.SetRedPointState(RedPointName.Home_Build, redPoint, count); } public void NextStep() { if (AreaStepList != null) { AreaStep++; if (AreaStep < AreaStepList.Count) { UpdateCurrentData(); } else { AreaIndex++; if (AreaIndex < AreaList.Count) { AreaStep = 0; UpdateCurrentData(); } else { AreaIndex = AreaList.Count; AreaStep = AreaStepList.Count; } } } else { GameDebug.LogError($"[CampData]NextStep: AreaStepList is empty!"); } } } }