DisasterRemovalItemValidator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Module\Farm\Validators;
  3. use App\Module\Farm\Enums\DISASTER_TYPE;
  4. use App\Module\GameItems\Services\ItemService;
  5. use UCore\Validator;
  6. /**
  7. * 灾害去除物品验证器
  8. *
  9. * 验证物品是否为有效的灾害去除道具,包括用户是否拥有该物品以及物品是否具有对应的灾害去除属性
  10. */
  11. class DisasterRemovalItemValidator extends Validator
  12. {
  13. /**
  14. * 灾害类型与物品属性的映射关系
  15. */
  16. private const DISASTER_ITEM_ATTRIBUTES = [
  17. DISASTER_TYPE::DROUGHT->value => 'fram_drought_rate', // 干旱 -> 浇水概率
  18. DISASTER_TYPE::PEST->value => 'fram_pesticide_rate', // 虫害 -> 除虫概率
  19. DISASTER_TYPE::WEED->value => 'fram_weedicide_rate', // 杂草 -> 除草概率
  20. ];
  21. /**
  22. * 灾害类型与操作名称的映射关系
  23. */
  24. private const DISASTER_ACTION_NAMES = [
  25. DISASTER_TYPE::DROUGHT->value => '浇水',
  26. DISASTER_TYPE::PEST->value => '除虫',
  27. DISASTER_TYPE::WEED->value => '除草',
  28. ];
  29. /**
  30. * 验证物品是否为有效的灾害去除道具
  31. *
  32. * @param mixed $value 物品ID
  33. * @param array $data 包含用户ID和灾害类型的数组
  34. * @return bool 验证是否通过
  35. */
  36. public function validate(mixed $value, array $data): bool
  37. {
  38. $itemId = (int)$value;
  39. // 从 args 获取字段键名
  40. $userIdKey = $this->args[0] ?? 'user_id';
  41. $disasterTypeKey = $this->args[1] ?? 'disaster_type';
  42. $userId = $data[$userIdKey] ?? null;
  43. $disasterType = $data[$disasterTypeKey] ?? null;
  44. if (!$userId) {
  45. $this->addError('用户ID不能为空');
  46. return false;
  47. }
  48. if (!$disasterType) {
  49. $this->addError('灾害类型不能为空');
  50. return false;
  51. }
  52. // 验证灾害类型是否支持
  53. if (!isset(self::DISASTER_ITEM_ATTRIBUTES[$disasterType])) {
  54. $this->addError("不支持的灾害类型: {$disasterType}");
  55. return false;
  56. }
  57. try {
  58. // 验证用户是否拥有该物品
  59. if (!$this->validateUserHasItem($userId, $itemId, $disasterType)) {
  60. return false;
  61. }
  62. // 验证物品是否具有对应的灾害去除属性
  63. if (!$this->validateItemAttribute($itemId, $disasterType)) {
  64. return false;
  65. }
  66. return true;
  67. } catch (\Exception $e) {
  68. $this->addError('验证物品时发生错误: ' . $e->getMessage());
  69. return false;
  70. }
  71. }
  72. /**
  73. * 验证用户是否拥有指定物品
  74. *
  75. * @param int $userId 用户ID
  76. * @param int $itemId 物品ID
  77. * @param int $disasterType 灾害类型
  78. * @return bool
  79. */
  80. private function validateUserHasItem(int $userId, int $itemId, int $disasterType): bool
  81. {
  82. $hasItem = ItemService::getUserItems($userId, ['item_id' => $itemId]);
  83. if ($hasItem->isEmpty()) {
  84. $actionName = self::DISASTER_ACTION_NAMES[$disasterType];
  85. $this->addError("您没有该{$actionName}物品");
  86. return false;
  87. }
  88. return true;
  89. }
  90. /**
  91. * 验证物品是否具有对应的灾害去除属性
  92. *
  93. * @param int $itemId 物品ID
  94. * @param int $disasterType 灾害类型
  95. * @return bool
  96. */
  97. private function validateItemAttribute(int $itemId, int $disasterType): bool
  98. {
  99. $attributeName = self::DISASTER_ITEM_ATTRIBUTES[$disasterType];
  100. $rate = ItemService::getItemNumericAttribute($itemId, $attributeName);
  101. if ($rate <= 0) {
  102. $actionName = self::DISASTER_ACTION_NAMES[$disasterType];
  103. $this->addError("不是{$actionName}物品");
  104. return false;
  105. }
  106. return true;
  107. }
  108. }