|
@@ -0,0 +1,131 @@
|
|
|
+import { InjectRedis } from '@liaoliaots/nestjs-redis';
|
|
|
+import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
|
|
+import { Redis } from 'ioredis';
|
|
|
+
|
|
|
+import { SocketGateway } from 'src/socket/socket.gateway';
|
|
|
+import { UsersService } from './users/users.service';
|
|
|
+
|
|
|
+@Injectable()
|
|
|
+export class RoomService {
|
|
|
+ constructor(
|
|
|
+ @Inject(forwardRef(() => SocketGateway))
|
|
|
+ private readonly socketGateway: SocketGateway,
|
|
|
+ @InjectRedis() private readonly redis: Redis,
|
|
|
+ private readonly userService: UsersService,
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ private _userInfo = {} as UserInfoType;
|
|
|
+ private _roomConfig = {} as RoomConfigType;
|
|
|
+
|
|
|
+ private get _roomId(): string {
|
|
|
+ return this._userInfo.RoomId;
|
|
|
+ }
|
|
|
+ private get _userId(): string {
|
|
|
+ return this._userInfo.UserId;
|
|
|
+ }
|
|
|
+ private get _isLeader(): boolean {
|
|
|
+ return this._userInfo.Role === 'leader';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化当前用户数据
|
|
|
+ * @param userInfo
|
|
|
+ */
|
|
|
+ initUserProfile(userInfo: UserInfoParams): void {
|
|
|
+ console.log('userInfo', userInfo);
|
|
|
+ this._userInfo = {
|
|
|
+ RoomId: userInfo.roomId,
|
|
|
+ Role: userInfo.role,
|
|
|
+ UserId: userInfo.userId,
|
|
|
+ Avatar: userInfo.avatar,
|
|
|
+ Nickname: userInfo.nickname,
|
|
|
+ IsClient: userInfo.isClient,
|
|
|
+ IsMuted: true,
|
|
|
+ IsWords: true,
|
|
|
+ Order: userInfo.role === 'leader' ? 0 : 1,
|
|
|
+ JoinTime: Date.now(),
|
|
|
+ InTime: Date.now(),
|
|
|
+ };
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 初始化房间配置
|
|
|
+ * @param userInfo
|
|
|
+ */
|
|
|
+ async handleRoomConfig(
|
|
|
+ RoomId: string,
|
|
|
+ RoomConfig: RoomConfigType,
|
|
|
+ ): Promise<void> {
|
|
|
+ await this.userService.setRoomConfig(RoomId, RoomConfig);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 加入房间
|
|
|
+ * @param userInfo
|
|
|
+ */
|
|
|
+
|
|
|
+ async handleUserJoin(userInfo: UserInfoParams): Promise<void> {
|
|
|
+ this.initUserProfile(userInfo);
|
|
|
+ console.log(this._userId);
|
|
|
+ let blockJoin = false;
|
|
|
+ if (this._roomId?.length && this._userId?.length) {
|
|
|
+ //房主设置房间配置
|
|
|
+ if (this._isLeader) {
|
|
|
+ const isValid = await this.userService.isRoomMaster(
|
|
|
+ this._roomId,
|
|
|
+ this._userId,
|
|
|
+ );
|
|
|
+
|
|
|
+ if (isValid) {
|
|
|
+ await this.userService.setRoomConfig(
|
|
|
+ this._roomId,
|
|
|
+ userInfo.roomConfig,
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ blockJoin = true;
|
|
|
+ this.socketGateway.server.emit('action', {
|
|
|
+ type: 'invalid-master',
|
|
|
+ code: 303,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const isExist = await this.userService.isUserInRooms(
|
|
|
+ this._roomId,
|
|
|
+ this._userId,
|
|
|
+ );
|
|
|
+ if (!isExist) {
|
|
|
+ if (this._isLeader) {
|
|
|
+ this._roomConfig = userInfo.roomConfig;
|
|
|
+ this.handleRoomConfig(this._roomId, this._roomConfig);
|
|
|
+ }
|
|
|
+ this.userService.insertUser(this._userInfo);
|
|
|
+ } else {
|
|
|
+ this.userService.updateUsers(this._userInfo);
|
|
|
+ }
|
|
|
+ const roomUsers = await this.userService.getRoomUsers(this._roomId);
|
|
|
+
|
|
|
+ if (!blockJoin) {
|
|
|
+ this.socketGateway.server.emit('join', {
|
|
|
+ user: this._userInfo,
|
|
|
+ members: roomUsers,
|
|
|
+ });
|
|
|
+ if (!this._userInfo.IsClient) {
|
|
|
+ this.socketGateway.server.to(this._roomId).emit('action', {
|
|
|
+ type: 'user-join',
|
|
|
+ user: this._userInfo,
|
|
|
+ members: roomUsers,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this.socketGateway.server.emit('action', { type: 'error', code: 403 });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解散房间
|
|
|
+ */
|
|
|
+
|
|
|
+ async handleRoomDismiss(): Promise<void> {
|
|
|
+ await this.userService.delRoom(this._roomId);
|
|
|
+ await this.userService.delRoomConfig(this._roomId);
|
|
|
+ }
|
|
|
+}
|