Item.php 935 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Module\Game\Logics\Reward;
  3. use App\Module\Game\Dtos\RewardItemDto;
  4. use UCore\Exception\LogicException;
  5. class Item
  6. {
  7. /**
  8. * 处理奖励项数量(支持随机数量)
  9. */
  10. public static function processQuantity(RewardItemDto $item, int $multiplier = 1): RewardItemDto
  11. {
  12. $processedItem = clone $item;
  13. // 检查是否有数量范围设置
  14. if ($item->minQuantity > 0 && $item->maxQuantity > 0) {
  15. if($multiplier> 1){
  16. throw new LogicException('倍率下,不能随机!');
  17. }
  18. $minQty = max(1, $item->minQuantity);
  19. $maxQty = max($minQty, $item->maxQuantity);
  20. $processedItem->quantity = mt_rand($minQty, $maxQty);
  21. } else {
  22. $processedItem->quantity = max(1, $item->quantity) * $multiplier;
  23. }
  24. return $processedItem;
  25. }
  26. }