HouseLogic.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Enums\UPGRADE_TYPE;
  4. use App\Module\Farm\Events\HouseUpgradedEvent;
  5. use App\Module\Farm\Models\FarmHouseConfig;
  6. use App\Module\Farm\Models\FarmUpgradeLog;
  7. use App\Module\Farm\Models\FarmUser;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Log;
  10. /**
  11. * 房屋管理逻辑
  12. */
  13. class HouseLogic
  14. {
  15. /**
  16. * 缓存键前缀
  17. *
  18. * @var string
  19. */
  20. private $cachePrefix = 'farm:house2:';
  21. /**
  22. * 缓存过期时间(秒)
  23. *
  24. * @var int
  25. */
  26. private $cacheExpiration = 20; // 24小时
  27. /**
  28. * 获取房屋配置
  29. *
  30. * @param int $level
  31. * @return FarmHouseConfig|null
  32. */
  33. public function getHouseConfig(int $level): ?FarmHouseConfig
  34. {
  35. try {
  36. // 尝试从缓存获取
  37. $cacheKey = $this->cachePrefix . 'config:' . $level;
  38. $cachedConfig = Cache::get($cacheKey);
  39. if ($cachedConfig) {
  40. return $cachedConfig;
  41. }
  42. // 从数据库获取
  43. $config = FarmHouseConfig::where('level', $level)->first();
  44. if (!$config) {
  45. return null;
  46. }
  47. // 缓存结果
  48. Cache::put($cacheKey, $config, $this->cacheExpiration);
  49. return $config;
  50. } catch (\Exception $e) {
  51. Log::error('获取房屋配置失败', [
  52. 'level' => $level,
  53. 'error' => $e->getMessage(),
  54. 'trace' => $e->getTraceAsString()
  55. ]);
  56. return null;
  57. }
  58. }
  59. /**
  60. * 获取所有房屋配置
  61. *
  62. * @return array
  63. */
  64. public function getAllHouseConfigs(): array
  65. {
  66. try {
  67. // 尝试从缓存获取
  68. $cacheKey = $this->cachePrefix . 'configs:all';
  69. $cachedConfigs = Cache::get($cacheKey);
  70. if ($cachedConfigs) {
  71. return $cachedConfigs;
  72. }
  73. // 从数据库获取
  74. $configs = FarmHouseConfig::orderBy('level')->get();
  75. $result = [];
  76. foreach ($configs as $config) {
  77. $result[$config->level] = [
  78. 'level' => $config->level,
  79. 'output_bonus' => $config->output_bonus,
  80. 'special_land_limit' => $config->special_land_limit,
  81. 'upgrade_materials' => $config->upgrade_materials,
  82. 'downgrade_days' => $config->downgrade_days,
  83. 'available_lands' => $config->available_lands,
  84. ];
  85. }
  86. // 缓存结果
  87. Cache::put($cacheKey, $result, $this->cacheExpiration);
  88. return $result;
  89. } catch (\Exception $e) {
  90. Log::error('获取所有房屋配置失败', [
  91. 'error' => $e->getMessage(),
  92. 'trace' => $e->getTraceAsString()
  93. ]);
  94. return [];
  95. }
  96. }
  97. /**
  98. * 获取下一级房屋配置
  99. *
  100. * @param int $currentLevel
  101. * @return FarmHouseConfig|null
  102. */
  103. public function getNextLevelConfig(int $currentLevel): ?FarmHouseConfig
  104. {
  105. try {
  106. // 尝试从缓存获取
  107. $cacheKey = $this->cachePrefix . 'config:' . ($currentLevel + 1);
  108. $cachedConfig = Cache::get($cacheKey);
  109. if ($cachedConfig) {
  110. return $cachedConfig;
  111. }
  112. // 从数据库获取
  113. $config = FarmHouseConfig::where('level', $currentLevel + 1)->first();
  114. if (!$config) {
  115. return null;
  116. }
  117. // 缓存结果
  118. Cache::put($cacheKey, $config, $this->cacheExpiration);
  119. return $config;
  120. } catch (\Exception $e) {
  121. Log::error('获取下一级房屋配置失败', [
  122. 'current_level' => $currentLevel,
  123. 'error' => $e->getMessage(),
  124. 'trace' => $e->getTraceAsString()
  125. ]);
  126. return null;
  127. }
  128. }
  129. /**
  130. * 升级房屋
  131. *
  132. * 注意:此方法不处理消耗逻辑,只负责升级房屋等级和记录升级日志
  133. * 消耗处理应在调用此方法前完成,并将消耗的材料信息传入
  134. *
  135. * @param int $userId 用户ID
  136. * @param array $materials 消耗的材料记录(已经消耗完成的)
  137. * @return bool 升级是否成功
  138. */
  139. public function upgradeHouse(int $userId, array $materials): bool
  140. {
  141. try {
  142. // 获取用户农场信息
  143. $farmUser = FarmUser::where('user_id', $userId)->first();
  144. if (!$farmUser) {
  145. throw new \Exception('用户农场不存在');
  146. }
  147. // 获取当前房屋等级
  148. $currentLevel = $farmUser->house_level;
  149. // 获取下一级房屋配置
  150. $nextLevelConfig = $this->getNextLevelConfig($currentLevel);
  151. if (!$nextLevelConfig) {
  152. throw new \Exception('已达到最高等级');
  153. }
  154. // 更新用户房屋等级
  155. $oldLevel = $farmUser->house_level;
  156. $newLevel = $oldLevel + 1;
  157. $farmUser->house_level = $newLevel;
  158. $farmUser->last_upgrade_time = now();
  159. $farmUser->save();
  160. // 创建升级记录
  161. $upgradeLog = new FarmUpgradeLog();
  162. $upgradeLog->user_id = $userId;
  163. $upgradeLog->upgrade_type = UPGRADE_TYPE::HOUSE->value;
  164. $upgradeLog->old_level = $oldLevel;
  165. $upgradeLog->new_level = $newLevel;
  166. $upgradeLog->materials_consumed = $materials;
  167. $upgradeLog->upgrade_time = now();
  168. $upgradeLog->created_at = now();
  169. $upgradeLog->save();
  170. Log::debug('HouseUpgradedEvent',[]);
  171. // 触发房屋升级事件
  172. event(new HouseUpgradedEvent($userId, $farmUser, $oldLevel, $newLevel, $upgradeLog));
  173. Log::info('房屋升级成功', [
  174. 'user_id' => $userId,
  175. 'old_level' => $oldLevel,
  176. 'new_level' => $newLevel,
  177. 'upgrade_log_id' => $upgradeLog->id
  178. ]);
  179. return true;
  180. } catch (\Exception $e) {
  181. Log::error('房屋升级失败', [
  182. 'user_id' => $userId,
  183. 'error' => $e->getMessage(),
  184. 'trace' => $e->getTraceAsString()
  185. ]);
  186. return false;
  187. }
  188. }
  189. /**
  190. * 获取房屋等级可用的土地数量
  191. *
  192. * @param int $level 房屋等级
  193. * @return int 可用土地数量
  194. */
  195. public function getAvailableLandsCount(int $level): int
  196. {
  197. try {
  198. $config = $this->getHouseConfig($level);
  199. if (!$config) {
  200. return 0;
  201. }
  202. // 确保返回整数类型
  203. return $config->available_lands ? (int)$config->available_lands : 0;
  204. } catch (\Exception $e) {
  205. Log::error('获取房屋等级可用土地数量失败', [
  206. 'level' => $level,
  207. 'error' => $e->getMessage(),
  208. 'trace' => $e->getTraceAsString()
  209. ]);
  210. return 0;
  211. }
  212. }
  213. /**
  214. * 清除房屋配置缓存
  215. *
  216. * @param int|null $level
  217. * @return bool
  218. */
  219. public function clearHouseConfigCache(?int $level = null): bool
  220. {
  221. try {
  222. if ($level) {
  223. // 清除指定等级的缓存
  224. Cache::forget($this->cachePrefix . 'config:' . $level);
  225. } else {
  226. // 清除所有房屋配置缓存
  227. Cache::forget($this->cachePrefix . 'configs:all');
  228. // 清除各等级缓存
  229. $maxLevel = 12; // 假设最高等级为12
  230. for ($i = 1; $i <= $maxLevel; $i++) {
  231. Cache::forget($this->cachePrefix . 'config:' . $i);
  232. }
  233. }
  234. return true;
  235. } catch (\Exception $e) {
  236. Log::error('清除房屋配置缓存失败', [
  237. 'level' => $level,
  238. 'error' => $e->getMessage(),
  239. 'trace' => $e->getTraceAsString()
  240. ]);
  241. return false;
  242. }
  243. }
  244. }