|
@@ -17,27 +17,16 @@ export class RoomService {
|
|
|
private readonly actionsService: ActionsService,
|
|
|
private readonly delayService: DelayService,
|
|
|
private readonly tempService: TempService,
|
|
|
- ) {}
|
|
|
+ ) { }
|
|
|
public readonly logger = new Logger('user');
|
|
|
public _sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
|
public _userInfo = {} as UserInfoType;
|
|
|
-
|
|
|
- private get _roomId(): string {
|
|
|
- return this._userInfo.RoomId;
|
|
|
- }
|
|
|
- private get _userId(): string {
|
|
|
- return this._userInfo.UserId;
|
|
|
- }
|
|
|
- public get _isLeader(): boolean {
|
|
|
- return this._userInfo.Role === 'leader';
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 初始化当前用户数据
|
|
|
* @param userInfo
|
|
|
*/
|
|
|
- initUserProfile(userInfo: UserInfoParams): void {
|
|
|
- this._userInfo = {
|
|
|
+ initUserProfile(userInfo: UserInfoParams): UserInfoType {
|
|
|
+ const data = {
|
|
|
id: userInfo.id,
|
|
|
RoomId: userInfo.roomId,
|
|
|
Role: userInfo.role,
|
|
@@ -53,6 +42,7 @@ export class RoomService {
|
|
|
InTime: Date.now(),
|
|
|
IsOnline: true,
|
|
|
};
|
|
|
+ return data;
|
|
|
}
|
|
|
async handleUserOnline(socket: Socket) {
|
|
|
if (socket.data.user) {
|
|
@@ -70,10 +60,9 @@ export class RoomService {
|
|
|
await this._sleep(500);
|
|
|
if (socket.data?.user) {
|
|
|
this.delayService.handleOffline(socket);
|
|
|
- const { RoomId, UserId } = socket.data.user;
|
|
|
+ const { RoomId, UserId, Role } = socket.data.user;
|
|
|
if (RoomId && UserId) {
|
|
|
//标记主动退出房间
|
|
|
-
|
|
|
if (socket.data.isRequestExit !== 1 && socket.data.isKick !== 1) {
|
|
|
const roomUsers = await this.userService.getRoomUsers(RoomId);
|
|
|
this.socketGateway.server.to(RoomId).emit('action', {
|
|
@@ -92,6 +81,10 @@ export class RoomService {
|
|
|
);
|
|
|
await this.handleRoomStatusAction(socket);
|
|
|
}
|
|
|
+ // 压测试直接去除信息
|
|
|
+ if (!!socket.data.isBenmark && Role === 'leader') {
|
|
|
+ this.handleRoomDismiss(RoomId);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -104,20 +97,25 @@ export class RoomService {
|
|
|
socket: Socket,
|
|
|
userInfo: UserInfoParams,
|
|
|
): Promise<void> {
|
|
|
- this.initUserProfile(userInfo);
|
|
|
- socket.data.user = this._userInfo;
|
|
|
+ const user = this.initUserProfile(userInfo);
|
|
|
+ this._userInfo = user;
|
|
|
+ socket.data.user = user;
|
|
|
+ socket.data.user.roomConfig = userInfo.roomConfig;
|
|
|
await this.handleUserOnline(socket);
|
|
|
await this.delayService.handleOnine(socket);
|
|
|
let blockJoin = false;
|
|
|
- const { RoomId, UserId, Role } = socket.data.user;
|
|
|
-
|
|
|
+ const { RoomId, UserId, Role, roomConfig } = socket.data.user;
|
|
|
+ const _isLeader = Role === 'leader';
|
|
|
if (RoomId?.length && UserId?.length) {
|
|
|
//房主设置房间配置
|
|
|
- if (this._isLeader) {
|
|
|
+ if (_isLeader) {
|
|
|
const isValid = await this.userService.isRoomMaster(RoomId, UserId);
|
|
|
//检查房主非法性
|
|
|
if (isValid) {
|
|
|
- await this.userService.setRoomConfig(RoomId, userInfo.roomConfig);
|
|
|
+ roomConfig.masterId = UserId;
|
|
|
+ this.logger.log(JSON.stringify(userInfo), 'room-config');
|
|
|
+ userInfo.roomConfig &&
|
|
|
+ (await this.userService.setRoomConfig(RoomId, roomConfig));
|
|
|
} else {
|
|
|
blockJoin = true;
|
|
|
socket.emit('manager-error', {
|
|
@@ -128,10 +126,7 @@ export class RoomService {
|
|
|
socket.disconnect(true);
|
|
|
}
|
|
|
}
|
|
|
- const isExist = await this.userService.isUserInRooms(
|
|
|
- this._roomId,
|
|
|
- this._userId,
|
|
|
- );
|
|
|
+ const isExist = await this.userService.isUserInRooms(RoomId, UserId);
|
|
|
const isMax = await this.userService.isMaxRoom(RoomId);
|
|
|
// console.log('isMaxRoom', isMax);
|
|
|
if (isMax && Role !== 'leader' && !isExist) {
|
|
@@ -146,9 +141,9 @@ export class RoomService {
|
|
|
|
|
|
if (!blockJoin) {
|
|
|
if (!isExist) {
|
|
|
- await this.userService.insertUser(this._userInfo);
|
|
|
+ await this.userService.insertUser(socket.data.user);
|
|
|
} else {
|
|
|
- const updated = await this.userService.updateUsers(this._userInfo);
|
|
|
+ const updated = await this.userService.updateUsers(socket.data.user);
|
|
|
if (!updated) {
|
|
|
socket.emit('manager-error', {
|
|
|
type: 'invalid-match-role',
|
|
@@ -163,8 +158,8 @@ export class RoomService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- await this.tempService.init(socket);
|
|
|
- const roomUsers = await this.userService.getRoomUsers(this._roomId);
|
|
|
+ this.tempService.init(socket);
|
|
|
+ const roomUsers = await this.userService.getRoomUsers(RoomId);
|
|
|
socket.emit('join', {
|
|
|
user: socket.data.user,
|
|
|
members: roomUsers,
|
|
@@ -205,7 +200,10 @@ export class RoomService {
|
|
|
* @param message
|
|
|
*/
|
|
|
async handleSyncAction(socket: Socket, message: any) {
|
|
|
- socket.broadcast.to(this._roomId).emit('sync', message);
|
|
|
+ if (socket.data?.user) {
|
|
|
+ const { RoomId } = socket.data?.user;
|
|
|
+ socket.broadcast.to(RoomId).emit('sync', message);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -213,7 +211,10 @@ export class RoomService {
|
|
|
* @param message
|
|
|
*/
|
|
|
async handlePaintAction(socket: Socket, message: any) {
|
|
|
- socket.broadcast.to(this._roomId).emit('paint', message);
|
|
|
+ if (socket.data?.user) {
|
|
|
+ const { RoomId } = socket.data?.user;
|
|
|
+ socket.broadcast.to(RoomId).emit('paint', message);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|