| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace App\Module\Game\Dtos;
- use UCore\Dto\BaseDto;
- /**
- * 任务变更临时数据传输对象
- *
- * 用于存储和传输任务变更的临时数据
- */
- class TaskChangeTempDto extends BaseDto
- {
- /**
- * 任务ID
- *
- * @var int
- */
- public int $taskId;
- /**
- * 任务名称
- *
- * @var string
- */
- public string $taskName;
- /**
- * 变更类型
- * 可能的值:progress_updated, completed, reward_claimed
- *
- * @var string
- */
- public string $changeType;
- /**
- * 任务状态
- * 可能的值:in_progress, completed, rewarded
- *
- * @var string
- */
- public string $status;
- /**
- * 任务进度(百分比)
- *
- * @var int|null
- */
- public ?int $progress = null;
- /**
- * 进度详情
- *
- * @var array|null
- */
- public ?array $progressDetails = null;
- /**
- * 任务完成时间戳
- *
- * @var int|null
- */
- public ?int $completedAt = null;
- /**
- * 奖励领取时间戳
- *
- * @var int|null
- */
- public ?int $rewardedAt = null;
- /**
- * 奖励列表
- *
- * @var array|null
- */
- public ?array $rewards = null;
- /**
- * 更新时间戳
- *
- * @var int
- */
- public int $updatedAt;
- /**
- * 从数组创建DTO实例
- *
- * @param array $data 数组数据
- * @return static DTO实例
- */
- public static function fromArray(array $data): static
- {
- $dto = new static();
- $dto->taskId = $data['task_id'] ?? 0;
- $dto->taskName = $data['task_name'] ?? '';
- $dto->changeType = $data['change_type'] ?? '';
- $dto->status = $data['status'] ?? '';
- $dto->progress = $data['progress'] ?? null;
- $dto->progressDetails = $data['progress_details'] ?? null;
- $dto->completedAt = $data['completed_at'] ?? null;
- $dto->rewardedAt = $data['rewarded_at'] ?? null;
- $dto->rewards = $data['rewards'] ?? null;
- $dto->updatedAt = $data['updated_at'] ?? time();
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array 数组数据
- */
- public function toArray(): array
- {
- return [
- 'task_id' => $this->taskId,
- 'task_name' => $this->taskName,
- 'change_type' => $this->changeType,
- 'status' => $this->status,
- 'progress' => $this->progress,
- 'progress_details' => $this->progressDetails,
- 'completed_at' => $this->completedAt,
- 'rewarded_at' => $this->rewardedAt,
- 'rewards' => $this->rewards,
- 'updated_at' => $this->updatedAt,
- ];
- }
- /**
- * 获取变更类型的中文描述
- *
- * @return string 中文描述
- */
- public function getChangeTypeDescription(): string
- {
- return match ($this->changeType) {
- 'progress_updated' => '进度更新',
- 'completed' => '任务完成',
- 'reward_claimed' => '奖励领取',
- default => '未知变更',
- };
- }
- /**
- * 获取任务状态的中文描述
- *
- * @return string 中文描述
- */
- public function getStatusDescription(): string
- {
- return match ($this->status) {
- 'in_progress' => '进行中',
- 'completed' => '已完成',
- 'rewarded' => '已领取奖励',
- default => '未知状态',
- };
- }
- /**
- * 检查是否为进度更新
- *
- * @return bool 是否为进度更新
- */
- public function isProgressUpdate(): bool
- {
- return $this->changeType === 'progress_updated';
- }
- /**
- * 检查是否为任务完成
- *
- * @return bool 是否为任务完成
- */
- public function isTaskCompleted(): bool
- {
- return $this->changeType === 'completed';
- }
- /**
- * 检查是否为奖励领取
- *
- * @return bool 是否为奖励领取
- */
- public function isRewardClaimed(): bool
- {
- return $this->changeType === 'reward_claimed';
- }
- /**
- * 获取格式化的进度文本
- *
- * @return string 格式化的进度文本
- */
- public function getFormattedProgress(): string
- {
- if ($this->progress === null) {
- return '无进度信息';
- }
- return $this->progress . '%';
- }
- /**
- * 获取奖励数量
- *
- * @return int 奖励数量
- */
- public function getRewardCount(): int
- {
- return $this->rewards ? count($this->rewards) : 0;
- }
- }
|