TestStealFunctionCommand.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Module\Pet\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\Pet\Services\PetStealService;
  5. use App\Module\Pet\Models\PetUser;
  6. use App\Module\Pet\Models\PetStealLog;
  7. use App\Module\Pet\Enums\PetStatus;
  8. use App\Module\Farm\Models\FarmLand;
  9. use App\Module\Farm\Models\FarmCrop;
  10. use App\Module\Farm\Enums\GROWTH_STAGE;
  11. /**
  12. * 测试偷菜功能命令
  13. */
  14. class TestStealFunctionCommand extends Command
  15. {
  16. /**
  17. * 命令签名
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'pet:test-steal {stealer_id} {target_id} {land_id} {pet_id}';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = '测试宠物偷菜功能';
  28. /**
  29. * 执行命令
  30. *
  31. * @return int
  32. */
  33. public function handle(): int
  34. {
  35. $stealerId = (int) $this->argument('stealer_id');
  36. $targetId = (int) $this->argument('target_id');
  37. $landId = (int) $this->argument('land_id');
  38. $petId = (int) $this->argument('pet_id');
  39. $this->info("开始测试偷菜功能");
  40. $this->info("偷菜者ID: {$stealerId}");
  41. $this->info("被偷者ID: {$targetId}");
  42. $this->info("土地ID: {$landId}");
  43. $this->info("宠物ID: {$petId}");
  44. $this->line('');
  45. try {
  46. // 获取作物ID
  47. $crop = FarmCrop::where('land_id', $landId)->first();
  48. if (!$crop) {
  49. $this->error("土地上没有作物");
  50. return 1;
  51. }
  52. $plantId = $crop->id;
  53. $this->info("作物ID: {$plantId}");
  54. $this->line('');
  55. // 1. 显示偷菜前的状态
  56. $this->showPreStealStatus($stealerId, $targetId, $landId);
  57. // 2. 检查是否可以偷菜
  58. $canStealResult = PetStealService::canSteal($stealerId, $targetId, $landId);
  59. $this->info("偷菜检查结果:");
  60. $this->line(" 可以偷菜: " . ($canStealResult['can_steal'] ? '是' : '否'));
  61. if (!$canStealResult['can_steal']) {
  62. $this->line(" 原因: " . $canStealResult['reason']);
  63. return 1;
  64. }
  65. $this->line('');
  66. // 3. 执行偷菜
  67. $this->info("执行偷菜操作...");
  68. $result = PetStealService::stealCrop($stealerId, $targetId, $plantId, $petId);
  69. // 4. 显示偷菜结果
  70. $this->showStealResult($result);
  71. // 5. 显示偷菜后的状态
  72. $this->showPostStealStatus($stealerId, $targetId, $landId);
  73. $this->info("偷菜测试完成!");
  74. return 0;
  75. } catch (\Exception $e) {
  76. $this->error("偷菜测试失败: " . $e->getMessage());
  77. return 1;
  78. }
  79. }
  80. /**
  81. * 显示偷菜前的状态
  82. *
  83. * @param int $stealerId 偷菜者ID
  84. * @param int $targetId 被偷者ID
  85. * @param int $landId 土地ID
  86. */
  87. private function showPreStealStatus(int $stealerId, int $targetId, int $landId): void
  88. {
  89. $this->info("偷菜前状态:");
  90. // 偷菜者宠物状态
  91. $stealerPet = PetUser::where('user_id', $stealerId)->first();
  92. if ($stealerPet) {
  93. $this->line(" 偷菜者宠物: 等级{$stealerPet->level}, 体力{$stealerPet->stamina}, 状态{$stealerPet->status->name}");
  94. } else {
  95. $this->line(" 偷菜者宠物: 无");
  96. }
  97. // 被偷者宠物状态
  98. $targetPet = PetUser::where('user_id', $targetId)->first();
  99. if ($targetPet) {
  100. $this->line(" 被偷者宠物: 等级{$targetPet->level}, 体力{$targetPet->stamina}, 状态{$targetPet->status->name}");
  101. } else {
  102. $this->line(" 被偷者宠物: 无");
  103. }
  104. // 土地和作物状态
  105. $land = FarmLand::find($landId);
  106. if ($land) {
  107. $crop = FarmCrop::where('land_id', $landId)->first();
  108. if ($crop) {
  109. $this->line(" 作物状态: {$crop->growth_stage->name}, 可摘取数量: {$crop->pickable_amount}");
  110. } else {
  111. $this->line(" 作物状态: 无作物");
  112. }
  113. } else {
  114. $this->line(" 土地状态: 土地不存在");
  115. }
  116. // 偷菜次数
  117. $stealInfo = PetStealService::getStealInfo($landId);
  118. $this->line(" 已被偷次数: {$stealInfo['successful_steal_count']}/5");
  119. $this->line('');
  120. }
  121. /**
  122. * 显示偷菜结果
  123. *
  124. * @param \App\Module\Pet\Dtos\StealResultDto $result 偷菜结果
  125. */
  126. private function showStealResult($result): void
  127. {
  128. $this->info("偷菜结果:");
  129. $this->line(" 偷菜成功: " . ($result->success ? '是' : '否'));
  130. $this->line(" 被防御: " . ($result->defended ? '是' : '否'));
  131. $this->line(" 偷取数量: {$result->stealAmount}");
  132. $this->line(" 偷菜者体力消耗: {$result->stealerStaminaCost}");
  133. $this->line(" 被偷者体力消耗: {$result->targetStaminaCost}");
  134. if ($result->itemId) {
  135. $this->line(" 获得物品ID: {$result->itemId}");
  136. }
  137. if ($result->failReason) {
  138. $this->line(" 失败原因: {$result->failReason}");
  139. }
  140. $this->line(" 偷菜日志ID: {$result->stealLogId}");
  141. $this->line('');
  142. }
  143. /**
  144. * 显示偷菜后的状态
  145. *
  146. * @param int $stealerId 偷菜者ID
  147. * @param int $targetId 被偷者ID
  148. * @param int $landId 土地ID
  149. */
  150. private function showPostStealStatus(int $stealerId, int $targetId, int $landId): void
  151. {
  152. $this->info("偷菜后状态:");
  153. // 偷菜者宠物状态
  154. $stealerPet = PetUser::where('user_id', $stealerId)->first();
  155. if ($stealerPet) {
  156. $this->line(" 偷菜者宠物: 等级{$stealerPet->level}, 体力{$stealerPet->stamina}, 状态{$stealerPet->status->name}");
  157. }
  158. // 被偷者宠物状态
  159. $targetPet = PetUser::where('user_id', $targetId)->first();
  160. if ($targetPet) {
  161. $this->line(" 被偷者宠物: 等级{$targetPet->level}, 体力{$targetPet->stamina}, 状态{$targetPet->status->name}");
  162. }
  163. // 作物状态
  164. $crop = FarmCrop::where('land_id', $landId)->first();
  165. if ($crop) {
  166. $this->line(" 作物状态: {$crop->growth_stage->name}, 可摘取数量: {$crop->pickable_amount}");
  167. }
  168. // 偷菜次数
  169. $stealInfo = PetStealService::getStealInfo($landId);
  170. $this->line(" 已被偷次数: {$stealInfo['successful_steal_count']}/5");
  171. // 偷菜统计
  172. $stealerStats = PetStealService::getStealStats($stealerId);
  173. $targetStats = PetStealService::getStealStats($targetId);
  174. $this->line(" 偷菜者今日偷菜次数: {$stealerStats['today']['steal_count']}");
  175. $this->line(" 被偷者今日被偷次数: {$targetStats['today']['being_stolen_count']}");
  176. $this->line('');
  177. }
  178. }