| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Module\Game\Logics;
- use App\Module\Farm\Events\BuffActivatedEvent;
- use App\Module\Game\Dtos\GodChangeTempDto;
- use Illuminate\Support\Facades\Log;
- use UCore\Helper\Cache;
- /**
- * 神像临时数据逻辑类
- *
- * 负责处理神像相关事件的临时数据存储逻辑,包括:
- * 1. 将神像激活数据临时存储
- * 2. 按照用户进行存储
- * 3. 同一神像多次变更进行数据覆盖
- */
- class GodTemp
- {
- /**
- * 临时数据键前缀
- */
- const TEMP_KEY_PREFIX = 'game:god:changed:';
- /**
- * 临时数据过期时间(秒)
- */
- const TEMP_TTL = 3600; // 1小时
- /**
- * 处理神像激活事件
- *
- * 将神像激活数据临时存储,按用户ID和神像类型进行存储
- * 同一神像多次变更会覆盖之前的数据
- *
- * @param BuffActivatedEvent $event 神像激活事件
- * @return void
- */
- public static function handleBuffActivated(BuffActivatedEvent $event): void
- {
- try {
- // 构建临时数据键,按用户ID进行存储
- $tempKey = self::TEMP_KEY_PREFIX . $event->userId;
- // 获取当前用户的神像变更临时数据
- $userGodsTemp = Cache::get($tempKey, []);
- // 计算过期时间戳
- $expireTime = $event->buff->expire_time ? $event->buff->expire_time->timestamp : null;
- $now = time();
- $duration = $expireTime ? ($expireTime - $now) : 0;
- // 构建神像变更数据
- $godData = [
- 'god_type' => $event->buff->buff_type,
- 'status' => true, // 激活状态
- 'expire_time' => $expireTime,
- 'duration' => $duration,
- 'updated_at' => $now,
- ];
- // 使用神像类型作为键,实现同一神像多次变更的数据覆盖
- $userGodsTemp[$event->buff->buff_type] = $godData;
- // 将更新后的数据存回临时存储
- Cache::put($tempKey, $userGodsTemp, self::TEMP_TTL);
- Log::info('神像变更数据已临时存储', [
- 'user_id' => $event->userId,
- 'god_type' => $event->buff->buff_type,
- 'expire_time' => $expireTime,
- 'duration' => $duration,
- ]);
- } catch (\Exception $e) {
- Log::error('神像变更数据临时存储失败', [
- 'error' => $e->getMessage(),
- 'user_id' => $event->userId,
- 'god_type' => $event->buff->buff_type ?? null,
- ]);
- }
- }
- /**
- * 获取用户的神像变更临时数据
- *
- * @param int $userId 用户ID
- * @return GodChangeTempDto[] 用户的神像变更数据
- */
- public static function getUserGodChanges(int $userId): array
- {
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- $rawData = Cache::get($tempKey, []);
- $result = [];
- foreach ($rawData as $godType => $godData) {
- $result[$godType] = GodChangeTempDto::fromArray($godData);
- }
- return $result;
- }
- /**
- * 获取用户特定神像类型的变更临时数据
- *
- * @param int $userId 用户ID
- * @param int $godType 神像类型
- * @return GodChangeTempDto|null 神像变更数据,不存在时返回null
- */
- public static function getUserGodChange(int $userId, int $godType): ?GodChangeTempDto
- {
- $userGodChanges = self::getUserGodChanges($userId);
- return $userGodChanges[$godType] ?? null;
- }
- /**
- * 清除用户的神像变更临时数据
- *
- * @param int $userId 用户ID
- * @return void
- */
- public static function clearUserGodChanges(int $userId): void
- {
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- Cache::put($tempKey, [], 0);
- }
- /**
- * 清除用户特定神像类型的变更临时数据
- *
- * @param int $userId 用户ID
- * @param int $godType 神像类型
- * @return void
- */
- public static function clearUserGodChange(int $userId, int $godType): void
- {
- $tempKey = self::TEMP_KEY_PREFIX . $userId;
- $userGodsTemp = Cache::get($tempKey, []);
- if (isset($userGodsTemp[$godType])) {
- unset($userGodsTemp[$godType]);
- Cache::put($tempKey, $userGodsTemp, self::TEMP_TTL);
- }
- }
- }
|