|
@@ -3,6 +3,7 @@ import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
|
|
import { Redis } from 'ioredis';
|
|
import { Redis } from 'ioredis';
|
|
|
|
|
|
import { SocketGateway } from 'src/socket/socket.gateway';
|
|
import { SocketGateway } from 'src/socket/socket.gateway';
|
|
|
|
+import { ActionsService } from './actions/actions.service';
|
|
import { UsersService } from './users/users.service';
|
|
import { UsersService } from './users/users.service';
|
|
|
|
|
|
@Injectable()
|
|
@Injectable()
|
|
@@ -11,12 +12,13 @@ export class RoomService {
|
|
@Inject(forwardRef(() => SocketGateway))
|
|
@Inject(forwardRef(() => SocketGateway))
|
|
private readonly socketGateway: SocketGateway,
|
|
private readonly socketGateway: SocketGateway,
|
|
@InjectRedis() private readonly redis: Redis,
|
|
@InjectRedis() private readonly redis: Redis,
|
|
- private readonly userService: UsersService, //
|
|
|
|
- ) { }
|
|
|
|
|
|
+ private readonly userService: UsersService,
|
|
|
|
+ private readonly actionsService: ActionsService,
|
|
|
|
+ ) {}
|
|
private readonly logger = new Logger('user');
|
|
private readonly logger = new Logger('user');
|
|
- private _userInfo = {} as UserInfoType;
|
|
|
|
|
|
+ public _userInfo = {} as UserInfoType;
|
|
private _roomConfig = {} as RoomConfigType;
|
|
private _roomConfig = {} as RoomConfigType;
|
|
-
|
|
|
|
|
|
+ private isJoin = false;
|
|
private get _roomId(): string {
|
|
private get _roomId(): string {
|
|
return this._userInfo.RoomId;
|
|
return this._userInfo.RoomId;
|
|
}
|
|
}
|
|
@@ -37,15 +39,49 @@ export class RoomService {
|
|
Role: userInfo.role,
|
|
Role: userInfo.role,
|
|
UserId: userInfo.userId,
|
|
UserId: userInfo.userId,
|
|
Avatar: userInfo.avatar,
|
|
Avatar: userInfo.avatar,
|
|
- Nickname: userInfo.nickname,
|
|
|
|
|
|
+ Nickname:
|
|
|
|
+ decodeURIComponent(userInfo.nickname) || `用户[${userInfo.userId}]`,
|
|
IsClient: userInfo.isClient,
|
|
IsClient: userInfo.isClient,
|
|
IsMuted: true,
|
|
IsMuted: true,
|
|
IsWords: true,
|
|
IsWords: true,
|
|
Order: userInfo.role === 'leader' ? 0 : 1,
|
|
Order: userInfo.role === 'leader' ? 0 : 1,
|
|
JoinTime: Date.now(),
|
|
JoinTime: Date.now(),
|
|
InTime: Date.now(),
|
|
InTime: Date.now(),
|
|
|
|
+ IsOnline: true,
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
+ async handleUserOnline() {
|
|
|
|
+ this._userInfo.IsOnline = true;
|
|
|
|
+ if (this._userId && this._roomId) {
|
|
|
|
+ await this.userService.updateUserOnlineState(
|
|
|
|
+ this._roomId,
|
|
|
|
+ this._userId,
|
|
|
|
+ true,
|
|
|
|
+ );
|
|
|
|
+ const roomUsers = await this.userService.getRoomUsers(this._roomId);
|
|
|
|
+ this.isJoin &&
|
|
|
|
+ this.socketGateway.server.to(this._roomId).emit('action', {
|
|
|
|
+ type: 'rooms-status',
|
|
|
|
+ members: roomUsers,
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async handleUserOffline() {
|
|
|
|
+ this._userInfo.IsOnline = false;
|
|
|
|
+ if (this._userId && this._roomId) {
|
|
|
|
+ await this.userService.updateUserOnlineState(
|
|
|
|
+ this._roomId,
|
|
|
|
+ this._userId,
|
|
|
|
+ false,
|
|
|
|
+ );
|
|
|
|
+ const roomUsers = await this.userService.getRoomUsers(this._roomId);
|
|
|
|
+ this.socketGateway.server.to(this._roomId).emit('action', {
|
|
|
|
+ type: 'rooms-status',
|
|
|
|
+ members: roomUsers,
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 加入房间
|
|
* 加入房间
|
|
@@ -100,6 +136,7 @@ export class RoomService {
|
|
members: roomUsers,
|
|
members: roomUsers,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+ this.isJoin = true;
|
|
this.logger.log(
|
|
this.logger.log(
|
|
JSON.stringify(this._userInfo),
|
|
JSON.stringify(this._userInfo),
|
|
`join-user-${this._userInfo.Role}`,
|
|
`join-user-${this._userInfo.Role}`,
|
|
@@ -115,6 +152,7 @@ export class RoomService {
|
|
* @param message
|
|
* @param message
|
|
*/
|
|
*/
|
|
async handleUserAction(message: any) {
|
|
async handleUserAction(message: any) {
|
|
|
|
+ this.actionsService.handleAllAction(message);
|
|
// this.socketGateway.server.to(this._roomId).emit('action', message);
|
|
// this.socketGateway.server.to(this._roomId).emit('action', message);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -133,6 +171,20 @@ export class RoomService {
|
|
async handlePaintAction(message: any) {
|
|
async handlePaintAction(message: any) {
|
|
this.socketGateway.server.to(this._roomId).emit('paint', message);
|
|
this.socketGateway.server.to(this._roomId).emit('paint', message);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 房间T人
|
|
|
|
+ * @param message
|
|
|
|
+ */
|
|
|
|
+ async handleKickAction(userId: string) {
|
|
|
|
+ await this.userService.deleteRoomUser(this._roomId, userId);
|
|
|
|
+ this.socketGateway.server.to(this._roomId).emit('action', {
|
|
|
|
+ type: 'kick-user',
|
|
|
|
+ data: {
|
|
|
|
+ userId: userId,
|
|
|
|
+ },
|
|
|
|
+ });
|
|
|
|
+ }
|
|
/**
|
|
/**
|
|
* 解散房间
|
|
* 解散房间
|
|
*/
|
|
*/
|