| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Module\AppGame\Validations;
- use App\Module\Pet\Validators\PetStealValidator;
- use App\Module\Pet\Validators\PetOwnershipValidator;
- /**
- * 宠物偷菜验证类
- *
- * 用于验证宠物偷菜请求的参数
- */
- class PetVegetealValidation extends ValidationBase
- {
- /**
- * 被偷的玩家ID
- *
- * @var int
- */
- public int $user_id = 0;
- /**
- * 宠物ID
- *
- * @var int
- */
- public int $pet_id = 0;
- /**
- * 作物ID (对应land_id)
- *
- * @var int
- */
- public int $plant_id = 0;
- /**
- * 当前用户ID (由系统注入)
- *
- * @var int
- */
- public int $current_user_id = 0;
- /**
- * 使用Protobuf-Message创建验证器,并注入current_user_id
- *
- * @param \Google\Protobuf\Internal\Message $message
- * @param $scene
- * @return static
- */
- public static function makeByProrobufUser(\Google\Protobuf\Internal\Message $message, $scene = ''): static
- {
- $data = json_decode($message->serializeToJsonString(), true);
- // 映射protobuf字段名到验证类字段名
- $mappedData = [];
- if (isset($data['userId'])) {
- $mappedData['user_id'] = $data['userId'];
- }
- if (isset($data['petId'])) {
- $mappedData['pet_id'] = $data['petId'];
- }
- if (isset($data['plantId'])) {
- $mappedData['plant_id'] = $data['plantId'];
- }
- // 注入当前用户ID
- $mappedData['current_user_id'] = \App\Module\AppGame\SessionApp::getUserId();
- return new static($mappedData, [], [], $scene);
- }
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'user_id,pet_id,plant_id,current_user_id', 'required'
- ],
- [
- 'user_id,pet_id,plant_id', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- // 验证宠物归属权和状态
- [
- 'pet_id', new PetOwnershipValidator($this, ['current_user_id']),
- 'msg' => '宠物验证失败'
- ],
- // 验证偷菜操作是否有效
- [
- 'plant_id', new PetStealValidator($this, ['current_user_id', 'user_id']),
- 'msg' => '偷菜操作验证失败'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [];
- }
- /**
- * 字段翻译
- *
- * @return array
- */
- public function translates(): array
- {
- return [
- 'user_id' => '被偷玩家ID',
- 'pet_id' => '宠物ID',
- 'plant_id' => '作物ID',
- 'current_user_id' => '当前用户ID',
- ];
- }
- }
|