调试日志

This commit is contained in:
YiHan0621
2025-09-08 18:28:21 +08:00
parent 29ba23403b
commit f550a87d81
2 changed files with 35 additions and 18 deletions

View File

@@ -18,7 +18,8 @@ namespace TcgEngine.Gameplay
[Header("Player Data")] [Header("Player Data")]
public List<PlayerTask> playerTasks = new List<PlayerTask>(); public List<PlayerTask> playerTasks = new List<PlayerTask>();
public DateTime lastDailyTaskAssigned;
public long lastDailyTaskAssigned = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
public int maxTasks = 5; // 玩家最多持有任务数 public int maxTasks = 5; // 玩家最多持有任务数
private GameLogic gameLogic; private GameLogic gameLogic;
@@ -68,13 +69,6 @@ namespace TcgEngine.Gameplay
{ {
SubscribeToGameEvents(); SubscribeToGameEvents();
} }
}
private void Update()
{
} }
private void OnDisable() private void OnDisable()
@@ -136,6 +130,7 @@ namespace TcgEngine.Gameplay
// 从服务器API获取任务配置 // 从服务器API获取任务配置
string url = ApiClient.ServerURL + "/api/tasks"; string url = ApiClient.ServerURL + "/api/tasks";
WebResponse res = await ApiClient.Get().SendGetRequest(url); WebResponse res = await ApiClient.Get().SendGetRequest(url);
Debug.Log($"<color=red>LoadTasksFromServer:{url}</color>");
Debug.LogError("从服务器API获取任务配置" + res.data + res.status); Debug.LogError("从服务器API获取任务配置" + res.data + res.status);
Debug.LogError(res.success); Debug.LogError(res.success);
if (res.success) if (res.success)
@@ -177,14 +172,13 @@ namespace TcgEngine.Gameplay
private async Task LoadPlayerTasksFromServer() private async Task LoadPlayerTasksFromServer()
{ {
Debug.LogError(ApiClient.Get() + "" + ApiClient.Get().IsLoggedIn());
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn()) if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
{ {
// 从服务器获取玩家任务数据 // 从服务器获取玩家任务数据
string url = ApiClient.ServerURL + $"/api/tasks/{userID}"; string url = ApiClient.ServerURL + $"/api/tasks/"+ApiClient.Get().UserID;
WebResponse res = await ApiClient.Get().SendGetRequest(url); WebResponse res = await ApiClient.Get().SendGetRequest(url);
Debug.Log($"<color=red>{url}</color>"); Debug.Log($"<color=red>LoadPlayerTasksFromServer:{url}</color>");
Debug.LogWarning($"从服务器获取玩家任务数据res_data--:{res.data},{res.status}"); Debug.LogWarning($"从服务器获取玩家任务数据res_data--:{res.data},{res.status}");
if (res.success) if (res.success)
{ {
@@ -192,7 +186,7 @@ namespace TcgEngine.Gameplay
try try
{ {
PlayerTasksResponse response = ApiTool.JsonToObject<PlayerTasksResponse>(res.data); PlayerTasksResponse response = ApiTool.JsonToObject<PlayerTasksResponse>(res.data);
lastDailyTaskAssigned = new DateTime(response.lastDailyTaskAssigned); lastDailyTaskAssigned = response.lastDailyTaskAssigned;
// 清除现有任务 // 清除现有任务
playerTasks.Clear(); playerTasks.Clear();
@@ -237,24 +231,42 @@ namespace TcgEngine.Gameplay
for (int i = 0; i < playerTasks.Count; i++) for (int i = 0; i < playerTasks.Count; i++)
{ {
taskResponses[i] = playerTasks[i].ToResponse(); taskResponses[i] = playerTasks[i].ToResponse();
Debug.Log($"数据[{i}]:{taskResponses[i].taskId}");
} }
foreach (PlayerTaskResponse item in taskResponses)
{
Debug.Log($"<color=red>id:{item.taskId}" +
$"开始时间:{item.assignedTime}" +
$"结束时间:{item.expireTime}" +
$"状态:{item.status}" +
$"进度:{item.progress}" +
$"</color>");
}
Debug.Log($"<color=red>{taskResponses.Length}</color>"); Debug.Log($"<color=red>{taskResponses.Length}</color>");
saveData.tasks = taskResponses; saveData.tasks = taskResponses;
saveData.lastDailyTaskAssigned = lastDailyTaskAssigned.Ticks; saveData.lastDailyTaskAssigned = lastDailyTaskAssigned;
Debug.Log($"saveData.tasks---{saveData.tasks.Length}");
string json = ApiTool.ToJson(saveData); string json = ApiTool.ToJson(saveData);
string url = ApiClient.ServerURL + $"/api/tasks/{userID}"; string url = ApiClient.ServerURL + $"/api/tasks/{userID}";
WebResponse res = await ApiClient.Get().SendPostRequest(url, json); WebResponse res = await ApiClient.Get().SendPostRequest(url, json);
Debug.Log(json);
Debug.Log($"<color=red>SavePlayerData:{url}</color>");
if (res.success) if (res.success)
{ {
Debug.Log("玩家任务已保存到服务器"); Debug.Log("玩家任务已保存到服务器");
} }
else else
{ {
Debug.LogWarning("未能将玩家任务保存到服务器:" + res.error); Debug.LogWarning("未能将玩家任务保存到服务器:" + res.error+"错误码:"+res.status);
} }
} }
} }
@@ -262,9 +274,13 @@ namespace TcgEngine.Gameplay
// 每日任务分配 // 每日任务分配
public void AssignDailyTaskIfNeeded() public void AssignDailyTaskIfNeeded()
{ {
// 检查是否应该分配新任务(每日一次) long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
DateTime now = DateTime.Now; long secondsSinceLastTask = now - lastDailyTaskAssigned;
if ((now - lastDailyTaskAssigned).TotalHours < 24)
// 转换为小时
double hoursSinceLastTask = secondsSinceLastTask / 3600.0;
if (hoursSinceLastTask < 24)
return; return;
// 检查玩家是否已达到最大任务数 // 检查玩家是否已达到最大任务数
@@ -580,7 +596,7 @@ namespace TcgEngine.Gameplay
{ {
PlayerTask playerTask = new PlayerTask(loginTask); PlayerTask playerTask = new PlayerTask(loginTask);
playerTasks.Add(playerTask); playerTasks.Add(playerTask);
lastDailyTaskAssigned = DateTime.Now; lastDailyTaskAssigned = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
SavePlayerData(); SavePlayerData();
Debug.Log($"分配登录任务: {loginTask.name}"); Debug.Log($"分配登录任务: {loginTask.name}");
} }

View File

@@ -57,6 +57,7 @@ namespace TcgEngine
{ {
string url = ApiClient.ServerURL + "/users/" + tuser; string url = ApiClient.ServerURL + "/users/" + tuser;
WebResponse res = await ApiClient.Get().SendGetRequest(url); WebResponse res = await ApiClient.Get().SendGetRequest(url);
Debug.LogError($"ID{res.data}");
UserData udata = ApiTool.JsonToObject<UserData>(res.data); UserData udata = ApiTool.JsonToObject<UserData>(res.data);
if (!res.success) if (!res.success)
error.text = res.error; error.text = res.error;