GodActivationValidator.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Module\Farm\Validators;
  3. use App\Module\Farm\Enums\BUFF_TYPE;
  4. use App\Module\Farm\Services\BuffService;
  5. use App\Module\GameItems\Services\ItemService;
  6. use UCore\Validator;
  7. /**
  8. * 神像激活验证器
  9. *
  10. * 验证神像激活操作是否有效,包括神像类型、用户是否拥有神像物品、是否已有有效buff等
  11. */
  12. class GodActivationValidator extends Validator
  13. {
  14. /**
  15. * 验证神像激活操作
  16. *
  17. * @param mixed $value 神像ID
  18. * @param array $data 包含用户ID和物品ID的数组
  19. * @return bool 验证是否通过
  20. */
  21. public function validate(mixed $value, array $data): bool
  22. {
  23. $godId = (int)$value;
  24. // 从 args 获取字段键名
  25. $userIdKey = $this->args[0] ?? 'user_id';
  26. $itemIdKey = $this->args[1] ?? 'item_id';
  27. $userId = $data[$userIdKey] ?? null;
  28. $itemId = $data[$itemIdKey] ?? null;
  29. if (!$userId) {
  30. $this->addError('用户ID不能为空');
  31. return false;
  32. }
  33. if (!$itemId) {
  34. $this->addError('物品ID不能为空');
  35. return false;
  36. }
  37. try {
  38. // 验证神像类型是否有效
  39. if (!$this->validateGodType($godId)) {
  40. return false;
  41. }
  42. // 验证用户是否已有该神像的有效加持
  43. if (!$this->validateNoActiveBuff($userId, $godId)) {
  44. return false;
  45. }
  46. // 验证用户是否拥有神像物品
  47. if (!$this->validateUserHasGodItem($userId, $itemId)) {
  48. return false;
  49. }
  50. // 验证物品是否具有神像时间属性和正确的神像种类
  51. if (!$this->validateItemGodAttribute($itemId, $godId)) {
  52. return false;
  53. }
  54. return true;
  55. } catch (\Exception $e) {
  56. $this->addError('验证神像激活时发生错误: ' . $e->getMessage());
  57. return false;
  58. }
  59. }
  60. /**
  61. * 验证神像类型是否有效
  62. *
  63. * @param int $godId 神像ID
  64. * @return bool
  65. */
  66. private function validateGodType(int $godId): bool
  67. {
  68. $validGodTypes = [
  69. BUFF_TYPE::HARVEST_GOD->value,
  70. BUFF_TYPE::RAIN_GOD->value,
  71. BUFF_TYPE::WEED_KILLER_GOD->value,
  72. BUFF_TYPE::PEST_CLEANER_GOD->value
  73. ];
  74. if (!in_array($godId, $validGodTypes)) {
  75. $this->addError("无效的神像类型: {$godId}");
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * 验证用户是否已有该神像的有效加持
  82. *
  83. * @param int $userId 用户ID
  84. * @param int $godId 神像ID
  85. * @return bool
  86. */
  87. private function validateNoActiveBuff(int $userId, int $godId): bool
  88. {
  89. $existingBuff = BuffService::getActiveUserBuff($userId, $godId);
  90. if ($existingBuff) {
  91. $godName = BUFF_TYPE::getName($godId);
  92. $expireTime = $existingBuff->expire_time->format('Y-m-d H:i:s');
  93. $this->addError("{$godName}已激活,有效期至:{$expireTime}");
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * 验证用户是否拥有神像物品
  100. *
  101. * @param int $userId 用户ID
  102. * @param int $itemId 物品ID
  103. * @return bool
  104. */
  105. private function validateUserHasGodItem(int $userId, int $itemId): bool
  106. {
  107. $userItems = ItemService::getUserItems($userId, ['item_id' => $itemId]);
  108. if ($userItems->isEmpty()) {
  109. $this->addError("您没有该神像物品");
  110. return false;
  111. }
  112. // 检查用户是否有足够的数量(至少需要1个)
  113. $totalQuantity = $userItems->sum('quantity');
  114. if ($totalQuantity < 1) {
  115. $this->addError("您的神像物品数量不足");
  116. return false;
  117. }
  118. return true;
  119. }
  120. /**
  121. * 验证物品是否具有神像时间属性和正确的神像种类
  122. *
  123. * @param int $itemId 物品ID
  124. * @param int $godId 神像ID
  125. * @return bool
  126. */
  127. private function validateItemGodAttribute(int $itemId, int $godId): bool
  128. {
  129. $godDuration = ItemService::getItemNumericAttribute($itemId, 'god_duration_seconds');
  130. if ($godDuration <= 0) {
  131. $this->addError("该物品不是神像物品");
  132. return false;
  133. }
  134. $godType = ItemService::getItemNumericAttribute($itemId, 'god_type');
  135. if ($godType !== $godId) {
  136. $godName = BUFF_TYPE::getName($godId);
  137. $this->addError("该物品不能开启{$godName}");
  138. return false;
  139. }
  140. return true;
  141. }
  142. }