天梯UI补充

This commit is contained in:
YiHan0621
2025-09-08 11:35:13 +08:00
parent bcb4f6fd3c
commit 409883801b
17 changed files with 745 additions and 51 deletions

View File

@@ -28,8 +28,11 @@ namespace TcgEngine.Gameplay
private GameClient gameClient;
public string userID;
private void Awake()
{
if (Instance == null)
{
Instance = this;
@@ -43,6 +46,7 @@ namespace TcgEngine.Gameplay
private void Start()
{
userID = ApiClient.Get().UserID;
}
private void OnEnable()
@@ -56,6 +60,7 @@ namespace TcgEngine.Gameplay
LoadPlayerData();
gameClient.onGameStart += OnGameStart;
gameClient.onGameEnd += OnGameEnd;
// gameClient.onConnectServer?.Invoke();
}
// 移除对GameLogic.Instance的错误引用改为检查gameLogic变量
@@ -63,6 +68,13 @@ namespace TcgEngine.Gameplay
{
SubscribeToGameEvents();
}
}
private void Update()
{
}
private void OnDisable()
@@ -122,16 +134,16 @@ namespace TcgEngine.Gameplay
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
{
// 从服务器API获取任务配置
string url = ApiClient.ServerURL + "/tasks";
string url = ApiClient.ServerURL + "/api/tasks";
WebResponse res = await ApiClient.Get().SendGetRequest(url);
Debug.LogError("从服务器API获取任务配置"+res.data);
if (res.success)
{
// 解析任务配置数据
try
{
TaskDataResponse[] taskResponses = ApiTool.JsonToObject<TaskDataResponse[]>(res.data);
Debug.Log("Loaded " + taskResponses.Length + " tasks from server");
Debug.Log("<color=red>Loaded </color>" + taskResponses.Length + " tasks from server");
// 在实际项目中这里应该将服务器数据转换为TaskData对象并存储在内存中
// 供后续使用而不是每次都从Resources加载
@@ -156,24 +168,6 @@ namespace TcgEngine.Gameplay
Debug.Log("Loaded " + localTasks.Length + " tasks from local resources as fallback");
}
#if UNITY_EDITOR
private void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
LoadTasks();
LoadPlayerData();
SavePlayerData();
}
if (Input.GetKeyDown(KeyCode.U))
{
UpdateTaskProgress(TaskConditionType.WinGames, "YiYongJun", "YiYongJun");
UpdateTaskProgress(TaskConditionType.SummonHeroWithAttributes, "WangGuoJun", "YiYongJun");
}
}
#endif
private async void LoadPlayerData()
{
// 从服务器加载玩家任务数据
@@ -185,11 +179,11 @@ namespace TcgEngine.Gameplay
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
{
// 从服务器获取玩家任务数据
string url = ApiClient.ServerURL + "/users/" + ApiClient.Get().UserID + "/tasks";
string url = ApiClient.ServerURL + $"/api/tasks/{userID}";
WebResponse res = await ApiClient.Get().SendGetRequest(url);
Debug.Log($"<color=red>{url}</color>");
Debug.LogWarning($"从服务器获取玩家任务数据res_data--:{res.data}");
if (res.success)
{
// 解析玩家任务数据
@@ -231,28 +225,31 @@ namespace TcgEngine.Gameplay
public async void SavePlayerData()
{
Debug.LogError("------Get():"+ApiClient.Get()+"------.IsLoggedIn():"+ ApiClient.Get().IsLoggedIn());
// 将玩家任务数据保存到服务器
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
if (ApiClient.Get() != null)
{
Debug.Log("<color=red> GO GO GO </color>");
// 准备要发送的数据
PlayerTasksResponse saveData = new PlayerTasksResponse();
// 转换任务数据
PlayerTaskResponse[] taskResponses = new PlayerTaskResponse[playerTasks.Count];
for (int i = 0; i < playerTasks.Count; i++)
{
taskResponses[i] = playerTasks[i].ToResponse();
}
Debug.Log($"<color=red>{taskResponses.Length}</color>");
saveData.tasks = taskResponses;
saveData.lastDailyTaskAssigned = lastDailyTaskAssigned.Ticks;
string json = ApiTool.ToJson(saveData);
string url = ApiClient.ServerURL + "/users/" + ApiClient.Get().UserID + "/tasks";
string url = ApiClient.ServerURL + $"/api/tasks/{userID}";
WebResponse res = await ApiClient.Get().SendPostRequest(url, json);
Debug.Log($"<color=red>{res}</color>");
Debug.Log($"1111<color=red>{url}</color>");
Debug.Log($"2222<color=red>{json}</color>");
if (res.success)
{
@@ -262,6 +259,8 @@ namespace TcgEngine.Gameplay
{
Debug.LogWarning("Failed to save player tasks to server: " + res.error);
}
Debug.LogWarning($"res_data--:{res.data}");
Debug.LogWarning($"res.success--:{res.success}");
}
}
@@ -326,7 +325,7 @@ namespace TcgEngine.Gameplay
lastDailyTaskAssigned = now;
SavePlayerData();
Debug.Log($"Assigned daily task: {selectedTask.name}");
Debug.Log($"分配任务: {selectedTask.name}");
}
// 检查并更新过期任务
@@ -463,6 +462,7 @@ namespace TcgEngine.Gameplay
if (progressUpdated)
{
Debug.LogError("读到了");
SavePlayerData();
}
}
@@ -470,6 +470,7 @@ namespace TcgEngine.Gameplay
// 事件处理方法
private void OnGameStart()
{
// 进行对战任务进度+1
UpdateTaskProgress(TaskConditionType.PlayGames);
}
@@ -549,6 +550,19 @@ namespace TcgEngine.Gameplay
// 玩家登录时检查任务
public void OnPlayerLogin()
{
// 检查是否已有登录任务
bool hasLoginTask = playerTasks.Any(t =>
{
TaskData config = GetTaskConfig(t.taskId);
return config != null && config.condition == TaskConditionType.LoginGame;
});
// 如果没有登录任务,就分配一个
if (!hasLoginTask)
{
AssignLoginTask();
}
// 登录任务完成
UpdateTaskProgress(TaskConditionType.LoginGame);
@@ -558,6 +572,29 @@ namespace TcgEngine.Gameplay
// 分配每日任务(如果需要)
AssignDailyTaskIfNeeded();
}
/// <summary>
/// 专门分配一个登录任务
/// </summary>
private void AssignLoginTask()
{
TaskData[] allTasks = Resources.LoadAll<TaskData>("Tasks");
var loginTask = allTasks.FirstOrDefault(t => t.isDailyTask && t.condition == TaskConditionType.LoginGame);
if (loginTask != null)
{
PlayerTask playerTask = new PlayerTask(loginTask);
playerTasks.Add(playerTask);
lastDailyTaskAssigned = DateTime.Now;
SavePlayerData();
Debug.Log($"分配登录任务: {loginTask.name}");
}
else
{
Debug.LogWarning("未找到登录任务配置!");
}
}
// 获取活跃任务
public List<PlayerTask> GetActiveTasks()