CraftService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Module\GameItems\Services;
  3. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  4. use App\Module\GameItems\Models\ItemRecipe;
  5. use App\Module\GameItems\Models\ItemCraftLog;
  6. use App\Module\GameItems\Models\ItemUserRecipe;
  7. use App\Module\Game\Services\ConsumeService;
  8. use App\Module\Game\Services\RewardService;
  9. use Exception;
  10. use Illuminate\Database\Eloquent\Collection;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use UCore\Dto\Res;
  14. use UCore\Exception\LogicException;
  15. use UCore\Exception\ValidateException;
  16. /**
  17. * 物品合成服务类
  18. *
  19. * 提供物品合成相关的服务,包括合成物品、获取用户可合成配方列表、解锁配方等功能。
  20. * 该类是物品合成模块对外提供服务的主要入口,封装了物品合成的复杂逻辑。
  21. *
  22. * 所有方法均为静态方法,可直接通过类名调用。
  23. */
  24. class CraftService
  25. {
  26. /**
  27. * 合成物品
  28. *
  29. * @param int $userId 用户ID
  30. * @param int $recipeId 配方ID
  31. * @param array $options 选项
  32. * @return Res
  33. */
  34. public static function craftItem(int $userId, int $recipeId,int $quantity, array $options = []):Res
  35. {
  36. try {
  37. // 获取配方信息
  38. $recipe = ItemRecipe::with(['consumeGroup', 'rewardGroup'])->findOrFail($recipeId);
  39. // 检查配方是否激活
  40. if (!$recipe->is_active) {
  41. throw new ValidateException("配方未激活");
  42. }
  43. // 检查用户是否可以合成该配方
  44. $canCraft = $recipe->canCraftByUser($userId);
  45. if (!$canCraft['can_craft']) {
  46. throw new ValidateException($canCraft['reason']);
  47. }
  48. // 检查消耗组是否存在
  49. if (!$recipe->consume_group_id || !$recipe->consumeGroup) {
  50. throw new ValidateException("配方消耗组不存在");
  51. }
  52. // 检查奖励组是否存在
  53. if (!$recipe->reward_group_id || !$recipe->rewardGroup) {
  54. throw new ValidateException("配方奖励组不存在");
  55. }
  56. // 获取合成数量
  57. // 开启事务
  58. DB::beginTransaction();
  59. // 执行消耗组
  60. $consumeResult = ConsumeService::executeConsume(
  61. $userId,
  62. $recipe->consume_group_id,
  63. REWARD_SOURCE_TYPE::CRAFT,
  64. $recipeId,
  65. true, // 检查消耗条件
  66. $quantity // 使用数量作为倍数
  67. );
  68. if (!$consumeResult->success) {
  69. throw new LogicException("消耗失败: " . $consumeResult->message);
  70. }
  71. // 判断合成是否成功(基于配方成功率)
  72. $isSuccess = self::rollCraftSuccess($recipe->success_rate);
  73. $rewardItems = [];
  74. // 如果合成成功,执行奖励组
  75. if ($isSuccess) {
  76. $rewardResult = RewardService::grantReward(
  77. $userId,
  78. $recipe->reward_group_id,
  79. REWARD_SOURCE_TYPE::CRAFT,
  80. $recipeId,
  81. $quantity
  82. );
  83. if ($rewardResult->success) {
  84. $rewardItems = $rewardResult->items;
  85. } else {
  86. // 奖励发放失败,视为合成失败
  87. $isSuccess = false;
  88. }
  89. }
  90. // 更新用户配方使用记录
  91. $userRecipe = ItemUserRecipe::firstOrCreate(
  92. ['user_id' => $userId, 'recipe_id' => $recipeId],
  93. ['is_unlocked' => true, 'unlock_time' => now(), 'craft_count' => 0]
  94. );
  95. $userRecipe->incrementCraftCount($quantity);
  96. // 创建合成记录
  97. $craftLog = new ItemCraftLog([
  98. 'user_id' => $userId,
  99. 'recipe_id' => $recipeId,
  100. 'materials' => $consumeResult->data['consumed'] ?? [],
  101. 'result_item_id' => $isSuccess && !empty($rewardItems) ? $rewardItems[0]->targetId : null,
  102. 'result_instance_id' => null,
  103. 'result_quantity' => $isSuccess && !empty($rewardItems) ? $rewardItems[0]->quantity : 0,
  104. 'is_success' => $isSuccess,
  105. 'craft_time' => now(),
  106. 'ip_address' => $options['ip_address'] ?? request()->ip(),
  107. 'device_info' => $options['device_info'] ?? request()->userAgent(),
  108. ]);
  109. $craftLog->save();
  110. // 提交事务
  111. DB::commit();
  112. // 返回结果
  113. return Res::success();
  114. // return [
  115. // 'success' => $isSuccess,
  116. // 'consumed' => $consumeResult['consumed'] ?? [],
  117. // 'rewards' => $isSuccess ? $rewardItems : []
  118. // ];
  119. } catch (\Exception $e) {
  120. // 回滚事务
  121. if (DB::transactionLevel() > 0) {
  122. DB::rollBack();
  123. }
  124. Log::error('合成物品失败', [
  125. 'user_id' => $userId,
  126. 'recipe_id' => $recipeId,
  127. 'error' => $e->getMessage(),
  128. 'trace' => $e->getTraceAsString()
  129. ]);
  130. return Res::error('合成失败: ' . $e->getMessage());
  131. }
  132. }
  133. /**
  134. * 获取用户可合成配方列表
  135. *
  136. * @param int $userId 用户ID
  137. * @param array $filters 过滤条件
  138. * @return Collection 配方列表
  139. */
  140. public static function getUserAvailableRecipes(int $userId, array $filters = []): Collection
  141. {
  142. $query = ItemRecipe::with(['consumeGroup', 'rewardGroup', 'conditionGroup'])
  143. ->where('is_active', true)
  144. ->whereHas('userRecipes', function ($q) use ($userId) {
  145. $q->where('user_id', $userId)
  146. ->where('is_unlocked', true);
  147. });
  148. // 应用过滤条件
  149. if (isset($filters['category_id'])) {
  150. $query->where('category_id', $filters['category_id']);
  151. }
  152. return $query->orderBy('sort_order', 'asc')->get();
  153. }
  154. /**
  155. * 解锁用户配方
  156. *
  157. * @param int $userId 用户ID
  158. * @param int $recipeId 配方ID
  159. * @return bool 是否成功
  160. */
  161. public static function unlockRecipe(int $userId, int $recipeId): bool
  162. {
  163. try {
  164. $recipe = ItemRecipe::findOrFail($recipeId);
  165. // 检查配方是否激活
  166. if (!$recipe->is_active) {
  167. return false;
  168. }
  169. // 检查是否已解锁
  170. $userRecipe = ItemUserRecipe::where('user_id', $userId)
  171. ->where('recipe_id', $recipeId)
  172. ->first();
  173. if ($userRecipe && $userRecipe->is_unlocked) {
  174. return true; // 已经解锁
  175. }
  176. // 创建或更新用户配方记录
  177. if (!$userRecipe) {
  178. $userRecipe = new ItemUserRecipe([
  179. 'user_id' => $userId,
  180. 'recipe_id' => $recipeId,
  181. 'is_unlocked' => true,
  182. 'unlock_time' => now(),
  183. 'craft_count' => 0
  184. ]);
  185. $userRecipe->save();
  186. } else {
  187. $userRecipe->is_unlocked = true;
  188. $userRecipe->unlock_time = now();
  189. $userRecipe->save();
  190. }
  191. return true;
  192. } catch (\Exception $e) {
  193. Log::error('解锁配方失败', [
  194. 'user_id' => $userId,
  195. 'recipe_id' => $recipeId,
  196. 'error' => $e->getMessage(),
  197. 'trace' => $e->getTraceAsString()
  198. ]);
  199. return false;
  200. }
  201. }
  202. /**
  203. * 根据成功率判断合成是否成功
  204. *
  205. * @param float $successRate 成功率(百分比,100.00 = 100%)
  206. * @return bool 是否成功
  207. */
  208. private static function rollCraftSuccess(float $successRate): bool
  209. {
  210. // 成功率小于等于0,必定失败
  211. if ($successRate <= 0) {
  212. return false;
  213. }
  214. // 成功率大于等于100,必定成功
  215. if ($successRate >= 100) {
  216. return true;
  217. }
  218. // 生成1-100的随机数进行概率判断
  219. $randomNumber = mt_rand(1, 100);
  220. return $randomNumber <= $successRate;
  221. }
  222. }