| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Module\GameItems\Validators;
- use App\Module\Game\Services\ConsumeService;
- use UCore\Validator;
- /**
- * 合成消耗验证器
- *
- * 基于组系统验证用户是否有足够的资源进行合成
- */
- class CraftConsumeValidator 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';
- $quantityKey = $this->args[1] ?? 'quantity';
- $recipeKey = $this->args[2] ?? 'recipe';
- $userId = $data[$userIdKey] ?? null;
- $quantity = $data[$quantityKey] ?? 1;
- $recipe = $this->validation->$recipeKey ?? null;
- if (!$userId || !$recipe) {
- $this->addError('验证合成消耗时缺少必要参数');
- return false;
- }
- // 检查配方是否有消耗组
- if (!$recipe->consume_group_id) {
- $this->addError('配方未配置消耗组');
- return false;
- }
- try {
- // 使用消耗组服务检查消耗条件
- $checkResult = ConsumeService::checkConsume($userId, $recipe->consume_group_id, $quantity);
- if (!$checkResult->success) {
- $this->addError($checkResult->message);
- return false;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证合成消耗时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|