HouseTemp.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. use App\Module\Farm\Events\HouseUpgradedEvent;
  4. use App\Module\Farm\Services\HouseService;
  5. use App\Module\Game\Dtos\HouseChangeTempDto;
  6. use Illuminate\Support\Facades\Log;
  7. use UCore\Helper\Cache;
  8. /**
  9. * 房屋临时数据逻辑类
  10. *
  11. * 负责处理房屋相关事件的临时数据存储逻辑,包括:
  12. * 1. 将房屋升级数据临时存储
  13. * 2. 将房屋降级数据临时存储
  14. * 3. 按照用户进行存储
  15. * 4. 同一用户的房屋多次变更进行数据覆盖
  16. */
  17. class HouseTemp
  18. {
  19. /**
  20. * 临时数据键前缀
  21. */
  22. const TEMP_KEY_PREFIX = 'game:house:changed:';
  23. /**
  24. * 临时数据过期时间(秒)
  25. */
  26. const TEMP_TTL = 3600; // 1小时
  27. /**
  28. * 处理房屋升级事件
  29. *
  30. * @param HouseUpgradedEvent $event 房屋升级事件
  31. * @return void
  32. */
  33. public static function handleHouseUpgraded(HouseUpgradedEvent $event): void
  34. {
  35. self::handleHouseChange($event->userId, $event->oldLevel, $event->newLevel, true);
  36. }
  37. /**
  38. * 处理房屋降级事件
  39. *
  40. * @param \App\Module\Farm\Events\HouseDowngradedEvent $event 房屋降级事件
  41. * @return void
  42. */
  43. public static function handleHouseDowngraded(\App\Module\Farm\Events\HouseDowngradedEvent $event): void
  44. {
  45. self::handleHouseChange($event->userId, $event->oldLevel, $event->newLevel, false);
  46. }
  47. /**
  48. * 处理房屋变更
  49. *
  50. * @param int $userId 用户ID
  51. * @param int $oldLevel 旧等级
  52. * @param int $newLevel 新等级
  53. * @param bool $isUpgrade 是否为升级
  54. * @return void
  55. */
  56. private static function handleHouseChange(int $userId, int $oldLevel, int $newLevel, bool $isUpgrade): void
  57. {
  58. try {
  59. // 构建临时数据键
  60. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  61. // 获取房屋配置信息
  62. $houseConfig = HouseService::getHouseConfig($newLevel);
  63. // 构建房屋变更数据
  64. $houseData = [
  65. 'old_level' => $oldLevel,
  66. 'new_level' => $newLevel,
  67. 'is_upgrade' => $isUpgrade,
  68. 'output_bonus' => $houseConfig ? ($houseConfig->output_bonus / 100) : 0.0, // 将百分比值转换为小数
  69. 'special_land_limit' => $houseConfig ? $houseConfig->special_land_limit : 0,
  70. 'updated_at' => time(),
  71. ];
  72. // 存储房屋变更数据
  73. Cache::put($tempKey, $houseData, self::TEMP_TTL);
  74. Log::info('房屋变更数据已临时存储', [
  75. 'user_id' => $userId,
  76. 'old_level' => $oldLevel,
  77. 'new_level' => $newLevel,
  78. 'is_upgrade' => $isUpgrade,
  79. ]);
  80. } catch (\Exception $e) {
  81. Log::error('房屋变更数据临时存储失败', [
  82. 'error' => $e->getMessage(),
  83. 'user_id' => $userId,
  84. 'old_level' => $oldLevel,
  85. 'new_level' => $newLevel,
  86. ]);
  87. }
  88. }
  89. /**
  90. * 获取用户的房屋变更临时数据
  91. *
  92. * @param int $userId 用户ID
  93. * @return HouseChangeTempDto|null 用户的房屋变更数据,不存在时返回null
  94. */
  95. public static function getUserHouseChange(int $userId): ?HouseChangeTempDto
  96. {
  97. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  98. $cachedData = Cache::get($tempKey);
  99. return HouseChangeTempDto::fromCacheSingle($cachedData);
  100. }
  101. /**
  102. * 清除用户的房屋变更临时数据
  103. *
  104. * @param int $userId 用户ID
  105. * @return void
  106. */
  107. public static function clearUserHouseChange(int $userId): void
  108. {
  109. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  110. Cache::put($tempKey, null, 0);
  111. }
  112. }