| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace App\Module\Farm\Logics;
- use App\Module\Farm\Enums\UPGRADE_TYPE;
- use App\Module\Farm\Events\HouseUpgradedEvent;
- use App\Module\Farm\Models\FarmHouseConfig;
- use App\Module\Farm\Models\FarmUpgradeLog;
- use App\Module\Farm\Models\FarmUser;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- /**
- * 房屋管理逻辑
- */
- class HouseLogic
- {
- /**
- * 缓存键前缀
- *
- * @var string
- */
- private $cachePrefix = 'farm:house2:';
- /**
- * 缓存过期时间(秒)
- *
- * @var int
- */
- private $cacheExpiration = 20; // 24小时
- /**
- * 获取房屋配置
- *
- * @param int $level
- * @return FarmHouseConfig|null
- */
- public function getHouseConfig(int $level): ?FarmHouseConfig
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'config:' . $level;
- $cachedConfig = Cache::get($cacheKey);
- if ($cachedConfig) {
- return $cachedConfig;
- }
- // 从数据库获取
- $config = FarmHouseConfig::where('level', $level)->first();
- if (!$config) {
- return null;
- }
- // 缓存结果
- Cache::put($cacheKey, $config, $this->cacheExpiration);
- return $config;
- } catch (\Exception $e) {
- Log::error('获取房屋配置失败', [
- 'level' => $level,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- /**
- * 获取所有房屋配置
- *
- * @return array
- */
- public function getAllHouseConfigs(): array
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'configs:all';
- $cachedConfigs = Cache::get($cacheKey);
- if ($cachedConfigs) {
- return $cachedConfigs;
- }
- // 从数据库获取
- $configs = FarmHouseConfig::orderBy('level')->get();
- $result = [];
- foreach ($configs as $config) {
- $result[$config->level] = [
- 'level' => $config->level,
- 'output_bonus' => $config->output_bonus,
- 'special_land_limit' => $config->special_land_limit,
- 'upgrade_materials' => $config->upgrade_materials,
- 'downgrade_days' => $config->downgrade_days,
- 'available_lands' => $config->available_lands,
- ];
- }
- // 缓存结果
- Cache::put($cacheKey, $result, $this->cacheExpiration);
- return $result;
- } catch (\Exception $e) {
- Log::error('获取所有房屋配置失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [];
- }
- }
- /**
- * 获取下一级房屋配置
- *
- * @param int $currentLevel
- * @return FarmHouseConfig|null
- */
- public function getNextLevelConfig(int $currentLevel): ?FarmHouseConfig
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'config:' . ($currentLevel + 1);
- $cachedConfig = Cache::get($cacheKey);
- if ($cachedConfig) {
- return $cachedConfig;
- }
- // 从数据库获取
- $config = FarmHouseConfig::where('level', $currentLevel + 1)->first();
- if (!$config) {
- return null;
- }
- // 缓存结果
- Cache::put($cacheKey, $config, $this->cacheExpiration);
- return $config;
- } catch (\Exception $e) {
- Log::error('获取下一级房屋配置失败', [
- 'current_level' => $currentLevel,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- /**
- * 升级房屋
- *
- * 注意:此方法不处理消耗逻辑,只负责升级房屋等级和记录升级日志
- * 消耗处理应在调用此方法前完成,并将消耗的材料信息传入
- *
- * @param int $userId 用户ID
- * @param array $materials 消耗的材料记录(已经消耗完成的)
- * @return bool 升级是否成功
- */
- public function upgradeHouse(int $userId, array $materials): bool
- {
- try {
- // 获取用户农场信息
- $farmUser = FarmUser::where('user_id', $userId)->first();
- if (!$farmUser) {
- throw new \Exception('用户农场不存在');
- }
- // 获取当前房屋等级
- $currentLevel = $farmUser->house_level;
- // 获取下一级房屋配置
- $nextLevelConfig = $this->getNextLevelConfig($currentLevel);
- if (!$nextLevelConfig) {
- throw new \Exception('已达到最高等级');
- }
- // 更新用户房屋等级
- $oldLevel = $farmUser->house_level;
- $newLevel = $oldLevel + 1;
- $farmUser->house_level = $newLevel;
- $farmUser->last_upgrade_time = now();
- $farmUser->save();
- // 创建升级记录
- $upgradeLog = new FarmUpgradeLog();
- $upgradeLog->user_id = $userId;
- $upgradeLog->upgrade_type = UPGRADE_TYPE::HOUSE->value;
- $upgradeLog->old_level = $oldLevel;
- $upgradeLog->new_level = $newLevel;
- $upgradeLog->materials_consumed = $materials;
- $upgradeLog->upgrade_time = now();
- $upgradeLog->created_at = now();
- $upgradeLog->save();
- Log::debug('HouseUpgradedEvent',[]);
- // 触发房屋升级事件
- event(new HouseUpgradedEvent($userId, $farmUser, $oldLevel, $newLevel, $upgradeLog));
- Log::info('房屋升级成功', [
- 'user_id' => $userId,
- 'old_level' => $oldLevel,
- 'new_level' => $newLevel,
- 'upgrade_log_id' => $upgradeLog->id
- ]);
- return true;
- } catch (\Exception $e) {
- Log::error('房屋升级失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- /**
- * 获取房屋等级可用的土地数量
- *
- * @param int $level 房屋等级
- * @return int 可用土地数量
- */
- public function getAvailableLandsCount(int $level): int
- {
- try {
- $config = $this->getHouseConfig($level);
- if (!$config) {
- return 0;
- }
- // 确保返回整数类型
- return $config->available_lands ? (int)$config->available_lands : 0;
- } catch (\Exception $e) {
- Log::error('获取房屋等级可用土地数量失败', [
- 'level' => $level,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 0;
- }
- }
- /**
- * 清除房屋配置缓存
- *
- * @param int|null $level
- * @return bool
- */
- public function clearHouseConfigCache(?int $level = null): bool
- {
- try {
- if ($level) {
- // 清除指定等级的缓存
- Cache::forget($this->cachePrefix . 'config:' . $level);
- } else {
- // 清除所有房屋配置缓存
- Cache::forget($this->cachePrefix . 'configs:all');
- // 清除各等级缓存
- $maxLevel = 12; // 假设最高等级为12
- for ($i = 1; $i <= $maxLevel; $i++) {
- Cache::forget($this->cachePrefix . 'config:' . $i);
- }
- }
- return true;
- } catch (\Exception $e) {
- Log::error('清除房屋配置缓存失败', [
- 'level' => $level,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- }
|