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; } }