|
|
@@ -20,24 +20,55 @@ use UCore\Helper\Cache;
|
|
|
* 3. 将土地状态变更数据临时存储
|
|
|
* 4. 按照用户进行存储
|
|
|
* 5. 同一土地多次变更进行数据覆盖
|
|
|
+ * 6. 统一处理所有类型的土地变更,不再区分类型变更和状态变更
|
|
|
*/
|
|
|
class LandTemp
|
|
|
{
|
|
|
/**
|
|
|
- * 临时数据键前缀 - 土地变更
|
|
|
+ * 临时数据键前缀 - 统一的土地变更
|
|
|
*/
|
|
|
- const TEMP_KEY_CHANGED_PREFIX = 'game:land:changed:';
|
|
|
+ const TEMP_KEY_PREFIX = 'game:land:changes:';
|
|
|
|
|
|
/**
|
|
|
- * 临时数据键前缀 - 土地状态变更
|
|
|
+ * 临时数据键前缀 - 土地变更(向后兼容)
|
|
|
+ * @deprecated 使用 TEMP_KEY_PREFIX 替代
|
|
|
*/
|
|
|
- const TEMP_KEY_STATUS_PREFIX = 'game:land:status:';
|
|
|
+ const TEMP_KEY_CHANGED_PREFIX = 'game:land:changes:';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 临时数据键前缀 - 土地状态变更(向后兼容)
|
|
|
+ * @deprecated 使用 TEMP_KEY_PREFIX 替代
|
|
|
+ */
|
|
|
+ const TEMP_KEY_STATUS_PREFIX = 'game:land:changes:';
|
|
|
|
|
|
/**
|
|
|
* 临时数据过期时间(秒)
|
|
|
*/
|
|
|
const TEMP_TTL = 3600; // 1小时
|
|
|
|
|
|
+ /**
|
|
|
+ * 统一存储土地变更数据
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param int $landId 土地ID
|
|
|
+ * @param array $landData 土地变更数据
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ private static function storeLandChange(int $userId, int $landId, array $landData): void
|
|
|
+ {
|
|
|
+ // 构建统一的临时数据键
|
|
|
+ $tempKey = self::TEMP_KEY_PREFIX . $userId;
|
|
|
+
|
|
|
+ // 获取当前用户的土地变更临时数据
|
|
|
+ $userLandsTemp = Cache::get($tempKey, []);
|
|
|
+
|
|
|
+ // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
|
|
|
+ $userLandsTemp[$landId] = $landData;
|
|
|
+
|
|
|
+ // 将更新后的数据存回临时存储
|
|
|
+ Cache::put($tempKey, $userLandsTemp, self::TEMP_TTL);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 处理土地创建事件
|
|
|
*
|
|
|
@@ -57,26 +88,23 @@ class LandTemp
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 构建临时数据键,按用户ID进行存储
|
|
|
- $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $event->userId;
|
|
|
-
|
|
|
- // 获取当前用户的土地变更临时数据
|
|
|
- $userLandsTemp = Cache::get($tempKey, []);
|
|
|
-
|
|
|
- // 构建土地变更数据
|
|
|
+ // 构建统一的土地变更数据
|
|
|
$landData = [
|
|
|
'land_id' => $event->landId,
|
|
|
'position' => $land->position,
|
|
|
+ 'land_type' => $land->landType,
|
|
|
'old_type' => 0, // 新创建的土地,旧类型为0
|
|
|
'new_type' => $land->landType,
|
|
|
+ 'old_status' => null,
|
|
|
+ 'new_status' => null,
|
|
|
+ 'crop_id' => null,
|
|
|
+ 'seed_id' => null,
|
|
|
+ 'change_type' => 'type',
|
|
|
'updated_at' => time(),
|
|
|
];
|
|
|
|
|
|
- // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
|
|
|
- $userLandsTemp[$event->landId] = $landData;
|
|
|
-
|
|
|
- // 将更新后的数据存回临时存储
|
|
|
- Cache::put($tempKey, $userLandsTemp, self::TEMP_TTL);
|
|
|
+ // 存储到统一的土地变更缓存
|
|
|
+ self::storeLandChange($event->userId, $event->landId, $landData);
|
|
|
|
|
|
Log::info('土地创建数据已临时存储', [
|
|
|
'user_id' => $event->userId,
|
|
|
@@ -112,26 +140,23 @@ class LandTemp
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 构建临时数据键,按用户ID进行存储
|
|
|
- $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $event->userId;
|
|
|
-
|
|
|
- // 获取当前用户的土地变更临时数据
|
|
|
- $userLandsTemp = Cache::get($tempKey, []);
|
|
|
-
|
|
|
- // 构建土地变更数据
|
|
|
+ // 构建统一的土地变更数据
|
|
|
$landData = [
|
|
|
'land_id' => $land->id,
|
|
|
'position' => $land->position,
|
|
|
+ 'land_type' => $land->landType,
|
|
|
'old_type' => $event->oldType,
|
|
|
'new_type' => $event->newType,
|
|
|
+ 'old_status' => null,
|
|
|
+ 'new_status' => null,
|
|
|
+ 'crop_id' => null,
|
|
|
+ 'seed_id' => null,
|
|
|
+ 'change_type' => 'type',
|
|
|
'updated_at' => time(),
|
|
|
];
|
|
|
|
|
|
- // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
|
|
|
- $userLandsTemp[$land->id] = $landData;
|
|
|
-
|
|
|
- // 将更新后的数据存回临时存储
|
|
|
- Cache::put($tempKey, $userLandsTemp, self::TEMP_TTL);
|
|
|
+ // 存储到统一的土地变更缓存
|
|
|
+ self::storeLandChange($event->userId, $land->id, $landData);
|
|
|
|
|
|
Log::info('土地升级数据已临时存储', [
|
|
|
'user_id' => $event->userId,
|
|
|
@@ -145,7 +170,7 @@ class LandTemp
|
|
|
'user_id' => $event->userId,
|
|
|
'land_id' => $event->land->id ?? 'unknown',
|
|
|
]);
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -167,12 +192,6 @@ class LandTemp
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 构建临时数据键,按用户ID进行存储
|
|
|
- $tempKey = self::TEMP_KEY_STATUS_PREFIX . $event->userId;
|
|
|
-
|
|
|
- // 获取当前用户的土地状态变更临时数据
|
|
|
- $userLandsStatusTemp = Cache::get($tempKey, []);
|
|
|
-
|
|
|
// 获取土地上的作物ID和种子ID(如果有)
|
|
|
$cropId = null;
|
|
|
$seedId = null;
|
|
|
@@ -181,23 +200,23 @@ class LandTemp
|
|
|
$seedId = $land->crop->seedId; // 获取种子配置ID
|
|
|
}
|
|
|
|
|
|
- // 构建土地状态变更数据
|
|
|
- $statusData = [
|
|
|
+ // 构建统一的土地变更数据
|
|
|
+ $landData = [
|
|
|
'land_id' => $event->landId,
|
|
|
'position' => $land->position,
|
|
|
'land_type' => $land->landType,
|
|
|
+ 'old_type' => null,
|
|
|
+ 'new_type' => null,
|
|
|
'old_status' => $event->oldStatus,
|
|
|
'new_status' => $event->newStatus,
|
|
|
'crop_id' => $cropId,
|
|
|
'seed_id' => $seedId, // 添加种子配置ID
|
|
|
+ 'change_type' => 'status',
|
|
|
'updated_at' => time(),
|
|
|
];
|
|
|
|
|
|
- // 使用土地ID作为键,实现同一土地多次变更的数据覆盖
|
|
|
- $userLandsStatusTemp[$event->landId] = $statusData;
|
|
|
-
|
|
|
- // 将更新后的数据存回临时存储
|
|
|
- Cache::put($tempKey, $userLandsStatusTemp, self::TEMP_TTL);
|
|
|
+ // 存储到统一的土地变更缓存
|
|
|
+ self::storeLandChange($event->userId, $event->landId, $landData);
|
|
|
|
|
|
Log::info('土地状态变更数据已临时存储', [
|
|
|
'user_id' => $event->userId,
|
|
|
@@ -215,14 +234,14 @@ class LandTemp
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取用户的土地变更临时数据
|
|
|
+ * 获取用户的所有土地变更临时数据(统一接口)
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @return array|LandChangeTempDto[] 用户的土地变更数据
|
|
|
*/
|
|
|
public static function getUserLandChanges(int $userId): array
|
|
|
{
|
|
|
- $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $userId;
|
|
|
+ $tempKey = self::TEMP_KEY_PREFIX . $userId;
|
|
|
$cachedData = Cache::get($tempKey, []);
|
|
|
|
|
|
return LandChangeTempDto::fromCache($cachedData);
|
|
|
@@ -242,54 +261,85 @@ class LandTemp
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取用户的土地状态变更临时数据
|
|
|
+ * 获取用户的土地状态变更临时数据(向后兼容)
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @return array 用户的土地状态变更数据
|
|
|
+ * @deprecated 使用 getUserLandChanges() 替代,然后过滤 changeType === 'status'
|
|
|
*/
|
|
|
public static function getUserLandStatusChanges(int $userId): array
|
|
|
{
|
|
|
- $tempKey = self::TEMP_KEY_STATUS_PREFIX . $userId;
|
|
|
- $cachedData = Cache::get($tempKey, []);
|
|
|
+ $allChanges = self::getUserLandChanges($userId);
|
|
|
+ $statusChanges = [];
|
|
|
+
|
|
|
+ foreach ($allChanges as $landId => $change) {
|
|
|
+ if ($change->isStatusChange()) {
|
|
|
+ // 为了向后兼容,转换为LandStatusTempDto格式
|
|
|
+ $statusChanges[$landId] = new LandStatusTempDto([
|
|
|
+ 'landId' => $change->landId,
|
|
|
+ 'position' => $change->position,
|
|
|
+ 'landType' => $change->landType,
|
|
|
+ 'oldStatus' => $change->oldStatus,
|
|
|
+ 'newStatus' => $change->newStatus,
|
|
|
+ 'cropId' => $change->cropId,
|
|
|
+ 'seedId' => $change->seedId,
|
|
|
+ 'updatedAt' => $change->updatedAt,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return LandStatusTempDto::fromCache($cachedData);
|
|
|
+ return $statusChanges;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取用户特定土地的状态变更临时数据
|
|
|
+ * 获取用户特定土地的状态变更临时数据(向后兼容)
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int $landId 土地ID
|
|
|
* @return LandStatusTempDto|null 土地状态变更数据,不存在时返回null
|
|
|
+ * @deprecated 使用 getUserLandChange() 替代,然后检查 isStatusChange()
|
|
|
*/
|
|
|
public static function getUserLandStatusChange(int $userId, int $landId): ?LandStatusTempDto
|
|
|
{
|
|
|
- $userLandStatusChanges = self::getUserLandStatusChanges($userId);
|
|
|
- return $userLandStatusChanges[$landId] ?? null;
|
|
|
+ $change = self::getUserLandChange($userId, $landId);
|
|
|
+ if ($change && $change->isStatusChange()) {
|
|
|
+ return new LandStatusTempDto([
|
|
|
+ 'landId' => $change->landId,
|
|
|
+ 'position' => $change->position,
|
|
|
+ 'landType' => $change->landType,
|
|
|
+ 'oldStatus' => $change->oldStatus,
|
|
|
+ 'newStatus' => $change->newStatus,
|
|
|
+ 'cropId' => $change->cropId,
|
|
|
+ 'seedId' => $change->seedId,
|
|
|
+ 'updatedAt' => $change->updatedAt,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 清除用户的土地变更临时数据
|
|
|
+ * 清除用户的所有土地变更临时数据(统一接口)
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @return void
|
|
|
*/
|
|
|
public static function clearUserLandChanges(int $userId): void
|
|
|
{
|
|
|
- $tempKey = self::TEMP_KEY_CHANGED_PREFIX . $userId;
|
|
|
+ $tempKey = self::TEMP_KEY_PREFIX . $userId;
|
|
|
Cache::put($tempKey, [], 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 清除用户的土地状态变更临时数据
|
|
|
+ * 清除用户的土地状态变更临时数据(向后兼容)
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @return void
|
|
|
+ * @deprecated 使用 clearUserLandChanges() 替代
|
|
|
*/
|
|
|
public static function clearUserLandStatusChanges(int $userId): void
|
|
|
{
|
|
|
- $tempKey = self::TEMP_KEY_STATUS_PREFIX . $userId;
|
|
|
- Cache::put($tempKey, [], 0);
|
|
|
+ // 现在统一存储,所以清除所有数据
|
|
|
+ self::clearUserLandChanges($userId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -301,6 +351,5 @@ class LandTemp
|
|
|
public static function clearUserAllLandTemp(int $userId): void
|
|
|
{
|
|
|
self::clearUserLandChanges($userId);
|
|
|
- self::clearUserLandStatusChanges($userId);
|
|
|
}
|
|
|
}
|