LandTemp.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. */
  21. class LandTemp
  22. {
  23. /**
  24. * 临时数据键前缀 - 土地变更
  25. */
  26. const TEMP_KEY_CHANGED_PREFIX = 'game:land:changed:';
  27. /**
  28. * 临时数据键前缀 - 土地状态变更
  29. */
  30. const TEMP_KEY_STATUS_PREFIX = 'game:land:status:';
  31. /**
  32. * 临时数据过期时间(秒)
  33. */
  34. const TEMP_TTL = 3600; // 1小时
  35. /**
  36. * 处理土地创建事件
  37. *
  38. * @param LandCreatedEvent $event 土地创建事件
  39. * @return void
  40. */
  41. public static function handleLandCreated(LandCreatedEvent $event): void
  42. {
  43. try {
  44. // 获取土地信息
  45. $land = LandService::getLandById($event->landId);
  46. if (!$land) {
  47. Log::error('处理土地创建事件失败:土地不存在', [
  48. 'user_id' => $event->userId,
  49. 'land_id' => $event->landId,
  50. ]);
  51. return;
  52. }
  53. // 构建临时数据键,按用户ID进行存储
  54. $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $event->userId;
  55. // 获取当前用户的土地变更临时数据
  56. $userLandsTemp = Cache::get($tempKey, []);
  57. // 构建土地变更数据
  58. $landData = [
  59. 'land_id' => $event->landId,
  60. 'position' => $land->position,
  61. 'old_type' => 0, // 新创建的土地,旧类型为0
  62. 'new_type' => $land->land_type,
  63. 'updated_at' => time(),
  64. ];
  65. // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
  66. $userLandsTemp[$event->landId] = $landData;
  67. // 将更新后的数据存回临时存储
  68. Cache::put($tempKey, $userLandsTemp, self::TEMP_TTL);
  69. Log::info('土地创建数据已临时存储', [
  70. 'user_id' => $event->userId,
  71. 'land_id' => $event->landId,
  72. 'position' => $land->position,
  73. 'land_type' => $land->land_type,
  74. ]);
  75. } catch (\Exception $e) {
  76. Log::error('土地创建数据临时存储失败', [
  77. 'error' => $e->getMessage(),
  78. 'user_id' => $event->userId,
  79. 'land_id' => $event->landId,
  80. ]);
  81. }
  82. }
  83. /**
  84. * 处理土地升级事件
  85. *
  86. * @param LandUpgradedEvent $event 土地升级事件
  87. * @return void
  88. */
  89. public static function handleLandUpgraded(LandUpgradedEvent $event): void
  90. {
  91. try {
  92. // 获取土地信息
  93. $land = LandService::getLandById($event->landId);
  94. if (!$land) {
  95. Log::error('处理土地升级事件失败:土地不存在', [
  96. 'user_id' => $event->userId,
  97. 'land_id' => $event->landId,
  98. ]);
  99. return;
  100. }
  101. // 构建临时数据键,按用户ID进行存储
  102. $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $event->userId;
  103. // 获取当前用户的土地变更临时数据
  104. $userLandsTemp = Cache::get($tempKey, []);
  105. // 构建土地变更数据
  106. $landData = [
  107. 'land_id' => $event->landId,
  108. 'position' => $land->position,
  109. 'old_type' => $event->oldType,
  110. 'new_type' => $event->newType,
  111. 'updated_at' => time(),
  112. ];
  113. // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
  114. $userLandsTemp[$event->landId] = $landData;
  115. // 将更新后的数据存回临时存储
  116. Cache::put($tempKey, $userLandsTemp, self::TEMP_TTL);
  117. Log::info('土地升级数据已临时存储', [
  118. 'user_id' => $event->userId,
  119. 'land_id' => $event->landId,
  120. 'old_type' => $event->oldType,
  121. 'new_type' => $event->newType,
  122. ]);
  123. } catch (\Exception $e) {
  124. Log::error('土地升级数据临时存储失败', [
  125. 'error' => $e->getMessage(),
  126. 'user_id' => $event->userId,
  127. 'land_id' => $event->landId,
  128. ]);
  129. }
  130. }
  131. /**
  132. * 处理土地状态变更事件
  133. *
  134. * @param LandStatusChangedEvent $event 土地状态变更事件
  135. * @return void
  136. */
  137. public static function handleLandStatusChanged(LandStatusChangedEvent $event): void
  138. {
  139. try {
  140. // 获取土地信息
  141. $land = LandService::getLandById($event->landId);
  142. if (!$land) {
  143. Log::error('处理土地状态变更事件失败:土地不存在', [
  144. 'user_id' => $event->userId,
  145. 'land_id' => $event->landId,
  146. ]);
  147. return;
  148. }
  149. // 构建临时数据键,按用户ID进行存储
  150. $tempKey = self::TEMP_KEY_STATUS_PREFIX . $event->userId;
  151. // 获取当前用户的土地状态变更临时数据
  152. $userLandsStatusTemp = Cache::get($tempKey, []);
  153. // 获取土地上的作物ID(如果有)
  154. $cropId = null;
  155. if ($land->crop) {
  156. $cropId = $land->crop->id;
  157. }
  158. // 构建土地状态变更数据
  159. $statusData = [
  160. 'land_id' => $event->landId,
  161. 'position' => $land->position,
  162. 'land_type' => $land->land_type,
  163. 'old_status' => $event->oldStatus,
  164. 'new_status' => $event->newStatus,
  165. 'crop_id' => $cropId,
  166. 'updated_at' => time(),
  167. ];
  168. // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
  169. $userLandsStatusTemp[$event->landId] = $statusData;
  170. // 将更新后的数据存回临时存储
  171. Cache::put($tempKey, $userLandsStatusTemp, self::TEMP_TTL);
  172. Log::info('土地状态变更数据已临时存储', [
  173. 'user_id' => $event->userId,
  174. 'land_id' => $event->landId,
  175. 'old_status' => $event->oldStatus,
  176. 'new_status' => $event->newStatus,
  177. ]);
  178. } catch (\Exception $e) {
  179. Log::error('土地状态变更数据临时存储失败', [
  180. 'error' => $e->getMessage(),
  181. 'user_id' => $event->userId,
  182. 'land_id' => $event->landId,
  183. ]);
  184. }
  185. }
  186. /**
  187. * 获取用户的土地变更临时数据
  188. *
  189. * @param int $userId 用户ID
  190. * @return array 用户的土地变更数据
  191. */
  192. public static function getUserLandChanges(int $userId): array
  193. {
  194. $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $userId;
  195. $cachedData = Cache::get($tempKey, []);
  196. return LandChangeTempDto::fromCache($cachedData);
  197. }
  198. /**
  199. * 获取用户特定土地的变更临时数据
  200. *
  201. * @param int $userId 用户ID
  202. * @param int $landId 土地ID
  203. * @return LandChangeTempDto|null 土地变更数据,不存在时返回null
  204. */
  205. public static function getUserLandChange(int $userId, int $landId): ?LandChangeTempDto
  206. {
  207. $userLandChanges = self::getUserLandChanges($userId);
  208. return $userLandChanges[$landId] ?? null;
  209. }
  210. /**
  211. * 获取用户的土地状态变更临时数据
  212. *
  213. * @param int $userId 用户ID
  214. * @return array 用户的土地状态变更数据
  215. */
  216. public static function getUserLandStatusChanges(int $userId): array
  217. {
  218. $tempKey = self::TEMP_KEY_STATUS_PREFIX . $userId;
  219. $cachedData = Cache::get($tempKey, []);
  220. return LandStatusTempDto::fromCache($cachedData);
  221. }
  222. /**
  223. * 获取用户特定土地的状态变更临时数据
  224. *
  225. * @param int $userId 用户ID
  226. * @param int $landId 土地ID
  227. * @return LandStatusTempDto|null 土地状态变更数据,不存在时返回null
  228. */
  229. public static function getUserLandStatusChange(int $userId, int $landId): ?LandStatusTempDto
  230. {
  231. $userLandStatusChanges = self::getUserLandStatusChanges($userId);
  232. return $userLandStatusChanges[$landId] ?? null;
  233. }
  234. /**
  235. * 清除用户的土地变更临时数据
  236. *
  237. * @param int $userId 用户ID
  238. * @return void
  239. */
  240. public static function clearUserLandChanges(int $userId): void
  241. {
  242. $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $userId;
  243. Cache::put($tempKey, [], 0);
  244. }
  245. /**
  246. * 清除用户的土地状态变更临时数据
  247. *
  248. * @param int $userId 用户ID
  249. * @return void
  250. */
  251. public static function clearUserLandStatusChanges(int $userId): void
  252. {
  253. $tempKey = self::TEMP_KEY_STATUS_PREFIX . $userId;
  254. Cache::put($tempKey, [], 0);
  255. }
  256. /**
  257. * 清除用户的所有土地临时数据
  258. *
  259. * @param int $userId 用户ID
  260. * @return void
  261. */
  262. public static function clearUserAllLandTemp(int $userId): void
  263. {
  264. self::clearUserLandChanges($userId);
  265. self::clearUserLandStatusChanges($userId);
  266. }
  267. }