| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace App\Module\Pet\Commands;
- use Illuminate\Console\Command;
- use App\Module\Pet\Services\PetStealService;
- use App\Module\Pet\Models\PetUser;
- use App\Module\Pet\Models\PetStealLog;
- use App\Module\Pet\Enums\PetStatus;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- /**
- * 测试偷菜功能命令
- */
- class TestStealFunctionCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'pet:test-steal {stealer_id} {target_id} {land_id} {pet_id}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试宠物偷菜功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $stealerId = (int) $this->argument('stealer_id');
- $targetId = (int) $this->argument('target_id');
- $landId = (int) $this->argument('land_id');
- $petId = (int) $this->argument('pet_id');
- $this->info("开始测试偷菜功能");
- $this->info("偷菜者ID: {$stealerId}");
- $this->info("被偷者ID: {$targetId}");
- $this->info("土地ID: {$landId}");
- $this->info("宠物ID: {$petId}");
- $this->line('');
- try {
- // 获取作物ID
- $crop = FarmCrop::where('land_id', $landId)->first();
- if (!$crop) {
- $this->error("土地上没有作物");
- return 1;
- }
- $plantId = $crop->id;
- $this->info("作物ID: {$plantId}");
- $this->line('');
- // 1. 显示偷菜前的状态
- $this->showPreStealStatus($stealerId, $targetId, $landId);
- // 2. 检查是否可以偷菜
- $canStealResult = PetStealService::canSteal($stealerId, $targetId, $landId);
- $this->info("偷菜检查结果:");
- $this->line(" 可以偷菜: " . ($canStealResult['can_steal'] ? '是' : '否'));
- if (!$canStealResult['can_steal']) {
- $this->line(" 原因: " . $canStealResult['reason']);
- return 1;
- }
- $this->line('');
- // 3. 执行偷菜
- $this->info("执行偷菜操作...");
- $result = PetStealService::stealCrop($stealerId, $targetId, $plantId, $petId);
- // 4. 显示偷菜结果
- $this->showStealResult($result);
- // 5. 显示偷菜后的状态
- $this->showPostStealStatus($stealerId, $targetId, $landId);
- $this->info("偷菜测试完成!");
- return 0;
- } catch (\Exception $e) {
- $this->error("偷菜测试失败: " . $e->getMessage());
- return 1;
- }
- }
- /**
- * 显示偷菜前的状态
- *
- * @param int $stealerId 偷菜者ID
- * @param int $targetId 被偷者ID
- * @param int $landId 土地ID
- */
- private function showPreStealStatus(int $stealerId, int $targetId, int $landId): void
- {
- $this->info("偷菜前状态:");
- // 偷菜者宠物状态
- $stealerPet = PetUser::where('user_id', $stealerId)->first();
- if ($stealerPet) {
- $this->line(" 偷菜者宠物: 等级{$stealerPet->level}, 体力{$stealerPet->stamina}, 状态{$stealerPet->status->name}");
- } else {
- $this->line(" 偷菜者宠物: 无");
- }
- // 被偷者宠物状态
- $targetPet = PetUser::where('user_id', $targetId)->first();
- if ($targetPet) {
- $this->line(" 被偷者宠物: 等级{$targetPet->level}, 体力{$targetPet->stamina}, 状态{$targetPet->status->name}");
- } else {
- $this->line(" 被偷者宠物: 无");
- }
- // 土地和作物状态
- $land = FarmLand::find($landId);
- if ($land) {
- $crop = FarmCrop::where('land_id', $landId)->first();
- if ($crop) {
- $this->line(" 作物状态: {$crop->growth_stage->name}, 可摘取数量: {$crop->pickable_amount}");
- } else {
- $this->line(" 作物状态: 无作物");
- }
- } else {
- $this->line(" 土地状态: 土地不存在");
- }
- // 偷菜次数
- $stealInfo = PetStealService::getStealInfo($landId);
- $this->line(" 已被偷次数: {$stealInfo['successful_steal_count']}/5");
- $this->line('');
- }
- /**
- * 显示偷菜结果
- *
- * @param \App\Module\Pet\Dtos\StealResultDto $result 偷菜结果
- */
- private function showStealResult($result): void
- {
- $this->info("偷菜结果:");
- $this->line(" 偷菜成功: " . ($result->success ? '是' : '否'));
- $this->line(" 被防御: " . ($result->defended ? '是' : '否'));
- $this->line(" 偷取数量: {$result->stealAmount}");
- $this->line(" 偷菜者体力消耗: {$result->stealerStaminaCost}");
- $this->line(" 被偷者体力消耗: {$result->targetStaminaCost}");
-
- if ($result->itemId) {
- $this->line(" 获得物品ID: {$result->itemId}");
- }
-
- if ($result->failReason) {
- $this->line(" 失败原因: {$result->failReason}");
- }
-
- $this->line(" 偷菜日志ID: {$result->stealLogId}");
- $this->line('');
- }
- /**
- * 显示偷菜后的状态
- *
- * @param int $stealerId 偷菜者ID
- * @param int $targetId 被偷者ID
- * @param int $landId 土地ID
- */
- private function showPostStealStatus(int $stealerId, int $targetId, int $landId): void
- {
- $this->info("偷菜后状态:");
- // 偷菜者宠物状态
- $stealerPet = PetUser::where('user_id', $stealerId)->first();
- if ($stealerPet) {
- $this->line(" 偷菜者宠物: 等级{$stealerPet->level}, 体力{$stealerPet->stamina}, 状态{$stealerPet->status->name}");
- }
- // 被偷者宠物状态
- $targetPet = PetUser::where('user_id', $targetId)->first();
- if ($targetPet) {
- $this->line(" 被偷者宠物: 等级{$targetPet->level}, 体力{$targetPet->stamina}, 状态{$targetPet->status->name}");
- }
- // 作物状态
- $crop = FarmCrop::where('land_id', $landId)->first();
- if ($crop) {
- $this->line(" 作物状态: {$crop->growth_stage->name}, 可摘取数量: {$crop->pickable_amount}");
- }
- // 偷菜次数
- $stealInfo = PetStealService::getStealInfo($landId);
- $this->line(" 已被偷次数: {$stealInfo['successful_steal_count']}/5");
- // 偷菜统计
- $stealerStats = PetStealService::getStealStats($stealerId);
- $targetStats = PetStealService::getStealStats($targetId);
-
- $this->line(" 偷菜者今日偷菜次数: {$stealerStats['today']['steal_count']}");
- $this->line(" 被偷者今日被偷次数: {$targetStats['today']['being_stolen_count']}");
- $this->line('');
- }
- }
|