| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace App\Module\Task\Commands;
- use App\Module\Pet\Events\PetExpGainedEvent;
- use App\Module\Pet\Models\PetUser;
- use App\Module\Task\Models\Task;
- use App\Module\Task\Models\TaskUserTask;
- use App\Module\Task\Services\TaskConditionService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- /**
- * 测试玫瑰之心任务命令
- *
- * 用于测试宠物经验累计任务的完整流程
- */
- class TestRoseHeartTaskCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'task:test-rose-heart {user_id} {--exp=500 : 每次增加的经验值} {--times=5 : 测试次数}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试玫瑰之心任务的完整流程';
- /**
- * 任务条件服务
- *
- * @var TaskConditionService
- */
- protected TaskConditionService $taskConditionService;
- /**
- * 构造函数
- *
- * @param TaskConditionService $taskConditionService
- */
- public function __construct(TaskConditionService $taskConditionService)
- {
- parent::__construct();
- $this->taskConditionService = $taskConditionService;
- }
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $userId = (int)$this->argument('user_id');
- $expPerTime = (int)$this->option('exp');
- $times = (int)$this->option('times');
- $this->info("开始测试玫瑰之心任务");
- $this->info("用户ID: {$userId}");
- $this->info("每次经验: {$expPerTime}");
- $this->info("测试次数: {$times}");
- $this->line('');
- try {
- // 检查用户是否有宠物
- $pet = PetUser::where('user_id', $userId)->first();
- if (!$pet) {
- $this->error("用户 {$userId} 没有宠物,无法测试");
- return 1;
- }
- $this->info("找到宠物: ID={$pet->id}, 名称={$pet->name}, 当前经验={$pet->experience}");
- $this->line('');
- // 检查玫瑰之心任务
- $roseHeartTask = Task::where('name', '玫瑰之心')->first();
- if (!$roseHeartTask) {
- $this->error("玫瑰之心任务不存在");
- return 1;
- }
- $this->info("找到玫瑰之心任务: ID={$roseHeartTask->id}");
- $this->line('');
- // 开始测试循环
- for ($i = 1; $i <= $times; $i++) {
- $this->info("=== 第 {$i} 次测试 ===");
-
- // 模拟宠物获得经验
- $this->simulatePetExpGain($userId, $pet->id, $expPerTime, $i);
-
- // 检查任务状态
- $this->checkTaskStatus($userId, $roseHeartTask->id);
-
- $this->line('');
-
- // 暂停一下,便于观察
- sleep(1);
- }
- $this->info("测试完成!");
- return 0;
- } catch (\Exception $e) {
- $this->error("测试失败: " . $e->getMessage());
- return 1;
- }
- }
- /**
- * 模拟宠物获得经验
- *
- * @param int $userId 用户ID
- * @param int $petId 宠物ID
- * @param int $expAmount 经验值
- * @param int $round 轮次
- * @return void
- */
- protected function simulatePetExpGain(int $userId, int $petId, int $expAmount, int $round): void
- {
- $this->info("模拟宠物获得 {$expAmount} 点经验...");
-
- // 触发宠物经验增加事件
- event(new PetExpGainedEvent(
- $userId,
- $petId,
- $expAmount,
- 'test_command',
- $round
- ));
-
- $this->info("✓ 宠物经验增加事件已触发");
- }
- /**
- * 检查任务状态
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @return void
- */
- protected function checkTaskStatus(int $userId, int $taskId): void
- {
- // 检查用户任务状态
- $userTask = TaskUserTask::where('user_id', $userId)
- ->where('task_id', $taskId)
- ->first();
- if (!$userTask) {
- $this->warn("⚠ 用户还没有接取玫瑰之心任务");
- return;
- }
- $this->info("任务状态: {$userTask->status}");
- $this->info("任务进度: {$userTask->progress}%");
-
- if ($userTask->completed_at) {
- $this->info("✓ 任务完成时间: {$userTask->completed_at}");
- }
-
- if ($userTask->rewarded_at) {
- $this->info("✓ 奖励领取时间: {$userTask->rewarded_at}");
- }
- // 检查任务进度详情
- $this->checkTaskProgress($userId, $taskId);
- }
- /**
- * 检查任务进度详情
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @return void
- */
- protected function checkTaskProgress(int $userId, int $taskId): void
- {
- try {
- $progressData = DB::table('task_user_progress as tup')
- ->join('task_achievement_conditions as tac', 'tup.achievement_condition_id', '=', 'tac.id')
- ->join('task_conditions as tc', 'tac.condition_id', '=', 'tc.id')
- ->where('tup.user_id', $userId)
- ->where('tup.task_id', $taskId)
- ->select([
- 'tc.name as condition_name',
- 'tup.current_value',
- 'tup.target_value',
- 'tac.operator'
- ])
- ->first();
- if ($progressData) {
- $this->info("条件: {$progressData->condition_name}");
- $this->info("进度: {$progressData->current_value} / {$progressData->target_value} ({$progressData->operator})");
-
- $percentage = round(($progressData->current_value / $progressData->target_value) * 100, 2);
- $this->info("完成度: {$percentage}%");
- } else {
- $this->warn("⚠ 没有找到任务进度数据");
- }
- } catch (\Exception $e) {
- $this->error("检查任务进度失败: " . $e->getMessage());
- }
- }
- }
|