HouseService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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->error){
  103. throw new LogicException($checkResult->message);
  104. }
  105. return true;
  106. }
  107. /**
  108. * 执行房屋升级
  109. * 要求调用者已开启事务
  110. *
  111. * @param int $userId 用户ID
  112. * @return bool 升级结果
  113. */
  114. public static function executeHouseUpgrade(int $userId): bool
  115. {
  116. // 验证事务是否已开启
  117. \UCore\Db\Helper::check_tr();
  118. // 获取用户当前房屋等级
  119. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  120. $currentLevel = $farmUser->house_level;
  121. $nextLevel = $currentLevel + 1;
  122. // 获取下一级房屋配置
  123. $nextLevelConfig = self::getHouseConfig($nextLevel);
  124. $consumeGroupId = $nextLevelConfig->upgrade_materials;
  125. // 执行消耗
  126. $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume($userId, $consumeGroupId, 'house_upgrade', $farmUser->id);
  127. if ($consumeResult->error) {
  128. throw new LogicException($consumeResult->message ?? '消耗材料失败');
  129. }
  130. // 构建消耗的材料数组
  131. $consumedItems = [];
  132. $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($consumeGroupId);
  133. if ($consumeGroup && $consumeGroup->consumeItems) {
  134. foreach ($consumeGroup->consumeItems as $item) {
  135. if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
  136. $consumedItems[] = [
  137. 'item_id' => $item->target_id,
  138. 'amount' => $item->quantity
  139. ];
  140. }
  141. }
  142. }
  143. // 执行升级
  144. $houseLogic = new HouseLogic();
  145. $upgradeResult = $houseLogic->upgradeHouse($userId, $consumedItems);
  146. if (!$upgradeResult) {
  147. throw new LogicException('升级房屋失败');
  148. }
  149. return true;
  150. }
  151. }