| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace App\Module\Game\Logics;
- use App\Module\Farm\Events\HouseUpgradedEvent;
- use App\Module\Farm\Services\HouseService;
- use App\Module\Game\Dtos\HouseChangeTempDto;
- use Illuminate\Support\Facades\Log;
- use UCore\Helper\Cache;
- /**
- * 房屋临时数据逻辑类
- *
- * 负责处理房屋相关事件的临时数据存储逻辑,包括:
- * 1. 将房屋升级数据临时存储
- * 2. 将房屋降级数据临时存储
- * 3. 按照用户进行存储
- * 4. 同一用户的房屋多次变更进行数据覆盖
- */
- class HouseTemp
- {
- /**
- * 临时数据键前缀
- */
- const TEMP_KEY_PREFIX = 'game:house:changed:';
- /**
- * 临时数据过期时间(秒)
- */
- const TEMP_TTL = 3600; // 1小时
- /**
- * 处理房屋升级事件
- *
- * @param HouseUpgradedEvent $event 房屋升级事件
- * @return void
- */
- public static function handleHouseUpgraded(HouseUpgradedEvent $event): void
- {
- self::handleHouseChange($event->userId, $event->oldLevel, $event->newLevel, true);
- }
- /**
- * 处理房屋降级事件
- *
- * @param \App\Module\Farm\Events\HouseDowngradedEvent $event 房屋降级事件
- * @return void
- */
- public static function handleHouseDowngraded(\App\Module\Farm\Events\HouseDowngradedEvent $event): void
- {
- self::handleHouseChange($event->userId, $event->oldLevel, $event->newLevel, false);
- }
- /**
- * 处理房屋变更
- *
- * @param int $userId 用户ID
- * @param int $oldLevel 旧等级
- * @param int $newLevel 新等级
- * @param bool $isUpgrade 是否为升级
- * @return void
- */
- private static function handleHouseChange(int $userId, int $oldLevel, int $newLevel, bool $isUpgrade): void
- {
- try {
- // 构建临时数据键
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- // 获取房屋配置信息
- $houseConfig = HouseService::getHouseConfig($newLevel);
- // 构建房屋变更数据
- $houseData = [
- 'old_level' => $oldLevel,
- 'new_level' => $newLevel,
- 'is_upgrade' => $isUpgrade,
- 'output_bonus' => $houseConfig ? ($houseConfig->output_bonus / 100) : 0.0, // 将百分比值转换为小数
- 'special_land_limit' => $houseConfig ? $houseConfig->special_land_limit : 0,
- 'updated_at' => time(),
- ];
- // 存储房屋变更数据
- Cache::put($tempKey, $houseData, self::TEMP_TTL);
- Log::info('房屋变更数据已临时存储', [
- 'user_id' => $userId,
- 'old_level' => $oldLevel,
- 'new_level' => $newLevel,
- 'is_upgrade' => $isUpgrade,
- ]);
- } catch (\Exception $e) {
- Log::error('房屋变更数据临时存储失败', [
- 'error' => $e->getMessage(),
- 'user_id' => $userId,
- 'old_level' => $oldLevel,
- 'new_level' => $newLevel,
- ]);
- }
- }
- /**
- * 获取用户的房屋变更临时数据
- *
- * @param int $userId 用户ID
- * @return HouseChangeTempDto|null 用户的房屋变更数据,不存在时返回null
- */
- public static function getUserHouseChange(int $userId): ?HouseChangeTempDto
- {
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- $cachedData = Cache::get($tempKey);
- return HouseChangeTempDto::fromCacheSingle($cachedData);
- }
- /**
- * 清除用户的房屋变更临时数据
- *
- * @param int $userId 用户ID
- * @return void
- */
- public static function clearUserHouseChange(int $userId): void
- {
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- Cache::put($tempKey, null, 0);
- }
- }
|