| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Module\GameItems\Validators;
- use App\Module\Game\Services\ConditionService;
- use UCore\Validator;
- /**
- * 合成条件验证器
- *
- * 基于组系统验证用户是否满足合成条件
- */
- class CraftConditionValidator extends Validator
- {
- /**
- * 验证合成条件
- *
- * @param mixed $value 配方ID
- * @param array $data 包含用户ID的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- // 从 args 获取参数键名
- $userIdKey = $this->args[0] ?? 'user_id';
- $recipeKey = $this->args[1] ?? 'recipe';
- $userId = $data[$userIdKey] ?? null;
- $recipe = $this->validation->$recipeKey ?? null;
- if (!$userId || !$recipe) {
- $this->addError('验证合成条件时缺少必要参数');
- return false;
- }
- // 如果配方没有条件组,则直接通过
- if (!$recipe->condition_group_id) {
- return true;
- }
- try {
- // 使用条件组服务检查条件
- $checkResult = ConditionService::checkCondition($userId, $recipe->condition_group_id);
-
- if (!$checkResult['success']) {
- $this->addError($checkResult['message']);
- return false;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证合成条件时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|