ItemDismantleRule.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 string $name 规则名称
  12. * @property string $code 规则编码(唯一)
  13. * @property string $description 规则描述
  14. * @property int $item_id 物品ID,外键关联kku_item_items表
  15. * @property int $category_id 分类ID,外键关联kku_item_categories表
  16. * @property int $consume_group_id 消耗组ID
  17. * @property int $reward_group_id 奖励组ID
  18. * @property int $condition_group_id 条件组ID
  19. * @property int $min_rarity 最小适用稀有度
  20. * @property int $max_rarity 最大适用稀有度
  21. * @property int $priority 规则优先级
  22. * @property int $sort_order 排序权重
  23. * @property bool $is_active 是否激活(0:否, 1:是)
  24. * @property \Carbon\Carbon $created_at 创建时间
  25. * @property \Carbon\Carbon $updated_at 更新时间
  26. * field end
  27. */
  28. class ItemDismantleRule extends ModelCore
  29. {
  30. /**
  31. * 与模型关联的表名
  32. *
  33. * @var string
  34. */
  35. protected $table = 'item_dismantle_rules';
  36. // attrlist start
  37. protected $fillable = [
  38. 'id',
  39. 'name',
  40. 'code',
  41. 'description',
  42. 'item_id',
  43. 'category_id',
  44. 'consume_group_id',
  45. 'reward_group_id',
  46. 'condition_group_id',
  47. 'min_rarity',
  48. 'max_rarity',
  49. 'priority',
  50. 'sort_order',
  51. 'is_active',
  52. ];
  53. // attrlist end
  54. /**
  55. * 应该被转换为原生类型的属性
  56. *
  57. * @var array
  58. */
  59. protected $casts = [
  60. 'item_id' => 'integer',
  61. 'category_id' => 'integer',
  62. 'consume_group_id' => 'integer',
  63. 'reward_group_id' => 'integer',
  64. 'condition_group_id' => 'integer',
  65. 'min_rarity' => 'integer',
  66. 'max_rarity' => 'integer',
  67. 'priority' => 'integer',
  68. 'sort_order' => 'integer',
  69. 'is_active' => 'boolean',
  70. ];
  71. /**
  72. * 获取关联的物品(如果有)
  73. *
  74. * @return BelongsTo
  75. */
  76. public function item(): BelongsTo
  77. {
  78. return $this->belongsTo(Item::class, 'item_id');
  79. }
  80. /**
  81. * 获取关联的分类(如果有)
  82. *
  83. * @return BelongsTo
  84. */
  85. public function category(): BelongsTo
  86. {
  87. return $this->belongsTo(ItemCategory::class, 'category_id');
  88. }
  89. /**
  90. * 获取消耗组
  91. *
  92. * @return BelongsTo
  93. */
  94. public function consumeGroup(): BelongsTo
  95. {
  96. return $this->belongsTo(\App\Module\Game\Models\GameConsumeGroup::class, 'consume_group_id');
  97. }
  98. /**
  99. * 获取奖励组
  100. *
  101. * @return BelongsTo
  102. */
  103. public function rewardGroup(): BelongsTo
  104. {
  105. return $this->belongsTo(\App\Module\Game\Models\GameRewardGroup::class, 'reward_group_id');
  106. }
  107. /**
  108. * 获取条件组
  109. *
  110. * @return BelongsTo
  111. */
  112. public function conditionGroup(): BelongsTo
  113. {
  114. return $this->belongsTo(\App\Module\Game\Models\GameConditionGroup::class, 'condition_group_id');
  115. }
  116. /**
  117. * 获取分解结果(保留兼容性)
  118. *
  119. * @return HasMany
  120. */
  121. public function results(): HasMany
  122. {
  123. return $this->hasMany(ItemDismantleResult::class, 'rule_id');
  124. }
  125. /**
  126. * 获取分解日志
  127. *
  128. * @return HasMany
  129. */
  130. public function dismantleLogs(): HasMany
  131. {
  132. return $this->hasMany(ItemDismantleLog::class, 'rule_id');
  133. }
  134. /**
  135. * 计算分解返还的金币
  136. *
  137. * @param int $itemPrice 物品价格
  138. * @return int
  139. */
  140. public function calculateCoinReturn(int $itemPrice): int
  141. {
  142. if ($this->coin_return_rate <= 0) {
  143. return 0;
  144. }
  145. return (int)($itemPrice * $this->coin_return_rate);
  146. }
  147. /**
  148. * 检查用户是否可以使用该分解规则
  149. *
  150. * @param int $userId 用户ID
  151. * @param int $itemId 物品ID
  152. * @param int $quantity 分解数量
  153. * @return array 检查结果
  154. */
  155. public function canDismantleByUser(int $userId, int $itemId, int $quantity = 1): array
  156. {
  157. // 检查规则是否激活
  158. if (!$this->is_active) {
  159. return [
  160. 'can_dismantle' => false,
  161. 'reason' => '分解规则未激活',
  162. ];
  163. }
  164. // 检查条件组(如果设置了条件组)
  165. if ($this->condition_group_id && $this->conditionGroup) {
  166. // TODO: 实现ConditionService::checkConditionGroup方法
  167. // $conditionService = app(\App\Module\Game\Services\ConditionService::class);
  168. // $conditionResult = $conditionService->checkConditionGroup($userId, $this->condition_group_id);
  169. //
  170. // if (!$conditionResult['is_satisfied']) {
  171. // return [
  172. // 'can_dismantle' => false,
  173. // 'reason' => '条件不满足',
  174. // 'condition_details' => $conditionResult['details'],
  175. // ];
  176. // }
  177. }
  178. // 检查消耗组(如果设置了消耗组,用于额外消耗,如分解工具等)
  179. if ($this->consume_group_id && $this->consumeGroup) {
  180. // TODO: 实现ConsumeService::checkConsumeGroup方法
  181. // $consumeService = app(\App\Module\Game\Services\ConsumeService::class);
  182. // $consumeResult = $consumeService->checkConsumeGroup($userId, $this->consume_group_id);
  183. //
  184. // if (!$consumeResult['can_consume']) {
  185. // return [
  186. // 'can_dismantle' => false,
  187. // 'reason' => '分解工具或材料不足',
  188. // 'consume_details' => $consumeResult['details'],
  189. // ];
  190. // }
  191. }
  192. return [
  193. 'can_dismantle' => true,
  194. ];
  195. }
  196. /**
  197. * 获取分解结果(使用奖励组)
  198. *
  199. * @return array
  200. */
  201. public function getDismantleResults(): array
  202. {
  203. // 如果设置了奖励组,使用奖励组系统
  204. if ($this->reward_group_id && $this->rewardGroup) {
  205. // TODO: 实现RewardService::generateRewards方法
  206. // $rewardService = app(\App\Module\Game\Services\RewardService::class);
  207. // return $rewardService->generateRewards($this->reward_group_id);
  208. // 临时返回空数组,等待RewardService实现
  209. return [];
  210. }
  211. // 兼容旧系统:使用原有的分解结果表
  212. $results = [];
  213. $dismantleResults = $this->results()->with('resultItem')->get();
  214. foreach ($dismantleResults as $result) {
  215. // 根据概率决定是否获得该物品
  216. if (mt_rand(1, 10000) <= $result->base_chance * 100) {
  217. // 计算数量
  218. $quantity = $result->min_quantity;
  219. if ($result->max_quantity > $result->min_quantity) {
  220. $quantity = mt_rand($result->min_quantity, $result->max_quantity);
  221. }
  222. if ($quantity > 0) {
  223. $results[] = [
  224. 'item_id' => $result->result_item_id,
  225. 'item_name' => $result->resultItem->name,
  226. 'quantity' => $quantity,
  227. ];
  228. }
  229. }
  230. }
  231. return $results;
  232. }
  233. }