| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Module\Game\Logics\RewardProcessors;
- use App\Module\Game\Dtos\RewardItemDto;
- use App\Module\GameItems\Services\ItemService;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 物品奖励处理器
- *
- * 处理物品类型的奖励发放
- */
- class ItemRewardProcessor
- {
- /**
- * 处理物品奖励
- *
- * @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 {
- // 发放物品奖励,传递来源信息
- ItemService::addItem($userId, $item->targetId, $item->quantity, [
- 'param1' => $item->param1,
- 'param2' => $item->param2,
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'source' => 'reward', // 保持向后兼容
- 'extra_data' => $item->extraData
- ]);
- Log::info("物品奖励发放成功", [
- 'userId' => $userId,
- 'itemId' => $item->targetId,
- 'quantity' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId
- ]);
- } catch (Exception $e) {
- Log::error("物品奖励发放失败", [
- 'userId' => $userId,
- 'itemId' => $item->targetId,
- 'quantity' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- throw new Exception("物品奖励发放失败: " . $e->getMessage());
- }
- }
- }
|