| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace App\Module\Game\Logics;
- use App\Module\Farm\Events\LandCreatedEvent;
- use App\Module\Farm\Events\LandStatusChangedEvent;
- use App\Module\Farm\Events\LandUpgradedEvent;
- use App\Module\Farm\Services\LandService;
- use App\Module\Game\Dtos\LandChangeTempDto;
- use App\Module\Game\Dtos\LandStatusTempDto;
- use Illuminate\Support\Facades\Log;
- use UCore\Helper\Cache;
- /**
- * 土地临时数据逻辑类
- *
- * 负责处理土地相关事件的临时数据存储逻辑,包括:
- * 1. 将土地创建数据临时存储
- * 2. 将土地升级数据临时存储
- * 3. 将土地状态变更数据临时存储
- * 4. 按照用户进行存储
- * 5. 同一土地多次变更进行数据覆盖
- * 6. 统一处理所有类型的土地变更,不再区分类型变更和状态变更
- */
- class LandTemp
- {
- /**
- * 临时数据键前缀 - 统一的土地变更
- */
- const TEMP_KEY_PREFIX = 'game:land:changes:';
- /**
- * 临时数据过期时间(秒)
- */
- const TEMP_TTL = 3600; // 1小时
- /**
- * 统一存储土地变更数据
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @param array $landData 土地变更数据
- * @return void
- */
- private static function storeLandChange(int $userId, int $landId, LandChangeTempDto $landData): void
- {
- // 构建统一的临时数据键
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- // 获取当前用户的土地变更临时数据
- $userLandsTemp = Cache::get($tempKey, []);
- // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
- $userLandsTemp[$landId] = $landData->toArray();
- // 将更新后的数据存回临时存储
- Cache::put($tempKey, $userLandsTemp, self::TEMP_TTL);
- }
- /**
- * 处理土地创建事件
- *
- * @param LandCreatedEvent $event 土地创建事件
- * @return void
- */
- public static function handleLandCreated(LandCreatedEvent $event): void
- {
- try {
- // 构建统一的土地变更数据
- $LandChangeTempDto = new LandChangeTempDto();
- $LandChangeTempDto->landId = $event->landId;
- $LandChangeTempDto->landType = 1;
- $LandChangeTempDto->changeType = 'type';
- $LandChangeTempDto->updatedAt = time();
- // 存储到统一的土地变更缓存
- self::storeLandChange($event->userId, $event->landId, $LandChangeTempDto);
- Log::info('土地创建数据已临时存储', [
- 'user_id' => $event->userId,
- 'land_id' => $event->landId,
- ]);
- } catch (\Exception $e) {
- Log::error('土地创建数据临时存储失败', [
- 'error' => $e->getMessage(),
- 'user_id' => $event->userId,
- 'land_id' => $event->landId,
- ]);
- }
- }
- /**
- * 处理土地升级事件
- *
- * @param LandUpgradedEvent $event 土地升级事件
- * @return void
- */
- public static function handleLandUpgraded(LandUpgradedEvent $event): void
- {
- try {
- // 从事件中直接获取土地信息
- $land = $event->land;
- if (!$land) {
- Log::error('处理土地升级事件失败:土地不存在', [
- 'user_id' => $event->userId,
- 'land_id' => $land->id ?? 'unknown',
- ]);
- return;
- }
- // 构建统一的土地变更数据
- $LandChangeTempDto = new LandChangeTempDto();
- $LandChangeTempDto->landId = $land->id;
- $LandChangeTempDto->landType = $land->landType->id;
- $LandChangeTempDto->changeType = 'type';
- $LandChangeTempDto->updatedAt = time();
- // 存储到统一的土地变更缓存
- self::storeLandChange($event->userId, $land->id, $LandChangeTempDto);
- Log::info('土地升级数据已临时存储', [
- 'user_id' => $event->userId,
- 'land_id' => $land->id,
- 'old_type' => $event->oldType,
- 'new_type' => $event->newType,
- ]);
- } catch (\Exception $e) {
- Log::error('土地升级数据临时存储失败', [
- 'error' => $e->getMessage(),
- 'user_id' => $event->userId,
- 'land_id' => $event->land->id ?? 'unknown',
- ]);
- }
- }
- /**
- * 处理土地状态变更事件
- *
- * @param LandStatusChangedEvent $event 土地状态变更事件
- * @return void
- */
- public static function handleLandStatusChanged(LandStatusChangedEvent $event): void
- {
- try {
- // 构建统一的土地变更数据
- $landData = new LandChangeTempDto();
- $landData->landId = $event->landId;
- $landData->oldStatus = $event->oldStatus;
- $landData->newStatus = $event->newStatus;
- $landData->changeType = 'status';
- $landData->updatedAt = time();
- // 存储到统一的土地变更缓存
- self::storeLandChange($event->userId, $event->landId, $landData);
- Log::info('土地状态变更数据已临时存储', [
- 'user_id' => $event->userId,
- 'land_id' => $event->landId,
- 'old_status' => $event->oldStatus,
- 'new_status' => $event->newStatus,
- ]);
- } catch (\Exception $e) {
- Log::error('土地状态变更数据临时存储失败', [
- 'error' => $e->getMessage(),
- 'user_id' => $event->userId,
- 'land_id' => $event->landId,
- ]);
- }
- }
- /**
- * 获取用户的所有土地变更临时数据(统一接口)
- *
- * @param int $userId 用户ID
- * @return array|LandChangeTempDto[] 用户的土地变更数据
- */
- public static function getUserLandChanges(int $userId): array
- {
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- $cachedData = Cache::get($tempKey, []);
- return LandChangeTempDto::fromCache($cachedData);
- }
- /**
- * 获取用户特定土地的变更临时数据
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @return LandChangeTempDto|null 土地变更数据,不存在时返回null
- */
- public static function getUserLandChange(int $userId, int $landId): ?LandChangeTempDto
- {
- $userLandChanges = self::getUserLandChanges($userId);
- return $userLandChanges[$landId] ?? null;
- }
- /**
- * 清除用户的所有土地变更临时数据(统一接口)
- *
- * @param int $userId 用户ID
- * @return void
- */
- public static function clearUserLandChanges(int $userId): void
- {
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- Cache::put($tempKey, [], 0);
- }
- /**
- * 清除用户的所有土地临时数据
- *
- * @param int $userId 用户ID
- * @return void
- */
- public static function clearUserAllLandTemp(int $userId): void
- {
- self::clearUserLandChanges($userId);
- }
- }
|