| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Module\Pet\Commands;
- use App\Module\Pet\Logic\PetLogic;
- use App\Module\Pet\Models\PetUser;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试宠物喂养事件命令
- */
- class TestPetFeedEventCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'pet:test-feed-event {pet_id} {item_id} {amount=1}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试宠物喂养功能中的经验增加事件触发';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $petId = (int) $this->argument('pet_id');
- $itemId = (int) $this->argument('item_id');
- $amount = (int) $this->argument('amount');
- try {
- // 检查宠物是否存在
- $pet = PetUser::find($petId);
- if (!$pet) {
- $this->error("宠物ID {$petId} 不存在");
- return 1;
- }
- $this->info("开始测试宠物喂养事件...");
- $this->info("宠物ID: {$petId}");
- $this->info("用户ID: {$pet->user_id}");
- $this->info("物品ID: {$itemId}");
- $this->info("数量: {$amount}");
- $this->info("当前经验: {$pet->experience}");
- $this->info("当前体力: {$pet->stamina}");
- // 开启事务并使用宠物逻辑类进行喂养
- DB::beginTransaction();
- try {
- $petLogic = new PetLogic();
- $feedResult = $petLogic->feedPet($petId, $itemId, $amount);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- throw $e;
- }
- // 刷新宠物数据
- $pet->refresh();
- $this->info("喂养成功!");
- $this->info("新经验值: {$pet->experience}");
- $this->info("新体力值: {$pet->stamina}");
- $this->info("经验增加: " . ($feedResult['exp_gained'] ?? 0));
- $this->info("体力增加: " . ($feedResult['stamina_gained'] ?? 0));
- $this->info("请检查日志以确认宠物经验增加事件已正确触发和处理");
- return 0;
- } catch (\Exception $e) {
- $this->error("测试失败: " . $e->getMessage());
- Log::error('宠物喂养测试失败', [
- 'pet_id' => $petId,
- 'item_id' => $itemId,
- 'amount' => $amount,
- 'error' => $e->getMessage()
- ]);
- return 1;
- }
- }
- }
|