ItemDismantleResult.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use UCore\ModelCore;
  5. /**
  6. * 物品分解结果
  7. *
  8. * field start
  9. * @property int $id 记录ID,主键
  10. * @property int $rule_id 分解规则ID,外键关联kku_item_dismantle_rules表
  11. * @property int $result_item_id 结果物品ID,外键关联kku_item_items表
  12. * @property int $min_quantity 最小数量
  13. * @property int $max_quantity 最大数量
  14. * @property float $base_chance 基础获取概率(百分比,最大100)
  15. * @property float $rarity_factor 稀有度影响因子
  16. * @property float $quality_factor 品质影响因子
  17. * @property \Carbon\Carbon $created_at 创建时间
  18. * @property \Carbon\Carbon $updated_at 更新时间
  19. * field end
  20. */
  21. class ItemDismantleResult extends ModelCore
  22. {
  23. /**
  24. * 与模型关联的表名
  25. *
  26. * @var string
  27. */
  28. protected $table = 'item_dismantle_results';
  29. // attrlist start
  30. protected $fillable = [
  31. 'id',
  32. 'rule_id',
  33. 'result_item_id',
  34. 'min_quantity',
  35. 'max_quantity',
  36. 'base_chance',
  37. 'rarity_factor',
  38. 'quality_factor',
  39. ];
  40. // attrlist end
  41. /**
  42. * 可批量赋值的属性
  43. *
  44. * @var array
  45. */
  46. protected $fillable = [
  47. 'rule_id',
  48. 'result_item_id',
  49. 'min_quantity',
  50. 'max_quantity',
  51. 'chance',
  52. ];
  53. /**
  54. * 应该被转换为原生类型的属性
  55. *
  56. * @var array
  57. */
  58. protected $casts = [
  59. 'min_quantity' => 'integer',
  60. 'max_quantity' => 'integer',
  61. 'chance' => 'float',
  62. ];
  63. /**
  64. * 获取关联的分解规则
  65. *
  66. * @return BelongsTo
  67. */
  68. public function rule(): BelongsTo
  69. {
  70. return $this->belongsTo(ItemDismantleRule::class, 'rule_id');
  71. }
  72. /**
  73. * 获取关联的结果物品
  74. *
  75. * @return BelongsTo
  76. */
  77. public function resultItem(): BelongsTo
  78. {
  79. return $this->belongsTo(ItemItem::class, 'result_item_id');
  80. }
  81. /**
  82. * 获取随机数量
  83. *
  84. * @return int
  85. */
  86. public function getRandomQuantity(): int
  87. {
  88. if ($this->min_quantity == $this->max_quantity) {
  89. return $this->min_quantity;
  90. }
  91. return mt_rand($this->min_quantity, $this->max_quantity);
  92. }
  93. /**
  94. * 检查是否命中概率
  95. *
  96. * @return bool
  97. */
  98. public function isChanceHit(): bool
  99. {
  100. if ($this->chance >= 1.0) {
  101. return true;
  102. }
  103. return mt_rand(1, 10000) <= $this->chance * 10000;
  104. }
  105. }