ItemRewardProcessor.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Module\Game\Logics\RewardProcessors;
  3. use App\Module\Game\Dtos\RewardItemDto;
  4. use App\Module\GameItems\Services\ItemService;
  5. use Exception;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 物品奖励处理器
  9. *
  10. * 处理物品类型的奖励发放
  11. */
  12. class ItemRewardProcessor
  13. {
  14. /**
  15. * 处理物品奖励
  16. *
  17. * @param int $userId 用户ID
  18. * @param RewardItemDto $item 奖励项
  19. * @param string $sourceType 来源类型
  20. * @param int $sourceId 来源ID
  21. * @return void
  22. * @throws Exception
  23. */
  24. public static function process(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
  25. {
  26. try {
  27. // 发放物品奖励,传递来源信息
  28. ItemService::addItem($userId, $item->targetId, $item->quantity, [
  29. 'param1' => $item->param1,
  30. 'param2' => $item->param2,
  31. 'source_type' => $sourceType,
  32. 'source_id' => $sourceId,
  33. 'source' => 'reward', // 保持向后兼容
  34. 'extra_data' => $item->extraData
  35. ]);
  36. Log::info("物品奖励发放成功", [
  37. 'userId' => $userId,
  38. 'itemId' => $item->targetId,
  39. 'quantity' => $item->quantity,
  40. 'sourceType' => $sourceType,
  41. 'sourceId' => $sourceId
  42. ]);
  43. } catch (Exception $e) {
  44. Log::error("物品奖励发放失败", [
  45. 'userId' => $userId,
  46. 'itemId' => $item->targetId,
  47. 'quantity' => $item->quantity,
  48. 'sourceType' => $sourceType,
  49. 'sourceId' => $sourceId,
  50. 'error' => $e->getMessage()
  51. ]);
  52. throw new Exception("物品奖励发放失败: " . $e->getMessage());
  53. }
  54. }
  55. }