| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace App\Module\Farm\Validators;
- use App\Module\Farm\Enums\BUFF_TYPE;
- use App\Module\Farm\Services\BuffService;
- use App\Module\GameItems\Services\ItemService;
- use UCore\Validator;
- /**
- * 神像激活验证器
- *
- * 验证神像激活操作是否有效,包括神像类型、用户是否拥有神像物品、是否已有有效buff等
- */
- class GodActivationValidator extends Validator
- {
- /**
- * 验证神像激活操作
- *
- * @param mixed $value 神像ID
- * @param array $data 包含用户ID和物品ID的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- $godId = (int)$value;
- // 从 args 获取字段键名
- $userIdKey = $this->args[0] ?? 'user_id';
- $itemIdKey = $this->args[1] ?? 'item_id';
- $userId = $data[$userIdKey] ?? null;
- $itemId = $data[$itemIdKey] ?? null;
- if (!$userId) {
- $this->addError('用户ID不能为空');
- return false;
- }
- if (!$itemId) {
- $this->addError('物品ID不能为空');
- return false;
- }
- try {
- // 验证神像类型是否有效
- if (!$this->validateGodType($godId)) {
- return false;
- }
- // 验证用户是否已有该神像的有效加持
- if (!$this->validateNoActiveBuff($userId, $godId)) {
- return false;
- }
- // 验证用户是否拥有神像物品
- if (!$this->validateUserHasGodItem($userId, $itemId)) {
- return false;
- }
- // 验证物品是否具有神像时间属性和正确的神像种类
- if (!$this->validateItemGodAttribute($itemId, $godId)) {
- return false;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证神像激活时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 验证神像类型是否有效
- *
- * @param int $godId 神像ID
- * @return bool
- */
- private function validateGodType(int $godId): bool
- {
- $validGodTypes = [
- BUFF_TYPE::HARVEST_GOD->value,
- BUFF_TYPE::RAIN_GOD->value,
- BUFF_TYPE::WEED_KILLER_GOD->value,
- BUFF_TYPE::PEST_CLEANER_GOD->value
- ];
- if (!in_array($godId, $validGodTypes)) {
- $this->addError("无效的神像类型: {$godId}");
- return false;
- }
- return true;
- }
- /**
- * 验证用户是否已有该神像的有效加持
- *
- * @param int $userId 用户ID
- * @param int $godId 神像ID
- * @return bool
- */
- private function validateNoActiveBuff(int $userId, int $godId): bool
- {
- $existingBuff = BuffService::getActiveUserBuff($userId, $godId);
- if ($existingBuff) {
- $godName = BUFF_TYPE::getName($godId);
- $expireTime = $existingBuff->expire_time->format('Y-m-d H:i:s');
- $this->addError("{$godName}已激活,有效期至:{$expireTime}");
- return false;
- }
- return true;
- }
- /**
- * 验证用户是否拥有神像物品
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @return bool
- */
- private function validateUserHasGodItem(int $userId, int $itemId): bool
- {
- $userItems = ItemService::getUserItems($userId, ['item_id' => $itemId]);
- if ($userItems->isEmpty()) {
- $this->addError("您没有该神像物品");
- return false;
- }
- // 检查用户是否有足够的数量(至少需要1个)
- $totalQuantity = $userItems->sum('quantity');
- if ($totalQuantity < 1) {
- $this->addError("您的神像物品数量不足");
- return false;
- }
- return true;
- }
- /**
- * 验证物品是否具有神像时间属性和正确的神像种类
- *
- * @param int $itemId 物品ID
- * @param int $godId 神像ID
- * @return bool
- */
- private function validateItemGodAttribute(int $itemId, int $godId): bool
- {
- $godDuration = ItemService::getItemNumericAttribute($itemId, 'god_duration_seconds');
- if ($godDuration <= 0) {
- $this->addError("该物品不是神像物品");
- return false;
- }
- $godType = ItemService::getItemNumericAttribute($itemId, 'god_type');
- if ($godType !== $godId) {
- $godName = BUFF_TYPE::getName($godId);
- $this->addError("该物品不能开启{$godName}");
- return false;
- }
- return true;
- }
- }
|