HouseService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Module\Farm\Services;
  3. use App\Module\Farm\Dtos\HouseRankDto;
  4. use App\Module\Farm\Dtos\WealthRankDto;
  5. use App\Module\Farm\Logics\HouseLogic;
  6. use App\Module\Farm\Models\FarmHouseConfig;
  7. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  8. use Illuminate\Support\Facades\Log;
  9. use UCore\Exception\LogicException;
  10. use UCore\Exception\ValidateException;
  11. /**
  12. * 房屋管理服务
  13. */
  14. class HouseService
  15. {
  16. /**
  17. * 获取房屋配置
  18. *
  19. * @param int $level
  20. * @return FarmHouseConfig|null
  21. */
  22. public static function getHouseConfig(int $level): ?FarmHouseConfig
  23. {
  24. try {
  25. $houseLogic = new HouseLogic();
  26. return $houseLogic->getHouseConfig($level);
  27. } catch (\Exception $e) {
  28. Log::error('获取房屋配置失败', [
  29. 'level' => $level,
  30. 'error' => $e->getMessage(),
  31. 'trace' => $e->getTraceAsString()
  32. ]);
  33. return null;
  34. }
  35. }
  36. /**
  37. * 获取所有房屋配置
  38. *
  39. * @return array
  40. */
  41. public static function getAllHouseConfigs(): array
  42. {
  43. try {
  44. $houseLogic = new HouseLogic();
  45. return $houseLogic->getAllHouseConfigs();
  46. } catch (\Exception $e) {
  47. Log::error('获取所有房屋配置失败', [
  48. 'error' => $e->getMessage(),
  49. 'trace' => $e->getTraceAsString()
  50. ]);
  51. return [];
  52. }
  53. }
  54. /**
  55. * 获取下一级房屋配置
  56. *
  57. * @param int $currentLevel
  58. * @return FarmHouseConfig|null
  59. */
  60. public static function getNextLevelConfig(int $currentLevel): ?FarmHouseConfig
  61. {
  62. try {
  63. $houseLogic = new HouseLogic();
  64. return $houseLogic->getNextLevelConfig($currentLevel);
  65. } catch (\Exception $e) {
  66. Log::error('获取下一级房屋配置失败', [
  67. 'current_level' => $currentLevel,
  68. 'error' => $e->getMessage(),
  69. 'trace' => $e->getTraceAsString()
  70. ]);
  71. return null;
  72. }
  73. }
  74. /**
  75. * 检查房屋升级条件
  76. *
  77. * @param int $userId 用户ID
  78. * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息,以及其他详细信息
  79. */
  80. public static function checkUpgradeRequirements(int $userId): true
  81. {
  82. // 获取用户当前房屋等级
  83. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  84. if (!$farmUser) {
  85. throw new LogicException('用户农场不存在');
  86. }
  87. $currentLevel = $farmUser->house_level;
  88. $nextLevel = $currentLevel + 1;
  89. // 获取下一级房屋配置
  90. $nextLevelConfig = self::getHouseConfig($nextLevel);
  91. if (!$nextLevelConfig) {
  92. Log::error('获取下一级房屋配置失败', [
  93. 'current_level' => $currentLevel,
  94. 'error' => '下一级房屋配置不存在'
  95. ]);
  96. throw new LogicException('已达到最高等级');
  97. }
  98. // 获取升级所需消耗组
  99. $consumeGroupId = $nextLevelConfig->upgrade_materials;
  100. if (!$consumeGroupId) {
  101. throw new LogicException('升级条件组不存在');
  102. }
  103. // 检查消耗条件
  104. $checkResult = \App\Module\Game\Services\ConsumeService::checkConsume($userId, $consumeGroupId);
  105. if($checkResult->error){
  106. throw new LogicException($checkResult->message);
  107. }
  108. return true;
  109. }
  110. /**
  111. * 获取房屋排行榜数据
  112. *
  113. * @param int $userId 当前用户ID
  114. * @param int $page 页码
  115. * @param int $pageSize 每页数量
  116. * @return HouseRankDto
  117. */
  118. public static function getHouseRankList(int $userId, int $page = 1, int $pageSize = 10): HouseRankDto
  119. {
  120. try {
  121. $houseLogic = new HouseLogic();
  122. return $houseLogic->getHouseRankList($userId, $page, $pageSize);
  123. } catch (\Exception $e) {
  124. Log::error('获取房屋排行榜失败', [
  125. 'user_id' => $userId,
  126. 'page' => $page,
  127. 'page_size' => $pageSize,
  128. 'error' => $e->getMessage(),
  129. 'trace' => $e->getTraceAsString()
  130. ]);
  131. return new HouseRankDto();
  132. }
  133. }
  134. /**
  135. * 获取财富排行榜数据
  136. *
  137. * @param int $userId 当前用户ID
  138. * @param int $page 页码
  139. * @param int $pageSize 每页数量
  140. * @return WealthRankDto
  141. */
  142. public static function getWealthRankList(int $userId, int $page = 1, int $pageSize = 10): WealthRankDto
  143. {
  144. try {
  145. $houseLogic = new HouseLogic();
  146. return $houseLogic->getWealthRankList($userId, $page, $pageSize);
  147. } catch (\Exception $e) {
  148. Log::error('获取财富排行榜失败', [
  149. 'user_id' => $userId,
  150. 'page' => $page,
  151. 'page_size' => $pageSize,
  152. 'error' => $e->getMessage(),
  153. 'trace' => $e->getTraceAsString()
  154. ]);
  155. return new WealthRankDto();
  156. }
  157. }
  158. /**
  159. * 执行房屋升级
  160. * 要求调用者已开启事务
  161. *
  162. * @param int $userId 用户ID
  163. * @return bool 升级结果
  164. */
  165. public static function executeHouseUpgrade(int $userId): bool
  166. {
  167. // 验证事务是否已开启
  168. \UCore\Db\Helper::check_tr();
  169. // 获取用户当前房屋等级
  170. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  171. $currentLevel = $farmUser->house_level;
  172. $nextLevel = $currentLevel + 1;
  173. // 获取下一级房屋配置
  174. $nextLevelConfig = self::getHouseConfig($nextLevel);
  175. $consumeGroupId = $nextLevelConfig->upgrade_materials;
  176. // 执行消耗
  177. $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume(
  178. $userId, $consumeGroupId,
  179. REWARD_SOURCE_TYPE::FARM_UPGRADE,
  180. $farmUser->id);
  181. if ($consumeResult->error) {
  182. throw new LogicException($consumeResult->message ?? '消耗材料失败');
  183. }
  184. // 构建消耗的材料数组
  185. $consumedItems = [];
  186. $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($consumeGroupId);
  187. if ($consumeGroup && $consumeGroup->consumeItems) {
  188. foreach ($consumeGroup->consumeItems as $item) {
  189. if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
  190. $consumedItems[] = [
  191. 'item_id' => $item->target_id,
  192. 'amount' => $item->quantity
  193. ];
  194. }
  195. }
  196. }
  197. // 执行升级
  198. $houseLogic = new HouseLogic();
  199. $upgradeResult = $houseLogic->upgradeHouse($userId, $consumedItems);
  200. if (!$upgradeResult) {
  201. throw new LogicException('升级房屋失败');
  202. }
  203. return true;
  204. }
  205. }