备份CatanBuilding瘦身独立工程
This commit is contained in:
501
Assets/Scripts/SandDig/model/DiggingGameModel.cs
Normal file
501
Assets/Scripts/SandDig/model/DiggingGameModel.cs
Normal file
@@ -0,0 +1,501 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using asap.core;
|
||||
using cfg;
|
||||
using GameCore;
|
||||
using Newtonsoft.Json;
|
||||
using Script.RuntimeScript;
|
||||
using Script.RuntimeScript.model.Data;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
|
||||
public class DiggingGameModel
|
||||
{
|
||||
#region 协议Const区域
|
||||
|
||||
//工具个数
|
||||
private const string DigCountKey = "DigCount";
|
||||
//棋盘管卡数据
|
||||
private const string DigGridDataKey = "DigGridData";
|
||||
|
||||
#endregion
|
||||
|
||||
#region 管卡json中原始数据
|
||||
private DinggingSceneData _dinggingSceneData;
|
||||
#endregion
|
||||
|
||||
#region 服务器数据
|
||||
/// <summary>
|
||||
/// 沙滩寻宝基本数据
|
||||
/// </summary>
|
||||
private SandDigServerData _serverData = null;
|
||||
/// <summary>
|
||||
/// 沙滩寻宝棋盘网格数据
|
||||
/// </summary>
|
||||
private List<ServerGridData> _allServerGridData = new List<ServerGridData>();
|
||||
public int TempIndex = -1;
|
||||
private int _serverDigCount = 0;
|
||||
int redDot = 10;
|
||||
//最新获取 道具个数 *** 已经废弃
|
||||
public int NewAddDigCount = 0;
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 本期活动开放的所有管卡
|
||||
/// </summary>
|
||||
private List<int> _allClamps = new List<int>();
|
||||
public List<int> AllClamps => _allClamps;
|
||||
|
||||
private int _curActivityId = 0;
|
||||
public int CurActivityId
|
||||
{
|
||||
get => _curActivityId;
|
||||
}
|
||||
public DiggingActivityInit digActivityConfig;
|
||||
public string ResourceTopic => digActivityConfig.ResourceTopic;
|
||||
#region 数据初始化
|
||||
|
||||
/// <summary>
|
||||
/// 在线时候 活动开启
|
||||
/// </summary>
|
||||
public void ActivityOpen(cfg.FishingEvent t)
|
||||
{
|
||||
if (_curActivityId == t.ID)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_curActivityId = t.ID;
|
||||
if (_curActivityId > 0)
|
||||
{
|
||||
digActivityConfig = GContext.container.Resolve<Tables>().TbDiggingActivityInit.Get(t.RedirectID);
|
||||
redDot = GContext.container.Resolve<FishingEventData>().GetRedDot(_curActivityId);
|
||||
}
|
||||
DiggingGameManager.LogError("沙滩寻宝开启");
|
||||
string countJson = PlayFabMgr.Instance.GetLocalData(DigCountKey);
|
||||
string digGridDataJson = PlayFabMgr.Instance.GetLocalData(DigGridDataKey);
|
||||
if (string.IsNullOrEmpty(countJson))
|
||||
{
|
||||
SaveNull();
|
||||
}
|
||||
DoServerData(countJson, digGridDataJson);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入游戏初始化数据
|
||||
/// </summary>
|
||||
/// <param name="userDatas"></param>
|
||||
public async System.Threading.Tasks.Task InitServerData()
|
||||
{
|
||||
_curActivityId = GContext.container.Resolve<DiggingGameManager>().GetActivityId();
|
||||
DiggingGameManager.LogError("沙滩寻宝数据初始化");
|
||||
if (_curActivityId > 0)
|
||||
{
|
||||
FishingEvent fishingEvent = GContext.container.Resolve<Tables>().TbFishingEvent.GetOrDefault(_curActivityId);
|
||||
digActivityConfig = GContext.container.Resolve<Tables>().TbDiggingActivityInit.Get(fishingEvent.RedirectID);
|
||||
}
|
||||
|
||||
string countJson = PlayFabMgr.Instance.GetLocalData(DigCountKey);
|
||||
string digGridDataJson = PlayFabMgr.Instance.GetLocalData(DigGridDataKey);
|
||||
DoServerData(countJson, digGridDataJson);
|
||||
await InitClientData(ClientDataHandleEnum.Init);
|
||||
}
|
||||
private void DoServerData(string countJson, string digGridDataJson)
|
||||
{
|
||||
if (_curActivityId <= 0)
|
||||
{
|
||||
SaveNull();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
redDot = GContext.container.Resolve<FishingEventData>().GetRedDot(_curActivityId);
|
||||
FishingEvent acInfo = GContext.container.Resolve<DiggingGameManager>().GetActivityConfig(_curActivityId);
|
||||
int redirectID = acInfo.RedirectID;
|
||||
|
||||
DiggingActivityInit digActivityConfig = GContext.container.Resolve<Tables>().TbDiggingActivityInit.Get(redirectID);
|
||||
if (digActivityConfig == null)
|
||||
{
|
||||
SaveNull();
|
||||
return;
|
||||
}
|
||||
_allClamps = digActivityConfig.StageList;
|
||||
|
||||
if (!string.IsNullOrEmpty(countJson))
|
||||
{
|
||||
try
|
||||
{
|
||||
SaveDigCountJson data = Newtonsoft.Json.JsonConvert.DeserializeObject<SaveDigCountJson>(countJson);
|
||||
if (data.ActivityId == _curActivityId)
|
||||
{
|
||||
_serverDigCount = data.DigCount;
|
||||
DiggingGameManager.LogError("玩家铲子个数 :::" + _serverDigCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveNull();
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SaveNull();
|
||||
Debug.LogError(e);
|
||||
}
|
||||
}
|
||||
_serverData = null;
|
||||
_allServerGridData.Clear();
|
||||
if (!string.IsNullOrEmpty(digGridDataJson))
|
||||
{
|
||||
SaveDataJson data = Newtonsoft.Json.JsonConvert.DeserializeObject<SaveDataJson>(digGridDataJson);
|
||||
if (data.ActivityId == _curActivityId)
|
||||
{
|
||||
_serverData = data.GetSerializableData();
|
||||
_allServerGridData = data.GetSerializableGridData().ToList();
|
||||
TempIndex = data.TempIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveNull();
|
||||
}
|
||||
}
|
||||
if (_serverData == null || (!_allClamps.Contains(_serverData.ClampId) && _serverData.ClampId > 0))
|
||||
{
|
||||
_serverData = new SandDigServerData();
|
||||
_serverData.ClampId = _allClamps[0];
|
||||
_allServerGridData.Clear();
|
||||
}
|
||||
SetRedPoint();
|
||||
//#if UNITY_EDITOR
|
||||
// //测试
|
||||
// _allServerGridData = new List<ServerGridData>();
|
||||
// _allServerGridPropsData = new List<ServerGridPropsData>();
|
||||
// _serverData = new SandDigServerData();
|
||||
// _serverData.ClampId = _allClamps[1];
|
||||
//#endif
|
||||
}
|
||||
}
|
||||
public void GMSetClamp(int index)
|
||||
{
|
||||
if (_curActivityId <= 0 || _serverData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (index >= _allClamps.Count)
|
||||
{
|
||||
index = _allClamps.Count - 1;
|
||||
}
|
||||
if (index < 0)
|
||||
{
|
||||
_serverData.ClampId = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_serverData.ClampId = _allClamps[index];
|
||||
}
|
||||
NewData();
|
||||
}
|
||||
public async Task<bool> InitClientData(ClientDataHandleEnum handleType)
|
||||
{
|
||||
if (_serverData == null || _serverData.ClampId <= 0)
|
||||
return false;
|
||||
DiggingGameManager.LogError("初始化客户端管卡数据");
|
||||
string JsonPath = $"Clamp{_serverData.ClampId}";// $"Assets/ABPackage/SandDig/ClampJson/Clamp{_serverData.ClampId}";
|
||||
|
||||
GContext.container.Resolve<DiggingGameManager>().GameState = SandDigGameState.LoadJson;
|
||||
AsyncOperationHandle<TextAsset> handle = Addressables.LoadAssetAsync<TextAsset>(JsonPath);
|
||||
TaskCompletionSource<bool> taskCompletion = new TaskCompletionSource<bool>();
|
||||
handle.Completed += (AsyncOperationHandle<TextAsset> obj) =>
|
||||
{
|
||||
if (obj.Status == AsyncOperationStatus.Succeeded)
|
||||
{
|
||||
DiggingGameManager.LogError("新管卡数据加载完毕");
|
||||
_dinggingSceneData = JsonConvert.DeserializeObject<DinggingSceneData>(obj.Result.ToString());
|
||||
_dinggingSceneData.AnalysisData(_allServerGridData);
|
||||
// 释放资源
|
||||
Addressables.Release(handle);
|
||||
if (handleType == ClientDataHandleEnum.PassClamp)
|
||||
{
|
||||
//可以切换了
|
||||
GContext.container.Resolve<DiggingGameManager>().GameState = SandDigGameState.WaitPassLoading;
|
||||
}
|
||||
else if (handleType == ClientDataHandleEnum.EnterGame)
|
||||
{
|
||||
GContext.container.Resolve<DiggingGameManager>().GameState = SandDigGameState.LoadJsonOver;
|
||||
}
|
||||
else if (handleType == ClientDataHandleEnum.Init)
|
||||
{
|
||||
InitCheckClampPass();
|
||||
}
|
||||
taskCompletion.SetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
taskCompletion.SetResult(false);
|
||||
DiggingGameManager.LogError("Failed to load asset: " + JsonPath);
|
||||
}
|
||||
};
|
||||
return await taskCompletion.Task;
|
||||
}
|
||||
public void InitCheckClampPass()
|
||||
{
|
||||
List<int> allHaveProps = new List<int>(GetAllPassIds());
|
||||
PropData[] allData = GetAllPropData();
|
||||
bool allFinded = true;
|
||||
|
||||
for (int i = 0; i < allData.Length; i++)
|
||||
{
|
||||
var propData = allData[i];
|
||||
if (!allHaveProps.Contains(propData.PropId))
|
||||
{
|
||||
allFinded = false;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
allHaveProps.Remove(propData.PropId);
|
||||
}
|
||||
}
|
||||
if (!allFinded)
|
||||
{
|
||||
allFinded = true;
|
||||
GridData[] gridDatas = GetAllGridData();
|
||||
for (int i = 0; i < gridDatas.Length; i++)
|
||||
{
|
||||
GridData gridData = gridDatas[i];
|
||||
if (gridData.IsBorder || gridData.IsOpen)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
allFinded = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allFinded)
|
||||
{
|
||||
DiggingStageReward config = GContext.container.Resolve<Tables>().TbDiggingStageReward.Get(_serverData.ClampId);
|
||||
_serverData.ClampId = config.NextTarget;
|
||||
NewData();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 通过当前管卡发奖
|
||||
/// </summary>
|
||||
public void PassClamp()
|
||||
{
|
||||
GContext.container.Resolve<DiggingGameManager>().ClampIsPass = true;
|
||||
DiggingStageReward config = GContext.container.Resolve<Tables>().TbDiggingStageReward.Get(_serverData.ClampId);
|
||||
|
||||
var playerData = GContext.container.Resolve<PlayerItemData>();
|
||||
|
||||
DiggingGameManager.LogError("获得的Drop奖励 :::" + config.DropID);
|
||||
GContext.container.Resolve<DiggingGameManager>().itemDatas = playerData.AddItemByDrop(config.DropID, true);
|
||||
|
||||
_serverData.ClampId = config.NextTarget;
|
||||
NewData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在线进入新关卡,数据初始化
|
||||
/// </summary>
|
||||
public void NewData()
|
||||
{
|
||||
TempIndex = -1;
|
||||
_serverData.ClickCount = 0;
|
||||
_serverData.Reset();
|
||||
_allServerGridData.Clear();
|
||||
_dinggingSceneData?.Reset();
|
||||
SaveAll();
|
||||
}
|
||||
public void NewClampInit()
|
||||
{
|
||||
GContext.Publish(new EventSandDigNewClamp(_serverData.ClampId));
|
||||
if (_serverData.ClampId <= 0)
|
||||
{
|
||||
//通关所有关卡
|
||||
DiggingGameManager.LogError("通关所有关卡!!!!!!!");
|
||||
}
|
||||
else
|
||||
{
|
||||
InitClientData(ClientDataHandleEnum.PassClamp);
|
||||
}
|
||||
}
|
||||
|
||||
#region 数据接口
|
||||
|
||||
/// <summary>
|
||||
/// 使用道具
|
||||
/// </summary>
|
||||
public void UseDig()
|
||||
{
|
||||
_serverDigCount--;
|
||||
_serverData.ClickCount++;
|
||||
SaveDigCount();
|
||||
|
||||
GContext.Publish(new EventSandDigPropUpdate());
|
||||
}
|
||||
/// <summary>
|
||||
/// 增加道具
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public void AddDigCount(int count)
|
||||
{
|
||||
_serverDigCount += count;
|
||||
|
||||
SaveDigCount();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获得目标道具
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
public void AddProp(int id)
|
||||
{
|
||||
_serverData.PassPropIds.Add(id);
|
||||
}
|
||||
|
||||
public List<int> GetAllPassIds()
|
||||
{
|
||||
return _serverData.PassPropIds;
|
||||
}
|
||||
|
||||
public DinggingSceneData GetBroadData()
|
||||
{
|
||||
return _dinggingSceneData;
|
||||
}
|
||||
|
||||
public BroadData[] GetAllBroadData()
|
||||
{
|
||||
return _dinggingSceneData.GetAllBroadData();
|
||||
}
|
||||
|
||||
public GridData[] GetAllGridData()
|
||||
{
|
||||
return _dinggingSceneData.GetAllGridData();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取一个格子数据
|
||||
/// </summary>
|
||||
/// <param name="row"></param>
|
||||
/// <param name="col"></param>
|
||||
/// <returns></returns>
|
||||
public GridData GetGridData(int row, int col)
|
||||
{
|
||||
return _dinggingSceneData.GetGridData(row, col);
|
||||
}
|
||||
|
||||
public PropData[] GetAllPropData()
|
||||
{
|
||||
return _dinggingSceneData.GetAllPropData();
|
||||
}
|
||||
|
||||
public List<PropTempData> GetOnePropTempData()
|
||||
{
|
||||
if (TempIndex < 0)
|
||||
{
|
||||
TempIndex = _dinggingSceneData.GetOnePropTempDataIndex();
|
||||
}
|
||||
return _dinggingSceneData.GetAllPropTempData(TempIndex);
|
||||
}
|
||||
public Vector3 GetTaskPosPositon()
|
||||
{
|
||||
return _dinggingSceneData.GetTaskPosPositon();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取道具个数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetDigCount()
|
||||
{
|
||||
return _serverDigCount;
|
||||
}
|
||||
|
||||
|
||||
public SandDigServerData GetServerData()
|
||||
{
|
||||
return _serverData;
|
||||
}
|
||||
|
||||
public int GetCurClampId()
|
||||
{
|
||||
return _serverData.ClampId;
|
||||
}
|
||||
public int GetClickCount()
|
||||
{
|
||||
return _serverData.ClickCount;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 数据存储
|
||||
|
||||
public void SaveDigCount()
|
||||
{
|
||||
SetRedPoint();
|
||||
SaveDigCountJson dataJson = new SaveDigCountJson();
|
||||
dataJson.ActivityId = _curActivityId;
|
||||
dataJson.DigCount = _serverDigCount;
|
||||
string json = JsonConvert.SerializeObject(dataJson);
|
||||
PlayFabMgr.Instance.UpdateUserDataValue(DigCountKey, json);
|
||||
GContext.container.Resolve<FishingEventData>().SaveTransitionData(dataJson.ActivityId, dataJson.DigCount);
|
||||
}
|
||||
void SetRedPoint()
|
||||
{
|
||||
RedPointManager.Instance.SetRedPointState("Home.Digging", _serverData != null && _serverData.ClampId > 0 && _serverDigCount >= redDot);
|
||||
}
|
||||
public void SaveData()
|
||||
{
|
||||
//通用数据 + 任务目标数据
|
||||
SaveDataJson dataJson = new SaveDataJson();
|
||||
dataJson.ActivityId = _curActivityId;
|
||||
dataJson.SandDigServerData = _serverData.ToJson();
|
||||
dataJson.TempIndex = TempIndex;
|
||||
//网格数据
|
||||
GridData[] allGridData = _dinggingSceneData?.GetAllGridData() ?? new GridData[0];
|
||||
string allGridStr = allGridData.Length > 0 ? "[" : string.Empty;
|
||||
|
||||
for (int i = 0; i < allGridData.Length; i++)
|
||||
{
|
||||
if (i == allGridData.Length - 1)
|
||||
{
|
||||
allGridStr += allGridData[i].ToJson() + "]";
|
||||
}
|
||||
else
|
||||
{
|
||||
allGridStr += allGridData[i].ToJson() + ",";
|
||||
}
|
||||
}
|
||||
dataJson.AllGridJson = allGridStr;
|
||||
|
||||
string json = JsonConvert.SerializeObject(dataJson);
|
||||
//DiggingGameManager.LogError(json);
|
||||
PlayFabMgr.Instance.UpdateUserDataValue(DigGridDataKey, json);
|
||||
}
|
||||
|
||||
public void SaveAll()
|
||||
{
|
||||
this.SaveDigCount();
|
||||
this.SaveData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空上一期活动数据
|
||||
/// </summary>
|
||||
private void SaveNull()
|
||||
{
|
||||
_serverDigCount = GContext.container.Resolve<FishingEventData>().GetInitWelcomeGift(_curActivityId);
|
||||
SaveDigCount();
|
||||
PlayFabMgr.Instance.UpdateUserDataValue(DigGridDataKey, string.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user