refactor:项目架构重构和命名规范化
- 统一文件命名为snake_case格式(kebab-case snake_case) - 重构zulip模块为zulip_core,明确Core层职责 - 重构user-mgmt模块为user_mgmt,统一命名规范 - 调整模块依赖关系,优化架构分层 - 删除过时的文件和目录结构 - 更新相关文档和配置文件 本次重构涉及大量文件重命名和模块重组, 旨在建立更清晰的项目架构和统一的命名规范。
This commit is contained in:
@@ -6,6 +6,12 @@
|
||||
* - 整合各个子服务,提供统一的业务接口
|
||||
* - 处理游戏客户端与Zulip之间的核心业务逻辑
|
||||
*
|
||||
* 职责分离:
|
||||
* - 业务协调:整合会话管理、消息过滤、事件处理等子服务
|
||||
* - 流程控制:管理玩家登录登出的完整业务流程
|
||||
* - 接口适配:在游戏协议和Zulip协议之间进行转换
|
||||
* - 错误处理:统一处理业务异常和降级策略
|
||||
*
|
||||
* 主要方法:
|
||||
* - handlePlayerLogin(): 处理玩家登录和Zulip客户端初始化
|
||||
* - handlePlayerLogout(): 处理玩家登出和资源清理
|
||||
@@ -17,9 +23,15 @@
|
||||
* - 会话管理和状态维护
|
||||
* - 消息格式转换和过滤
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-07: 代码规范优化 - 注释规范检查和修正 (修改者: moyin)
|
||||
* - 2026-01-07: 代码规范优化 - 拆分过长方法,提取validateLoginParams和createUserSession私有方法 (修改者: moyin)
|
||||
* - 2026-01-07: 代码规范优化 - 完善文件头注释和修改记录 (修改者: moyin)
|
||||
*
|
||||
* @author angjustinl
|
||||
* @version 1.1.0
|
||||
* @version 1.2.0
|
||||
* @since 2026-01-06
|
||||
* @lastModified 2026-01-07
|
||||
*/
|
||||
|
||||
import { Injectable, Logger, Inject } from '@nestjs/common';
|
||||
@@ -30,9 +42,9 @@ import { ZulipEventProcessorService } from './services/zulip_event_processor.ser
|
||||
import {
|
||||
IZulipClientPoolService,
|
||||
IZulipConfigService,
|
||||
} from '../../core/zulip/interfaces/zulip-core.interfaces';
|
||||
import { ApiKeySecurityService } from '../../core/zulip/services/api_key_security.service';
|
||||
import { LoginService } from '../auth/services/login.service';
|
||||
IApiKeySecurityService,
|
||||
} from '../../core/zulip_core/interfaces/zulip_core.interfaces';
|
||||
import { LoginCoreService } from '../../core/login_core/login_core.service';
|
||||
|
||||
/**
|
||||
* 玩家登录请求接口
|
||||
@@ -116,8 +128,9 @@ export class ZulipService {
|
||||
private readonly eventProcessor: ZulipEventProcessorService,
|
||||
@Inject('ZULIP_CONFIG_SERVICE')
|
||||
private readonly configManager: IZulipConfigService,
|
||||
private readonly apiKeySecurityService: ApiKeySecurityService,
|
||||
private readonly loginService: LoginService,
|
||||
@Inject('API_KEY_SECURITY_SERVICE')
|
||||
private readonly apiKeySecurityService: IApiKeySecurityService,
|
||||
private readonly loginCoreService: LoginCoreService,
|
||||
) {
|
||||
this.logger.log('ZulipService初始化完成');
|
||||
|
||||
@@ -144,6 +157,18 @@ export class ZulipService {
|
||||
*
|
||||
* @throws UnauthorizedException 当Token验证失败时
|
||||
* @throws InternalServerErrorException 当系统操作失败时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const loginRequest: PlayerLoginRequest = {
|
||||
* token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
|
||||
* socketId: 'socket_12345'
|
||||
* };
|
||||
* const result = await zulipService.handlePlayerLogin(loginRequest);
|
||||
* if (result.success) {
|
||||
* console.log(`用户 ${result.username} 登录成功`);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
async handlePlayerLogin(request: PlayerLoginRequest): Promise<LoginResponse> {
|
||||
const startTime = Date.now();
|
||||
@@ -156,28 +181,15 @@ export class ZulipService {
|
||||
|
||||
try {
|
||||
// 1. 验证请求参数
|
||||
if (!request.token || !request.token.trim()) {
|
||||
this.logger.warn('登录失败:Token为空', {
|
||||
operation: 'handlePlayerLogin',
|
||||
socketId: request.socketId,
|
||||
});
|
||||
const paramValidation = this.validateLoginParams(request);
|
||||
if (!paramValidation.isValid) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Token不能为空',
|
||||
error: paramValidation.error,
|
||||
};
|
||||
}
|
||||
|
||||
if (!request.socketId || !request.socketId.trim()) {
|
||||
this.logger.warn('登录失败:socketId为空', {
|
||||
operation: 'handlePlayerLogin',
|
||||
});
|
||||
return {
|
||||
success: false,
|
||||
error: 'socketId不能为空',
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 验证游戏Token并获取用户信息 调用认证服务验证Token
|
||||
// 2. 验证游戏Token并获取用户信息
|
||||
const userInfo = await this.validateGameToken(request.token);
|
||||
if (!userInfo) {
|
||||
this.logger.warn('登录失败:Token验证失败', {
|
||||
@@ -190,80 +202,28 @@ export class ZulipService {
|
||||
};
|
||||
}
|
||||
|
||||
// 3. 生成会话ID
|
||||
const sessionId = randomUUID();
|
||||
|
||||
// 调试日志:检查用户信息
|
||||
this.logger.log('用户信息检查', {
|
||||
operation: 'handlePlayerLogin',
|
||||
userId: userInfo.userId,
|
||||
hasZulipApiKey: !!userInfo.zulipApiKey,
|
||||
zulipApiKeyLength: userInfo.zulipApiKey?.length || 0,
|
||||
zulipEmail: userInfo.zulipEmail,
|
||||
email: userInfo.email,
|
||||
});
|
||||
|
||||
// 4. 创建Zulip客户端(如果有API Key)
|
||||
let zulipQueueId = `queue_${sessionId}`;
|
||||
// 3. 创建Zulip客户端和会话
|
||||
const sessionResult = await this.createUserSession(request.socketId, userInfo);
|
||||
|
||||
if (userInfo.zulipApiKey) {
|
||||
try {
|
||||
const zulipConfig = this.configManager.getZulipConfig();
|
||||
const clientInstance = await this.zulipClientPool.createUserClient(userInfo.userId, {
|
||||
username: userInfo.zulipEmail || userInfo.email,
|
||||
apiKey: userInfo.zulipApiKey,
|
||||
realm: zulipConfig.zulipServerUrl,
|
||||
});
|
||||
|
||||
if (clientInstance.queueId) {
|
||||
zulipQueueId = clientInstance.queueId;
|
||||
}
|
||||
|
||||
this.logger.log('Zulip客户端创建成功', {
|
||||
operation: 'handlePlayerLogin',
|
||||
userId: userInfo.userId,
|
||||
queueId: zulipQueueId,
|
||||
});
|
||||
} catch (zulipError) {
|
||||
const err = zulipError as Error;
|
||||
this.logger.warn('Zulip客户端创建失败,使用本地模式', {
|
||||
operation: 'handlePlayerLogin',
|
||||
userId: userInfo.userId,
|
||||
error: err.message,
|
||||
});
|
||||
// Zulip客户端创建失败不影响登录,使用本地模式
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 创建游戏会话
|
||||
const session = await this.sessionManager.createSession(
|
||||
request.socketId,
|
||||
userInfo.userId,
|
||||
zulipQueueId,
|
||||
userInfo.username,
|
||||
this.DEFAULT_MAP,
|
||||
{ x: 400, y: 300 },
|
||||
);
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
this.logger.log('玩家登录处理完成', {
|
||||
operation: 'handlePlayerLogin',
|
||||
socketId: request.socketId,
|
||||
sessionId,
|
||||
sessionId: sessionResult.sessionId,
|
||||
userId: userInfo.userId,
|
||||
username: userInfo.username,
|
||||
currentMap: session.currentMap,
|
||||
currentMap: sessionResult.currentMap,
|
||||
duration,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
sessionId,
|
||||
sessionId: sessionResult.sessionId,
|
||||
userId: userInfo.userId,
|
||||
username: userInfo.username,
|
||||
currentMap: session.currentMap,
|
||||
currentMap: sessionResult.currentMap,
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
@@ -285,6 +245,108 @@ export class ZulipService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证登录请求参数
|
||||
*
|
||||
* @param request 登录请求
|
||||
* @returns 验证结果
|
||||
* @private
|
||||
*/
|
||||
private validateLoginParams(request: PlayerLoginRequest): { isValid: boolean; error?: string } {
|
||||
if (!request.token || !request.token.trim()) {
|
||||
this.logger.warn('登录失败:Token为空', {
|
||||
operation: 'validateLoginParams',
|
||||
socketId: request.socketId,
|
||||
});
|
||||
return {
|
||||
isValid: false,
|
||||
error: 'Token不能为空',
|
||||
};
|
||||
}
|
||||
|
||||
if (!request.socketId || !request.socketId.trim()) {
|
||||
this.logger.warn('登录失败:socketId为空', {
|
||||
operation: 'validateLoginParams',
|
||||
});
|
||||
return {
|
||||
isValid: false,
|
||||
error: 'socketId不能为空',
|
||||
};
|
||||
}
|
||||
|
||||
return { isValid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户会话和Zulip客户端
|
||||
*
|
||||
* @param socketId Socket连接ID
|
||||
* @param userInfo 用户信息
|
||||
* @returns 会话创建结果
|
||||
* @private
|
||||
*/
|
||||
private async createUserSession(socketId: string, userInfo: any): Promise<{ sessionId: string; currentMap: string }> {
|
||||
// 生成会话ID
|
||||
const sessionId = randomUUID();
|
||||
|
||||
// 调试日志:检查用户信息
|
||||
this.logger.log('用户信息检查', {
|
||||
operation: 'createUserSession',
|
||||
userId: userInfo.userId,
|
||||
hasZulipApiKey: !!userInfo.zulipApiKey,
|
||||
zulipApiKeyLength: userInfo.zulipApiKey?.length || 0,
|
||||
zulipEmail: userInfo.zulipEmail,
|
||||
email: userInfo.email,
|
||||
});
|
||||
|
||||
// 创建Zulip客户端(如果有API Key)
|
||||
let zulipQueueId = `queue_${sessionId}`;
|
||||
|
||||
if (userInfo.zulipApiKey) {
|
||||
try {
|
||||
const zulipConfig = this.configManager.getZulipConfig();
|
||||
const clientInstance = await this.zulipClientPool.createUserClient(userInfo.userId, {
|
||||
username: userInfo.zulipEmail || userInfo.email,
|
||||
apiKey: userInfo.zulipApiKey,
|
||||
realm: zulipConfig.zulipServerUrl,
|
||||
});
|
||||
|
||||
if (clientInstance.queueId) {
|
||||
zulipQueueId = clientInstance.queueId;
|
||||
}
|
||||
|
||||
this.logger.log('Zulip客户端创建成功', {
|
||||
operation: 'createUserSession',
|
||||
userId: userInfo.userId,
|
||||
queueId: zulipQueueId,
|
||||
});
|
||||
} catch (zulipError) {
|
||||
const err = zulipError as Error;
|
||||
this.logger.warn('Zulip客户端创建失败,使用本地模式', {
|
||||
operation: 'createUserSession',
|
||||
userId: userInfo.userId,
|
||||
error: err.message,
|
||||
});
|
||||
// Zulip客户端创建失败不影响登录,使用本地模式
|
||||
}
|
||||
}
|
||||
|
||||
// 创建游戏会话
|
||||
const session = await this.sessionManager.createSession(
|
||||
socketId,
|
||||
userInfo.userId,
|
||||
zulipQueueId,
|
||||
userInfo.username,
|
||||
this.DEFAULT_MAP,
|
||||
{ x: 400, y: 300 },
|
||||
);
|
||||
|
||||
return {
|
||||
sessionId,
|
||||
currentMap: session.currentMap,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证游戏Token
|
||||
*
|
||||
@@ -308,8 +370,8 @@ export class ZulipService {
|
||||
});
|
||||
|
||||
try {
|
||||
// 1. 使用LoginService验证JWT token
|
||||
const payload = await this.loginService.verifyToken(token, 'access');
|
||||
// 1. 使用LoginCoreService验证JWT token
|
||||
const payload = await this.loginCoreService.verifyToken(token, 'access');
|
||||
|
||||
if (!payload || !payload.sub) {
|
||||
this.logger.warn('Token载荷无效', {
|
||||
|
||||
Reference in New Issue
Block a user