TestRoseHeartTaskCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Module\Task\Commands;
  3. use App\Module\Pet\Events\PetExpGainedEvent;
  4. use App\Module\Pet\Models\PetUser;
  5. use App\Module\Task\Models\Task;
  6. use App\Module\Task\Models\TaskUserTask;
  7. use App\Module\Task\Services\TaskConditionService;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 测试玫瑰之心任务命令
  12. *
  13. * 用于测试宠物经验累计任务的完整流程
  14. */
  15. class TestRoseHeartTaskCommand extends Command
  16. {
  17. /**
  18. * 命令签名
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'task:test-rose-heart {user_id} {--exp=500 : 每次增加的经验值} {--times=5 : 测试次数}';
  23. /**
  24. * 命令描述
  25. *
  26. * @var string
  27. */
  28. protected $description = '测试玫瑰之心任务的完整流程';
  29. /**
  30. * 任务条件服务
  31. *
  32. * @var TaskConditionService
  33. */
  34. protected TaskConditionService $taskConditionService;
  35. /**
  36. * 构造函数
  37. *
  38. * @param TaskConditionService $taskConditionService
  39. */
  40. public function __construct(TaskConditionService $taskConditionService)
  41. {
  42. parent::__construct();
  43. $this->taskConditionService = $taskConditionService;
  44. }
  45. /**
  46. * 执行命令
  47. *
  48. * @return int
  49. */
  50. public function handle(): int
  51. {
  52. $userId = (int)$this->argument('user_id');
  53. $expPerTime = (int)$this->option('exp');
  54. $times = (int)$this->option('times');
  55. $this->info("开始测试玫瑰之心任务");
  56. $this->info("用户ID: {$userId}");
  57. $this->info("每次经验: {$expPerTime}");
  58. $this->info("测试次数: {$times}");
  59. $this->line('');
  60. try {
  61. // 检查用户是否有宠物
  62. $pet = PetUser::where('user_id', $userId)->first();
  63. if (!$pet) {
  64. $this->error("用户 {$userId} 没有宠物,无法测试");
  65. return 1;
  66. }
  67. $this->info("找到宠物: ID={$pet->id}, 名称={$pet->name}, 当前经验={$pet->experience}");
  68. $this->line('');
  69. // 检查玫瑰之心任务
  70. $roseHeartTask = Task::where('name', '玫瑰之心')->first();
  71. if (!$roseHeartTask) {
  72. $this->error("玫瑰之心任务不存在");
  73. return 1;
  74. }
  75. $this->info("找到玫瑰之心任务: ID={$roseHeartTask->id}");
  76. $this->line('');
  77. // 开始测试循环
  78. for ($i = 1; $i <= $times; $i++) {
  79. $this->info("=== 第 {$i} 次测试 ===");
  80. // 模拟宠物获得经验
  81. $this->simulatePetExpGain($userId, $pet->id, $expPerTime, $i);
  82. // 检查任务状态
  83. $this->checkTaskStatus($userId, $roseHeartTask->id);
  84. $this->line('');
  85. // 暂停一下,便于观察
  86. sleep(1);
  87. }
  88. $this->info("测试完成!");
  89. return 0;
  90. } catch (\Exception $e) {
  91. $this->error("测试失败: " . $e->getMessage());
  92. return 1;
  93. }
  94. }
  95. /**
  96. * 模拟宠物获得经验
  97. *
  98. * @param int $userId 用户ID
  99. * @param int $petId 宠物ID
  100. * @param int $expAmount 经验值
  101. * @param int $round 轮次
  102. * @return void
  103. */
  104. protected function simulatePetExpGain(int $userId, int $petId, int $expAmount, int $round): void
  105. {
  106. $this->info("模拟宠物获得 {$expAmount} 点经验...");
  107. // 触发宠物经验增加事件
  108. event(new PetExpGainedEvent(
  109. $userId,
  110. $petId,
  111. $expAmount,
  112. 'test_command',
  113. $round
  114. ));
  115. $this->info("✓ 宠物经验增加事件已触发");
  116. }
  117. /**
  118. * 检查任务状态
  119. *
  120. * @param int $userId 用户ID
  121. * @param int $taskId 任务ID
  122. * @return void
  123. */
  124. protected function checkTaskStatus(int $userId, int $taskId): void
  125. {
  126. // 检查用户任务状态
  127. $userTask = TaskUserTask::where('user_id', $userId)
  128. ->where('task_id', $taskId)
  129. ->first();
  130. if (!$userTask) {
  131. $this->warn("⚠ 用户还没有接取玫瑰之心任务");
  132. return;
  133. }
  134. $this->info("任务状态: {$userTask->status}");
  135. $this->info("任务进度: {$userTask->progress}%");
  136. if ($userTask->completed_at) {
  137. $this->info("✓ 任务完成时间: {$userTask->completed_at}");
  138. }
  139. if ($userTask->rewarded_at) {
  140. $this->info("✓ 奖励领取时间: {$userTask->rewarded_at}");
  141. }
  142. // 检查任务进度详情
  143. $this->checkTaskProgress($userId, $taskId);
  144. }
  145. /**
  146. * 检查任务进度详情
  147. *
  148. * @param int $userId 用户ID
  149. * @param int $taskId 任务ID
  150. * @return void
  151. */
  152. protected function checkTaskProgress(int $userId, int $taskId): void
  153. {
  154. try {
  155. $progressData = DB::table('task_user_progress as tup')
  156. ->join('task_achievement_conditions as tac', 'tup.achievement_condition_id', '=', 'tac.id')
  157. ->join('task_conditions as tc', 'tac.condition_id', '=', 'tc.id')
  158. ->where('tup.user_id', $userId)
  159. ->where('tup.task_id', $taskId)
  160. ->select([
  161. 'tc.name as condition_name',
  162. 'tup.current_value',
  163. 'tup.target_value',
  164. 'tac.operator'
  165. ])
  166. ->first();
  167. if ($progressData) {
  168. $this->info("条件: {$progressData->condition_name}");
  169. $this->info("进度: {$progressData->current_value} / {$progressData->target_value} ({$progressData->operator})");
  170. $percentage = round(($progressData->current_value / $progressData->target_value) * 100, 2);
  171. $this->info("完成度: {$percentage}%");
  172. } else {
  173. $this->warn("⚠ 没有找到任务进度数据");
  174. }
  175. } catch (\Exception $e) {
  176. $this->error("检查任务进度失败: " . $e->getMessage());
  177. }
  178. }
  179. }