LandService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Module\Farm\Services;
  3. use App\Module\Farm\Dtos\LandInfoDto;
  4. use App\Module\Farm\Logics\LandLogic;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 土地管理服务
  9. */
  10. class LandService
  11. {
  12. /**
  13. * 获取用户的所有土地
  14. *
  15. * @param int $userId
  16. * @return Collection
  17. */
  18. public static function getUserLands(int $userId): Collection
  19. {
  20. try {
  21. $landLogic = new LandLogic();
  22. return $landLogic->getUserLands($userId);
  23. } catch (\Exception $e) {
  24. Log::error('获取用户土地失败', [
  25. 'user_id' => $userId,
  26. 'error' => $e->getMessage(),
  27. 'trace' => $e->getTraceAsString()
  28. ]);
  29. return collect();
  30. }
  31. }
  32. public static function getUserLand(int $userId,int $landId)
  33. {
  34. // 根据用户id,和土地id获取土地信息
  35. }
  36. /**
  37. * 获取用户指定位置的土地
  38. *
  39. * @param int $userId
  40. * @param int $position
  41. * @return LandInfoDto|null
  42. */
  43. public static function getUserLandByPosition(int $userId, int $position): ?LandInfoDto
  44. {
  45. try {
  46. $landLogic = new LandLogic();
  47. return $landLogic->getUserLandByPosition($userId, $position);
  48. } catch (\Exception $e) {
  49. Log::error('获取用户指定位置土地失败', [
  50. 'user_id' => $userId,
  51. 'position' => $position,
  52. 'error' => $e->getMessage(),
  53. 'trace' => $e->getTraceAsString()
  54. ]);
  55. return null;
  56. }
  57. }
  58. /**
  59. * 升级土地
  60. *
  61. * @param int $userId
  62. * @param int $landId
  63. * @param int $targetType
  64. * @param array $materials 消耗的材料
  65. * @return bool
  66. */
  67. public static function upgradeLand(int $userId, int $landId, int $targetType, array $materials): bool
  68. {
  69. try {
  70. $landLogic = new LandLogic();
  71. return $landLogic->upgradeLand($userId, $landId, $targetType, $materials);
  72. } catch (\Exception $e) {
  73. Log::error('升级土地失败', [
  74. 'user_id' => $userId,
  75. 'land_id' => $landId,
  76. 'target_type' => $targetType,
  77. 'error' => $e->getMessage(),
  78. 'trace' => $e->getTraceAsString()
  79. ]);
  80. return false;
  81. }
  82. }
  83. /**
  84. * 获取可用的升级路径
  85. *
  86. * @param int $userId
  87. * @param int $landId
  88. * @return array
  89. */
  90. public static function getAvailableUpgradePaths(int $userId, int $landId): array
  91. {
  92. try {
  93. $landLogic = new LandLogic();
  94. return $landLogic->getAvailableUpgradePaths($userId, $landId);
  95. } catch (\Exception $e) {
  96. Log::error('获取可用升级路径失败', [
  97. 'user_id' => $userId,
  98. 'land_id' => $landId,
  99. 'error' => $e->getMessage(),
  100. 'trace' => $e->getTraceAsString()
  101. ]);
  102. return [];
  103. }
  104. }
  105. }