|
|
@@ -11,6 +11,9 @@ use Exception;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use UCore\Dto\Res;
|
|
|
+use UCore\Exception\LogicException;
|
|
|
+use UCore\Exception\ValidateException;
|
|
|
|
|
|
/**
|
|
|
* 物品合成服务类
|
|
|
@@ -28,9 +31,9 @@ class CraftService
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int $recipeId 配方ID
|
|
|
* @param array $options 选项
|
|
|
- * @return array|bool 合成结果或失败标志
|
|
|
+ * @return Res
|
|
|
*/
|
|
|
- public static function craftItem(int $userId, int $recipeId, array $options = [])
|
|
|
+ public static function craftItem(int $userId, int $recipeId,int $quantity, array $options = []):Res
|
|
|
{
|
|
|
try {
|
|
|
// 获取配方信息
|
|
|
@@ -38,27 +41,26 @@ class CraftService
|
|
|
|
|
|
// 检查配方是否激活
|
|
|
if (!$recipe->is_active) {
|
|
|
- throw new Exception("配方未激活");
|
|
|
+ throw new ValidateException("配方未激活");
|
|
|
}
|
|
|
|
|
|
// 检查用户是否可以合成该配方
|
|
|
$canCraft = $recipe->canCraftByUser($userId);
|
|
|
- if (!$canCraft['can_craft']) {
|
|
|
+ if (!$canCraft['ValidateException']) {
|
|
|
throw new Exception($canCraft['reason']);
|
|
|
}
|
|
|
|
|
|
// 检查消耗组是否存在
|
|
|
if (!$recipe->consume_group_id || !$recipe->consumeGroup) {
|
|
|
- throw new Exception("配方消耗组不存在");
|
|
|
+ throw new ValidateException("配方消耗组不存在");
|
|
|
}
|
|
|
|
|
|
// 检查奖励组是否存在
|
|
|
if (!$recipe->reward_group_id || !$recipe->rewardGroup) {
|
|
|
- throw new Exception("配方奖励组不存在");
|
|
|
+ throw new ValidateException("配方奖励组不存在");
|
|
|
}
|
|
|
|
|
|
// 获取合成数量
|
|
|
- $quantity = $options['quantity'] ?? 1;
|
|
|
|
|
|
// 开启事务
|
|
|
DB::beginTransaction();
|
|
|
@@ -74,7 +76,7 @@ class CraftService
|
|
|
);
|
|
|
|
|
|
if (!$consumeResult['success']) {
|
|
|
- throw new Exception("消耗失败: " . $consumeResult['message']);
|
|
|
+ throw new LogicException("消耗失败: " . $consumeResult['message']);
|
|
|
}
|
|
|
|
|
|
// 判断合成是否成功(基于配方成功率)
|
|
|
@@ -87,7 +89,8 @@ class CraftService
|
|
|
$userId,
|
|
|
$recipe->reward_group_id,
|
|
|
'craft',
|
|
|
- $recipeId
|
|
|
+ $recipeId,
|
|
|
+ $quantity
|
|
|
);
|
|
|
|
|
|
if ($rewardResult->success) {
|
|
|
@@ -124,11 +127,12 @@ class CraftService
|
|
|
DB::commit();
|
|
|
|
|
|
// 返回结果
|
|
|
- return [
|
|
|
- 'success' => $isSuccess,
|
|
|
- 'consumed' => $consumeResult['consumed'] ?? [],
|
|
|
- 'rewards' => $isSuccess ? $rewardItems : []
|
|
|
- ];
|
|
|
+ return Res::success();
|
|
|
+// return [
|
|
|
+// 'success' => $isSuccess,
|
|
|
+// 'consumed' => $consumeResult['consumed'] ?? [],
|
|
|
+// 'rewards' => $isSuccess ? $rewardItems : []
|
|
|
+// ];
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
// 回滚事务
|
|
|
@@ -136,6 +140,7 @@ class CraftService
|
|
|
DB::rollBack();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
Log::error('合成物品失败', [
|
|
|
'user_id' => $userId,
|
|
|
'recipe_id' => $recipeId,
|
|
|
@@ -143,7 +148,7 @@ class CraftService
|
|
|
'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
- return false;
|
|
|
+ return Res::success('合成失败');
|
|
|
}
|
|
|
}
|
|
|
|