| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Module\Game\Logics\RewardProcessors;
- use App\Module\Game\Dtos\RewardItemDto;
- use App\Module\Game\Services\SkinService;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 皮肤奖励处理器
- *
- * 处理皮肤(SKIN)类型的奖励发放
- */
- class SkinRewardProcessor
- {
- /**
- * 处理皮肤奖励
- *
- * @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 {
- // 验证皮肤ID
- if (!SkinService::isValidSkinId($item->targetId)) {
- throw new Exception("无效的皮肤ID: {$item->targetId}");
- }
- // 检查用户是否已拥有该皮肤
- if (SkinService::hasSkin($userId, $item->targetId)) {
- Log::info("用户已拥有该皮肤,跳过发放", [
- 'userId' => $userId,
- 'skinId' => $item->targetId,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId
- ]);
- return; // 已拥有则跳过,不抛异常
- }
- // 为用户添加皮肤
- $result = SkinService::addSkin($userId, $item->targetId);
-
- if (!$result->success) {
- throw new Exception("皮肤奖励发放失败: " . $result->errorMessage);
- }
- Log::info("皮肤奖励发放成功", [
- 'userId' => $userId,
- 'skinId' => $item->targetId,
- 'skinName' => SkinService::getSkinName($item->targetId),
- 'quantity' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId
- ]);
- } catch (Exception $e) {
- Log::error("皮肤奖励处理失败", [
- 'userId' => $userId,
- 'rewardType' => $item->rewardType,
- 'targetId' => $item->targetId,
- 'quantity' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- throw $e;
- }
- }
- }
|