PetFoodValidator.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Module\Pet\Validators;
  3. use App\Module\GameItems\Services\ItemService;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Log;
  6. use UCore\Validator;
  7. use UCore\Validator\ValidationMessage;
  8. /**
  9. * 宠物口粮验证器
  10. *
  11. * 用于验证物品是否为宠物口粮,根据物品的pet_power/pet_exp属性判断。
  12. * 该类提供了验证方法,用于在宠物喂养过程中验证物品是否可用作宠物口粮。
  13. */
  14. class PetFoodValidator extends Validator
  15. {
  16. use ValidationMessage;
  17. /**
  18. * 缓存键前缀
  19. *
  20. * @var string
  21. */
  22. protected $cachePrefix = 'pet_food_validator_';
  23. /**
  24. * 缓存过期时间(秒)
  25. *
  26. * @var int
  27. */
  28. protected $cacheExpiration = 3600; // 1小时
  29. /**
  30. * 验证物品是否为宠物口粮
  31. *
  32. * @param mixed $value 物品ID
  33. * @param array $data 所有数据
  34. * @return bool 验证是否通过
  35. */
  36. public function validate(mixed $value, array $data): bool
  37. {
  38. $itemId = (int)$value;
  39. // 尝试从缓存获取结果
  40. $cacheKey = "{$this->cachePrefix}{$itemId}";
  41. $cachedResult = Cache::get($cacheKey);
  42. if ($cachedResult !== null) {
  43. if (!$cachedResult) {
  44. $this->addError("物品({$itemId})不是宠物口粮");
  45. }
  46. return $cachedResult;
  47. }
  48. try {
  49. // 创建物品服务实例
  50. $itemService = new ItemService();
  51. // 获取物品信息
  52. $item = $itemService->getItemInfo($itemId);
  53. if (!$item) {
  54. // 物品不存在
  55. Cache::put($cacheKey, false, $this->cacheExpiration);
  56. $this->addError("物品({$itemId})不存在");
  57. return false;
  58. }
  59. // 检查物品是否有宠物相关属性
  60. $isPetFood = $this->checkPetFoodAttributes($item);
  61. // 缓存结果
  62. Cache::put($cacheKey, $isPetFood, $this->cacheExpiration);
  63. if (!$isPetFood) {
  64. $this->addError("物品({$itemId})不是宠物口粮");
  65. }
  66. return $isPetFood;
  67. } catch (\Exception $e) {
  68. Log::error('验证宠物口粮失败', [
  69. 'item_id' => $itemId,
  70. 'error' => $e->getMessage()
  71. ]);
  72. $this->addError('验证过程发生错误: ' . $e->getMessage());
  73. return false;
  74. }
  75. }
  76. /**
  77. * 检查物品是否具有宠物口粮属性
  78. *
  79. * @param object|array $item 物品信息
  80. * @return bool 是否具有宠物口粮属性
  81. */
  82. protected function checkPetFoodAttributes($item): bool
  83. {
  84. // 检查物品是否有pet_power或pet_exp属性
  85. $numericAttributes = $item->numeric_attributes ?? [];
  86. if (is_string($numericAttributes)) {
  87. $numericAttributes = json_decode($numericAttributes, true) ?? [];
  88. } elseif (is_object($numericAttributes)) {
  89. $numericAttributes = (array)$numericAttributes;
  90. }
  91. // 检查是否有宠物相关属性
  92. $hasPetPower = isset($numericAttributes['pet_power']) && $numericAttributes['pet_power'] > 0;
  93. $hasPetExp = isset($numericAttributes['pet_exp']) && $numericAttributes['pet_exp'] > 0;
  94. // 如果有任一宠物属性,则认为是宠物口粮
  95. return $hasPetPower || $hasPetExp;
  96. }
  97. /**
  98. * 获取物品的宠物属性
  99. *
  100. * @param int $itemId 物品ID
  101. * @return array 宠物属性
  102. */
  103. public function getPetFoodAttributes(int $itemId): array
  104. {
  105. try {
  106. // 创建物品服务实例
  107. $itemService = new ItemService();
  108. // 获取物品信息
  109. $item = $itemService->getItemInfo($itemId);
  110. if (!$item) {
  111. return [
  112. 'pet_power' => 0,
  113. 'pet_exp' => 0
  114. ];
  115. }
  116. // 获取物品的宠物属性
  117. $numericAttributes = $item->numeric_attributes ?? [];
  118. if (is_string($numericAttributes)) {
  119. $numericAttributes = json_decode($numericAttributes, true) ?? [];
  120. } elseif (is_object($numericAttributes)) {
  121. $numericAttributes = (array)$numericAttributes;
  122. }
  123. return [
  124. 'pet_power' => $numericAttributes['pet_power'] ?? 0,
  125. 'pet_exp' => $numericAttributes['pet_exp'] ?? 0
  126. ];
  127. } catch (\Exception $e) {
  128. Log::error('获取宠物口粮属性失败', [
  129. 'item_id' => $itemId,
  130. 'error' => $e->getMessage()
  131. ]);
  132. return [
  133. 'pet_power' => 0,
  134. 'pet_exp' => 0
  135. ];
  136. }
  137. }
  138. /**
  139. * 清除物品的缓存结果
  140. *
  141. * @param int $itemId 物品ID
  142. * @return void
  143. */
  144. public function clearCache(int $itemId): void
  145. {
  146. $cacheKey = "{$this->cachePrefix}{$itemId}";
  147. Cache::forget($cacheKey);
  148. }
  149. /**
  150. * 清除所有缓存结果
  151. *
  152. * @return void
  153. */
  154. public function clearAllCache(): void
  155. {
  156. // 注意:这种方式可能不适用于所有缓存驱动
  157. // 对于生产环境,可能需要更精细的缓存清理方式
  158. $pattern = "{$this->cachePrefix}*";
  159. // 使用Redis的模式匹配删除键(如果使用Redis缓存)
  160. if (config('cache.default') === 'redis') {
  161. $redis = Cache::getRedis();
  162. $keys = $redis->keys($pattern);
  163. if (!empty($keys)) {
  164. $redis->del($keys);
  165. }
  166. } else {
  167. // 对于其他缓存驱动,可能需要单独实现
  168. Log::warning('清除所有宠物口粮验证缓存可能不完全', [
  169. 'cache_driver' => config('cache.default')
  170. ]);
  171. }
  172. }
  173. }