LandTemp.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. use App\Module\Farm\Events\LandCreatedEvent;
  4. use App\Module\Farm\Events\LandStatusChangedEvent;
  5. use App\Module\Farm\Events\LandUpgradedEvent;
  6. use App\Module\Farm\Services\LandService;
  7. use App\Module\Game\Dtos\LandChangeTempDto;
  8. use App\Module\Game\Dtos\LandStatusTempDto;
  9. use Illuminate\Support\Facades\Log;
  10. use UCore\Helper\Cache;
  11. /**
  12. * 土地临时数据逻辑类
  13. *
  14. * 负责处理土地相关事件的临时数据存储逻辑,包括:
  15. * 1. 将土地创建数据临时存储
  16. * 2. 将土地升级数据临时存储
  17. * 3. 将土地状态变更数据临时存储
  18. * 4. 按照用户进行存储
  19. * 5. 同一土地多次变更进行数据覆盖
  20. * 6. 统一处理所有类型的土地变更,不再区分类型变更和状态变更
  21. */
  22. class LandTemp
  23. {
  24. /**
  25. * 临时数据键前缀 - 统一的土地变更
  26. */
  27. const TEMP_KEY_PREFIX = 'game:land:changes:';
  28. /**
  29. * 临时数据过期时间(秒)
  30. */
  31. const TEMP_TTL = 3600; // 1小时
  32. /**
  33. * 统一存储土地变更数据
  34. *
  35. * @param int $userId 用户ID
  36. * @param int $landId 土地ID
  37. * @param array $landData 土地变更数据
  38. * @return void
  39. */
  40. private static function storeLandChange(int $userId, int $landId, LandChangeTempDto $landData): void
  41. {
  42. // 构建统一的临时数据键
  43. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  44. // 获取当前用户的土地变更临时数据
  45. $userLandsTemp = Cache::get($tempKey, []);
  46. // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
  47. $userLandsTemp[$landId] = $landData->toArray();
  48. // 将更新后的数据存回临时存储
  49. Cache::put($tempKey, $userLandsTemp, self::TEMP_TTL);
  50. }
  51. /**
  52. * 处理土地创建事件
  53. *
  54. * @param LandCreatedEvent $event 土地创建事件
  55. * @return void
  56. */
  57. public static function handleLandCreated(LandCreatedEvent $event): void
  58. {
  59. try {
  60. // 构建统一的土地变更数据
  61. $LandChangeTempDto = new LandChangeTempDto();
  62. $LandChangeTempDto->landId = $event->landId;
  63. $LandChangeTempDto->landType = 1;
  64. $LandChangeTempDto->changeType = 'type';
  65. $LandChangeTempDto->updatedAt = time();
  66. // 存储到统一的土地变更缓存
  67. self::storeLandChange($event->userId, $event->landId, $LandChangeTempDto);
  68. Log::info('土地创建数据已临时存储', [
  69. 'user_id' => $event->userId,
  70. 'land_id' => $event->landId,
  71. ]);
  72. } catch (\Exception $e) {
  73. Log::error('土地创建数据临时存储失败', [
  74. 'error' => $e->getMessage(),
  75. 'user_id' => $event->userId,
  76. 'land_id' => $event->landId,
  77. ]);
  78. }
  79. }
  80. /**
  81. * 处理土地升级事件
  82. *
  83. * @param LandUpgradedEvent $event 土地升级事件
  84. * @return void
  85. */
  86. public static function handleLandUpgraded(LandUpgradedEvent $event): void
  87. {
  88. try {
  89. // 从事件中直接获取土地信息
  90. $land = $event->land;
  91. if (!$land) {
  92. Log::error('处理土地升级事件失败:土地不存在', [
  93. 'user_id' => $event->userId,
  94. 'land_id' => $land->id ?? 'unknown',
  95. ]);
  96. return;
  97. }
  98. // 构建统一的土地变更数据
  99. $LandChangeTempDto = new LandChangeTempDto();
  100. $LandChangeTempDto->landId = $land->id;
  101. $LandChangeTempDto->landType = $land->landType->id;
  102. $LandChangeTempDto->changeType = 'type';
  103. $LandChangeTempDto->updatedAt = time();
  104. // 存储到统一的土地变更缓存
  105. self::storeLandChange($event->userId, $land->id, $LandChangeTempDto);
  106. Log::info('土地升级数据已临时存储', [
  107. 'user_id' => $event->userId,
  108. 'land_id' => $land->id,
  109. 'old_type' => $event->oldType,
  110. 'new_type' => $event->newType,
  111. ]);
  112. } catch (\Exception $e) {
  113. Log::error('土地升级数据临时存储失败', [
  114. 'error' => $e->getMessage(),
  115. 'user_id' => $event->userId,
  116. 'land_id' => $event->land->id ?? 'unknown',
  117. ]);
  118. }
  119. }
  120. /**
  121. * 处理土地状态变更事件
  122. *
  123. * @param LandStatusChangedEvent $event 土地状态变更事件
  124. * @return void
  125. */
  126. public static function handleLandStatusChanged(LandStatusChangedEvent $event): void
  127. {
  128. try {
  129. // 构建统一的土地变更数据
  130. $landData = new LandChangeTempDto();
  131. $landData->landId = $event->landId;
  132. $landData->oldStatus = $event->oldStatus;
  133. $landData->newStatus = $event->newStatus;
  134. $landData->changeType = 'status';
  135. $landData->updatedAt = time();
  136. // 存储到统一的土地变更缓存
  137. self::storeLandChange($event->userId, $event->landId, $landData);
  138. Log::info('土地状态变更数据已临时存储', [
  139. 'user_id' => $event->userId,
  140. 'land_id' => $event->landId,
  141. 'old_status' => $event->oldStatus,
  142. 'new_status' => $event->newStatus,
  143. ]);
  144. } catch (\Exception $e) {
  145. Log::error('土地状态变更数据临时存储失败', [
  146. 'error' => $e->getMessage(),
  147. 'user_id' => $event->userId,
  148. 'land_id' => $event->landId,
  149. ]);
  150. }
  151. }
  152. /**
  153. * 获取用户的所有土地变更临时数据(统一接口)
  154. *
  155. * @param int $userId 用户ID
  156. * @return array|LandChangeTempDto[] 用户的土地变更数据
  157. */
  158. public static function getUserLandChanges(int $userId): array
  159. {
  160. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  161. $cachedData = Cache::get($tempKey, []);
  162. return LandChangeTempDto::fromCache($cachedData);
  163. }
  164. /**
  165. * 获取用户特定土地的变更临时数据
  166. *
  167. * @param int $userId 用户ID
  168. * @param int $landId 土地ID
  169. * @return LandChangeTempDto|null 土地变更数据,不存在时返回null
  170. */
  171. public static function getUserLandChange(int $userId, int $landId): ?LandChangeTempDto
  172. {
  173. $userLandChanges = self::getUserLandChanges($userId);
  174. return $userLandChanges[$landId] ?? null;
  175. }
  176. /**
  177. * 清除用户的所有土地变更临时数据(统一接口)
  178. *
  179. * @param int $userId 用户ID
  180. * @return void
  181. */
  182. public static function clearUserLandChanges(int $userId): void
  183. {
  184. $tempKey = self::TEMP_KEY_PREFIX . $userId;
  185. Cache::put($tempKey, [], 0);
  186. }
  187. /**
  188. * 清除用户的所有土地临时数据
  189. *
  190. * @param int $userId 用户ID
  191. * @return void
  192. */
  193. public static function clearUserAllLandTemp(int $userId): void
  194. {
  195. self::clearUserLandChanges($userId);
  196. }
  197. }