ItemDismantleResult.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 $casts = [
  47. 'min_quantity' => 'integer',
  48. 'max_quantity' => 'integer',
  49. 'chance' => 'float',
  50. ];
  51. /**
  52. * 获取关联的分解规则
  53. *
  54. * @return BelongsTo
  55. */
  56. public function rule(): BelongsTo
  57. {
  58. return $this->belongsTo(ItemDismantleRule::class, 'rule_id');
  59. }
  60. /**
  61. * 获取关联的结果物品
  62. *
  63. * @return BelongsTo
  64. */
  65. public function resultItem(): BelongsTo
  66. {
  67. return $this->belongsTo(Item::class, 'result_item_id');
  68. }
  69. /**
  70. * 获取随机数量
  71. *
  72. * @return int
  73. */
  74. public function getRandomQuantity(): int
  75. {
  76. if ($this->min_quantity == $this->max_quantity) {
  77. return $this->min_quantity;
  78. }
  79. return mt_rand($this->min_quantity, $this->max_quantity);
  80. }
  81. /**
  82. * 检查是否命中概率
  83. *
  84. * @return bool
  85. */
  86. public function isChanceHit(): bool
  87. {
  88. if ($this->chance >= 1.0) {
  89. return true;
  90. }
  91. return mt_rand(1, 10000) <= $this->chance * 10000;
  92. }
  93. }