177 lines
6.4 KiB
C#
177 lines
6.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace TcgEngine
|
||
{
|
||
/// <summary>
|
||
/// 任务配置数据,用于定义各种任务的属性和条件
|
||
/// </summary>
|
||
[CreateAssetMenu(fileName = "TaskData", menuName = "TcgEngine/TaskData")]
|
||
public class TaskData : ScriptableObject
|
||
{
|
||
public string id;
|
||
public string name;
|
||
public string desc;
|
||
public TaskConditionType condition;
|
||
public int value1; // 任务进度上限
|
||
public string value2; // 边界条件1
|
||
public string value3; // 边界条件2
|
||
public TaskRewardType[] rewardTypes;
|
||
public int[] rewardNums;
|
||
public bool isDailyTask = true; // 是否为每日任务
|
||
public int durationHours = 24; // 任务有效期(小时)
|
||
|
||
// 从服务器响应数据创建任务配置
|
||
public static TaskData CreateFromResponse(TaskDataResponse response)
|
||
{
|
||
TaskData taskData = ScriptableObject.CreateInstance<TaskData>();
|
||
taskData.id = response.id;
|
||
taskData.name = response.name;
|
||
taskData.desc = response.desc;
|
||
taskData.condition = (TaskConditionType)response.condition;
|
||
taskData.value1 = response.value1;
|
||
taskData.value2 = response.value2;
|
||
taskData.value3 = response.value3;
|
||
|
||
// 转换奖励类型数组
|
||
taskData.rewardTypes = new TaskRewardType[response.rewardTypes.Length];
|
||
for (int i = 0; i < response.rewardTypes.Length; i++)
|
||
{
|
||
taskData.rewardTypes[i] = (TaskRewardType)response.rewardTypes[i];
|
||
}
|
||
|
||
taskData.rewardNums = response.rewardNums;
|
||
taskData.isDailyTask = response.isDailyTask;
|
||
taskData.durationHours = response.durationHours;
|
||
|
||
return taskData;
|
||
}
|
||
|
||
// 本地读取的缓存存储处
|
||
// 还没改
|
||
public TaskData LoadFromResponse(TaskDataResponse response)
|
||
{
|
||
TaskData task = ScriptableObject.CreateInstance<TaskData>();
|
||
task.id = response.id;
|
||
task.name = response.name;
|
||
task.desc = response.desc;
|
||
task.condition = (TaskConditionType)response.condition;
|
||
task.value1 = response.value1;
|
||
task.value2 = response.value2;
|
||
task.value3 = response.value3;
|
||
|
||
if (response.rewardTypes != null)
|
||
{
|
||
task.rewardTypes = new TaskRewardType[response.rewardTypes.Length];
|
||
for (int i = 0; i < response.rewardTypes.Length; i++)
|
||
{
|
||
task.rewardTypes[i] = (TaskRewardType)response.rewardTypes[i];
|
||
}
|
||
}
|
||
else
|
||
{
|
||
task.rewardTypes = new TaskRewardType[0];
|
||
}
|
||
|
||
task.rewardNums = response.rewardNums != null ? response.rewardNums : new int[0];
|
||
task.isDailyTask = response.isDailyTask;
|
||
task.durationHours = response.durationHours;
|
||
|
||
return task;
|
||
}
|
||
}
|
||
|
||
public enum TaskConditionType
|
||
{
|
||
LoginGame = 1, // 登入游戏
|
||
PlayGames = 2, // 进行X场对战
|
||
WinGames = 3, // 胜利X场
|
||
DefeatHeroWithAttributes = 4, // 击败Y属性和Z属性的英雄X次
|
||
SummonHeroWithAttributes = 5, // 召唤Y属性和Z属性的英雄X次
|
||
UseHeroSkillWithAttributes = 6 // 使用Y属性和Z属性英雄的技能X次
|
||
}
|
||
|
||
public enum TaskRewardType
|
||
{
|
||
Coins = 0, // 金币
|
||
// 后续可扩展其他奖励类型
|
||
}
|
||
|
||
[Serializable]
|
||
public class PlayerTask
|
||
{
|
||
public string taskId;
|
||
public long assignedTime;
|
||
public long expireTime;
|
||
public TaskStatus status;
|
||
public int progress; // 当前进度
|
||
|
||
public PlayerTask(TaskData taskConfig)
|
||
{
|
||
taskId = taskConfig.id;
|
||
assignedTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||
expireTime = DateTimeOffset.FromUnixTimeSeconds(assignedTime).AddDays(1).ToUnixTimeSeconds();
|
||
status = TaskStatus.Active;
|
||
progress = 0;
|
||
}
|
||
|
||
|
||
// 从服务器响应数据创建玩家任务
|
||
public PlayerTask(PlayerTaskResponse response)
|
||
{
|
||
taskId = response.taskId;
|
||
assignedTime = Iso8601ToTimestamp(response.assignedTime);
|
||
expireTime = Iso8601ToTimestamp(response.expireTime);
|
||
status = (TaskStatus)response.status;
|
||
progress = response.progress;
|
||
}
|
||
|
||
// 转换为服务器响应数据,传递向服务器
|
||
public PlayerTaskResponse ToResponse()
|
||
{
|
||
PlayerTaskResponse response = new PlayerTaskResponse();
|
||
response.taskId = taskId;
|
||
response.assignedTime = TimestampToIso8601(assignedTime);
|
||
response.expireTime = TimestampToIso8601(expireTime);
|
||
response.status = (int)status;
|
||
response.progress = progress;
|
||
return response;
|
||
}
|
||
|
||
// 时间戳(秒)转 ISO 8601 格式字符串
|
||
public static string TimestampToIso8601(long timestampSeconds)
|
||
{
|
||
// 从 Unix 时间戳(秒)创建 DateTimeOffset(UTC 时间)
|
||
DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(timestampSeconds);
|
||
// 转换为带毫秒的 ISO 8601 格式(UTC+0 时区,带 Z 标识)
|
||
return dateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
|
||
}
|
||
|
||
public static long Iso8601ToTimestamp(string iso8601String)
|
||
{
|
||
// 清除字符串前后的空白字符和可能的隐藏字符
|
||
string cleaned = iso8601String?.Trim() ?? string.Empty;
|
||
|
||
if (DateTimeOffset.TryParse(cleaned, out DateTimeOffset dateTimeOffset))
|
||
{
|
||
return dateTimeOffset.ToUnixTimeSeconds();
|
||
}
|
||
|
||
// 输出错误信息便于排查(实际运行可删除)
|
||
Debug.LogError($"解析失败,原始字符串: [{iso8601String}], 清理后: [{cleaned}]");
|
||
throw new ArgumentException("无效的 ISO 8601 格式字符串");
|
||
}
|
||
|
||
}
|
||
|
||
public enum TaskStatus
|
||
{
|
||
Active = 0, // 激活
|
||
Completed = 1, // 完成
|
||
Expired = 2, // 过期
|
||
Claimed = 3 // 已领取
|
||
}
|
||
|
||
|
||
} |