|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Module\Pet\Logic;
|
|
|
|
|
|
+use App\Module\GameItems\Dtos\ItemDto;
|
|
|
use App\Module\GameItems\Services\ItemService;
|
|
|
use App\Module\Pet\Enums\PetStatus;
|
|
|
use App\Module\Pet\Events\PetCreatedEvent;
|
|
|
@@ -219,10 +220,16 @@ class PetLogic
|
|
|
public function feedPet(int $petId, int $itemId, int $amount): array
|
|
|
{
|
|
|
// 获取宠物信息
|
|
|
+ /**
|
|
|
+ * @var PetUser $pet
|
|
|
+ */
|
|
|
$pet = PetUser::findOrFail($petId);
|
|
|
|
|
|
|
|
|
// 获取物品信息
|
|
|
+ /**
|
|
|
+ * @var ItemDto $item
|
|
|
+ */
|
|
|
$item = ItemService::getItemInfo($itemId);
|
|
|
if (!$item) {
|
|
|
throw new LogicException("物品不存在");
|
|
|
@@ -246,41 +253,11 @@ class PetLogic
|
|
|
}
|
|
|
|
|
|
// 计算获得的经验值和体力
|
|
|
- $expGained = ($item->numeric_attributes->pet_exp ?? 0) * $amount;
|
|
|
- $staminaGained = ($item->numeric_attributes->pet_power ?? 0) * $amount;
|
|
|
-
|
|
|
- // 更新宠物状态为喂养中
|
|
|
- $oldStatus = $pet->status;
|
|
|
- $pet->status = PetStatus::FEEDING;
|
|
|
- $pet->save();
|
|
|
-
|
|
|
- // 创建旧状态数据
|
|
|
- $oldStatusData = new \App\Module\Pet\Dtos\DataPetSimpleDto();
|
|
|
- $oldStatusData->id = $pet->id;
|
|
|
- $oldStatusData->status = $oldStatus->value;
|
|
|
-
|
|
|
- // 创建新状态数据
|
|
|
- $newStatusData = new \App\Module\Pet\Dtos\DataPetSimpleDto();
|
|
|
- $newStatusData->id = $pet->id;
|
|
|
- $newStatusData->status = PetStatus::FEEDING->value;
|
|
|
-
|
|
|
- // 创建完整的宠物数据
|
|
|
- $petData = new \App\Module\Pet\Dtos\DataPetSimpleDto();
|
|
|
- $petData->id = $pet->id;
|
|
|
- $petData->name = $pet->name;
|
|
|
- $petData->level = $pet->level;
|
|
|
-// $petData->grade = $pet->grade->value;
|
|
|
- $petData->power = $pet->stamina;
|
|
|
-
|
|
|
- // 触发宠物状态变更事件
|
|
|
- event(new PetStatusChangedEvent(
|
|
|
- $pet->user_id,
|
|
|
- $pet->id,
|
|
|
- $oldStatusData,
|
|
|
- $newStatusData,
|
|
|
- 'pet_feed',
|
|
|
- $petData
|
|
|
- ));
|
|
|
+ $expGained = ($item->numericAttributes['pet_exp'] ?? 0) * $amount;
|
|
|
+ $staminaGained = ($item->numericAttributes['pet_power'] ?? 0) * $amount;
|
|
|
+ if(!$expGained && !$staminaGained ){
|
|
|
+ throw new LogicException('错误的参数');
|
|
|
+ }
|
|
|
|
|
|
// 增加经验值
|
|
|
$levelUpOccurred = $this->addExperience($petId, $expGained);
|
|
|
@@ -323,12 +300,12 @@ class PetLogic
|
|
|
));
|
|
|
|
|
|
Log::info('宠物喂养成功', [
|
|
|
- 'pet_id' => $petId,
|
|
|
- 'item_id' => $itemId,
|
|
|
- 'amount' => $amount,
|
|
|
- 'exp_gained' => $expGained,
|
|
|
+ 'pet_id' => $petId,
|
|
|
+ 'item_id' => $itemId,
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'exp_gained' => $expGained,
|
|
|
'stamina_gained' => $staminaGained,
|
|
|
- 'level_up' => $levelUpOccurred
|
|
|
+ 'level_up' => $levelUpOccurred
|
|
|
]);
|
|
|
|
|
|
return [
|
|
|
@@ -390,7 +367,7 @@ class PetLogic
|
|
|
|
|
|
// 计算实际增加的体力值
|
|
|
$oldStamina = $pet->stamina;
|
|
|
- $newStamina = min($maxStamina, $oldStamina + $staminaAmount);
|
|
|
+ $newStamina = max($maxStamina, $oldStamina + $staminaAmount);
|
|
|
$actualGained = $newStamina - $oldStamina;
|
|
|
|
|
|
// 更新体力值
|