ItemDismantleRule.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use UCore\ModelCore;
  6. /**
  7. * 物品分解规则
  8. *
  9. * field start
  10. * @property int $id 规则ID,主键
  11. * @property int $item_id 物品ID,外键关联kku_item_items表
  12. * @property int $category_id 分类ID,外键关联kku_item_categories表
  13. * @property int $min_rarity 最小适用稀有度
  14. * @property int $max_rarity 最大适用稀有度
  15. * @property int $priority 规则优先级
  16. * @property int $is_active 是否激活(0:否, 1:是)
  17. * @property \Carbon\Carbon $created_at 创建时间
  18. * @property \Carbon\Carbon $updated_at 更新时间
  19. * field end
  20. */
  21. class ItemDismantleRule extends ModelCore
  22. {
  23. /**
  24. * 与模型关联的表名
  25. *
  26. * @var string
  27. */
  28. protected $table = 'item_dismantle_rules';
  29. // attrlist start
  30. protected $fillable = [
  31. 'id',
  32. 'item_id',
  33. 'category_id',
  34. 'min_rarity',
  35. 'max_rarity',
  36. 'priority',
  37. 'is_active',
  38. ];
  39. // attrlist end
  40. /**
  41. * 可批量赋值的属性
  42. *
  43. * @var array
  44. */
  45. protected $fillable = [
  46. 'item_id',
  47. 'category_id',
  48. 'priority',
  49. 'coin_return_rate',
  50. 'is_active',
  51. ];
  52. /**
  53. * 应该被转换为原生类型的属性
  54. *
  55. * @var array
  56. */
  57. protected $casts = [
  58. 'priority' => 'integer',
  59. 'coin_return_rate' => 'float',
  60. 'is_active' => 'boolean',
  61. ];
  62. /**
  63. * 获取关联的物品(如果有)
  64. *
  65. * @return BelongsTo
  66. */
  67. public function item(): BelongsTo
  68. {
  69. return $this->belongsTo(ItemItem::class, 'item_id');
  70. }
  71. /**
  72. * 获取关联的分类(如果有)
  73. *
  74. * @return BelongsTo
  75. */
  76. public function category(): BelongsTo
  77. {
  78. return $this->belongsTo(ItemCategory::class, 'category_id');
  79. }
  80. /**
  81. * 获取分解结果
  82. *
  83. * @return HasMany
  84. */
  85. public function results(): HasMany
  86. {
  87. return $this->hasMany(ItemDismantleResult::class, 'rule_id');
  88. }
  89. /**
  90. * 获取分解日志
  91. *
  92. * @return HasMany
  93. */
  94. public function dismantleLogs(): HasMany
  95. {
  96. return $this->hasMany(ItemDismantleLog::class, 'rule_id');
  97. }
  98. /**
  99. * 计算分解返还的金币
  100. *
  101. * @param int $itemPrice 物品价格
  102. * @return int
  103. */
  104. public function calculateCoinReturn(int $itemPrice): int
  105. {
  106. if ($this->coin_return_rate <= 0) {
  107. return 0;
  108. }
  109. return (int)($itemPrice * $this->coin_return_rate);
  110. }
  111. /**
  112. * 获取分解结果
  113. *
  114. * @return array
  115. */
  116. public function getDismantleResults(): array
  117. {
  118. $results = [];
  119. $dismantleResults = $this->results()->with('resultItem')->get();
  120. foreach ($dismantleResults as $result) {
  121. // 根据概率决定是否获得该物品
  122. if (mt_rand(1, 10000) <= $result->chance * 100) {
  123. // 计算数量
  124. $quantity = $result->min_quantity;
  125. if ($result->max_quantity > $result->min_quantity) {
  126. $quantity = mt_rand($result->min_quantity, $result->max_quantity);
  127. }
  128. if ($quantity > 0) {
  129. $results[] = [
  130. 'item_id' => $result->result_item_id,
  131. 'item_name' => $result->resultItem->name,
  132. 'quantity' => $quantity,
  133. ];
  134. }
  135. }
  136. }
  137. return $results;
  138. }
  139. }