| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace App\Module\Game\Logics\RewardProcessors;
- use App\Module\Game\Dtos\RewardItemDto;
- use Exception;
- use Illuminate\Support\Facades\Log;
- use ReflectionClass;
- /**
- * 宠物体力奖励处理器
- *
- * 处理宠物体力(PET_ENERGY)类型的奖励发放
- */
- class PetEnergyRewardProcessor
- {
- /**
- * 处理宠物体力奖励
- *
- * @param int $userId 用户ID
- * @param RewardItemDto $item 奖励项
- * @param string $sourceType 来源类型
- * @param int $sourceId 来源ID
- * @return void
- * @throws Exception
- */
- public static function process(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
- {
- try {
- // 获取用户的宠物
- $pet = \App\Module\Pet\Models\PetUser::where('user_id', $userId)
- ->where('id', $item->targetId)
- ->first();
- if (!$pet) {
- throw new Exception("找不到ID为 {$item->targetId} 的宠物");
- }
- // 使用宠物逻辑类来增加体力
- $petLogic = new \App\Module\Pet\Logic\PetLogic();
- $reflection = new ReflectionClass($petLogic);
- $method = $reflection->getMethod('addStamina');
- $method->setAccessible(true);
- $actualGained = $method->invoke($petLogic, $item->targetId, $item->quantity);
- Log::info("宠物体力奖励发放成功", [
- 'userId' => $userId,
- 'petId' => $item->targetId,
- 'staminaAmount' => $item->quantity,
- 'actualGained' => $actualGained,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId
- ]);
- } catch (Exception $e) {
- Log::error("宠物体力奖励发放失败", [
- 'userId' => $userId,
- 'petId' => $item->targetId,
- 'staminaAmount' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- throw new Exception("宠物体力奖励发放失败: " . $e->getMessage());
- }
- }
- }
|