GodTemp.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. use App\Module\Farm\Events\BuffActivatedEvent;
  4. use App\Module\Game\Dtos\GodChangeTempDto;
  5. use Illuminate\Support\Facades\Log;
  6. use UCore\Helper\Cache;
  7. /**
  8. * 神像临时数据逻辑类
  9. *
  10. * 负责处理神像相关事件的临时数据存储逻辑,包括:
  11. * 1. 将神像激活数据临时存储
  12. * 2. 按照用户进行存储
  13. * 3. 同一神像多次变更进行数据覆盖
  14. */
  15. class GodTemp
  16. {
  17. /**
  18. * 临时数据键前缀
  19. */
  20. const TEMP_KEY_PREFIX = 'game:god:changed:';
  21. /**
  22. * 临时数据过期时间(秒)
  23. */
  24. const TEMP_TTL = 3600; // 1小时
  25. /**
  26. * 处理神像激活事件
  27. *
  28. * 将神像激活数据临时存储,按用户ID和神像类型进行存储
  29. * 同一神像多次变更会覆盖之前的数据
  30. *
  31. * @param BuffActivatedEvent $event 神像激活事件
  32. * @return void
  33. */
  34. public static function handleBuffActivated(BuffActivatedEvent $event): void
  35. {
  36. try {
  37. // 构建临时数据键,按用户ID进行存储
  38. $tempKey = self::TEMP_KEY_PREFIX . $event->userId;
  39. // 获取当前用户的神像变更临时数据
  40. $userGodsTemp = Cache::get($tempKey, []);
  41. // 计算过期时间戳
  42. $expireTime = $event->buff->expire_time ? $event->buff->expire_time->timestamp : null;
  43. $now = time();
  44. $duration = $expireTime ? ($expireTime - $now) : 0;
  45. // 构建神像变更数据
  46. $godData = [
  47. 'god_type' => $event->buff->buff_type,
  48. 'status' => true, // 激活状态
  49. 'expire_time' => $expireTime,
  50. 'duration' => $duration,
  51. 'updated_at' => $now,
  52. ];
  53. // 使用神像类型作为键,实现同一神像多次变更的数据覆盖
  54. $userGodsTemp[$event->buff->buff_type] = $godData;
  55. // 将更新后的数据存回临时存储
  56. Cache::put($tempKey, $userGodsTemp, self::TEMP_TTL);
  57. Log::info('神像变更数据已临时存储', [
  58. 'user_id' => $event->userId,
  59. 'god_type' => $event->buff->buff_type,
  60. 'expire_time' => $expireTime,
  61. 'duration' => $duration,
  62. ]);
  63. } catch (\Exception $e) {
  64. Log::error('神像变更数据临时存储失败', [
  65. 'error' => $e->getMessage(),
  66. 'user_id' => $event->userId,
  67. 'god_type' => $event->buff->buff_type ?? null,
  68. ]);
  69. }
  70. }
  71. /**
  72. * 获取用户的神像变更临时数据
  73. *
  74. * @param int $userId 用户ID
  75. * @return GodChangeTempDto[] 用户的神像变更数据
  76. */
  77. public static function getUserGodChanges(int $userId): array
  78. {
  79. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  80. $rawData = Cache::get($tempKey, []);
  81. $result = [];
  82. foreach ($rawData as $godType => $godData) {
  83. $result[$godType] = GodChangeTempDto::fromArray($godData);
  84. }
  85. return $result;
  86. }
  87. /**
  88. * 获取用户特定神像类型的变更临时数据
  89. *
  90. * @param int $userId 用户ID
  91. * @param int $godType 神像类型
  92. * @return GodChangeTempDto|null 神像变更数据,不存在时返回null
  93. */
  94. public static function getUserGodChange(int $userId, int $godType): ?GodChangeTempDto
  95. {
  96. $userGodChanges = self::getUserGodChanges($userId);
  97. return $userGodChanges[$godType] ?? null;
  98. }
  99. /**
  100. * 清除用户的神像变更临时数据
  101. *
  102. * @param int $userId 用户ID
  103. * @return void
  104. */
  105. public static function clearUserGodChanges(int $userId): void
  106. {
  107. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  108. Cache::put($tempKey, [], 0);
  109. }
  110. /**
  111. * 清除用户特定神像类型的变更临时数据
  112. *
  113. * @param int $userId 用户ID
  114. * @param int $godType 神像类型
  115. * @return void
  116. */
  117. public static function clearUserGodChange(int $userId, int $godType): void
  118. {
  119. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  120. $userGodsTemp = Cache::get($tempKey, []);
  121. if (isset($userGodsTemp[$godType])) {
  122. unset($userGodsTemp[$godType]);
  123. Cache::put($tempKey, $userGodsTemp, self::TEMP_TTL);
  124. }
  125. }
  126. }