HouseService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Module\Farm\Services;
  3. use App\Module\Farm\Logics\HouseLogic;
  4. use App\Module\Farm\Models\FarmHouseConfig;
  5. use Illuminate\Support\Facades\Log;
  6. use UCore\Exception\LogicException;
  7. use UCore\Exception\ValidateException;
  8. /**
  9. * 房屋管理服务
  10. */
  11. class HouseService
  12. {
  13. /**
  14. * 获取房屋配置
  15. *
  16. * @param int $level
  17. * @return FarmHouseConfig|null
  18. */
  19. public static function getHouseConfig(int $level): ?FarmHouseConfig
  20. {
  21. try {
  22. $houseLogic = new HouseLogic();
  23. return $houseLogic->getHouseConfig($level);
  24. } catch (\Exception $e) {
  25. Log::error('获取房屋配置失败', [
  26. 'level' => $level,
  27. 'error' => $e->getMessage(),
  28. 'trace' => $e->getTraceAsString()
  29. ]);
  30. return null;
  31. }
  32. }
  33. /**
  34. * 获取所有房屋配置
  35. *
  36. * @return array
  37. */
  38. public static function getAllHouseConfigs(): array
  39. {
  40. try {
  41. $houseLogic = new HouseLogic();
  42. return $houseLogic->getAllHouseConfigs();
  43. } catch (\Exception $e) {
  44. Log::error('获取所有房屋配置失败', [
  45. 'error' => $e->getMessage(),
  46. 'trace' => $e->getTraceAsString()
  47. ]);
  48. return [];
  49. }
  50. }
  51. /**
  52. * 获取下一级房屋配置
  53. *
  54. * @param int $currentLevel
  55. * @return FarmHouseConfig|null
  56. */
  57. public static function getNextLevelConfig(int $currentLevel): ?FarmHouseConfig
  58. {
  59. try {
  60. $houseLogic = new HouseLogic();
  61. return $houseLogic->getNextLevelConfig($currentLevel);
  62. } catch (\Exception $e) {
  63. Log::error('获取下一级房屋配置失败', [
  64. 'current_level' => $currentLevel,
  65. 'error' => $e->getMessage(),
  66. 'trace' => $e->getTraceAsString()
  67. ]);
  68. return null;
  69. }
  70. }
  71. /**
  72. * 检查房屋升级条件
  73. *
  74. * @param int $userId 用户ID
  75. * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息,以及其他详细信息
  76. */
  77. public static function checkUpgradeRequirements(int $userId): true
  78. {
  79. // 获取用户当前房屋等级
  80. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  81. if (!$farmUser) {
  82. throw new LogicException('用户农场不存在');
  83. }
  84. $currentLevel = $farmUser->house_level;
  85. $nextLevel = $currentLevel + 1;
  86. // 获取下一级房屋配置
  87. $nextLevelConfig = self::getHouseConfig($nextLevel);
  88. if (!$nextLevelConfig) {
  89. Log::error('获取下一级房屋配置失败', [
  90. 'current_level' => $currentLevel,
  91. 'error' => '下一级房屋配置不存在'
  92. ]);
  93. throw new LogicException('已达到最高等级');
  94. }
  95. // 获取升级所需消耗组
  96. $consumeGroupId = $nextLevelConfig->upgrade_materials;
  97. if (!$consumeGroupId) {
  98. throw new LogicException('升级条件组不存在');
  99. }
  100. // 检查消耗条件
  101. $checkResult = \App\Module\Game\Services\ConsumeService::checkConsume($userId, $consumeGroupId);
  102. if(!$checkResult['success']){
  103. throw new LogicException($checkResult['message']);
  104. }
  105. return true;
  106. }
  107. /**
  108. * 执行房屋升级
  109. *
  110. * @param int $userId 用户ID
  111. * @return array 升级结果,包含success字段表示是否成功,message字段表示错误信息,以及其他详细信息
  112. */
  113. public static function executeHouseUpgrade(int $userId): true
  114. {
  115. // 获取用户当前房屋等级
  116. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  117. $currentLevel = $farmUser->house_level;
  118. $nextLevel = $currentLevel + 1;
  119. // 获取下一级房屋配置
  120. $nextLevelConfig = self::getHouseConfig($nextLevel);
  121. $consumeGroupId = $nextLevelConfig->upgrade_materials;
  122. // 开始事务
  123. \Illuminate\Support\Facades\DB::beginTransaction();
  124. try {
  125. // 执行消耗
  126. $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume($userId, $consumeGroupId, 'house_upgrade', $farmUser->id);
  127. if (!$consumeResult['success']) {
  128. \Illuminate\Support\Facades\DB::rollBack();
  129. return [
  130. 'success' => false,
  131. 'message' => $consumeResult['message'] ?? '消耗材料失败',
  132. 'code' => 'CONSUME_FAILED'
  133. ];
  134. }
  135. // 构建消耗的材料数组
  136. $consumedItems = [];
  137. $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($consumeGroupId);
  138. if ($consumeGroup && $consumeGroup->consumeItems) {
  139. foreach ($consumeGroup->consumeItems as $item) {
  140. if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
  141. $consumedItems[] = [
  142. 'item_id' => $item->target_id,
  143. 'amount' => $item->quantity
  144. ];
  145. }
  146. }
  147. }
  148. // 执行升级
  149. $houseLogic = new HouseLogic();
  150. $upgradeResult = $houseLogic->upgradeHouse($userId, $consumedItems);
  151. if (!$upgradeResult) {
  152. throw new LogicException('升级房屋失败');
  153. }
  154. // 提交事务
  155. \Illuminate\Support\Facades\DB::commit();
  156. return true;
  157. } catch (\Exception $e) {
  158. // 回滚事务
  159. \Illuminate\Support\Facades\DB::rollBack();
  160. throw $e;
  161. }
  162. }
  163. }