|
|
@@ -5,12 +5,15 @@ namespace App\Module\Farm\Services;
|
|
|
use App\Module\Farm\Logics\HouseLogic;
|
|
|
use App\Module\Farm\Models\FarmHouseConfig;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use UCore\Exception\LogicException;
|
|
|
+use UCore\Exception\ValidateException;
|
|
|
|
|
|
/**
|
|
|
* 房屋管理服务
|
|
|
*/
|
|
|
class HouseService
|
|
|
{
|
|
|
+
|
|
|
/**
|
|
|
* 获取房屋配置
|
|
|
*
|
|
|
@@ -21,6 +24,7 @@ class HouseService
|
|
|
{
|
|
|
try {
|
|
|
$houseLogic = new HouseLogic();
|
|
|
+
|
|
|
return $houseLogic->getHouseConfig($level);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取房屋配置失败', [
|
|
|
@@ -42,6 +46,7 @@ class HouseService
|
|
|
{
|
|
|
try {
|
|
|
$houseLogic = new HouseLogic();
|
|
|
+
|
|
|
return $houseLogic->getAllHouseConfigs();
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取所有房屋配置失败', [
|
|
|
@@ -63,12 +68,13 @@ class HouseService
|
|
|
{
|
|
|
try {
|
|
|
$houseLogic = new HouseLogic();
|
|
|
+
|
|
|
return $houseLogic->getNextLevelConfig($currentLevel);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取下一级房屋配置失败', [
|
|
|
'current_level' => $currentLevel,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return null;
|
|
|
@@ -81,133 +87,47 @@ class HouseService
|
|
|
* @param int $userId 用户ID
|
|
|
* @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息,以及其他详细信息
|
|
|
*/
|
|
|
- public static function checkUpgradeRequirements(int $userId): array
|
|
|
+ public static function checkUpgradeRequirements(int $userId): true
|
|
|
{
|
|
|
- try {
|
|
|
+
|
|
|
// 获取用户当前房屋等级
|
|
|
$farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
|
|
|
if (!$farmUser) {
|
|
|
- return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => '用户农场不存在',
|
|
|
- 'code' => 'FARM_NOT_FOUND'
|
|
|
- ];
|
|
|
+ throw new LogicException('用户农场不存在');
|
|
|
+
|
|
|
}
|
|
|
|
|
|
$currentLevel = $farmUser->house_level;
|
|
|
- $nextLevel = $currentLevel + 1;
|
|
|
+ $nextLevel = $currentLevel + 1;
|
|
|
|
|
|
// 获取下一级房屋配置
|
|
|
$nextLevelConfig = self::getHouseConfig($nextLevel);
|
|
|
if (!$nextLevelConfig) {
|
|
|
- return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => '已达到最高等级',
|
|
|
- 'code' => 'MAX_LEVEL_REACHED',
|
|
|
- 'current_level' => $currentLevel
|
|
|
- ];
|
|
|
+ Log::error('获取下一级房屋配置失败', [
|
|
|
+ 'current_level' => $currentLevel,
|
|
|
+ 'error' => '下一级房屋配置不存在'
|
|
|
+ ]);
|
|
|
+ throw new LogicException('已达到最高等级');
|
|
|
+
|
|
|
}
|
|
|
|
|
|
// 获取升级所需消耗组
|
|
|
$consumeGroupId = $nextLevelConfig->upgrade_materials;
|
|
|
if (!$consumeGroupId) {
|
|
|
- return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => '升级配置错误:未设置消耗组',
|
|
|
- 'code' => 'CONFIG_ERROR',
|
|
|
- 'current_level' => $currentLevel,
|
|
|
- 'next_level' => $nextLevel
|
|
|
- ];
|
|
|
+ throw new LogicException('升级条件组不存在');
|
|
|
}
|
|
|
|
|
|
// 检查消耗条件
|
|
|
$checkResult = \App\Module\Game\Services\ConsumeService::checkConsume($userId, $consumeGroupId);
|
|
|
|
|
|
- // 获取消耗组详情,用于返回更详细的信息
|
|
|
- $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::with('consumeItems')->find($consumeGroupId);
|
|
|
- $consumeItems = [];
|
|
|
+ if(!$checkResult['success']){
|
|
|
+ throw new LogicException($checkResult['message']);
|
|
|
+ }
|
|
|
|
|
|
- if ($consumeGroup && $consumeGroup->consumeItems) {
|
|
|
- foreach ($consumeGroup->consumeItems as $item) {
|
|
|
- $itemDetail = [
|
|
|
- 'consume_type' => $item->consume_type,
|
|
|
- 'target_id' => $item->target_id,
|
|
|
- 'quantity' => $item->quantity,
|
|
|
- 'is_satisfied' => true,
|
|
|
- 'actual' => 0
|
|
|
- ];
|
|
|
-
|
|
|
- // 根据消耗类型获取详细信息
|
|
|
- if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
|
|
|
- // 获取物品信息
|
|
|
- $itemInfo = \App\Module\GameItems\Services\ItemService::getItemInfo($item->target_id);
|
|
|
- $itemDetail['name'] = $itemInfo ? $itemInfo->name : "物品 {$item->target_id}";
|
|
|
-
|
|
|
- // 获取用户物品数量
|
|
|
- $userItems = \App\Module\GameItems\Services\ItemService::getUserItems($userId, ['item_id' => $item->target_id]);
|
|
|
- $totalQuantity = 0;
|
|
|
- foreach ($userItems as $userItem) {
|
|
|
- $totalQuantity += $userItem->quantity;
|
|
|
- }
|
|
|
-
|
|
|
- $itemDetail['actual'] = $totalQuantity;
|
|
|
- $itemDetail['is_satisfied'] = $totalQuantity >= $item->quantity;
|
|
|
- } elseif ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::CURRENCY->value) {
|
|
|
- // 获取货币信息
|
|
|
- $currencyInfo = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
|
|
|
- $itemDetail['name'] = $currencyInfo ? $currencyInfo->name : "货币 {$item->target_id}";
|
|
|
-
|
|
|
- // 获取用户货币余额
|
|
|
- $account = \App\Module\Fund\Logic\User::get_account($userId, $item->target_id);
|
|
|
- $balance = $account ? $account->balance : 0;
|
|
|
-
|
|
|
- $itemDetail['actual'] = $balance;
|
|
|
- $itemDetail['is_satisfied'] = $balance >= $item->quantity;
|
|
|
- } elseif ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::FUND->value) {
|
|
|
- // 获取代币账户信息
|
|
|
- $fundInfo = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
|
|
|
- $itemDetail['name'] = $fundInfo ? $fundInfo->name : "代币账户 {$item->target_id}";
|
|
|
-
|
|
|
- // 获取用户代币余额
|
|
|
- $account = \App\Module\Fund\Logic\User::get_account($userId, $item->target_id);
|
|
|
- $balance = $account ? $account->balance : 0;
|
|
|
-
|
|
|
- $itemDetail['actual'] = $balance;
|
|
|
- $itemDetail['is_satisfied'] = $balance >= $item->quantity;
|
|
|
- }
|
|
|
|
|
|
- $consumeItems[] = $itemDetail;
|
|
|
- }
|
|
|
- }
|
|
|
+ return true;
|
|
|
|
|
|
- // 构建返回结果
|
|
|
- $result = [
|
|
|
- 'success' => $checkResult['success'],
|
|
|
- 'message' => $checkResult['message'],
|
|
|
- 'current_level' => $currentLevel,
|
|
|
- 'next_level' => $nextLevel,
|
|
|
- 'consume_group' => [
|
|
|
- 'id' => $consumeGroupId,
|
|
|
- 'name' => $consumeGroup ? $consumeGroup->name : '',
|
|
|
- 'code' => $consumeGroup ? $consumeGroup->code : '',
|
|
|
- ],
|
|
|
- 'consume_items' => $consumeItems
|
|
|
- ];
|
|
|
-
|
|
|
- return $result;
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('检查房屋升级条件失败', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
- ]);
|
|
|
|
|
|
- return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => '检查升级条件时发生错误: ' . $e->getMessage(),
|
|
|
- 'code' => 'SYSTEM_ERROR'
|
|
|
- ];
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -216,107 +136,70 @@ class HouseService
|
|
|
* @param int $userId 用户ID
|
|
|
* @return array 升级结果,包含success字段表示是否成功,message字段表示错误信息,以及其他详细信息
|
|
|
*/
|
|
|
- public static function executeHouseUpgrade(int $userId): array
|
|
|
+ public static function executeHouseUpgrade(int $userId): true
|
|
|
{
|
|
|
- try {
|
|
|
- // 先检查升级条件
|
|
|
- $checkResult = self::checkUpgradeRequirements($userId);
|
|
|
- if (!$checkResult['success']) {
|
|
|
- return $checkResult;
|
|
|
- }
|
|
|
|
|
|
- // 获取用户当前房屋等级
|
|
|
- $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
|
|
|
- $currentLevel = $farmUser->house_level;
|
|
|
- $nextLevel = $currentLevel + 1;
|
|
|
+ // 获取用户当前房屋等级
|
|
|
+ $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
|
|
|
+ $currentLevel = $farmUser->house_level;
|
|
|
+ $nextLevel = $currentLevel + 1;
|
|
|
|
|
|
- // 获取下一级房屋配置
|
|
|
- $nextLevelConfig = self::getHouseConfig($nextLevel);
|
|
|
- $consumeGroupId = $nextLevelConfig->upgrade_materials;
|
|
|
+ // 获取下一级房屋配置
|
|
|
+ $nextLevelConfig = self::getHouseConfig($nextLevel);
|
|
|
+ $consumeGroupId = $nextLevelConfig->upgrade_materials;
|
|
|
|
|
|
- // 开始事务
|
|
|
- \Illuminate\Support\Facades\DB::beginTransaction();
|
|
|
-
|
|
|
- try {
|
|
|
- // 执行消耗
|
|
|
- $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume($userId, $consumeGroupId, 'house_upgrade', $farmUser->id);
|
|
|
- if (!$consumeResult['success']) {
|
|
|
- \Illuminate\Support\Facades\DB::rollBack();
|
|
|
- return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => $consumeResult['message'] ?? '消耗材料失败',
|
|
|
- 'code' => 'CONSUME_FAILED'
|
|
|
- ];
|
|
|
- }
|
|
|
+ // 开始事务
|
|
|
+ \Illuminate\Support\Facades\DB::beginTransaction();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 执行消耗
|
|
|
+ $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume($userId, $consumeGroupId, 'house_upgrade', $farmUser->id);
|
|
|
+ if (!$consumeResult['success']) {
|
|
|
+ \Illuminate\Support\Facades\DB::rollBack();
|
|
|
|
|
|
- // 构建消耗的材料数组
|
|
|
- $consumedItems = [];
|
|
|
- $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($consumeGroupId);
|
|
|
- if ($consumeGroup && $consumeGroup->consumeItems) {
|
|
|
- foreach ($consumeGroup->consumeItems as $item) {
|
|
|
- if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
|
|
|
- $consumedItems[] = [
|
|
|
- 'item_id' => $item->target_id,
|
|
|
- 'amount' => $item->quantity
|
|
|
- ];
|
|
|
- }
|
|
|
+ return [
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => $consumeResult['message'] ?? '消耗材料失败',
|
|
|
+ 'code' => 'CONSUME_FAILED'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建消耗的材料数组
|
|
|
+ $consumedItems = [];
|
|
|
+ $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($consumeGroupId);
|
|
|
+ if ($consumeGroup && $consumeGroup->consumeItems) {
|
|
|
+ foreach ($consumeGroup->consumeItems as $item) {
|
|
|
+ if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
|
|
|
+ $consumedItems[] = [
|
|
|
+ 'item_id' => $item->target_id,
|
|
|
+ 'amount' => $item->quantity
|
|
|
+ ];
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 执行升级
|
|
|
- $houseLogic = new HouseLogic();
|
|
|
- $upgradeResult = $houseLogic->upgradeHouse($userId, $consumedItems);
|
|
|
-
|
|
|
- if (!$upgradeResult) {
|
|
|
- \Illuminate\Support\Facades\DB::rollBack();
|
|
|
- return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => '房屋升级失败',
|
|
|
- 'code' => 'UPGRADE_FAILED'
|
|
|
- ];
|
|
|
- }
|
|
|
+ // 执行升级
|
|
|
+ $houseLogic = new HouseLogic();
|
|
|
+ $upgradeResult = $houseLogic->upgradeHouse($userId, $consumedItems);
|
|
|
|
|
|
- // 提交事务
|
|
|
- \Illuminate\Support\Facades\DB::commit();
|
|
|
+ if (!$upgradeResult) {
|
|
|
+ throw new LogicException('升级房屋失败');
|
|
|
+ }
|
|
|
|
|
|
- return [
|
|
|
- 'success' => true,
|
|
|
- 'message' => '房屋升级成功',
|
|
|
- 'old_level' => $currentLevel,
|
|
|
- 'new_level' => $nextLevel,
|
|
|
- 'consumed_items' => $consumedItems
|
|
|
- ];
|
|
|
- } catch (\Exception $e) {
|
|
|
- // 回滚事务
|
|
|
- \Illuminate\Support\Facades\DB::rollBack();
|
|
|
+ // 提交事务
|
|
|
+ \Illuminate\Support\Facades\DB::commit();
|
|
|
|
|
|
- throw $e;
|
|
|
- }
|
|
|
+ return true;
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('执行房屋升级失败', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
- ]);
|
|
|
+ // 回滚事务
|
|
|
+ \Illuminate\Support\Facades\DB::rollBack();
|
|
|
|
|
|
- return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => '执行房屋升级时发生错误: ' . $e->getMessage(),
|
|
|
- 'code' => 'SYSTEM_ERROR'
|
|
|
- ];
|
|
|
+ throw $e;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 升级房屋(兼容旧版本)
|
|
|
- *
|
|
|
- * @param int $userId
|
|
|
- * @return bool
|
|
|
- * @deprecated 使用 executeHouseUpgrade 替代
|
|
|
- */
|
|
|
- public static function upgradeHouse(int $userId): bool
|
|
|
- {
|
|
|
- $result = self::executeHouseUpgrade($userId);
|
|
|
- return $result['success'];
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|