ItemRecipe.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use App\Module\GameItems\Casts\TransactionDetailsCast;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use UCore\ModelCore;
  7. /**
  8. * 物品合成配方
  9. *
  10. * field start
  11. * @property int $id 配方ID,主键
  12. * @property string $name 配方名称
  13. * @property int $result_item_id 产出物品ID,外键关联kku_item_items表
  14. * @property int $result_min_quantity 最小产出数量
  15. * @property int $result_max_quantity 最大产出数量
  16. * @property float $success_rate 成功率(百分比,最大100)
  17. * @property object|array $coin_cost 货币成本,以JSON格式存储多种货币类型和数量
  18. * @property int $level_required 所需等级
  19. * @property int $is_default_unlocked 是否默认解锁(0:否, 1:是)
  20. * @property object|array $unlock_condition 解锁条件,以JSON格式存储
  21. * @property int $cooldown_seconds 冷却时间(秒)
  22. * @property int $category_id 配方分类ID
  23. * @property int $sort_order 排序权重
  24. * @property int $is_active 是否激活(0:否, 1:是)
  25. * @property \Carbon\Carbon $created_at 创建时间
  26. * @property \Carbon\Carbon $updated_at 更新时间
  27. * field end
  28. */
  29. class ItemRecipe extends ModelCore
  30. {
  31. /**
  32. * 与模型关联的表名
  33. *
  34. * @var string
  35. */
  36. protected $table = 'item_recipes';
  37. // attrlist start
  38. protected $fillable = [
  39. 'id',
  40. 'name',
  41. 'result_item_id',
  42. 'result_min_quantity',
  43. 'result_max_quantity',
  44. 'success_rate',
  45. 'coin_cost',
  46. 'level_required',
  47. 'is_default_unlocked',
  48. 'unlock_condition',
  49. 'cooldown_seconds',
  50. 'category_id',
  51. 'sort_order',
  52. 'is_active',
  53. ];
  54. // attrlist end
  55. /**
  56. * 应该被转换为原生类型的属性
  57. *
  58. * @var array
  59. */
  60. protected $casts = [
  61. 'result_quantity' => 'integer',
  62. 'success_rate' => 'float',
  63. 'coin_cost' => TransactionDetailsCast::class,
  64. 'cooldown_seconds' => 'integer',
  65. 'is_visible' => 'boolean',
  66. 'unlock_condition' => TransactionDetailsCast::class,
  67. ];
  68. /**
  69. * 获取合成结果物品
  70. *
  71. * @return BelongsTo
  72. */
  73. public function resultItem(): BelongsTo
  74. {
  75. return $this->belongsTo(Item::class, 'result_item_id');
  76. }
  77. /**
  78. * 获取配方材料
  79. *
  80. * @return HasMany
  81. */
  82. public function materials(): HasMany
  83. {
  84. return $this->hasMany(ItemRecipeMaterial::class, 'recipe_id');
  85. }
  86. /**
  87. * 获取用户配方解锁状态
  88. *
  89. * @return HasMany
  90. */
  91. public function userRecipes(): HasMany
  92. {
  93. return $this->hasMany(ItemUserRecipe::class, 'recipe_id');
  94. }
  95. /**
  96. * 获取合成记录
  97. *
  98. * @return HasMany
  99. */
  100. public function craftLogs(): HasMany
  101. {
  102. return $this->hasMany(ItemCraftLog::class, 'recipe_id');
  103. }
  104. /**
  105. * 检查用户是否已解锁该配方
  106. *
  107. * @param int $userId 用户ID
  108. * @return bool
  109. */
  110. public function isUnlockedByUser(int $userId): bool
  111. {
  112. $userRecipe = $this->userRecipes()
  113. ->where('user_id', $userId)
  114. ->first();
  115. return $userRecipe && $userRecipe->is_unlocked;
  116. }
  117. /**
  118. * 检查用户是否可以合成该配方
  119. *
  120. * @param int $userId 用户ID
  121. * @return array 检查结果,包含是否可合成和原因
  122. */
  123. public function canCraftByUser(int $userId): array
  124. {
  125. // 检查配方是否已解锁
  126. if (!$this->isUnlockedByUser($userId)) {
  127. return [
  128. 'can_craft' => false,
  129. 'reason' => '配方未解锁',
  130. ];
  131. }
  132. // 检查冷却时间
  133. if ($this->cooldown_seconds > 0) {
  134. $lastCraft = $this->craftLogs()
  135. ->where('user_id', $userId)
  136. ->where('created_at', '>', now()->subSeconds($this->cooldown_seconds))
  137. ->first();
  138. if ($lastCraft) {
  139. $remainingSeconds = $this->cooldown_seconds - now()->diffInSeconds($lastCraft->created_at);
  140. return [
  141. 'can_craft' => false,
  142. 'reason' => '冷却中',
  143. 'remaining_seconds' => $remainingSeconds,
  144. ];
  145. }
  146. }
  147. // 检查材料是否足够
  148. $materials = $this->materials;
  149. $missingMaterials = [];
  150. foreach ($materials as $material) {
  151. $userItem = ItemUser::where('user_id', $userId)
  152. ->where('item_id', $material->item_id)
  153. ->whereNull('instance_id')
  154. ->sum('quantity');
  155. if ($userItem < $material->quantity) {
  156. $missingMaterials[] = [
  157. 'item_id' => $material->item_id,
  158. 'item_name' => $material->item->name,
  159. 'required' => $material->quantity,
  160. 'available' => $userItem,
  161. ];
  162. }
  163. }
  164. if (!empty($missingMaterials)) {
  165. return [
  166. 'can_craft' => false,
  167. 'reason' => '材料不足',
  168. 'missing_materials' => $missingMaterials,
  169. ];
  170. }
  171. return [
  172. 'can_craft' => true,
  173. ];
  174. }
  175. }