TaskChangeTempDto.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. use UCore\Dto\BaseDto;
  4. /**
  5. * 任务变更临时数据传输对象
  6. *
  7. * 用于存储和传输任务变更的临时数据
  8. */
  9. class TaskChangeTempDto extends BaseDto
  10. {
  11. /**
  12. * 任务ID
  13. *
  14. * @var int
  15. */
  16. public int $taskId;
  17. /**
  18. * 任务名称
  19. *
  20. * @var string
  21. */
  22. public string $taskName;
  23. /**
  24. * 变更类型
  25. * 可能的值:progress_updated, completed, reward_claimed
  26. *
  27. * @var string
  28. */
  29. public string $changeType;
  30. /**
  31. * 任务状态
  32. * 可能的值:in_progress, completed, rewarded
  33. *
  34. * @var string
  35. */
  36. public string $status;
  37. /**
  38. * 任务进度(百分比)
  39. *
  40. * @var int|null
  41. */
  42. public ?int $progress = null;
  43. /**
  44. * 进度详情
  45. *
  46. * @var array|null
  47. */
  48. public ?array $progressDetails = null;
  49. /**
  50. * 任务完成时间戳
  51. *
  52. * @var int|null
  53. */
  54. public ?int $completedAt = null;
  55. /**
  56. * 奖励领取时间戳
  57. *
  58. * @var int|null
  59. */
  60. public ?int $rewardedAt = null;
  61. /**
  62. * 奖励列表
  63. *
  64. * @var array|null
  65. */
  66. public ?array $rewards = null;
  67. /**
  68. * 更新时间戳
  69. *
  70. * @var int
  71. */
  72. public int $updatedAt;
  73. /**
  74. * 从数组创建DTO实例
  75. *
  76. * @param array $data 数组数据
  77. * @return static DTO实例
  78. */
  79. public static function fromArray(array $data): static
  80. {
  81. $dto = new static();
  82. $dto->taskId = $data['task_id'] ?? 0;
  83. $dto->taskName = $data['task_name'] ?? '';
  84. $dto->changeType = $data['change_type'] ?? '';
  85. $dto->status = $data['status'] ?? '';
  86. $dto->progress = $data['progress'] ?? null;
  87. $dto->progressDetails = $data['progress_details'] ?? null;
  88. $dto->completedAt = $data['completed_at'] ?? null;
  89. $dto->rewardedAt = $data['rewarded_at'] ?? null;
  90. $dto->rewards = $data['rewards'] ?? null;
  91. $dto->updatedAt = $data['updated_at'] ?? time();
  92. return $dto;
  93. }
  94. /**
  95. * 转换为数组
  96. *
  97. * @return array 数组数据
  98. */
  99. public function toArray(): array
  100. {
  101. return [
  102. 'task_id' => $this->taskId,
  103. 'task_name' => $this->taskName,
  104. 'change_type' => $this->changeType,
  105. 'status' => $this->status,
  106. 'progress' => $this->progress,
  107. 'progress_details' => $this->progressDetails,
  108. 'completed_at' => $this->completedAt,
  109. 'rewarded_at' => $this->rewardedAt,
  110. 'rewards' => $this->rewards,
  111. 'updated_at' => $this->updatedAt,
  112. ];
  113. }
  114. /**
  115. * 获取变更类型的中文描述
  116. *
  117. * @return string 中文描述
  118. */
  119. public function getChangeTypeDescription(): string
  120. {
  121. return match ($this->changeType) {
  122. 'progress_updated' => '进度更新',
  123. 'completed' => '任务完成',
  124. 'reward_claimed' => '奖励领取',
  125. default => '未知变更',
  126. };
  127. }
  128. /**
  129. * 获取任务状态的中文描述
  130. *
  131. * @return string 中文描述
  132. */
  133. public function getStatusDescription(): string
  134. {
  135. return match ($this->status) {
  136. 'in_progress' => '进行中',
  137. 'completed' => '已完成',
  138. 'rewarded' => '已领取奖励',
  139. default => '未知状态',
  140. };
  141. }
  142. /**
  143. * 检查是否为进度更新
  144. *
  145. * @return bool 是否为进度更新
  146. */
  147. public function isProgressUpdate(): bool
  148. {
  149. return $this->changeType === 'progress_updated';
  150. }
  151. /**
  152. * 检查是否为任务完成
  153. *
  154. * @return bool 是否为任务完成
  155. */
  156. public function isTaskCompleted(): bool
  157. {
  158. return $this->changeType === 'completed';
  159. }
  160. /**
  161. * 检查是否为奖励领取
  162. *
  163. * @return bool 是否为奖励领取
  164. */
  165. public function isRewardClaimed(): bool
  166. {
  167. return $this->changeType === 'reward_claimed';
  168. }
  169. /**
  170. * 获取格式化的进度文本
  171. *
  172. * @return string 格式化的进度文本
  173. */
  174. public function getFormattedProgress(): string
  175. {
  176. if ($this->progress === null) {
  177. return '无进度信息';
  178. }
  179. return $this->progress . '%';
  180. }
  181. /**
  182. * 获取奖励数量
  183. *
  184. * @return int 奖励数量
  185. */
  186. public function getRewardCount(): int
  187. {
  188. return $this->rewards ? count($this->rewards) : 0;
  189. }
  190. }