From f550a87d81bf44149a757701127af9ab95a2b637 Mon Sep 17 00:00:00 2001 From: YiHan0621 <2857295085@qq.com> Date: Mon, 8 Sep 2025 18:28:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/GameLogic/TaskManager.cs | 52 ++++++++++++------- .../Scripts/Tools/ChangePermission.cs | 1 + 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs b/Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs index bb6c94d..133ba17 100644 --- a/Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs +++ b/Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs @@ -18,7 +18,8 @@ namespace TcgEngine.Gameplay [Header("Player Data")] public List playerTasks = new List(); - public DateTime lastDailyTaskAssigned; + + public long lastDailyTaskAssigned = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); public int maxTasks = 5; // 玩家最多持有任务数 private GameLogic gameLogic; @@ -68,13 +69,6 @@ namespace TcgEngine.Gameplay { SubscribeToGameEvents(); } - } - - private void Update() - { - - - } private void OnDisable() @@ -136,6 +130,7 @@ namespace TcgEngine.Gameplay // 从服务器API获取任务配置 string url = ApiClient.ServerURL + "/api/tasks"; WebResponse res = await ApiClient.Get().SendGetRequest(url); + Debug.Log($"LoadTasksFromServer:{url}"); Debug.LogError("从服务器API获取任务配置" + res.data + res.status); Debug.LogError(res.success); if (res.success) @@ -177,14 +172,13 @@ namespace TcgEngine.Gameplay private async Task LoadPlayerTasksFromServer() { - Debug.LogError(ApiClient.Get() + "" + 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); - Debug.Log($"{url}"); + Debug.Log($"LoadPlayerTasksFromServer:{url}"); Debug.LogWarning($"从服务器获取玩家任务数据res_data--:{res.data},{res.status}"); if (res.success) { @@ -192,7 +186,7 @@ namespace TcgEngine.Gameplay try { PlayerTasksResponse response = ApiTool.JsonToObject(res.data); - lastDailyTaskAssigned = new DateTime(response.lastDailyTaskAssigned); + lastDailyTaskAssigned = response.lastDailyTaskAssigned; // 清除现有任务 playerTasks.Clear(); @@ -237,24 +231,42 @@ namespace TcgEngine.Gameplay for (int i = 0; i < playerTasks.Count; i++) { taskResponses[i] = playerTasks[i].ToResponse(); + Debug.Log($"数据[{i}]:{taskResponses[i].taskId}"); } + foreach (PlayerTaskResponse item in taskResponses) + { + Debug.Log($"id:{item.taskId}" + + $"开始时间:{item.assignedTime}" + + $"结束时间:{item.expireTime}" + + $"状态:{item.status}" + + $"进度:{item.progress}" + + $""); + } + + Debug.Log($"{taskResponses.Length}"); saveData.tasks = taskResponses; - saveData.lastDailyTaskAssigned = lastDailyTaskAssigned.Ticks; + saveData.lastDailyTaskAssigned = lastDailyTaskAssigned; + + Debug.Log($"saveData.tasks---{saveData.tasks.Length}"); string json = ApiTool.ToJson(saveData); string url = ApiClient.ServerURL + $"/api/tasks/{userID}"; WebResponse res = await ApiClient.Get().SendPostRequest(url, json); + Debug.Log(json); + + Debug.Log($"SavePlayerData:{url}"); + if (res.success) { Debug.Log("玩家任务已保存到服务器"); } else { - Debug.LogWarning("未能将玩家任务保存到服务器:" + res.error); + Debug.LogWarning("未能将玩家任务保存到服务器:" + res.error+"错误码:"+res.status); } } } @@ -262,9 +274,13 @@ namespace TcgEngine.Gameplay // 每日任务分配 public void AssignDailyTaskIfNeeded() { - // 检查是否应该分配新任务(每日一次) - DateTime now = DateTime.Now; - if ((now - lastDailyTaskAssigned).TotalHours < 24) + long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + long secondsSinceLastTask = now - lastDailyTaskAssigned; + + // 转换为小时 + double hoursSinceLastTask = secondsSinceLastTask / 3600.0; + + if (hoursSinceLastTask < 24) return; // 检查玩家是否已达到最大任务数 @@ -580,7 +596,7 @@ namespace TcgEngine.Gameplay { PlayerTask playerTask = new PlayerTask(loginTask); playerTasks.Add(playerTask); - lastDailyTaskAssigned = DateTime.Now; + lastDailyTaskAssigned = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); SavePlayerData(); Debug.Log($"分配登录任务: {loginTask.name}"); } diff --git a/Assets/TcgEngine/Scripts/Tools/ChangePermission.cs b/Assets/TcgEngine/Scripts/Tools/ChangePermission.cs index 437b684..0b9ff7a 100644 --- a/Assets/TcgEngine/Scripts/Tools/ChangePermission.cs +++ b/Assets/TcgEngine/Scripts/Tools/ChangePermission.cs @@ -57,6 +57,7 @@ namespace TcgEngine { string url = ApiClient.ServerURL + "/users/" + tuser; WebResponse res = await ApiClient.Get().SendGetRequest(url); + Debug.LogError($"ID:{res.data}"); UserData udata = ApiTool.JsonToObject(res.data); if (!res.success) error.text = res.error;