forked from datawhale/whale-town-end
refactor:重构核心模块架构
- 重构用户管理服务,优化内存服务实现 - 简化zulip_core模块结构,移除冗余配置和接口 - 更新用户状态枚举和实体定义 - 优化登录核心服务的测试覆盖
This commit is contained in:
224
src/core/location_broadcast_core/README.md
Normal file
224
src/core/location_broadcast_core/README.md
Normal file
@@ -0,0 +1,224 @@
|
||||
# Location Broadcast Core 模块
|
||||
|
||||
## 模块概述
|
||||
|
||||
Location Broadcast Core 是位置广播系统的核心技术实现模块,专门为位置广播业务提供技术支撑。该模块负责管理用户会话、位置数据缓存、数据持久化等核心技术功能,确保位置广播系统的高性能和可靠性。
|
||||
|
||||
### 模块组成
|
||||
- **LocationBroadcastCore**: 位置广播核心服务,处理会话管理和位置缓存
|
||||
- **UserPositionCore**: 用户位置持久化核心服务,处理数据库操作
|
||||
- **接口定义**: 核心服务接口和数据结构定义
|
||||
|
||||
### 技术架构
|
||||
- **架构层级**: Core层(核心技术实现)
|
||||
- **命名规范**: 使用`_core`后缀,表明为业务支撑模块
|
||||
- **职责边界**: 专注技术实现,不包含业务逻辑
|
||||
|
||||
## 对外接口
|
||||
|
||||
### LocationBroadcastCore 服务接口
|
||||
|
||||
#### 会话管理
|
||||
- `addUserToSession(sessionId, userId, socketId)` - 添加用户到会话
|
||||
- `removeUserFromSession(sessionId, userId)` - 从会话中移除用户
|
||||
- `getSessionUsers(sessionId)` - 获取会话中的用户列表
|
||||
|
||||
#### 位置数据管理
|
||||
- `setUserPosition(userId, position)` - 设置用户位置到Redis缓存
|
||||
- `getUserPosition(userId)` - 从Redis获取用户位置
|
||||
- `getSessionPositions(sessionId)` - 获取会话中所有用户位置
|
||||
- `getMapPositions(mapId)` - 获取地图中所有用户位置
|
||||
|
||||
#### 数据清理维护
|
||||
- `cleanupUserData(userId)` - 清理用户相关数据
|
||||
- `cleanupEmptySession(sessionId)` - 清理空会话
|
||||
- `cleanupExpiredData(expireTime)` - 清理过期数据
|
||||
|
||||
### UserPositionCore 服务接口
|
||||
|
||||
#### 数据持久化
|
||||
- `saveUserPosition(userId, position)` - 保存用户位置到数据库
|
||||
- `loadUserPosition(userId)` - 从数据库加载用户位置
|
||||
|
||||
#### 历史记录管理
|
||||
- `savePositionHistory(userId, position, sessionId?)` - 保存位置历史记录
|
||||
- `getPositionHistory(userId, limit?)` - 获取位置历史记录
|
||||
|
||||
#### 批量操作
|
||||
- `batchUpdateUserStatus(userIds, status)` - 批量更新用户状态
|
||||
- `cleanupExpiredPositions(expireTime)` - 清理过期位置数据
|
||||
|
||||
#### 统计分析
|
||||
- `getUserPositionStats(userId)` - 获取用户位置统计信息
|
||||
- `migratePositionData(fromUserId, toUserId)` - 迁移位置数据
|
||||
|
||||
## 内部依赖
|
||||
|
||||
### 项目内部依赖
|
||||
|
||||
#### Redis服务依赖
|
||||
- **依赖标识**: `REDIS_SERVICE`
|
||||
- **用途**: 高性能位置数据缓存、会话状态管理
|
||||
- **关键操作**: sadd, setex, get, del, smembers, scard等
|
||||
|
||||
#### 用户档案服务依赖
|
||||
- **依赖标识**: `IUserProfilesService`
|
||||
- **用途**: 用户位置数据持久化、用户信息查询
|
||||
- **关键操作**: updatePosition, findByUserId, batchUpdateStatus
|
||||
|
||||
### 数据结构依赖
|
||||
- **Position接口**: 位置数据结构定义
|
||||
- **SessionUser接口**: 会话用户数据结构
|
||||
- **PositionHistory接口**: 位置历史记录结构
|
||||
- **核心服务接口**: ILocationBroadcastCore, IUserPositionCore
|
||||
|
||||
## 核心特性
|
||||
|
||||
### 技术特性
|
||||
|
||||
#### 高性能缓存
|
||||
- **Redis缓存**: 位置数据存储在Redis中,支持毫秒级读写
|
||||
- **过期策略**: 会话数据3600秒过期,位置数据1800秒过期
|
||||
- **批量操作**: 支持批量数据读写,优化性能
|
||||
|
||||
#### 数据一致性
|
||||
- **双写策略**: 位置数据同时写入Redis缓存和MySQL数据库
|
||||
- **事务处理**: 确保数据操作的原子性
|
||||
- **异常恢复**: 完善的错误处理和数据恢复机制
|
||||
|
||||
#### 可扩展性
|
||||
- **接口抽象**: 通过依赖注入实现服务解耦
|
||||
- **模块化设计**: 清晰的职责分离和边界定义
|
||||
- **配置化**: 关键参数通过常量定义,便于调整
|
||||
|
||||
### 功能特性
|
||||
|
||||
#### 实时会话管理
|
||||
- **用户加入/离开**: 实时更新会话状态
|
||||
- **Socket映射**: 维护用户与WebSocket连接的映射关系
|
||||
- **自动清理**: 空会话和过期数据的自动清理
|
||||
|
||||
#### 位置数据处理
|
||||
- **多地图支持**: 支持用户在不同地图间切换
|
||||
- **位置历史**: 记录用户位置变化轨迹
|
||||
- **地理查询**: 按地图或会话查询用户位置
|
||||
|
||||
#### 数据维护
|
||||
- **定期清理**: 支持过期数据的批量清理
|
||||
- **数据迁移**: 支持用户数据的迁移操作
|
||||
- **统计分析**: 提供位置数据的统计功能
|
||||
|
||||
### 质量特性
|
||||
|
||||
#### 可靠性
|
||||
- **异常处理**: 全面的错误处理和日志记录
|
||||
- **数据校验**: 严格的输入参数验证
|
||||
- **容错机制**: 部分失败不影响整体功能
|
||||
|
||||
#### 可观测性
|
||||
- **详细日志**: 操作开始、成功、失败的完整日志
|
||||
- **性能监控**: 记录操作耗时和性能指标
|
||||
- **错误追踪**: 完整的错误堆栈和上下文信息
|
||||
|
||||
#### 可测试性
|
||||
- **单元测试**: 60个测试用例,100%方法覆盖
|
||||
- **Mock支持**: 完善的依赖Mock机制
|
||||
- **边界测试**: 包含正常、异常、边界条件测试
|
||||
|
||||
## 潜在风险
|
||||
|
||||
### 技术风险
|
||||
|
||||
#### Redis依赖风险
|
||||
- **风险描述**: Redis服务不可用导致位置数据无法缓存
|
||||
- **影响程度**: 高 - 影响实时位置功能
|
||||
- **缓解措施**:
|
||||
- 实现Redis连接重试机制
|
||||
- 考虑Redis集群部署
|
||||
- 添加降级策略,临时使用数据库
|
||||
|
||||
#### 内存使用风险
|
||||
- **风险描述**: 大量用户同时在线导致Redis内存占用过高
|
||||
- **影响程度**: 中 - 可能影响系统性能
|
||||
- **缓解措施**:
|
||||
- 合理设置数据过期时间
|
||||
- 监控内存使用情况
|
||||
- 实现数据清理策略
|
||||
|
||||
#### 数据一致性风险
|
||||
- **风险描述**: Redis和数据库数据不一致
|
||||
- **影响程度**: 中 - 可能导致数据错误
|
||||
- **缓解措施**:
|
||||
- 实现数据同步检查机制
|
||||
- 添加数据修复功能
|
||||
- 定期进行数据一致性校验
|
||||
|
||||
### 业务风险
|
||||
|
||||
#### 位置数据丢失
|
||||
- **风险描述**: 系统故障导致用户位置数据丢失
|
||||
- **影响程度**: 中 - 影响用户体验
|
||||
- **缓解措施**:
|
||||
- 实现位置数据备份机制
|
||||
- 添加数据恢复功能
|
||||
- 提供位置重置选项
|
||||
|
||||
#### 会话状态错误
|
||||
- **风险描述**: 用户会话状态不正确,影响位置广播
|
||||
- **影响程度**: 中 - 影响功能正常使用
|
||||
- **缓解措施**:
|
||||
- 实现会话状态校验
|
||||
- 添加会话修复机制
|
||||
- 提供手动会话管理功能
|
||||
|
||||
### 运维风险
|
||||
|
||||
#### 性能监控缺失
|
||||
- **风险描述**: 缺乏有效的性能监控,问题发现滞后
|
||||
- **影响程度**: 中 - 影响问题响应速度
|
||||
- **缓解措施**:
|
||||
- 集成APM监控工具
|
||||
- 设置关键指标告警
|
||||
- 建立性能基线
|
||||
|
||||
#### 日志存储风险
|
||||
- **风险描述**: 大量日志导致存储空间不足
|
||||
- **影响程度**: 低 - 可能影响日志记录
|
||||
- **缓解措施**:
|
||||
- 实现日志轮转机制
|
||||
- 设置日志级别控制
|
||||
- 定期清理历史日志
|
||||
|
||||
### 安全风险
|
||||
|
||||
#### 数据访问控制
|
||||
- **风险描述**: 位置数据可能被未授权访问
|
||||
- **影响程度**: 高 - 涉及用户隐私
|
||||
- **缓解措施**:
|
||||
- 实现严格的权限控制
|
||||
- 添加数据访问审计
|
||||
- 对敏感数据进行加密
|
||||
|
||||
#### 注入攻击风险
|
||||
- **风险描述**: 恶意输入可能导致数据库注入攻击
|
||||
- **影响程度**: 高 - 可能导致数据泄露
|
||||
- **缓解措施**:
|
||||
- 使用参数化查询
|
||||
- 严格输入验证
|
||||
- 实现SQL注入检测
|
||||
|
||||
#### 缓存投毒风险
|
||||
- **风险描述**: 恶意数据写入Redis缓存
|
||||
- **影响程度**: 中 - 可能影响数据准确性
|
||||
- **缓解措施**:
|
||||
- 实现数据校验机制
|
||||
- 添加缓存数据签名
|
||||
- 定期缓存数据校验
|
||||
|
||||
---
|
||||
|
||||
## 版本信息
|
||||
- **当前版本**: 1.0.6
|
||||
- **最后更新**: 2026-01-08
|
||||
- **维护者**: moyin
|
||||
- **测试覆盖**: 60个测试用例全部通过
|
||||
421
src/core/location_broadcast_core/core_services.interface.ts
Normal file
421
src/core/location_broadcast_core/core_services.interface.ts
Normal file
@@ -0,0 +1,421 @@
|
||||
/**
|
||||
* 核心服务接口定义
|
||||
*
|
||||
* 功能描述:
|
||||
* - 定义位置广播系统核心服务的接口规范
|
||||
* - 提供服务间交互的标准化接口
|
||||
* - 支持依赖注入和模块化设计
|
||||
* - 实现核心技术功能的抽象层
|
||||
*
|
||||
* 职责分离:
|
||||
* - 接口定义:核心服务的方法签名和契约
|
||||
* - 类型安全:TypeScript接口约束
|
||||
* - 模块解耦:服务间的松耦合设计
|
||||
* - 可测试性:支持Mock和单元测试
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-08: 文件重命名 - 修正kebab-case为snake_case命名规范 (修改者: moyin)
|
||||
* - 2026-01-08: 功能新增 - 创建核心服务接口定义
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.1
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
import { Position, PositionUpdate, PositionHistory, PositionQuery, PositionStats } from './position.interface';
|
||||
import { GameSession, SessionUser, JoinSessionRequest, JoinSessionResponse, LeaveSessionRequest, SessionQuery, SessionStats } from './session.interface';
|
||||
|
||||
/**
|
||||
* 位置广播核心服务接口
|
||||
*
|
||||
* 职责:
|
||||
* - 提供位置广播系统的核心功能
|
||||
* - 管理用户会话和位置数据
|
||||
* - 协调Redis缓存和数据库持久化
|
||||
* - 处理位置更新和广播逻辑
|
||||
*/
|
||||
export interface ILocationBroadcastCore {
|
||||
// 会话数据管理
|
||||
/**
|
||||
* 添加用户到会话
|
||||
* @param sessionId 会话ID
|
||||
* @param userId 用户ID
|
||||
* @param socketId WebSocket连接ID
|
||||
*/
|
||||
addUserToSession(sessionId: string, userId: string, socketId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 从会话中移除用户
|
||||
* @param sessionId 会话ID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
removeUserFromSession(sessionId: string, userId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 获取会话中的用户列表
|
||||
* @param sessionId 会话ID
|
||||
* @returns 会话用户列表
|
||||
*/
|
||||
getSessionUsers(sessionId: string): Promise<SessionUser[]>;
|
||||
|
||||
// 位置数据管理
|
||||
/**
|
||||
* 设置用户位置
|
||||
* @param userId 用户ID
|
||||
* @param position 位置信息
|
||||
*/
|
||||
setUserPosition(userId: string, position: Position): Promise<void>;
|
||||
|
||||
/**
|
||||
* 获取用户位置
|
||||
* @param userId 用户ID
|
||||
* @returns 用户位置信息
|
||||
*/
|
||||
getUserPosition(userId: string): Promise<Position | null>;
|
||||
|
||||
/**
|
||||
* 获取会话中所有用户的位置
|
||||
* @param sessionId 会话ID
|
||||
* @returns 位置信息列表
|
||||
*/
|
||||
getSessionPositions(sessionId: string): Promise<Position[]>;
|
||||
|
||||
/**
|
||||
* 获取地图中所有用户的位置
|
||||
* @param mapId 地图ID
|
||||
* @returns 位置信息列表
|
||||
*/
|
||||
getMapPositions(mapId: string): Promise<Position[]>;
|
||||
|
||||
// 清理操作
|
||||
/**
|
||||
* 清理用户数据
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
cleanupUserData(userId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 清理空会话
|
||||
* @param sessionId 会话ID
|
||||
*/
|
||||
cleanupEmptySession(sessionId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 清理过期数据
|
||||
* @param expireTime 过期时间
|
||||
* @returns 清理的记录数
|
||||
*/
|
||||
cleanupExpiredData(expireTime: Date): Promise<number>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话管理核心服务接口
|
||||
*
|
||||
* 职责:
|
||||
* - 管理游戏会话的生命周期
|
||||
* - 处理用户加入和离开会话
|
||||
* - 维护会话状态和配置
|
||||
* - 提供会话查询和统计功能
|
||||
*/
|
||||
export interface ILocationSessionCore {
|
||||
/**
|
||||
* 创建新会话
|
||||
* @param sessionId 会话ID
|
||||
* @param config 会话配置
|
||||
*/
|
||||
createSession(sessionId: string, config?: any): Promise<GameSession>;
|
||||
|
||||
/**
|
||||
* 用户加入会话
|
||||
* @param request 加入会话请求
|
||||
* @returns 加入会话响应
|
||||
*/
|
||||
joinSession(request: JoinSessionRequest): Promise<JoinSessionResponse>;
|
||||
|
||||
/**
|
||||
* 用户离开会话
|
||||
* @param request 离开会话请求
|
||||
*/
|
||||
leaveSession(request: LeaveSessionRequest): Promise<void>;
|
||||
|
||||
/**
|
||||
* 获取会话信息
|
||||
* @param sessionId 会话ID
|
||||
* @returns 会话信息
|
||||
*/
|
||||
getSession(sessionId: string): Promise<GameSession | null>;
|
||||
|
||||
/**
|
||||
* 获取用户当前会话
|
||||
* @param userId 用户ID
|
||||
* @returns 会话ID
|
||||
*/
|
||||
getUserSession(userId: string): Promise<string | null>;
|
||||
|
||||
/**
|
||||
* 查询会话列表
|
||||
* @param query 查询条件
|
||||
* @returns 会话列表
|
||||
*/
|
||||
querySessions(query: SessionQuery): Promise<GameSession[]>;
|
||||
|
||||
/**
|
||||
* 获取会话统计信息
|
||||
* @param sessionId 会话ID
|
||||
* @returns 统计信息
|
||||
*/
|
||||
getSessionStats(sessionId: string): Promise<SessionStats>;
|
||||
|
||||
/**
|
||||
* 更新会话配置
|
||||
* @param sessionId 会话ID
|
||||
* @param config 新配置
|
||||
*/
|
||||
updateSessionConfig(sessionId: string, config: any): Promise<void>;
|
||||
|
||||
/**
|
||||
* 结束会话
|
||||
* @param sessionId 会话ID
|
||||
* @param reason 结束原因
|
||||
*/
|
||||
endSession(sessionId: string, reason: string): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置管理核心服务接口
|
||||
*
|
||||
* 职责:
|
||||
* - 管理用户位置数据的缓存
|
||||
* - 处理位置更新和验证
|
||||
* - 提供位置查询和统计功能
|
||||
* - 协调位置数据的持久化
|
||||
*/
|
||||
export interface ILocationPositionCore {
|
||||
/**
|
||||
* 更新用户位置
|
||||
* @param userId 用户ID
|
||||
* @param update 位置更新数据
|
||||
*/
|
||||
updatePosition(userId: string, update: PositionUpdate): Promise<Position>;
|
||||
|
||||
/**
|
||||
* 获取用户位置
|
||||
* @param userId 用户ID
|
||||
* @returns 位置信息
|
||||
*/
|
||||
getPosition(userId: string): Promise<Position | null>;
|
||||
|
||||
/**
|
||||
* 批量获取用户位置
|
||||
* @param userIds 用户ID列表
|
||||
* @returns 位置信息列表
|
||||
*/
|
||||
getBatchPositions(userIds: string[]): Promise<Position[]>;
|
||||
|
||||
/**
|
||||
* 查询位置数据
|
||||
* @param query 查询条件
|
||||
* @returns 位置信息列表
|
||||
*/
|
||||
queryPositions(query: PositionQuery): Promise<Position[]>;
|
||||
|
||||
/**
|
||||
* 获取位置统计信息
|
||||
* @returns 统计信息
|
||||
*/
|
||||
getPositionStats(): Promise<PositionStats>;
|
||||
|
||||
/**
|
||||
* 验证位置数据
|
||||
* @param position 位置信息
|
||||
* @returns 验证结果
|
||||
*/
|
||||
validatePosition(position: Position): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 清理用户位置
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
cleanupUserPosition(userId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 清理地图位置数据
|
||||
* @param mapId 地图ID
|
||||
* @returns 清理的记录数
|
||||
*/
|
||||
cleanupMapPositions(mapId: string): Promise<number>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户位置持久化核心服务接口
|
||||
*
|
||||
* 职责:
|
||||
* - 管理用户位置的数据库持久化
|
||||
* - 处理位置历史记录
|
||||
* - 提供位置数据的长期存储
|
||||
* - 支持位置数据的恢复和迁移
|
||||
*/
|
||||
export interface IUserPositionCore {
|
||||
/**
|
||||
* 保存用户位置到数据库
|
||||
* @param userId 用户ID
|
||||
* @param position 位置信息
|
||||
*/
|
||||
saveUserPosition(userId: string, position: Position): Promise<void>;
|
||||
|
||||
/**
|
||||
* 从数据库加载用户位置
|
||||
* @param userId 用户ID
|
||||
* @returns 位置信息
|
||||
*/
|
||||
loadUserPosition(userId: string): Promise<Position | null>;
|
||||
|
||||
/**
|
||||
* 保存位置历史记录
|
||||
* @param userId 用户ID
|
||||
* @param position 位置信息
|
||||
* @param sessionId 会话ID(可选)
|
||||
*/
|
||||
savePositionHistory(userId: string, position: Position, sessionId?: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 获取位置历史记录
|
||||
* @param userId 用户ID
|
||||
* @param limit 限制数量
|
||||
* @returns 历史记录列表
|
||||
*/
|
||||
getPositionHistory(userId: string, limit?: number): Promise<PositionHistory[]>;
|
||||
|
||||
/**
|
||||
* 批量更新用户状态
|
||||
* @param userIds 用户ID列表
|
||||
* @param status 状态值
|
||||
* @returns 更新的记录数
|
||||
*/
|
||||
batchUpdateUserStatus(userIds: string[], status: number): Promise<number>;
|
||||
|
||||
/**
|
||||
* 清理过期位置数据
|
||||
* @param expireTime 过期时间
|
||||
* @returns 清理的记录数
|
||||
*/
|
||||
cleanupExpiredPositions(expireTime: Date): Promise<number>;
|
||||
|
||||
/**
|
||||
* 获取用户位置统计
|
||||
* @param userId 用户ID
|
||||
* @returns 统计信息
|
||||
*/
|
||||
getUserPositionStats(userId: string): Promise<any>;
|
||||
|
||||
/**
|
||||
* 迁移位置数据
|
||||
* @param fromUserId 源用户ID
|
||||
* @param toUserId 目标用户ID
|
||||
*/
|
||||
migratePositionData(fromUserId: string, toUserId: string): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置广播事件服务接口
|
||||
*
|
||||
* 职责:
|
||||
* - 处理位置广播相关的事件
|
||||
* - 管理事件的发布和订阅
|
||||
* - 提供事件驱动的系统架构
|
||||
* - 支持异步事件处理
|
||||
*/
|
||||
export interface ILocationBroadcastEventService {
|
||||
/**
|
||||
* 发布位置更新事件
|
||||
* @param userId 用户ID
|
||||
* @param position 位置信息
|
||||
* @param sessionId 会话ID
|
||||
*/
|
||||
publishPositionUpdate(userId: string, position: Position, sessionId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 发布用户加入事件
|
||||
* @param userId 用户ID
|
||||
* @param sessionId 会话ID
|
||||
*/
|
||||
publishUserJoined(userId: string, sessionId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 发布用户离开事件
|
||||
* @param userId 用户ID
|
||||
* @param sessionId 会话ID
|
||||
* @param reason 离开原因
|
||||
*/
|
||||
publishUserLeft(userId: string, sessionId: string, reason: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* 订阅位置更新事件
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
subscribePositionUpdates(callback: (userId: string, position: Position) => void): void;
|
||||
|
||||
/**
|
||||
* 订阅会话事件
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
subscribeSessionEvents(callback: (event: any) => void): void;
|
||||
|
||||
/**
|
||||
* 取消订阅
|
||||
* @param eventType 事件类型
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
unsubscribe(eventType: string, callback: Function): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置广播配置服务接口
|
||||
*
|
||||
* 职责:
|
||||
* - 管理位置广播系统的配置
|
||||
* - 提供配置的动态更新
|
||||
* - 支持配置的验证和默认值
|
||||
* - 处理配置的持久化存储
|
||||
*/
|
||||
export interface ILocationBroadcastConfigService {
|
||||
/**
|
||||
* 获取配置值
|
||||
* @param key 配置键
|
||||
* @param defaultValue 默认值
|
||||
* @returns 配置值
|
||||
*/
|
||||
get<T>(key: string, defaultValue?: T): T;
|
||||
|
||||
/**
|
||||
* 设置配置值
|
||||
* @param key 配置键
|
||||
* @param value 配置值
|
||||
*/
|
||||
set<T>(key: string, value: T): Promise<void>;
|
||||
|
||||
/**
|
||||
* 获取所有配置
|
||||
* @returns 配置对象
|
||||
*/
|
||||
getAll(): Record<string, any>;
|
||||
|
||||
/**
|
||||
* 重新加载配置
|
||||
*/
|
||||
reload(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 验证配置
|
||||
* @param config 配置对象
|
||||
* @returns 验证结果
|
||||
*/
|
||||
validate(config: Record<string, any>): boolean;
|
||||
|
||||
/**
|
||||
* 获取默认配置
|
||||
* @returns 默认配置对象
|
||||
*/
|
||||
getDefaults(): Record<string, any>;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* 位置广播核心模块
|
||||
*
|
||||
* 功能描述:
|
||||
* - 提供位置广播系统核心服务的模块配置
|
||||
* - 管理核心服务的依赖注入和生命周期
|
||||
* - 集成Redis缓存和用户档案数据服务
|
||||
* - 为业务层提供统一的核心服务接口
|
||||
*
|
||||
* 职责分离:
|
||||
* - 模块配置:定义核心服务的提供者和导出
|
||||
* - 依赖管理:配置服务间的依赖注入关系
|
||||
* - 接口抽象:提供统一的服务接口供业务层使用
|
||||
* - 生命周期:管理核心服务的初始化和销毁
|
||||
*
|
||||
* 架构设计:
|
||||
* - 核心层:提供技术基础设施和数据管理
|
||||
* - 服务解耦:通过接口实现服务间的松耦合
|
||||
* - 可测试性:支持Mock服务进行单元测试
|
||||
* - 可扩展性:便于添加新的核心服务
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-08: 功能新增 - 创建位置广播核心模块配置
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.0
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
import { Module } from '@nestjs/common';
|
||||
import { LocationBroadcastCore } from './location_broadcast_core.service';
|
||||
import { UserPositionCore } from './user_position_core.service';
|
||||
import { UserProfilesModule } from '../db/user_profiles/user_profiles.module';
|
||||
import { RedisModule } from '../redis/redis.module';
|
||||
|
||||
/**
|
||||
* 位置广播核心模块类
|
||||
*
|
||||
* 职责:
|
||||
* - 配置位置广播系统的核心服务
|
||||
* - 管理服务间的依赖关系和注入
|
||||
* - 提供统一的核心服务接口
|
||||
* - 支持业务层的功能实现
|
||||
*
|
||||
* 模块特性:
|
||||
* - 核心服务:LocationBroadcastCore, UserPositionCore
|
||||
* - 依赖模块:UserProfilesModule, RedisModule
|
||||
* - 接口导出:提供标准化的服务接口
|
||||
* - 配置灵活:支持不同环境的配置需求
|
||||
*
|
||||
* 服务说明:
|
||||
* - LocationBroadcastCore: 位置广播的核心逻辑和缓存管理
|
||||
* - UserPositionCore: 用户位置的数据库持久化管理
|
||||
*
|
||||
* 依赖说明:
|
||||
* - UserProfilesModule: 提供用户档案数据访问服务
|
||||
* - RedisModule: 提供Redis缓存服务
|
||||
*
|
||||
* 使用场景:
|
||||
* - 业务层模块导入此模块获取核心服务
|
||||
* - 单元测试时Mock核心服务接口
|
||||
* - 系统集成时配置核心服务依赖
|
||||
*/
|
||||
@Module({
|
||||
imports: [
|
||||
// 导入用户档案模块,提供数据库访问能力
|
||||
UserProfilesModule.forRoot(), // 自动根据环境选择MySQL或内存模式
|
||||
|
||||
// 导入Redis模块,提供缓存服务
|
||||
RedisModule, // 使用现有的Redis模块配置
|
||||
],
|
||||
providers: [
|
||||
// 位置广播核心服务
|
||||
LocationBroadcastCore,
|
||||
{
|
||||
provide: 'ILocationBroadcastCore',
|
||||
useClass: LocationBroadcastCore,
|
||||
},
|
||||
|
||||
// 用户位置持久化核心服务
|
||||
UserPositionCore,
|
||||
{
|
||||
provide: 'IUserPositionCore',
|
||||
useClass: UserPositionCore,
|
||||
},
|
||||
|
||||
// TODO: 后续可以添加更多核心服务
|
||||
// LocationSessionCore,
|
||||
// LocationPositionCore,
|
||||
// LocationBroadcastEventService,
|
||||
// LocationBroadcastConfigService,
|
||||
],
|
||||
exports: [
|
||||
// 导出核心服务供业务层使用
|
||||
LocationBroadcastCore,
|
||||
'ILocationBroadcastCore',
|
||||
|
||||
UserPositionCore,
|
||||
'IUserPositionCore',
|
||||
|
||||
// TODO: 导出其他核心服务接口
|
||||
// 'ILocationSessionCore',
|
||||
// 'ILocationPositionCore',
|
||||
// 'ILocationBroadcastEventService',
|
||||
// 'ILocationBroadcastConfigService',
|
||||
],
|
||||
})
|
||||
export class LocationBroadcastCoreModule {
|
||||
/**
|
||||
* 模块初始化时的日志记录
|
||||
*/
|
||||
constructor() {
|
||||
console.log('🚀 LocationBroadcastCoreModule initialized');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,602 @@
|
||||
/**
|
||||
* 位置广播核心服务单元测试
|
||||
*
|
||||
* 功能描述:
|
||||
* - 测试位置广播核心服务的所有功能
|
||||
* - 验证会话管理和位置数据操作
|
||||
* - 确保错误处理和边界条件正确
|
||||
* - 提供完整的测试覆盖率
|
||||
*
|
||||
* 测试范围:
|
||||
* - 用户会话管理(加入/离开)
|
||||
* - 位置数据操作(设置/获取)
|
||||
* - 数据清理和维护功能
|
||||
* - 异常情况处理
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.0
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LocationBroadcastCore } from './location_broadcast_core.service';
|
||||
import { Position } from './position.interface';
|
||||
import { SessionUserStatus } from './session.interface';
|
||||
|
||||
describe('LocationBroadcastCore', () => {
|
||||
let service: LocationBroadcastCore;
|
||||
let mockRedisService: any;
|
||||
let mockUserProfilesService: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
// 创建Redis服务的Mock
|
||||
mockRedisService = {
|
||||
sadd: jest.fn(),
|
||||
setex: jest.fn(),
|
||||
expire: jest.fn(),
|
||||
srem: jest.fn(),
|
||||
del: jest.fn(),
|
||||
get: jest.fn(),
|
||||
smembers: jest.fn(),
|
||||
scard: jest.fn(),
|
||||
};
|
||||
|
||||
// 创建用户档案服务的Mock
|
||||
mockUserProfilesService = {
|
||||
findOne: jest.fn(),
|
||||
update: jest.fn(),
|
||||
};
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
LocationBroadcastCore,
|
||||
{
|
||||
provide: 'REDIS_SERVICE',
|
||||
useValue: mockRedisService,
|
||||
},
|
||||
{
|
||||
provide: 'IUserProfilesService',
|
||||
useValue: mockUserProfilesService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<LocationBroadcastCore>(LocationBroadcastCore);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('addUserToSession', () => {
|
||||
it('应该成功添加用户到会话', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userId = 'test-user';
|
||||
const socketId = 'test-socket';
|
||||
|
||||
mockRedisService.sadd.mockResolvedValue(1);
|
||||
mockRedisService.setex.mockResolvedValue('OK');
|
||||
mockRedisService.expire.mockResolvedValue(1);
|
||||
|
||||
// Act
|
||||
await service.addUserToSession(sessionId, userId, socketId);
|
||||
|
||||
// Assert
|
||||
expect(mockRedisService.sadd).toHaveBeenCalledWith(
|
||||
`session:${sessionId}:users`,
|
||||
userId
|
||||
);
|
||||
expect(mockRedisService.setex).toHaveBeenCalledWith(
|
||||
`user:${userId}:session`,
|
||||
3600,
|
||||
sessionId
|
||||
);
|
||||
expect(mockRedisService.setex).toHaveBeenCalledWith(
|
||||
`user:${userId}:socket`,
|
||||
3600,
|
||||
socketId
|
||||
);
|
||||
expect(mockRedisService.setex).toHaveBeenCalledWith(
|
||||
`socket:${socketId}:user`,
|
||||
3600,
|
||||
userId
|
||||
);
|
||||
});
|
||||
|
||||
it('应该处理Redis操作失败的情况', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userId = 'test-user';
|
||||
const socketId = 'test-socket';
|
||||
|
||||
mockRedisService.sadd.mockRejectedValue(new Error('Redis连接失败'));
|
||||
|
||||
// Act & Assert
|
||||
await expect(
|
||||
service.addUserToSession(sessionId, userId, socketId)
|
||||
).rejects.toThrow('Redis连接失败');
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeUserFromSession', () => {
|
||||
it('应该成功从会话中移除用户', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userId = 'test-user';
|
||||
const socketId = 'test-socket';
|
||||
|
||||
mockRedisService.srem.mockResolvedValue(1);
|
||||
mockRedisService.get.mockResolvedValue(socketId);
|
||||
mockRedisService.del.mockResolvedValue(1);
|
||||
mockRedisService.scard.mockResolvedValue(0);
|
||||
|
||||
// Act
|
||||
await service.removeUserFromSession(sessionId, userId);
|
||||
|
||||
// Assert
|
||||
expect(mockRedisService.srem).toHaveBeenCalledWith(
|
||||
`session:${sessionId}:users`,
|
||||
userId
|
||||
);
|
||||
expect(mockRedisService.del).toHaveBeenCalledWith(`user:${userId}:session`);
|
||||
expect(mockRedisService.del).toHaveBeenCalledWith(`user:${userId}:socket`);
|
||||
expect(mockRedisService.del).toHaveBeenCalledWith(`socket:${socketId}:user`);
|
||||
});
|
||||
|
||||
it('应该在会话为空时清理会话', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userId = 'test-user';
|
||||
|
||||
mockRedisService.srem.mockResolvedValue(1);
|
||||
mockRedisService.get.mockResolvedValue(null);
|
||||
mockRedisService.del.mockResolvedValue(1);
|
||||
mockRedisService.scard.mockResolvedValue(0); // 会话为空
|
||||
|
||||
const cleanupEmptySessionSpy = jest.spyOn(service, 'cleanupEmptySession');
|
||||
cleanupEmptySessionSpy.mockResolvedValue();
|
||||
|
||||
// Act
|
||||
await service.removeUserFromSession(sessionId, userId);
|
||||
|
||||
// Assert
|
||||
expect(cleanupEmptySessionSpy).toHaveBeenCalledWith(sessionId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSessionUsers', () => {
|
||||
it('应该返回会话中的用户列表', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userIds = ['user1', 'user2'];
|
||||
const socketId1 = 'socket1';
|
||||
const socketId2 = 'socket2';
|
||||
|
||||
mockRedisService.smembers.mockResolvedValue(userIds);
|
||||
mockRedisService.get
|
||||
.mockResolvedValueOnce(socketId1) // user1的socket
|
||||
.mockResolvedValueOnce(socketId2); // user2的socket
|
||||
|
||||
// Mock getUserPosition方法
|
||||
const getUserPositionSpy = jest.spyOn(service, 'getUserPosition');
|
||||
getUserPositionSpy
|
||||
.mockResolvedValueOnce({
|
||||
userId: 'user1',
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
})
|
||||
.mockResolvedValueOnce(null); // user2没有位置
|
||||
|
||||
// Act
|
||||
const result = await service.getSessionUsers(sessionId);
|
||||
|
||||
// Assert
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0]).toMatchObject({
|
||||
userId: 'user1',
|
||||
socketId: socketId1,
|
||||
status: SessionUserStatus.ONLINE
|
||||
});
|
||||
expect(result[0].position).toBeDefined();
|
||||
expect(result[1]).toMatchObject({
|
||||
userId: 'user2',
|
||||
socketId: socketId2,
|
||||
status: SessionUserStatus.ONLINE
|
||||
});
|
||||
expect(result[1].position).toBeNull();
|
||||
});
|
||||
|
||||
it('应该在会话不存在时返回空数组', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'non-existent-session';
|
||||
mockRedisService.smembers.mockResolvedValue([]);
|
||||
|
||||
// Act
|
||||
const result = await service.getSessionUsers(sessionId);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('应该处理获取用户信息失败的情况', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userIds = ['user1', 'user2'];
|
||||
|
||||
mockRedisService.smembers.mockResolvedValue(userIds);
|
||||
mockRedisService.get.mockRejectedValue(new Error('Redis错误'));
|
||||
|
||||
// Act
|
||||
const result = await service.getSessionUsers(sessionId);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual([]); // 应该返回空数组而不是抛出异常
|
||||
});
|
||||
});
|
||||
|
||||
describe('setUserPosition', () => {
|
||||
it('应该成功设置用户位置', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
const sessionId = 'test-session';
|
||||
|
||||
mockRedisService.get.mockResolvedValue(null); // No previous position data
|
||||
mockRedisService.setex.mockResolvedValue('OK');
|
||||
mockRedisService.sadd.mockResolvedValue(1);
|
||||
mockRedisService.expire.mockResolvedValue(1);
|
||||
mockRedisService.srem.mockResolvedValue(1);
|
||||
|
||||
// Act
|
||||
await service.setUserPosition(userId, position);
|
||||
|
||||
// Assert
|
||||
expect(mockRedisService.setex).toHaveBeenCalledWith(
|
||||
`location:user:${userId}`,
|
||||
1800,
|
||||
expect.stringContaining('"x":100')
|
||||
);
|
||||
expect(mockRedisService.sadd).toHaveBeenCalledWith(
|
||||
`map:${position.mapId}:users`,
|
||||
userId
|
||||
);
|
||||
});
|
||||
|
||||
it('应该处理用户地图切换', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const newPosition: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'forest',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
const oldPosition = {
|
||||
x: 50,
|
||||
y: 100,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now() - 1000
|
||||
};
|
||||
|
||||
mockRedisService.get
|
||||
.mockResolvedValueOnce('test-session') // 当前会话
|
||||
.mockResolvedValueOnce(JSON.stringify(oldPosition)); // 旧位置
|
||||
mockRedisService.setex.mockResolvedValue('OK');
|
||||
mockRedisService.sadd.mockResolvedValue(1);
|
||||
mockRedisService.expire.mockResolvedValue(1);
|
||||
mockRedisService.srem.mockResolvedValue(1);
|
||||
|
||||
// Act
|
||||
await service.setUserPosition(userId, newPosition);
|
||||
|
||||
// Assert
|
||||
expect(mockRedisService.srem).toHaveBeenCalledWith(
|
||||
`map:${oldPosition.mapId}:users`,
|
||||
userId
|
||||
);
|
||||
expect(mockRedisService.sadd).toHaveBeenCalledWith(
|
||||
`map:${newPosition.mapId}:users`,
|
||||
userId
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUserPosition', () => {
|
||||
it('应该成功获取用户位置', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const positionData = {
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockRedisService.get.mockResolvedValue(JSON.stringify(positionData));
|
||||
|
||||
// Act
|
||||
const result = await service.getUserPosition(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchObject({
|
||||
userId,
|
||||
x: positionData.x,
|
||||
y: positionData.y,
|
||||
mapId: positionData.mapId,
|
||||
timestamp: positionData.timestamp
|
||||
});
|
||||
});
|
||||
|
||||
it('应该在用户位置不存在时返回null', async () => {
|
||||
// Arrange
|
||||
const userId = 'non-existent-user';
|
||||
mockRedisService.get.mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const result = await service.getUserPosition(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('应该处理JSON解析错误', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
mockRedisService.get.mockResolvedValue('invalid-json');
|
||||
|
||||
// Act
|
||||
const result = await service.getUserPosition(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSessionPositions', () => {
|
||||
it('应该返回会话中所有用户的位置', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userIds = ['user1', 'user2'];
|
||||
const position1: Position = {
|
||||
userId: 'user1',
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockRedisService.smembers.mockResolvedValue(userIds);
|
||||
|
||||
const getUserPositionSpy = jest.spyOn(service, 'getUserPosition');
|
||||
getUserPositionSpy
|
||||
.mockResolvedValueOnce(position1)
|
||||
.mockResolvedValueOnce(null); // user2没有位置
|
||||
|
||||
// Act
|
||||
const result = await service.getSessionPositions(sessionId);
|
||||
|
||||
// Assert
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toEqual(position1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getMapPositions', () => {
|
||||
it('应该返回地图中所有用户的位置', async () => {
|
||||
// Arrange
|
||||
const mapId = 'plaza';
|
||||
const userIds = ['user1', 'user2'];
|
||||
const position1: Position = {
|
||||
userId: 'user1',
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockRedisService.smembers.mockResolvedValue(userIds);
|
||||
|
||||
const getUserPositionSpy = jest.spyOn(service, 'getUserPosition');
|
||||
getUserPositionSpy
|
||||
.mockResolvedValueOnce(position1)
|
||||
.mockResolvedValueOnce({
|
||||
userId: 'user2',
|
||||
x: 150,
|
||||
y: 250,
|
||||
mapId: 'forest', // 不同地图,应该被过滤
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
});
|
||||
|
||||
// Act
|
||||
const result = await service.getMapPositions(mapId);
|
||||
|
||||
// Assert
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toEqual(position1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanupUserData', () => {
|
||||
it('应该成功清理用户数据', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const sessionId = 'test-session';
|
||||
const socketId = 'test-socket';
|
||||
|
||||
mockRedisService.get
|
||||
.mockResolvedValueOnce(sessionId)
|
||||
.mockResolvedValueOnce(socketId);
|
||||
mockRedisService.del.mockResolvedValue(1);
|
||||
|
||||
const removeUserFromSessionSpy = jest.spyOn(service, 'removeUserFromSession');
|
||||
removeUserFromSessionSpy.mockResolvedValue();
|
||||
|
||||
// Act
|
||||
await service.cleanupUserData(userId);
|
||||
|
||||
// Assert
|
||||
expect(removeUserFromSessionSpy).toHaveBeenCalledWith(sessionId, userId);
|
||||
// removeUserFromSession 内部会调用 del 方法,所以总调用次数会更多
|
||||
expect(mockRedisService.del).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('应该处理用户不在会话中的情况', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
|
||||
mockRedisService.get.mockResolvedValue(null); // 用户不在任何会话中
|
||||
mockRedisService.del.mockResolvedValue(1);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.cleanupUserData(userId)).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanupEmptySession', () => {
|
||||
it('应该清理空会话', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'empty-session';
|
||||
mockRedisService.scard.mockResolvedValue(0); // 会话为空
|
||||
mockRedisService.del.mockResolvedValue(1);
|
||||
|
||||
// Act
|
||||
await service.cleanupEmptySession(sessionId);
|
||||
|
||||
// Assert
|
||||
expect(mockRedisService.del).toHaveBeenCalledTimes(4); // 4个会话相关键被删除
|
||||
});
|
||||
|
||||
it('应该跳过非空会话的清理', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'non-empty-session';
|
||||
mockRedisService.scard.mockResolvedValue(2); // 会话不为空
|
||||
|
||||
// Act
|
||||
await service.cleanupEmptySession(sessionId);
|
||||
|
||||
// Assert
|
||||
expect(mockRedisService.del).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanupExpiredData', () => {
|
||||
it('应该返回清理的记录数', async () => {
|
||||
// Arrange
|
||||
const expireTime = new Date();
|
||||
|
||||
// Act
|
||||
const result = await service.cleanupExpiredData(expireTime);
|
||||
|
||||
// Assert
|
||||
expect(typeof result).toBe('number');
|
||||
expect(result).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('错误处理', () => {
|
||||
it('应该在Redis连接失败时记录错误', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'test-session';
|
||||
const userId = 'test-user';
|
||||
const socketId = 'test-socket';
|
||||
|
||||
mockRedisService.sadd.mockRejectedValue(new Error('连接超时'));
|
||||
|
||||
// Act & Assert
|
||||
await expect(
|
||||
service.addUserToSession(sessionId, userId, socketId)
|
||||
).rejects.toThrow('连接超时');
|
||||
});
|
||||
|
||||
it('应该处理并发操作冲突', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
// 模拟并发冲突
|
||||
mockRedisService.get.mockResolvedValue('test-session');
|
||||
mockRedisService.setex.mockRejectedValueOnce(new Error('并发冲突'));
|
||||
mockRedisService.setex.mockResolvedValue('OK');
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.setUserPosition(userId, position)).rejects.toThrow('并发冲突');
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界条件测试', () => {
|
||||
it('应该处理空字符串参数', async () => {
|
||||
// 这些测试应该通过,因为服务没有对空字符串进行验证
|
||||
// 实际的验证应该在业务层进行
|
||||
|
||||
// Act & Assert - 这些操作应该成功,因为Core层专注技术实现
|
||||
await expect(service.addUserToSession('', 'user', 'socket')).resolves.not.toThrow();
|
||||
await expect(service.addUserToSession('session', '', 'socket')).resolves.not.toThrow();
|
||||
await expect(service.addUserToSession('session', 'user', '')).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('应该处理极大的坐标值', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: Number.MAX_SAFE_INTEGER,
|
||||
y: Number.MAX_SAFE_INTEGER,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockRedisService.get.mockResolvedValue(null); // No previous position data
|
||||
mockRedisService.setex.mockResolvedValue('OK');
|
||||
mockRedisService.sadd.mockResolvedValue(1);
|
||||
mockRedisService.expire.mockResolvedValue(1);
|
||||
mockRedisService.srem.mockResolvedValue(1);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.setUserPosition(userId, position)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('应该处理大量用户的会话', async () => {
|
||||
// Arrange
|
||||
const sessionId = 'large-session';
|
||||
const userIds = Array.from({ length: 1000 }, (_, i) => `user${i}`);
|
||||
|
||||
mockRedisService.smembers.mockResolvedValue(userIds);
|
||||
mockRedisService.get.mockResolvedValue('socket');
|
||||
|
||||
const getUserPositionSpy = jest.spyOn(service, 'getUserPosition');
|
||||
getUserPositionSpy.mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const result = await service.getSessionUsers(sessionId);
|
||||
|
||||
// Assert
|
||||
expect(result).toHaveLength(1000);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,763 @@
|
||||
/**
|
||||
* 位置广播核心服务
|
||||
*
|
||||
* 功能描述:
|
||||
* - 提供位置广播系统的核心技术实现
|
||||
* - 管理用户会话和位置数据的Redis缓存
|
||||
* - 协调会话管理和位置更新的核心操作
|
||||
* - 处理数据清理和过期管理
|
||||
*
|
||||
* 职责分离:
|
||||
* - 会话管理:用户加入/离开会话的核心逻辑
|
||||
* - 位置缓存:Redis中位置数据的存储和查询
|
||||
* - 数据协调:缓存和持久化之间的数据同步
|
||||
* - 清理维护:过期数据和空会话的自动清理
|
||||
*
|
||||
* 技术实现:
|
||||
* - Redis缓存:高性能的位置数据存储
|
||||
* - 批量操作:优化的数据读写性能
|
||||
* - 异常处理:完善的错误处理和恢复机制
|
||||
* - 日志监控:详细的操作日志和性能统计
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-08: 功能新增 - 创建位置广播核心服务实现 (修改者: moyin)
|
||||
* - 2026-01-08: 注释优化 - 完善类注释和方法注释规范 (修改者: moyin)
|
||||
* - 2026-01-08: 注释完善 - 补充所有辅助方法的完整注释 (修改者: moyin)
|
||||
* - 2026-01-08: 代码质量优化 - 添加常量定义和减少代码重复,完善日志记录优化 (修改者: moyin)
|
||||
* - 2026-01-08: 代码质量优化 - 统一所有方法的日志记录模式,减少代码重复 (修改者: moyin)
|
||||
* - 2026-01-08: 架构分层检查 - 确认Core层专注技术实现,修正注释中的业务逻辑描述 (修改者: moyin)
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.6
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
import { Injectable, Inject, Logger } from '@nestjs/common';
|
||||
import { ILocationBroadcastCore } from './core_services.interface';
|
||||
import { Position } from './position.interface';
|
||||
import { SessionUser, SessionUserStatus } from './session.interface';
|
||||
|
||||
// 常量定义
|
||||
const SESSION_EXPIRE_TIME = 3600; // 会话过期时间(秒)
|
||||
const POSITION_CACHE_EXPIRE_TIME = 1800; // 位置缓存过期时间(秒)
|
||||
|
||||
@Injectable()
|
||||
/**
|
||||
* 位置广播核心服务类
|
||||
*
|
||||
* 职责:
|
||||
* - 管理用户会话的加入和离开操作
|
||||
* - 处理用户位置数据的Redis缓存
|
||||
* - 协调会话状态和位置信息的同步
|
||||
* - 提供数据清理和维护功能
|
||||
*
|
||||
* 主要方法:
|
||||
* - addUserToSession: 添加用户到会话
|
||||
* - removeUserFromSession: 从会话中移除用户
|
||||
* - setUserPosition: 设置用户位置
|
||||
* - getUserPosition: 获取用户位置
|
||||
* - cleanupUserData: 清理用户数据
|
||||
*
|
||||
* 使用场景:
|
||||
* - 位置广播业务层调用核心功能
|
||||
* - WebSocket连接管理用户会话
|
||||
* - 位置数据的实时缓存和查询
|
||||
* - 系统维护和数据清理
|
||||
*/
|
||||
export class LocationBroadcastCore implements ILocationBroadcastCore {
|
||||
private readonly logger = new Logger(LocationBroadcastCore.name);
|
||||
|
||||
constructor(
|
||||
@Inject('REDIS_SERVICE')
|
||||
private readonly redisService: any, // 使用现有的Redis服务接口
|
||||
@Inject('IUserProfilesService')
|
||||
private readonly userProfilesService: any, // 使用用户档案服务
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 记录操作开始日志
|
||||
* @param operation 操作名称
|
||||
* @param params 操作参数
|
||||
* @returns 开始时间
|
||||
*/
|
||||
private logOperationStart(operation: string, params: Record<string, any>): number {
|
||||
const startTime = Date.now();
|
||||
this.logger.log(`开始${this.getOperationDescription(operation)}`, {
|
||||
operation,
|
||||
...params,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
return startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录操作成功日志
|
||||
* @param operation 操作名称
|
||||
* @param params 操作参数
|
||||
* @param startTime 开始时间
|
||||
*/
|
||||
private logOperationSuccess(operation: string, params: Record<string, any>, startTime: number): void {
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.log(`${this.getOperationDescription(operation)}成功`, {
|
||||
operation,
|
||||
...params,
|
||||
duration,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录操作失败日志
|
||||
* @param operation 操作名称
|
||||
* @param params 操作参数
|
||||
* @param startTime 开始时间
|
||||
* @param error 错误信息
|
||||
*/
|
||||
private logOperationError(operation: string, params: Record<string, any>, startTime: number, error: any): void {
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.error(`${this.getOperationDescription(operation)}失败`, {
|
||||
operation,
|
||||
...params,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
duration,
|
||||
timestamp: new Date().toISOString()
|
||||
}, error instanceof Error ? error.stack : undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取操作描述
|
||||
* @param operation 操作名称
|
||||
* @returns 操作描述
|
||||
*/
|
||||
private getOperationDescription(operation: string): string {
|
||||
const descriptions: Record<string, string> = {
|
||||
'addUserToSession': '添加用户到会话',
|
||||
'removeUserFromSession': '从会话中移除用户',
|
||||
'getSessionUsers': '获取会话用户列表',
|
||||
'setUserPosition': '设置用户位置',
|
||||
'getUserPosition': '获取用户位置',
|
||||
'getSessionPositions': '获取会话位置列表',
|
||||
'getMapPositions': '获取地图位置列表',
|
||||
'cleanupUserData': '清理用户数据',
|
||||
'cleanupEmptySession': '清理空会话',
|
||||
'cleanupExpiredData': '清理过期数据',
|
||||
'cleanupUserPositionData': '清理用户位置数据'
|
||||
};
|
||||
return descriptions[operation] || operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加用户到会话
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 将用户ID添加到会话用户集合
|
||||
* 2. 设置用户到会话的映射关系
|
||||
* 3. 设置用户到Socket的映射关系
|
||||
* 4. 设置相关数据的过期时间
|
||||
* 5. 记录操作日志和性能指标
|
||||
*
|
||||
* @param sessionId 会话ID
|
||||
* @param userId 用户ID
|
||||
* @param socketId WebSocket连接ID
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws Error 当Redis操作失败时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await locationBroadcastCore.addUserToSession('session123', 'user456', 'socket789');
|
||||
* ```
|
||||
*/
|
||||
async addUserToSession(sessionId: string, userId: string, socketId: string): Promise<void> {
|
||||
const startTime = this.logOperationStart('addUserToSession', { sessionId, userId, socketId });
|
||||
|
||||
try {
|
||||
// 1. 添加用户到会话集合
|
||||
await this.redisService.sadd(`session:${sessionId}:users`, userId);
|
||||
|
||||
// 2. 设置用户会话映射
|
||||
await this.redisService.setex(`user:${userId}:session`, SESSION_EXPIRE_TIME, sessionId);
|
||||
|
||||
// 3. 设置用户Socket映射
|
||||
await this.redisService.setex(`user:${userId}:socket`, SESSION_EXPIRE_TIME, socketId);
|
||||
|
||||
// 4. 设置Socket到用户的反向映射
|
||||
await this.redisService.setex(`socket:${socketId}:user`, SESSION_EXPIRE_TIME, userId);
|
||||
|
||||
// 5. 设置会话过期时间
|
||||
await this.redisService.expire(`session:${sessionId}:users`, SESSION_EXPIRE_TIME);
|
||||
|
||||
// 6. 更新会话最后活动时间
|
||||
await this.redisService.setex(`session:${sessionId}:lastActivity`, SESSION_EXPIRE_TIME, Date.now().toString());
|
||||
|
||||
this.logOperationSuccess('addUserToSession', { sessionId, userId, socketId }, startTime);
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('addUserToSession', { sessionId, userId, socketId }, startTime, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从会话中移除用户
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 从会话用户集合中移除用户
|
||||
* 2. 删除用户相关的映射关系
|
||||
* 3. 清理用户的位置数据
|
||||
* 4. 检查并清理空会话
|
||||
* 5. 记录操作日志
|
||||
*
|
||||
* @param sessionId 会话ID
|
||||
* @param userId 用户ID
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws Error 当Redis操作失败时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await locationBroadcastCore.removeUserFromSession('session123', 'user456');
|
||||
* ```
|
||||
*/
|
||||
async removeUserFromSession(sessionId: string, userId: string): Promise<void> {
|
||||
const startTime = this.logOperationStart('removeUserFromSession', { sessionId, userId });
|
||||
|
||||
try {
|
||||
// 1. 从会话集合中移除用户
|
||||
await this.redisService.srem(`session:${sessionId}:users`, userId);
|
||||
|
||||
// 2. 获取用户的Socket ID(用于清理)
|
||||
const socketId = await this.redisService.get(`user:${userId}:socket`);
|
||||
|
||||
// 3. 删除用户相关映射
|
||||
await Promise.all([
|
||||
this.redisService.del(`user:${userId}:session`),
|
||||
this.redisService.del(`user:${userId}:socket`),
|
||||
socketId ? this.redisService.del(`socket:${socketId}:user`) : Promise.resolve(),
|
||||
]);
|
||||
|
||||
// 4. 清理用户位置数据
|
||||
await this.cleanupUserPositionData(userId);
|
||||
|
||||
// 5. 检查会话是否为空,如果为空则清理
|
||||
const remainingUsers = await this.redisService.scard(`session:${sessionId}:users`);
|
||||
if (remainingUsers === 0) {
|
||||
await this.cleanupEmptySession(sessionId);
|
||||
}
|
||||
|
||||
this.logOperationSuccess('removeUserFromSession', { sessionId, userId, socketId, remainingUsers }, startTime);
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('removeUserFromSession', { sessionId, userId }, startTime, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话中的用户列表
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 从Redis获取会话中的用户ID列表
|
||||
* 2. 批量获取每个用户的详细信息
|
||||
* 3. 构建SessionUser对象列表
|
||||
* 4. 处理用户信息获取失败的情况
|
||||
* 5. 记录操作日志和性能指标
|
||||
*
|
||||
* @param sessionId 会话ID
|
||||
* @returns Promise<SessionUser[]> 会话用户列表
|
||||
* @throws Error 当Redis操作失败时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const users = await locationBroadcastCore.getSessionUsers('session123');
|
||||
* console.log(`会话中有 ${users.length} 个用户`);
|
||||
* ```
|
||||
*/
|
||||
async getSessionUsers(sessionId: string): Promise<SessionUser[]> {
|
||||
const startTime = this.logOperationStart('getSessionUsers', { sessionId });
|
||||
|
||||
try {
|
||||
// 1. 获取会话中的用户ID列表
|
||||
const userIds = await this.redisService.smembers(`session:${sessionId}:users`);
|
||||
|
||||
if (!userIds || userIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 2. 批量获取用户信息
|
||||
const sessionUsers: SessionUser[] = [];
|
||||
|
||||
for (const userId of userIds) {
|
||||
try {
|
||||
// 获取用户的Socket ID
|
||||
const socketId = await this.redisService.get(`user:${userId}:socket`);
|
||||
|
||||
// 获取用户位置
|
||||
const position = await this.getUserPosition(userId);
|
||||
|
||||
// 构建会话用户对象
|
||||
const sessionUser: SessionUser = {
|
||||
userId,
|
||||
socketId: socketId || '',
|
||||
joinedAt: Date.now(), // 这里可以从Redis获取实际的加入时间
|
||||
lastSeen: Date.now(),
|
||||
position,
|
||||
status: SessionUserStatus.ONLINE,
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
sessionUsers.push(sessionUser);
|
||||
} catch (userError) {
|
||||
this.logger.warn('获取用户信息失败,跳过该用户', {
|
||||
operation: 'getSessionUsers',
|
||||
sessionId,
|
||||
userId,
|
||||
error: userError instanceof Error ? userError.message : String(userError)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
this.logOperationSuccess('getSessionUsers', {
|
||||
sessionId,
|
||||
userCount: sessionUsers.length
|
||||
}, startTime);
|
||||
|
||||
return sessionUsers;
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('getSessionUsers', { sessionId }, startTime, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用户位置
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 获取用户当前会话信息
|
||||
* 2. 构建位置数据并存储到Redis
|
||||
* 3. 更新地图用户集合
|
||||
* 4. 处理地图切换的清理工作
|
||||
* 5. 记录操作日志和性能指标
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param position 位置信息
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws Error 当Redis操作失败时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const position: Position = {
|
||||
* userId: '123',
|
||||
* x: 100,
|
||||
* y: 200,
|
||||
* mapId: 'plaza',
|
||||
* timestamp: Date.now()
|
||||
* };
|
||||
* await locationBroadcastCore.setUserPosition('123', position);
|
||||
* ```
|
||||
*/
|
||||
async setUserPosition(userId: string, position: Position): Promise<void> {
|
||||
const startTime = this.logOperationStart('setUserPosition', {
|
||||
userId,
|
||||
mapId: position.mapId,
|
||||
x: position.x,
|
||||
y: position.y
|
||||
});
|
||||
|
||||
try {
|
||||
// 1. 获取用户当前会话
|
||||
const sessionId = await this.redisService.get(`user:${userId}:session`);
|
||||
|
||||
// 2. 构建位置数据
|
||||
const positionData = {
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
mapId: position.mapId,
|
||||
timestamp: position.timestamp || Date.now(),
|
||||
sessionId: sessionId || null
|
||||
};
|
||||
|
||||
// 3. 存储用户位置到Redis
|
||||
await this.redisService.setex(
|
||||
`location:user:${userId}`,
|
||||
POSITION_CACHE_EXPIRE_TIME, // 30分钟过期
|
||||
JSON.stringify(positionData)
|
||||
);
|
||||
|
||||
// 4. 添加用户到地图集合
|
||||
await this.redisService.sadd(`map:${position.mapId}:users`, userId);
|
||||
await this.redisService.expire(`map:${position.mapId}:users`, POSITION_CACHE_EXPIRE_TIME);
|
||||
|
||||
// 5. 如果用户之前在其他地图,从旧地图集合中移除
|
||||
const oldPositionData = await this.redisService.get(`location:user:${userId}:previous`);
|
||||
if (oldPositionData) {
|
||||
const oldPosition = JSON.parse(oldPositionData);
|
||||
if (oldPosition.mapId !== position.mapId) {
|
||||
await this.redisService.srem(`map:${oldPosition.mapId}:users`, userId);
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 保存当前位置作为"上一个位置"
|
||||
await this.redisService.setex(
|
||||
`location:user:${userId}:previous`,
|
||||
POSITION_CACHE_EXPIRE_TIME,
|
||||
JSON.stringify(positionData)
|
||||
);
|
||||
|
||||
this.logOperationSuccess('setUserPosition', {
|
||||
userId,
|
||||
mapId: position.mapId,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
sessionId
|
||||
}, startTime);
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('setUserPosition', { userId, position }, startTime, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户位置
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 从Redis获取用户位置数据
|
||||
* 2. 解析JSON格式的位置信息
|
||||
* 3. 构建标准的Position对象
|
||||
* 4. 处理数据不存在或解析失败的情况
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @returns Promise<Position | null> 用户位置信息,不存在时返回null
|
||||
* @throws 不抛出异常,错误时返回null并记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const position = await locationBroadcastCore.getUserPosition('123');
|
||||
* if (position) {
|
||||
* console.log(`用户位置: (${position.x}, ${position.y}) 在地图 ${position.mapId}`);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
async getUserPosition(userId: string): Promise<Position | null> {
|
||||
try {
|
||||
const data = await this.redisService.get(`location:user:${userId}`);
|
||||
if (!data) return null;
|
||||
|
||||
const positionData = JSON.parse(data);
|
||||
return {
|
||||
userId,
|
||||
x: positionData.x,
|
||||
y: positionData.y,
|
||||
mapId: positionData.mapId,
|
||||
timestamp: positionData.timestamp,
|
||||
metadata: positionData.metadata || {}
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error('获取用户位置失败', {
|
||||
operation: 'getUserPosition',
|
||||
userId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话中所有用户的位置
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 获取会话中的所有用户ID
|
||||
* 2. 批量获取每个用户的位置信息
|
||||
* 3. 过滤掉无效的位置数据
|
||||
* 4. 返回有效位置信息列表
|
||||
*
|
||||
* @param sessionId 会话ID
|
||||
* @returns Promise<Position[]> 位置信息列表
|
||||
* @throws 不抛出异常,错误时返回空数组并记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const positions = await locationBroadcastCore.getSessionPositions('session123');
|
||||
* positions.forEach(pos => {
|
||||
* console.log(`用户 ${pos.userId} 在 (${pos.x}, ${pos.y})`);
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
async getSessionPositions(sessionId: string): Promise<Position[]> {
|
||||
try {
|
||||
// 1. 获取会话中的所有用户
|
||||
const userIds = await this.redisService.smembers(`session:${sessionId}:users`);
|
||||
|
||||
// 2. 批量获取用户位置
|
||||
const positions: Position[] = [];
|
||||
for (const userId of userIds) {
|
||||
const position = await this.getUserPosition(userId);
|
||||
if (position) {
|
||||
positions.push(position);
|
||||
}
|
||||
}
|
||||
|
||||
return positions;
|
||||
} catch (error) {
|
||||
this.logger.error('获取会话位置列表失败', {
|
||||
operation: 'getSessionPositions',
|
||||
sessionId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取地图中所有用户的位置
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 获取地图中的所有用户ID
|
||||
* 2. 批量获取每个用户的位置信息
|
||||
* 3. 验证位置数据的地图ID匹配
|
||||
* 4. 返回有效位置信息列表
|
||||
*
|
||||
* @param mapId 地图ID
|
||||
* @returns Promise<Position[]> 位置信息列表
|
||||
* @throws 不抛出异常,错误时返回空数组并记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const positions = await locationBroadcastCore.getMapPositions('plaza');
|
||||
* console.log(`广场地图中有 ${positions.length} 个用户`);
|
||||
* ```
|
||||
*/
|
||||
async getMapPositions(mapId: string): Promise<Position[]> {
|
||||
try {
|
||||
// 1. 获取地图中的所有用户
|
||||
const userIds = await this.redisService.smembers(`map:${mapId}:users`);
|
||||
|
||||
// 2. 批量获取用户位置
|
||||
const positions: Position[] = [];
|
||||
for (const userId of userIds) {
|
||||
const position = await this.getUserPosition(userId);
|
||||
if (position && position.mapId === mapId) {
|
||||
positions.push(position);
|
||||
}
|
||||
}
|
||||
|
||||
return positions;
|
||||
} catch (error) {
|
||||
this.logger.error('获取地图位置列表失败', {
|
||||
operation: 'getMapPositions',
|
||||
mapId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理用户数据
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 获取用户当前会话和Socket信息
|
||||
* 2. 从会话中移除用户
|
||||
* 3. 删除用户相关的Redis键
|
||||
* 4. 清理用户位置数据
|
||||
* 5. 记录清理操作日志
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws 不抛出异常,错误时记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await locationBroadcastCore.cleanupUserData('123');
|
||||
* console.log('用户数据清理完成');
|
||||
* ```
|
||||
*/
|
||||
async cleanupUserData(userId: string): Promise<void> {
|
||||
try {
|
||||
// 1. 获取用户当前会话和Socket
|
||||
const [sessionId, socketId] = await Promise.all([
|
||||
this.redisService.get(`user:${userId}:session`),
|
||||
this.redisService.get(`user:${userId}:socket`)
|
||||
]);
|
||||
|
||||
// 2. 如果用户在会话中,从会话中移除
|
||||
if (sessionId) {
|
||||
await this.removeUserFromSession(sessionId, userId);
|
||||
}
|
||||
|
||||
// 3. 清理用户相关的所有Redis数据
|
||||
const keysToDelete = [
|
||||
`user:${userId}:session`,
|
||||
`user:${userId}:socket`,
|
||||
`location:user:${userId}`,
|
||||
`location:user:${userId}:previous`
|
||||
];
|
||||
|
||||
if (socketId) {
|
||||
keysToDelete.push(`socket:${socketId}:user`);
|
||||
}
|
||||
|
||||
await Promise.all(keysToDelete.map(key => this.redisService.del(key)));
|
||||
|
||||
// 4. 清理用户位置数据
|
||||
await this.cleanupUserPositionData(userId);
|
||||
|
||||
this.logger.log('用户数据清理完成', {
|
||||
operation: 'cleanupUserData',
|
||||
userId,
|
||||
sessionId,
|
||||
socketId
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('用户数据清理失败', {
|
||||
operation: 'cleanupUserData',
|
||||
userId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理空会话
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 检查会话是否真的为空
|
||||
* 2. 删除会话相关的所有Redis键
|
||||
* 3. 记录清理操作日志
|
||||
* 4. 处理非空会话的跳过逻辑
|
||||
*
|
||||
* @param sessionId 会话ID
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws 不抛出异常,错误时记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await locationBroadcastCore.cleanupEmptySession('session123');
|
||||
* console.log('空会话清理完成');
|
||||
* ```
|
||||
*/
|
||||
async cleanupEmptySession(sessionId: string): Promise<void> {
|
||||
try {
|
||||
// 1. 检查会话是否真的为空
|
||||
const userCount = await this.redisService.scard(`session:${sessionId}:users`);
|
||||
if (userCount > 0) {
|
||||
this.logger.warn('会话不为空,跳过清理', {
|
||||
operation: 'cleanupEmptySession',
|
||||
sessionId,
|
||||
userCount
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 删除会话相关的所有数据
|
||||
const keysToDelete = [
|
||||
`session:${sessionId}:users`,
|
||||
`session:${sessionId}:lastActivity`,
|
||||
`session:${sessionId}:config`,
|
||||
`session:${sessionId}:metadata`
|
||||
];
|
||||
|
||||
await Promise.all(keysToDelete.map(key => this.redisService.del(key)));
|
||||
|
||||
this.logger.log('空会话清理完成', {
|
||||
operation: 'cleanupEmptySession',
|
||||
sessionId
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('空会话清理失败', {
|
||||
operation: 'cleanupEmptySession',
|
||||
sessionId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理过期数据
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 扫描长时间未活动的会话
|
||||
* 2. 清理过期的位置数据
|
||||
* 3. 统计清理的记录数量
|
||||
* 4. 记录清理操作日志
|
||||
*
|
||||
* @param expireTime 过期时间
|
||||
* @returns Promise<number> 清理的记录数
|
||||
* @throws 不抛出异常,错误时返回0并记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const expireTime = new Date(Date.now() - 24 * 60 * 60 * 1000); // 24小时前
|
||||
* const count = await locationBroadcastCore.cleanupExpiredData(expireTime);
|
||||
* console.log(`清理了 ${count} 条过期数据`);
|
||||
* ```
|
||||
*/
|
||||
async cleanupExpiredData(expireTime: Date): Promise<number> {
|
||||
let cleanedCount = 0;
|
||||
|
||||
try {
|
||||
// 这里可以实现更复杂的过期数据清理逻辑
|
||||
// 例如:清理长时间未活动的会话、过期的位置数据等
|
||||
|
||||
this.logger.log('过期数据清理完成', {
|
||||
operation: 'cleanupExpiredData',
|
||||
expireTime: expireTime.toISOString(),
|
||||
cleanedCount
|
||||
});
|
||||
|
||||
return cleanedCount;
|
||||
} catch (error) {
|
||||
this.logger.error('过期数据清理失败', {
|
||||
operation: 'cleanupExpiredData',
|
||||
expireTime: expireTime.toISOString(),
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理用户位置数据(私有方法)
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 获取用户当前位置信息
|
||||
* 2. 从地图用户集合中移除用户
|
||||
* 3. 删除位置相关的Redis键
|
||||
* 4. 处理数据不存在的情况
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws 不抛出异常,错误时记录日志
|
||||
*/
|
||||
private async cleanupUserPositionData(userId: string): Promise<void> {
|
||||
try {
|
||||
// 1. 获取用户当前位置信息
|
||||
const positionData = await this.redisService.get(`location:user:${userId}`);
|
||||
|
||||
if (positionData) {
|
||||
const position = JSON.parse(positionData);
|
||||
|
||||
// 2. 从地图用户集合中移除用户
|
||||
if (position.mapId) {
|
||||
await this.redisService.srem(`map:${position.mapId}:users`, userId);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 删除位置相关的Redis键
|
||||
await Promise.all([
|
||||
this.redisService.del(`location:user:${userId}`),
|
||||
this.redisService.del(`location:user:${userId}:previous`)
|
||||
]);
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('用户位置数据清理失败', {
|
||||
operation: 'cleanupUserPositionData',
|
||||
userId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
203
src/core/location_broadcast_core/position.interface.ts
Normal file
203
src/core/location_broadcast_core/position.interface.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
/**
|
||||
* 位置相关接口定义
|
||||
*
|
||||
* 功能描述:
|
||||
* - 定义位置数据的核心接口和类型
|
||||
* - 提供位置广播系统的数据结构规范
|
||||
* - 支持多地图和多用户的位置管理
|
||||
* - 实现类型安全的位置数据传输
|
||||
*
|
||||
* 职责分离:
|
||||
* - 数据结构:定义位置相关的数据模型
|
||||
* - 类型安全:提供TypeScript类型约束
|
||||
* - 接口规范:统一的数据交换格式
|
||||
* - 扩展性:支持未来功能扩展
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-08: 功能新增 - 创建位置接口定义,支持位置广播系统
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.0
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
/**
|
||||
* 位置坐标接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义二维坐标系统的基础数据结构
|
||||
* - 支持浮点数精度的位置表示
|
||||
* - 提供位置计算的基础数据类型
|
||||
*/
|
||||
export interface Coordinates {
|
||||
/** X轴坐标 */
|
||||
x: number;
|
||||
/** Y轴坐标 */
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置信息接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义完整的用户位置信息
|
||||
* - 包含用户标识、坐标、地图和时间戳
|
||||
* - 支持位置数据的完整传输和存储
|
||||
*/
|
||||
export interface Position extends Coordinates {
|
||||
/** 用户ID */
|
||||
userId: string;
|
||||
/** 地图ID */
|
||||
mapId: string;
|
||||
/** 位置更新时间戳 */
|
||||
timestamp: number;
|
||||
/** 扩展元数据 */
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置更新数据接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义位置更新操作的数据结构
|
||||
* - 支持增量位置更新
|
||||
* - 提供位置变化的核心信息
|
||||
*/
|
||||
export interface PositionUpdate extends Coordinates {
|
||||
/** 目标地图ID */
|
||||
mapId: string;
|
||||
/** 更新时间戳 */
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置历史记录接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义位置历史数据的存储结构
|
||||
* - 支持位置轨迹的记录和查询
|
||||
* - 提供历史数据分析的基础
|
||||
*/
|
||||
export interface PositionHistory extends Position {
|
||||
/** 历史记录ID */
|
||||
id: number;
|
||||
/** 关联的游戏会话ID */
|
||||
sessionId?: string;
|
||||
/** 记录创建时间 */
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* 地图边界接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义地图的有效坐标范围
|
||||
* - 支持位置验证和边界检查
|
||||
* - 提供地图约束的数据结构
|
||||
*/
|
||||
export interface MapBounds {
|
||||
/** 地图ID */
|
||||
mapId: string;
|
||||
/** 最小X坐标 */
|
||||
minX: number;
|
||||
/** 最大X坐标 */
|
||||
maxX: number;
|
||||
/** 最小Y坐标 */
|
||||
minY: number;
|
||||
/** 最大Y坐标 */
|
||||
maxY: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置查询条件接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义位置查询的过滤条件
|
||||
* - 支持多维度的位置数据筛选
|
||||
* - 提供灵活的查询参数组合
|
||||
*/
|
||||
export interface PositionQuery {
|
||||
/** 地图ID过滤 */
|
||||
mapId?: string;
|
||||
/** 用户ID列表过滤 */
|
||||
userIds?: string[];
|
||||
/** 时间范围过滤 - 开始时间 */
|
||||
startTime?: number;
|
||||
/** 时间范围过滤 - 结束时间 */
|
||||
endTime?: number;
|
||||
/** 坐标范围过滤 */
|
||||
bounds?: {
|
||||
minX: number;
|
||||
maxX: number;
|
||||
minY: number;
|
||||
maxY: number;
|
||||
};
|
||||
/** 分页限制 */
|
||||
limit?: number;
|
||||
/** 分页偏移 */
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置统计信息接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义位置数据的统计结果
|
||||
* - 支持位置分析和监控
|
||||
* - 提供系统性能指标
|
||||
*/
|
||||
export interface PositionStats {
|
||||
/** 总用户数 */
|
||||
totalUsers: number;
|
||||
/** 在线用户数 */
|
||||
onlineUsers: number;
|
||||
/** 按地图分组的用户数 */
|
||||
usersByMap: Record<string, number>;
|
||||
/** 位置更新频率 (次/分钟) */
|
||||
updateRate: number;
|
||||
/** 平均响应时间 (毫秒) */
|
||||
averageResponseTime: number;
|
||||
/** 统计时间戳 */
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置验证结果接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义位置数据验证的结果
|
||||
* - 支持位置合法性检查
|
||||
* - 提供验证错误的详细信息
|
||||
*/
|
||||
export interface PositionValidationResult {
|
||||
/** 验证是否通过 */
|
||||
isValid: boolean;
|
||||
/** 验证错误信息 */
|
||||
errors: string[];
|
||||
/** 修正后的位置 (如果可以自动修正) */
|
||||
correctedPosition?: Position;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置服务配置接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义位置服务的配置参数
|
||||
* - 支持系统行为的自定义配置
|
||||
* - 提供性能调优的配置选项
|
||||
*/
|
||||
export interface PositionServiceConfig {
|
||||
/** Redis缓存过期时间 (秒) */
|
||||
cacheExpireTime: number;
|
||||
/** 位置更新频率限制 (次/秒) */
|
||||
updateRateLimit: number;
|
||||
/** 批量操作大小限制 */
|
||||
batchSizeLimit: number;
|
||||
/** 历史记录保留天数 */
|
||||
historyRetentionDays: number;
|
||||
/** 是否启用位置验证 */
|
||||
enableValidation: boolean;
|
||||
/** 默认地图边界 */
|
||||
defaultMapBounds: MapBounds;
|
||||
}
|
||||
351
src/core/location_broadcast_core/session.interface.ts
Normal file
351
src/core/location_broadcast_core/session.interface.ts
Normal file
@@ -0,0 +1,351 @@
|
||||
/**
|
||||
* 会话相关接口定义
|
||||
*
|
||||
* 功能描述:
|
||||
* - 定义游戏会话的核心接口和类型
|
||||
* - 提供会话管理系统的数据结构规范
|
||||
* - 支持多用户会话和状态管理
|
||||
* - 实现类型安全的会话数据传输
|
||||
*
|
||||
* 职责分离:
|
||||
* - 数据结构:定义会话相关的数据模型
|
||||
* - 类型安全:提供TypeScript类型约束
|
||||
* - 接口规范:统一的会话数据交换格式
|
||||
* - 扩展性:支持未来会话功能扩展
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-08: 功能新增 - 创建会话接口定义,支持位置广播系统
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.0
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
import { Position } from './position.interface';
|
||||
|
||||
/**
|
||||
* 会话用户接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义会话中用户的基本信息
|
||||
* - 包含用户标识、连接状态和时间信息
|
||||
* - 支持用户会话状态的管理
|
||||
*/
|
||||
export interface SessionUser {
|
||||
/** 用户ID */
|
||||
userId: string;
|
||||
/** WebSocket连接ID */
|
||||
socketId: string;
|
||||
/** 加入会话时间 */
|
||||
joinedAt: number;
|
||||
/** 最后活跃时间 */
|
||||
lastSeen: number;
|
||||
/** 用户当前位置 */
|
||||
position?: Position;
|
||||
/** 用户状态 */
|
||||
status: SessionUserStatus;
|
||||
/** 用户元数据 */
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话用户状态枚举
|
||||
*
|
||||
* 职责:
|
||||
* - 定义用户在会话中的状态类型
|
||||
* - 支持用户状态的精确管理
|
||||
* - 提供状态转换的基础
|
||||
*/
|
||||
export enum SessionUserStatus {
|
||||
/** 在线状态 */
|
||||
ONLINE = 'online',
|
||||
/** 离线状态 */
|
||||
OFFLINE = 'offline',
|
||||
/** 忙碌状态 */
|
||||
BUSY = 'busy',
|
||||
/** 隐身状态 */
|
||||
INVISIBLE = 'invisible',
|
||||
/** 暂时离开 */
|
||||
AWAY = 'away'
|
||||
}
|
||||
|
||||
/**
|
||||
* 游戏会话接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义完整的游戏会话信息
|
||||
* - 包含会话标识、用户列表和配置
|
||||
* - 支持会话的创建、管理和销毁
|
||||
*/
|
||||
export interface GameSession {
|
||||
/** 会话ID */
|
||||
sessionId: string;
|
||||
/** 会话中的用户列表 */
|
||||
users: SessionUser[];
|
||||
/** 会话创建时间 */
|
||||
createdAt: number;
|
||||
/** 最后活动时间 */
|
||||
lastActivity: number;
|
||||
/** 会话配置 */
|
||||
config: SessionConfig;
|
||||
/** 会话状态 */
|
||||
status: SessionStatus;
|
||||
/** 会话元数据 */
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话状态枚举
|
||||
*
|
||||
* 职责:
|
||||
* - 定义会话的生命周期状态
|
||||
* - 支持会话状态的管理和监控
|
||||
* - 提供会话清理的依据
|
||||
*/
|
||||
export enum SessionStatus {
|
||||
/** 活跃状态 */
|
||||
ACTIVE = 'active',
|
||||
/** 空闲状态 */
|
||||
IDLE = 'idle',
|
||||
/** 暂停状态 */
|
||||
PAUSED = 'paused',
|
||||
/** 已结束 */
|
||||
ENDED = 'ended'
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话配置接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义会话的配置参数
|
||||
* - 支持会话行为的自定义
|
||||
* - 提供会话管理的策略配置
|
||||
*/
|
||||
export interface SessionConfig {
|
||||
/** 最大用户数限制 */
|
||||
maxUsers: number;
|
||||
/** 会话超时时间 (秒) */
|
||||
timeoutSeconds: number;
|
||||
/** 是否允许观察者 */
|
||||
allowObservers: boolean;
|
||||
/** 是否需要密码 */
|
||||
requirePassword: boolean;
|
||||
/** 会话密码 (如果需要) */
|
||||
password?: string;
|
||||
/** 地图限制 (如果指定,只能在特定地图中) */
|
||||
mapRestriction?: string[];
|
||||
/** 位置广播范围 (米) */
|
||||
broadcastRange?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入会话请求接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义用户加入会话的请求数据
|
||||
* - 包含认证和配置信息
|
||||
* - 支持会话加入的验证
|
||||
*/
|
||||
export interface JoinSessionRequest {
|
||||
/** 会话ID */
|
||||
sessionId: string;
|
||||
/** 用户认证token */
|
||||
token: string;
|
||||
/** 会话密码 (如果需要) */
|
||||
password?: string;
|
||||
/** 初始位置 */
|
||||
initialPosition?: Position;
|
||||
/** 用户偏好设置 */
|
||||
preferences?: SessionUserPreferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入会话响应接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义加入会话的响应数据
|
||||
* - 包含会话信息和用户列表
|
||||
* - 提供会话状态的完整视图
|
||||
*/
|
||||
export interface JoinSessionResponse {
|
||||
/** 是否成功加入 */
|
||||
success: boolean;
|
||||
/** 错误信息 (如果失败) */
|
||||
error?: string;
|
||||
/** 会话信息 */
|
||||
session?: GameSession;
|
||||
/** 当前用户在会话中的信息 */
|
||||
userInfo?: SessionUser;
|
||||
/** 其他用户的位置信息 */
|
||||
otherPositions?: Position[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 离开会话请求接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义用户离开会话的请求数据
|
||||
* - 支持主动离开和被动清理
|
||||
* - 提供离开原因的记录
|
||||
*/
|
||||
export interface LeaveSessionRequest {
|
||||
/** 会话ID */
|
||||
sessionId: string;
|
||||
/** 用户ID */
|
||||
userId: string;
|
||||
/** 离开原因 */
|
||||
reason: LeaveReason;
|
||||
/** 是否保存最终位置 */
|
||||
saveFinalPosition: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 离开会话原因枚举
|
||||
*
|
||||
* 职责:
|
||||
* - 定义用户离开会话的原因类型
|
||||
* - 支持离开行为的分类和统计
|
||||
* - 提供会话管理的数据分析基础
|
||||
*/
|
||||
export enum LeaveReason {
|
||||
/** 用户主动离开 */
|
||||
USER_LEFT = 'user_left',
|
||||
/** 连接断开 */
|
||||
CONNECTION_LOST = 'connection_lost',
|
||||
/** 会话超时 */
|
||||
SESSION_TIMEOUT = 'session_timeout',
|
||||
/** 被管理员踢出 */
|
||||
KICKED_BY_ADMIN = 'kicked_by_admin',
|
||||
/** 系统错误 */
|
||||
SYSTEM_ERROR = 'system_error'
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户会话偏好设置接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义用户在会话中的个人偏好
|
||||
* - 支持个性化的会话体验
|
||||
* - 提供用户行为的配置选项
|
||||
*/
|
||||
export interface SessionUserPreferences {
|
||||
/** 是否接收位置广播 */
|
||||
receivePositionUpdates: boolean;
|
||||
/** 是否广播自己的位置 */
|
||||
broadcastOwnPosition: boolean;
|
||||
/** 位置更新频率 (毫秒) */
|
||||
updateFrequency: number;
|
||||
/** 是否显示其他用户 */
|
||||
showOtherUsers: boolean;
|
||||
/** 通知设置 */
|
||||
notifications: {
|
||||
userJoined: boolean;
|
||||
userLeft: boolean;
|
||||
positionUpdates: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话统计信息接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义会话的统计数据
|
||||
* - 支持会话性能监控
|
||||
* - 提供会话分析的数据基础
|
||||
*/
|
||||
export interface SessionStats {
|
||||
/** 会话ID */
|
||||
sessionId: string;
|
||||
/** 当前用户数 */
|
||||
currentUserCount: number;
|
||||
/** 历史最大用户数 */
|
||||
maxUserCount: number;
|
||||
/** 会话持续时间 (秒) */
|
||||
duration: number;
|
||||
/** 位置更新总数 */
|
||||
totalPositionUpdates: number;
|
||||
/** 平均用户在线时长 (秒) */
|
||||
averageUserDuration: number;
|
||||
/** 消息发送总数 */
|
||||
totalMessages: number;
|
||||
/** 统计时间戳 */
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话查询条件接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义会话查询的过滤条件
|
||||
* - 支持多维度的会话数据筛选
|
||||
* - 提供灵活的查询参数组合
|
||||
*/
|
||||
export interface SessionQuery {
|
||||
/** 会话状态过滤 */
|
||||
status?: SessionStatus;
|
||||
/** 用户数范围过滤 */
|
||||
userCountRange?: {
|
||||
min: number;
|
||||
max: number;
|
||||
};
|
||||
/** 创建时间范围过滤 */
|
||||
createdTimeRange?: {
|
||||
start: number;
|
||||
end: number;
|
||||
};
|
||||
/** 地图过滤 */
|
||||
mapIds?: string[];
|
||||
/** 分页限制 */
|
||||
limit?: number;
|
||||
/** 分页偏移 */
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话事件接口
|
||||
*
|
||||
* 职责:
|
||||
* - 定义会话中发生的事件类型
|
||||
* - 支持事件驱动的会话管理
|
||||
* - 提供事件处理的数据结构
|
||||
*/
|
||||
export interface SessionEvent {
|
||||
/** 事件ID */
|
||||
eventId: string;
|
||||
/** 会话ID */
|
||||
sessionId: string;
|
||||
/** 事件类型 */
|
||||
type: SessionEventType;
|
||||
/** 事件数据 */
|
||||
data: any;
|
||||
/** 事件时间戳 */
|
||||
timestamp: number;
|
||||
/** 触发用户ID */
|
||||
triggeredBy?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话事件类型枚举
|
||||
*
|
||||
* 职责:
|
||||
* - 定义会话中可能发生的事件类型
|
||||
* - 支持事件的分类和处理
|
||||
* - 提供事件监听的基础
|
||||
*/
|
||||
export enum SessionEventType {
|
||||
/** 用户加入 */
|
||||
USER_JOINED = 'user_joined',
|
||||
/** 用户离开 */
|
||||
USER_LEFT = 'user_left',
|
||||
/** 位置更新 */
|
||||
POSITION_UPDATED = 'position_updated',
|
||||
/** 会话创建 */
|
||||
SESSION_CREATED = 'session_created',
|
||||
/** 会话结束 */
|
||||
SESSION_ENDED = 'session_ended',
|
||||
/** 配置更新 */
|
||||
CONFIG_UPDATED = 'config_updated',
|
||||
/** 错误发生 */
|
||||
ERROR_OCCURRED = 'error_occurred'
|
||||
}
|
||||
@@ -0,0 +1,629 @@
|
||||
/**
|
||||
* 用户位置持久化核心服务单元测试
|
||||
*
|
||||
* 功能描述:
|
||||
* - 测试用户位置持久化核心服务的所有功能
|
||||
* - 验证数据库操作和位置数据管理
|
||||
* - 确保错误处理和边界条件正确
|
||||
* - 提供完整的测试覆盖率
|
||||
*
|
||||
* 测试范围:
|
||||
* - 位置数据持久化(保存/加载)
|
||||
* - 位置历史记录管理
|
||||
* - 批量操作和统计功能
|
||||
* - 异常情况处理
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.0
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UserPositionCore } from './user_position_core.service';
|
||||
import { Position } from './position.interface';
|
||||
|
||||
describe('UserPositionCore', () => {
|
||||
let service: UserPositionCore;
|
||||
let mockUserProfilesService: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
// 创建用户档案服务的Mock
|
||||
mockUserProfilesService = {
|
||||
updatePosition: jest.fn(),
|
||||
findByUserId: jest.fn(),
|
||||
batchUpdateStatus: jest.fn(),
|
||||
};
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
UserPositionCore,
|
||||
{
|
||||
provide: 'IUserProfilesService',
|
||||
useValue: mockUserProfilesService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<UserPositionCore>(UserPositionCore);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('saveUserPosition', () => {
|
||||
it('应该成功保存用户位置到数据库', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockUserProfilesService.updatePosition.mockResolvedValue(undefined);
|
||||
|
||||
// Act
|
||||
await service.saveUserPosition(userId, position);
|
||||
|
||||
// Assert
|
||||
expect(mockUserProfilesService.updatePosition).toHaveBeenCalledWith(
|
||||
BigInt(userId),
|
||||
{
|
||||
current_map: position.mapId,
|
||||
pos_x: position.x,
|
||||
pos_y: position.y
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('应该在用户ID为空时抛出异常', async () => {
|
||||
// Arrange
|
||||
const position: Position = {
|
||||
userId: 'test',
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.saveUserPosition('', position)).rejects.toThrow('用户ID和位置信息不能为空');
|
||||
});
|
||||
|
||||
it('应该在位置信息为空时抛出异常', async () => {
|
||||
// Act & Assert
|
||||
await expect(service.saveUserPosition('123', null as any)).rejects.toThrow('用户ID和位置信息不能为空');
|
||||
});
|
||||
|
||||
it('应该在坐标不是数字时抛出异常', async () => {
|
||||
// Arrange
|
||||
const position: Position = {
|
||||
userId: 'test',
|
||||
x: 'invalid' as any,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.saveUserPosition('123', position)).rejects.toThrow('位置坐标必须是数字类型');
|
||||
});
|
||||
|
||||
it('应该在地图ID为空时抛出异常', async () => {
|
||||
// Arrange
|
||||
const position: Position = {
|
||||
userId: 'test',
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: '',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.saveUserPosition('123', position)).rejects.toThrow('地图ID不能为空');
|
||||
});
|
||||
|
||||
it('应该处理数据库操作失败的情况', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockUserProfilesService.updatePosition.mockRejectedValue(new Error('数据库连接失败'));
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.saveUserPosition(userId, position)).rejects.toThrow('数据库连接失败');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadUserPosition', () => {
|
||||
it('应该成功从数据库加载用户位置', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
const mockUserProfile = {
|
||||
pos_x: 100,
|
||||
pos_y: 200,
|
||||
current_map: 'plaza',
|
||||
last_position_update: new Date(),
|
||||
status: 1,
|
||||
last_login_at: new Date()
|
||||
};
|
||||
|
||||
mockUserProfilesService.findByUserId.mockResolvedValue(mockUserProfile);
|
||||
|
||||
// Act
|
||||
const result = await service.loadUserPosition(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchObject({
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: mockUserProfile.last_position_update.getTime()
|
||||
});
|
||||
expect(result?.metadata).toMatchObject({
|
||||
status: 1,
|
||||
lastLogin: mockUserProfile.last_login_at.getTime()
|
||||
});
|
||||
});
|
||||
|
||||
it('应该在用户ID为空时返回null', async () => {
|
||||
// Act
|
||||
const result = await service.loadUserPosition('');
|
||||
|
||||
// Assert
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('应该在用户档案不存在时返回null', async () => {
|
||||
// Arrange
|
||||
const userId = '999'; // 使用数字字符串
|
||||
mockUserProfilesService.findByUserId.mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const result = await service.loadUserPosition(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('应该处理数据库查询失败的情况', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
mockUserProfilesService.findByUserId.mockRejectedValue(new Error('数据库查询失败'));
|
||||
|
||||
// Act
|
||||
const result = await service.loadUserPosition(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('应该使用默认值处理缺失的位置数据', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
const mockUserProfile = {
|
||||
pos_x: null,
|
||||
pos_y: null,
|
||||
current_map: null,
|
||||
last_position_update: null,
|
||||
status: 0,
|
||||
last_login_at: null
|
||||
};
|
||||
|
||||
mockUserProfilesService.findByUserId.mockResolvedValue(mockUserProfile);
|
||||
|
||||
// Act
|
||||
const result = await service.loadUserPosition(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchObject({
|
||||
userId,
|
||||
x: 0,
|
||||
y: 0,
|
||||
mapId: 'plaza'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('savePositionHistory', () => {
|
||||
it('应该成功保存位置历史记录', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
const sessionId = 'test-session';
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.savePositionHistory(userId, position, sessionId)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('应该处理没有会话ID的情况', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.savePositionHistory(userId, position)).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPositionHistory', () => {
|
||||
it('应该返回位置历史记录列表', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
const limit = 5;
|
||||
|
||||
// Act
|
||||
const result = await service.getPositionHistory(userId, limit);
|
||||
|
||||
// Assert
|
||||
expect(Array.isArray(result)).toBe(true);
|
||||
expect(result).toHaveLength(0); // 当前版本返回空数组
|
||||
});
|
||||
|
||||
it('应该使用默认限制数量', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
|
||||
// Act
|
||||
const result = await service.getPositionHistory(userId);
|
||||
|
||||
// Assert
|
||||
expect(Array.isArray(result)).toBe(true);
|
||||
});
|
||||
|
||||
it('应该处理查询失败的情况', async () => {
|
||||
// Arrange
|
||||
const userId = 'test-user';
|
||||
|
||||
// Act
|
||||
const result = await service.getPositionHistory(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('batchUpdateUserStatus', () => {
|
||||
it('应该成功批量更新用户状态', async () => {
|
||||
// Arrange
|
||||
const userIds = ['123', '456', '789']; // 使用数字字符串
|
||||
const status = 1;
|
||||
const expectedUpdatedCount = 3;
|
||||
|
||||
mockUserProfilesService.batchUpdateStatus.mockResolvedValue(expectedUpdatedCount);
|
||||
|
||||
// Act
|
||||
const result = await service.batchUpdateUserStatus(userIds, status);
|
||||
|
||||
// Assert
|
||||
expect(result).toBe(expectedUpdatedCount);
|
||||
expect(mockUserProfilesService.batchUpdateStatus).toHaveBeenCalledWith(
|
||||
[BigInt('123'), BigInt('456'), BigInt('789')],
|
||||
status
|
||||
);
|
||||
});
|
||||
|
||||
it('应该在用户ID列表为空时抛出异常', async () => {
|
||||
// Act & Assert
|
||||
await expect(service.batchUpdateUserStatus([], 1)).rejects.toThrow('用户ID列表不能为空');
|
||||
});
|
||||
|
||||
it('应该在状态值无效时抛出异常', async () => {
|
||||
// Arrange
|
||||
const userIds = ['123']; // 使用数字字符串
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.batchUpdateUserStatus(userIds, -1)).rejects.toThrow('状态值必须是0-255之间的数字');
|
||||
await expect(service.batchUpdateUserStatus(userIds, 256)).rejects.toThrow('状态值必须是0-255之间的数字');
|
||||
await expect(service.batchUpdateUserStatus(userIds, 'invalid' as any)).rejects.toThrow('状态值必须是0-255之间的数字');
|
||||
});
|
||||
|
||||
it('应该在用户ID无效时抛出异常', async () => {
|
||||
// Arrange
|
||||
const userIds = ['invalid-id'];
|
||||
const status = 1;
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.batchUpdateUserStatus(userIds, status)).rejects.toThrow('无效的用户ID: invalid-id');
|
||||
});
|
||||
|
||||
it('应该处理数据库批量操作失败的情况', async () => {
|
||||
// Arrange
|
||||
const userIds = ['123', '456']; // 使用数字字符串
|
||||
const status = 1;
|
||||
|
||||
mockUserProfilesService.batchUpdateStatus.mockRejectedValue(new Error('批量更新失败'));
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.batchUpdateUserStatus(userIds, status)).rejects.toThrow('批量更新失败');
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanupExpiredPositions', () => {
|
||||
it('应该返回清理的记录数', async () => {
|
||||
// Arrange
|
||||
const expireTime = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); // 7天前
|
||||
|
||||
// Act
|
||||
const result = await service.cleanupExpiredPositions(expireTime);
|
||||
|
||||
// Assert
|
||||
expect(typeof result).toBe('number');
|
||||
expect(result).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it('应该处理清理操作失败的情况', async () => {
|
||||
// Arrange
|
||||
const expireTime = new Date();
|
||||
|
||||
// Act
|
||||
const result = await service.cleanupExpiredPositions(expireTime);
|
||||
|
||||
// Assert
|
||||
expect(result).toBe(0); // 错误时返回0
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUserPositionStats', () => {
|
||||
it('应该返回用户位置统计信息', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
const mockPosition: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
const loadUserPositionSpy = jest.spyOn(service, 'loadUserPosition');
|
||||
loadUserPositionSpy.mockResolvedValue(mockPosition);
|
||||
|
||||
// Act
|
||||
const result = await service.getUserPositionStats(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchObject({
|
||||
userId,
|
||||
hasCurrentPosition: true,
|
||||
currentPosition: mockPosition,
|
||||
historyCount: 0,
|
||||
totalMaps: 1
|
||||
});
|
||||
});
|
||||
|
||||
it('应该处理用户没有位置数据的情况', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
|
||||
const loadUserPositionSpy = jest.spyOn(service, 'loadUserPosition');
|
||||
loadUserPositionSpy.mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const result = await service.getUserPositionStats(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchObject({
|
||||
userId,
|
||||
hasCurrentPosition: false,
|
||||
currentPosition: null,
|
||||
totalMaps: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('应该处理统计获取失败的情况', async () => {
|
||||
// Arrange
|
||||
const userId = '123'; // 使用数字字符串
|
||||
|
||||
const loadUserPositionSpy = jest.spyOn(service, 'loadUserPosition');
|
||||
loadUserPositionSpy.mockRejectedValue(new Error('统计失败'));
|
||||
|
||||
// Act
|
||||
const result = await service.getUserPositionStats(userId);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchObject({
|
||||
userId,
|
||||
hasCurrentPosition: false,
|
||||
error: '统计失败'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('migratePositionData', () => {
|
||||
it('应该成功迁移位置数据', async () => {
|
||||
// Arrange
|
||||
const fromUserId = '123'; // 使用数字字符串
|
||||
const toUserId = '456'; // 使用数字字符串
|
||||
const sourcePosition: Position = {
|
||||
userId: fromUserId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
const loadUserPositionSpy = jest.spyOn(service, 'loadUserPosition');
|
||||
const saveUserPositionSpy = jest.spyOn(service, 'saveUserPosition');
|
||||
|
||||
loadUserPositionSpy.mockResolvedValue(sourcePosition);
|
||||
saveUserPositionSpy.mockResolvedValue(undefined);
|
||||
|
||||
// Act
|
||||
await service.migratePositionData(fromUserId, toUserId);
|
||||
|
||||
// Assert
|
||||
expect(loadUserPositionSpy).toHaveBeenCalledWith(fromUserId);
|
||||
expect(saveUserPositionSpy).toHaveBeenCalledWith(toUserId, {
|
||||
...sourcePosition,
|
||||
userId: toUserId
|
||||
});
|
||||
});
|
||||
|
||||
it('应该在用户ID为空时抛出异常', async () => {
|
||||
// Act & Assert
|
||||
await expect(service.migratePositionData('', '456')).rejects.toThrow('源用户ID和目标用户ID不能为空');
|
||||
await expect(service.migratePositionData('123', '')).rejects.toThrow('源用户ID和目标用户ID不能为空');
|
||||
});
|
||||
|
||||
it('应该在用户ID相同时抛出异常', async () => {
|
||||
// Act & Assert
|
||||
await expect(service.migratePositionData('123', '123')).rejects.toThrow('源用户ID和目标用户ID不能相同');
|
||||
});
|
||||
|
||||
it('应该处理源用户没有位置数据的情况', async () => {
|
||||
// Arrange
|
||||
const fromUserId = '123'; // 使用数字字符串
|
||||
const toUserId = '456'; // 使用数字字符串
|
||||
|
||||
const loadUserPositionSpy = jest.spyOn(service, 'loadUserPosition');
|
||||
loadUserPositionSpy.mockResolvedValue(null);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.migratePositionData(fromUserId, toUserId)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('应该处理迁移操作失败的情况', async () => {
|
||||
// Arrange
|
||||
const fromUserId = '123'; // 使用数字字符串
|
||||
const toUserId = '456'; // 使用数字字符串
|
||||
const sourcePosition: Position = {
|
||||
userId: fromUserId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
const loadUserPositionSpy = jest.spyOn(service, 'loadUserPosition');
|
||||
const saveUserPositionSpy = jest.spyOn(service, 'saveUserPosition');
|
||||
|
||||
loadUserPositionSpy.mockResolvedValue(sourcePosition);
|
||||
saveUserPositionSpy.mockRejectedValue(new Error('迁移失败'));
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.migratePositionData(fromUserId, toUserId)).rejects.toThrow('迁移失败');
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界条件测试', () => {
|
||||
it('应该处理极大的坐标值', async () => {
|
||||
// Arrange
|
||||
const userId = '123';
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: Number.MAX_SAFE_INTEGER,
|
||||
y: Number.MAX_SAFE_INTEGER,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockUserProfilesService.updatePosition.mockResolvedValue(undefined);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.saveUserPosition(userId, position)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('应该处理极小的坐标值', async () => {
|
||||
// Arrange
|
||||
const userId = '123';
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: Number.MIN_SAFE_INTEGER,
|
||||
y: Number.MIN_SAFE_INTEGER,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockUserProfilesService.updatePosition.mockResolvedValue(undefined);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.saveUserPosition(userId, position)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('应该处理大量用户的批量操作', async () => {
|
||||
// Arrange
|
||||
const userIds = Array.from({ length: 1000 }, (_, i) => (i + 1).toString()); // 使用数字字符串
|
||||
const status = 1;
|
||||
|
||||
mockUserProfilesService.batchUpdateStatus.mockResolvedValue(1000);
|
||||
|
||||
// Act
|
||||
const result = await service.batchUpdateUserStatus(userIds, status);
|
||||
|
||||
// Assert
|
||||
expect(result).toBe(1000);
|
||||
});
|
||||
|
||||
it('应该处理状态值边界', async () => {
|
||||
// Arrange
|
||||
const userIds = ['123']; // 使用数字字符串
|
||||
|
||||
mockUserProfilesService.batchUpdateStatus.mockResolvedValue(1);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.batchUpdateUserStatus(userIds, 0)).resolves.toBe(1);
|
||||
await expect(service.batchUpdateUserStatus(userIds, 255)).resolves.toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('性能测试', () => {
|
||||
it('应该在合理时间内完成位置保存', async () => {
|
||||
// Arrange
|
||||
const userId = '123';
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: 100,
|
||||
y: 200,
|
||||
mapId: 'plaza',
|
||||
timestamp: Date.now(),
|
||||
metadata: {}
|
||||
};
|
||||
|
||||
mockUserProfilesService.updatePosition.mockResolvedValue(undefined);
|
||||
|
||||
// Act
|
||||
const startTime = Date.now();
|
||||
await service.saveUserPosition(userId, position);
|
||||
const endTime = Date.now();
|
||||
|
||||
// Assert
|
||||
expect(endTime - startTime).toBeLessThan(1000); // 应该在1秒内完成
|
||||
});
|
||||
});
|
||||
});
|
||||
630
src/core/location_broadcast_core/user_position_core.service.ts
Normal file
630
src/core/location_broadcast_core/user_position_core.service.ts
Normal file
@@ -0,0 +1,630 @@
|
||||
/**
|
||||
* 用户位置持久化核心服务
|
||||
*
|
||||
* 功能描述:
|
||||
* - 管理用户位置数据的数据库持久化操作
|
||||
* - 处理user_profiles表的位置字段更新
|
||||
* - 提供位置历史记录的存储和查询
|
||||
* - 支持位置数据的批量操作和统计分析
|
||||
*
|
||||
* 职责分离:
|
||||
* - 数据持久化:将位置数据保存到MySQL数据库
|
||||
* - 历史管理:维护用户位置的历史轨迹记录
|
||||
* - 批量操作:优化的批量数据处理能力
|
||||
* - 数据恢复:支持位置数据的加载和恢复
|
||||
*
|
||||
* 技术实现:
|
||||
* - 数据库操作:通过UserProfiles服务操作数据库
|
||||
* - 事务处理:确保数据操作的原子性
|
||||
* - 异常处理:完善的错误处理和回滚机制
|
||||
* - 性能优化:批量操作和索引优化
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-08: 功能新增 - 创建用户位置持久化核心服务 (修改者: moyin)
|
||||
* - 2026-01-08: 注释优化 - 完善类注释和方法注释规范 (修改者: moyin)
|
||||
* - 2026-01-08: 注释完善 - 补充所有辅助方法的完整注释 (修改者: moyin)
|
||||
* - 2026-01-08: 代码质量优化 - 添加常量定义和参数验证优化,完善日志记录优化 (修改者: moyin)
|
||||
* - 2026-01-08: 代码质量优化 - 统一所有方法的日志记录模式,减少代码重复 (修改者: moyin)
|
||||
* - 2026-01-08: 架构分层检查 - 确认Core层专注技术实现,职责分离清晰 (修改者: moyin)
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.6
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
*/
|
||||
|
||||
import { Injectable, Inject, Logger } from '@nestjs/common';
|
||||
import { IUserPositionCore } from './core_services.interface';
|
||||
import { Position, PositionHistory } from './position.interface';
|
||||
|
||||
// 常量定义
|
||||
const MIN_STATUS_VALUE = 0; // 最小状态值
|
||||
const MAX_STATUS_VALUE = 255; // 最大状态值
|
||||
const DEFAULT_HISTORY_LIMIT = 10; // 默认历史记录限制数量
|
||||
|
||||
@Injectable()
|
||||
/**
|
||||
* 用户位置持久化核心服务类
|
||||
*
|
||||
* 职责:
|
||||
* - 管理用户位置数据的数据库持久化
|
||||
* - 处理位置历史记录的存储和查询
|
||||
* - 提供批量位置数据操作功能
|
||||
* - 支持位置数据的统计和分析
|
||||
*
|
||||
* 主要方法:
|
||||
* - saveUserPosition: 保存用户位置到数据库
|
||||
* - loadUserPosition: 从数据库加载用户位置
|
||||
* - savePositionHistory: 保存位置历史记录
|
||||
* - batchUpdateUserStatus: 批量更新用户状态
|
||||
* - getUserPositionStats: 获取用户位置统计
|
||||
*
|
||||
* 使用场景:
|
||||
* - 位置数据的长期存储和备份
|
||||
* - 用户位置历史轨迹分析
|
||||
* - 批量数据处理和维护
|
||||
* - 位置相关的统计报表
|
||||
*/
|
||||
export class UserPositionCore implements IUserPositionCore {
|
||||
private readonly logger = new Logger(UserPositionCore.name);
|
||||
|
||||
constructor(
|
||||
@Inject('IUserProfilesService')
|
||||
private readonly userProfilesService: any, // 用户档案服务
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 记录操作开始日志
|
||||
* @param operation 操作名称
|
||||
* @param params 操作参数
|
||||
* @returns 开始时间
|
||||
*/
|
||||
private logOperationStart(operation: string, params: Record<string, any>): number {
|
||||
const startTime = Date.now();
|
||||
this.logger.log(`开始${this.getOperationDescription(operation)}`, {
|
||||
operation,
|
||||
...params,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
return startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录操作成功日志
|
||||
* @param operation 操作名称
|
||||
* @param params 操作参数
|
||||
* @param startTime 开始时间
|
||||
*/
|
||||
private logOperationSuccess(operation: string, params: Record<string, any>, startTime: number): void {
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.log(`${this.getOperationDescription(operation)}成功`, {
|
||||
operation,
|
||||
...params,
|
||||
duration,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录操作失败日志
|
||||
* @param operation 操作名称
|
||||
* @param params 操作参数
|
||||
* @param startTime 开始时间
|
||||
* @param error 错误信息
|
||||
*/
|
||||
private logOperationError(operation: string, params: Record<string, any>, startTime: number, error: any): void {
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.error(`${this.getOperationDescription(operation)}失败`, {
|
||||
operation,
|
||||
...params,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
duration,
|
||||
timestamp: new Date().toISOString()
|
||||
}, error instanceof Error ? error.stack : undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取操作描述
|
||||
* @param operation 操作名称
|
||||
* @returns 操作描述
|
||||
*/
|
||||
private getOperationDescription(operation: string): string {
|
||||
const descriptions: Record<string, string> = {
|
||||
'saveUserPosition': '保存用户位置到数据库',
|
||||
'loadUserPosition': '从数据库加载用户位置',
|
||||
'savePositionHistory': '保存位置历史记录',
|
||||
'getPositionHistory': '获取位置历史记录',
|
||||
'batchUpdateUserStatus': '批量更新用户状态',
|
||||
'cleanupExpiredPositions': '清理过期位置数据',
|
||||
'getUserPositionStats': '获取用户位置统计',
|
||||
'migratePositionData': '迁移位置数据'
|
||||
};
|
||||
return descriptions[operation] || operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存用户位置到数据库
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 验证用户ID和位置数据的有效性
|
||||
* 2. 调用用户档案服务更新位置字段
|
||||
* 3. 更新last_position_update时间戳
|
||||
* 4. 记录操作日志和性能指标
|
||||
* 5. 处理异常情况和错误恢复
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param position 位置信息
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws Error 当用户ID或位置数据无效时抛出异常
|
||||
* @throws Error 当数据库操作失败时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const position: Position = {
|
||||
* userId: '123',
|
||||
* x: 100,
|
||||
* y: 200,
|
||||
* mapId: 'plaza',
|
||||
* timestamp: Date.now()
|
||||
* };
|
||||
* await userPositionCore.saveUserPosition('123', position);
|
||||
* ```
|
||||
*/
|
||||
async saveUserPosition(userId: string, position: Position): Promise<void> {
|
||||
try {
|
||||
// 1. 验证输入参数
|
||||
if (!userId || !position) {
|
||||
throw new Error('用户ID和位置信息不能为空');
|
||||
}
|
||||
|
||||
const startTime = this.logOperationStart('saveUserPosition', {
|
||||
userId,
|
||||
mapId: position.mapId,
|
||||
x: position.x,
|
||||
y: position.y
|
||||
});
|
||||
|
||||
if (typeof position.x !== 'number' || typeof position.y !== 'number') {
|
||||
throw new Error('位置坐标必须是数字类型');
|
||||
}
|
||||
|
||||
if (!position.mapId || position.mapId.trim() === '') {
|
||||
throw new Error('地图ID不能为空');
|
||||
}
|
||||
|
||||
// 2. 调用用户档案服务更新位置
|
||||
await this.userProfilesService.updatePosition(BigInt(userId), {
|
||||
current_map: position.mapId,
|
||||
pos_x: position.x,
|
||||
pos_y: position.y
|
||||
});
|
||||
|
||||
this.logOperationSuccess('saveUserPosition', {
|
||||
userId,
|
||||
mapId: position.mapId,
|
||||
x: position.x,
|
||||
y: position.y
|
||||
}, startTime);
|
||||
|
||||
} catch (error) {
|
||||
const startTime = Date.now();
|
||||
this.logOperationError('saveUserPosition', { userId, position }, startTime, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从数据库加载用户位置
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 通过用户档案服务查询用户信息
|
||||
* 2. 提取位置相关字段数据
|
||||
* 3. 构建标准的Position对象
|
||||
* 4. 处理数据不存在的情况
|
||||
* 5. 记录查询日志和性能指标
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @returns Promise<Position | null> 位置信息,如果不存在返回null
|
||||
* @throws Error 当用户ID为空时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const position = await userPositionCore.loadUserPosition('123');
|
||||
* if (position) {
|
||||
* console.log(`用户在地图 ${position.mapId} 的位置: (${position.x}, ${position.y})`);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
async loadUserPosition(userId: string): Promise<Position | null> {
|
||||
const startTime = this.logOperationStart('loadUserPosition', { userId });
|
||||
|
||||
try {
|
||||
// 1. 验证用户ID
|
||||
if (!userId) {
|
||||
throw new Error('用户ID不能为空');
|
||||
}
|
||||
|
||||
// 2. 查询用户档案信息
|
||||
const userProfile = await this.userProfilesService.findByUserId(BigInt(userId));
|
||||
|
||||
if (!userProfile) {
|
||||
this.logger.warn('用户档案不存在', {
|
||||
operation: 'loadUserPosition',
|
||||
userId
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. 构建位置对象
|
||||
const position: Position = {
|
||||
userId,
|
||||
x: userProfile.pos_x || 0,
|
||||
y: userProfile.pos_y || 0,
|
||||
mapId: userProfile.current_map || 'plaza',
|
||||
timestamp: userProfile.last_position_update?.getTime() || Date.now(),
|
||||
metadata: {
|
||||
status: userProfile.status,
|
||||
lastLogin: userProfile.last_login_at?.getTime()
|
||||
}
|
||||
};
|
||||
|
||||
this.logOperationSuccess('loadUserPosition', {
|
||||
userId,
|
||||
mapId: position.mapId,
|
||||
x: position.x,
|
||||
y: position.y
|
||||
}, startTime);
|
||||
|
||||
return position;
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('loadUserPosition', { userId }, startTime, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存位置历史记录
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 构建位置历史记录数据
|
||||
* 2. 插入到位置历史表中
|
||||
* 3. 处理会话ID的关联
|
||||
* 4. 实现历史记录的清理策略
|
||||
*
|
||||
* 注意:这个方法需要创建位置历史表,当前先记录日志
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param position 位置信息
|
||||
* @param sessionId 会话ID(可选)
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws 不抛出异常,历史记录保存失败不影响主要功能
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const position: Position = {
|
||||
* userId: '123',
|
||||
* x: 100,
|
||||
* y: 200,
|
||||
* mapId: 'plaza',
|
||||
* timestamp: Date.now()
|
||||
* };
|
||||
* await userPositionCore.savePositionHistory('123', position, 'session456');
|
||||
* ```
|
||||
*/
|
||||
async savePositionHistory(userId: string, position: Position, sessionId?: string): Promise<void> {
|
||||
const startTime = this.logOperationStart('savePositionHistory', {
|
||||
userId,
|
||||
mapId: position.mapId,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
sessionId
|
||||
});
|
||||
|
||||
try {
|
||||
// TODO: 实现位置历史表的创建和数据插入
|
||||
// 当前版本先记录日志,后续版本实现完整的历史记录功能
|
||||
|
||||
this.logOperationSuccess('savePositionHistory', {
|
||||
userId,
|
||||
mapId: position.mapId,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
sessionId,
|
||||
note: '当前版本仅记录日志'
|
||||
}, startTime);
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('savePositionHistory', { userId, position, sessionId }, startTime, error);
|
||||
// 历史记录保存失败不应该影响主要功能,所以不抛出异常
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取位置历史记录
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 从位置历史表查询用户记录
|
||||
* 2. 按时间倒序排列
|
||||
* 3. 限制返回记录数量
|
||||
* 4. 构建PositionHistory对象列表
|
||||
*
|
||||
* 注意:当前版本返回空数组,后续版本实现完整查询功能
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param limit 限制数量,默认10条
|
||||
* @returns Promise<PositionHistory[]> 历史记录列表
|
||||
* @throws 不抛出异常,错误时返回空数组并记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const history = await userPositionCore.getPositionHistory('123', 20);
|
||||
* console.log(`用户有 ${history.length} 条位置历史记录`);
|
||||
* ```
|
||||
*/
|
||||
async getPositionHistory(userId: string, limit: number = DEFAULT_HISTORY_LIMIT): Promise<PositionHistory[]> {
|
||||
const startTime = this.logOperationStart('getPositionHistory', { userId, limit });
|
||||
|
||||
try {
|
||||
// TODO: 实现从位置历史表查询数据
|
||||
// 当前版本返回空数组,后续版本实现完整的查询功能
|
||||
|
||||
const historyRecords: PositionHistory[] = [];
|
||||
|
||||
this.logOperationSuccess('getPositionHistory', {
|
||||
userId,
|
||||
limit,
|
||||
recordCount: historyRecords.length,
|
||||
note: '当前版本返回空数组'
|
||||
}, startTime);
|
||||
|
||||
return historyRecords;
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('getPositionHistory', { userId, limit }, startTime, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新用户状态
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 验证用户ID列表和状态值
|
||||
* 2. 调用用户档案服务的批量更新方法
|
||||
* 3. 记录批量操作的结果和性能
|
||||
* 4. 处理部分成功的情况
|
||||
*
|
||||
* @param userIds 用户ID列表
|
||||
* @param status 状态值(0-255之间的数字)
|
||||
* @returns Promise<number> 更新的记录数
|
||||
* @throws Error 当用户ID列表为空或状态值无效时抛出异常
|
||||
* @throws Error 当数据库批量操作失败时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const userIds = ['123', '456', '789'];
|
||||
* const count = await userPositionCore.batchUpdateUserStatus(userIds, 1);
|
||||
* console.log(`成功更新了 ${count} 个用户的状态`);
|
||||
* ```
|
||||
*/
|
||||
async batchUpdateUserStatus(userIds: string[], status: number): Promise<number> {
|
||||
const startTime = this.logOperationStart('batchUpdateUserStatus', {
|
||||
userCount: userIds.length,
|
||||
status
|
||||
});
|
||||
|
||||
try {
|
||||
// 1. 验证输入参数
|
||||
if (!userIds || userIds.length === 0) {
|
||||
throw new Error('用户ID列表不能为空');
|
||||
}
|
||||
|
||||
if (typeof status !== 'number' || status < MIN_STATUS_VALUE || status > MAX_STATUS_VALUE) {
|
||||
throw new Error(`状态值必须是${MIN_STATUS_VALUE}-${MAX_STATUS_VALUE}之间的数字`);
|
||||
}
|
||||
|
||||
// 2. 转换用户ID为bigint类型
|
||||
const bigintUserIds = userIds.map(id => {
|
||||
try {
|
||||
return BigInt(id);
|
||||
} catch (error) {
|
||||
throw new Error(`无效的用户ID: ${id}`);
|
||||
}
|
||||
});
|
||||
|
||||
// 3. 调用用户档案服务批量更新
|
||||
const updatedCount = await this.userProfilesService.batchUpdateStatus(bigintUserIds, status);
|
||||
|
||||
this.logOperationSuccess('batchUpdateUserStatus', {
|
||||
userCount: userIds.length,
|
||||
status,
|
||||
updatedCount
|
||||
}, startTime);
|
||||
|
||||
return updatedCount;
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('batchUpdateUserStatus', { userCount: userIds.length, status }, startTime, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理过期位置数据
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 基于last_position_update字段查找过期数据
|
||||
* 2. 批量删除过期的位置记录
|
||||
* 3. 统计清理的记录数量
|
||||
* 4. 记录清理操作日志
|
||||
*
|
||||
* 注意:当前版本返回0,后续版本实现完整的清理逻辑
|
||||
*
|
||||
* @param expireTime 过期时间
|
||||
* @returns Promise<number> 清理的记录数
|
||||
* @throws 不抛出异常,错误时返回0并记录日志
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const expireTime = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); // 7天前
|
||||
* const count = await userPositionCore.cleanupExpiredPositions(expireTime);
|
||||
* console.log(`清理了 ${count} 条过期位置数据`);
|
||||
* ```
|
||||
*/
|
||||
async cleanupExpiredPositions(expireTime: Date): Promise<number> {
|
||||
const startTime = this.logOperationStart('cleanupExpiredPositions', {
|
||||
expireTime: expireTime.toISOString()
|
||||
});
|
||||
|
||||
try {
|
||||
// TODO: 实现过期位置数据的清理逻辑
|
||||
// 可以基于last_position_update字段进行清理
|
||||
|
||||
let cleanedCount = 0;
|
||||
|
||||
this.logOperationSuccess('cleanupExpiredPositions', {
|
||||
expireTime: expireTime.toISOString(),
|
||||
cleanedCount
|
||||
}, startTime);
|
||||
|
||||
return cleanedCount;
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('cleanupExpiredPositions', { expireTime: expireTime.toISOString() }, startTime, error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户位置统计
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 获取用户当前位置信息
|
||||
* 2. 统计历史记录数量
|
||||
* 3. 计算活跃度指标
|
||||
* 4. 构建统计信息对象
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @returns Promise<any> 统计信息对象
|
||||
* @throws 不抛出异常,错误时返回错误信息对象
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const stats = await userPositionCore.getUserPositionStats('123');
|
||||
* if (stats.hasCurrentPosition) {
|
||||
* console.log(`用户当前在地图 ${stats.currentPosition.mapId}`);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
async getUserPositionStats(userId: string): Promise<any> {
|
||||
const startTime = this.logOperationStart('getUserPositionStats', { userId });
|
||||
|
||||
try {
|
||||
// 1. 获取用户当前位置
|
||||
const currentPosition = await this.loadUserPosition(userId);
|
||||
|
||||
// 2. 构建统计信息
|
||||
const stats = {
|
||||
userId,
|
||||
hasCurrentPosition: !!currentPosition,
|
||||
currentPosition,
|
||||
lastUpdateTime: currentPosition?.timestamp,
|
||||
// TODO: 添加更多统计信息,如历史记录数量、活跃度等
|
||||
historyCount: 0,
|
||||
totalMaps: currentPosition ? 1 : 0,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
this.logOperationSuccess('getUserPositionStats', {
|
||||
userId,
|
||||
hasCurrentPosition: stats.hasCurrentPosition
|
||||
}, startTime);
|
||||
|
||||
return stats;
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('getUserPositionStats', { userId }, startTime, error);
|
||||
|
||||
return {
|
||||
userId,
|
||||
hasCurrentPosition: false,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
timestamp: Date.now()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 迁移位置数据
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 验证源用户ID和目标用户ID
|
||||
* 2. 加载源用户的位置数据
|
||||
* 3. 将位置数据保存到目标用户
|
||||
* 4. 迁移历史记录数据(TODO)
|
||||
* 5. 记录迁移操作日志
|
||||
*
|
||||
* @param fromUserId 源用户ID
|
||||
* @param toUserId 目标用户ID
|
||||
* @returns Promise<void> 操作完成的Promise
|
||||
* @throws Error 当用户ID无效或相同时抛出异常
|
||||
* @throws Error 当数据库操作失败时抛出异常
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await userPositionCore.migratePositionData('oldUser123', 'newUser456');
|
||||
* console.log('位置数据迁移完成');
|
||||
* ```
|
||||
*/
|
||||
async migratePositionData(fromUserId: string, toUserId: string): Promise<void> {
|
||||
const startTime = this.logOperationStart('migratePositionData', { fromUserId, toUserId });
|
||||
|
||||
try {
|
||||
// 1. 验证输入参数
|
||||
if (!fromUserId || !toUserId) {
|
||||
throw new Error('源用户ID和目标用户ID不能为空');
|
||||
}
|
||||
|
||||
if (fromUserId === toUserId) {
|
||||
throw new Error('源用户ID和目标用户ID不能相同');
|
||||
}
|
||||
|
||||
// 2. 加载源用户位置数据
|
||||
const sourcePosition = await this.loadUserPosition(fromUserId);
|
||||
|
||||
if (!sourcePosition) {
|
||||
this.logger.warn('源用户没有位置数据,跳过迁移', {
|
||||
operation: 'migratePositionData',
|
||||
fromUserId,
|
||||
toUserId
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 将位置数据保存到目标用户
|
||||
const targetPosition: Position = {
|
||||
...sourcePosition,
|
||||
userId: toUserId
|
||||
};
|
||||
|
||||
await this.saveUserPosition(toUserId, targetPosition);
|
||||
|
||||
// 4. TODO: 迁移历史记录数据
|
||||
|
||||
this.logOperationSuccess('migratePositionData', {
|
||||
fromUserId,
|
||||
toUserId,
|
||||
migratedPosition: {
|
||||
mapId: sourcePosition.mapId,
|
||||
x: sourcePosition.x,
|
||||
y: sourcePosition.y
|
||||
}
|
||||
}, startTime);
|
||||
|
||||
} catch (error) {
|
||||
this.logOperationError('migratePositionData', { fromUserId, toUserId }, startTime, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user