ItemDismantleRule.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 $casts = [
  46. 'priority' => 'integer',
  47. 'coin_return_rate' => 'float',
  48. 'is_active' => 'boolean',
  49. ];
  50. /**
  51. * 获取关联的物品(如果有)
  52. *
  53. * @return BelongsTo
  54. */
  55. public function item(): BelongsTo
  56. {
  57. return $this->belongsTo(Item::class, 'item_id');
  58. }
  59. /**
  60. * 获取关联的分类(如果有)
  61. *
  62. * @return BelongsTo
  63. */
  64. public function category(): BelongsTo
  65. {
  66. return $this->belongsTo(ItemCategory::class, 'category_id');
  67. }
  68. /**
  69. * 获取分解结果
  70. *
  71. * @return HasMany
  72. */
  73. public function results(): HasMany
  74. {
  75. return $this->hasMany(ItemDismantleResult::class, 'rule_id');
  76. }
  77. /**
  78. * 获取分解日志
  79. *
  80. * @return HasMany
  81. */
  82. public function dismantleLogs(): HasMany
  83. {
  84. return $this->hasMany(ItemDismantleLog::class, 'rule_id');
  85. }
  86. /**
  87. * 计算分解返还的金币
  88. *
  89. * @param int $itemPrice 物品价格
  90. * @return int
  91. */
  92. public function calculateCoinReturn(int $itemPrice): int
  93. {
  94. if ($this->coin_return_rate <= 0) {
  95. return 0;
  96. }
  97. return (int)($itemPrice * $this->coin_return_rate);
  98. }
  99. /**
  100. * 获取分解结果
  101. *
  102. * @return array
  103. */
  104. public function getDismantleResults(): array
  105. {
  106. $results = [];
  107. $dismantleResults = $this->results()->with('resultItem')->get();
  108. foreach ($dismantleResults as $result) {
  109. // 根据概率决定是否获得该物品
  110. if (mt_rand(1, 10000) <= $result->chance * 100) {
  111. // 计算数量
  112. $quantity = $result->min_quantity;
  113. if ($result->max_quantity > $result->min_quantity) {
  114. $quantity = mt_rand($result->min_quantity, $result->max_quantity);
  115. }
  116. if ($quantity > 0) {
  117. $results[] = [
  118. 'item_id' => $result->result_item_id,
  119. 'item_name' => $result->resultItem->name,
  120. 'quantity' => $quantity,
  121. ];
  122. }
  123. }
  124. }
  125. return $results;
  126. }
  127. }