| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\Module\Game\Logics\RewardProcessors;
- use App\Module\Game\Dtos\RewardItemDto;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 宠物经验奖励处理器
- *
- * 处理宠物经验(PET_EXP)类型的奖励发放
- */
- class PetExpRewardProcessor
- {
- /**
- * 处理宠物经验奖励
- *
- * @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();
- $levelUpOccurred = $petLogic->addExperienceReward($item->targetId, $item->quantity, $sourceType, $sourceId);
- Log::info("宠物经验奖励发放成功", [
- 'userId' => $userId,
- 'petId' => $item->targetId,
- 'expAmount' => $item->quantity,
- 'levelUp' => $levelUpOccurred,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId
- ]);
- } catch (Exception $e) {
- Log::error("宠物经验奖励发放失败", [
- 'userId' => $userId,
- 'petId' => $item->targetId,
- 'expAmount' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- throw new Exception("宠物经验奖励发放失败: " . $e->getMessage());
- }
- }
- }
|