gemercheung пре 2 година
родитељ
комит
d4153eea01

+ 31 - 0
src/room-manager/room-manager.controller.ts

@@ -160,4 +160,35 @@ export class RoomManagerController {
       return false;
     }
   }
+  @Get('/setRoomMax/:id/:max')
+  @ApiOperation({ summary: '设置房间总人员' })
+  @ApiParam({
+    name: 'id',
+    required: true,
+    description: '房间ID',
+    schema: { oneOf: [{ type: 'string' }] },
+    type: 'string',
+  })
+  @ApiParam({
+    name: 'max',
+    required: true,
+    description: '总人数',
+    schema: { oneOf: [{ type: 'number' }] },
+    type: 'number',
+  })
+  @ApiResponse({
+    description: '返回值',
+    type: Boolean,
+  })
+  async setRoomMaxNumber(@Param('id') id: string, @Param('max') max: number) {
+    this.roomService.logger.log(`max: ${max}`, 'api-setRoomMaxNumber');
+    if (max > 0 && max <= 50 && id) {
+      const lim = max as number as RoomConfigType['limit'];
+      return this.usersService.setRoomConfig(id, {
+        limit: lim,
+      });
+    } else {
+      return false;
+    }
+  }
 }

+ 2 - 2
src/room/config.d.ts

@@ -1,6 +1,6 @@
 interface RoomConfigType {
-  limit: IntRange<5, 50>;
-  masterId: string;
+  limit?: IntRange<5, 51>;
+  masterId?: string;
 }
 
 type Enumerate<

+ 15 - 2
src/room/room.service.ts

@@ -17,7 +17,7 @@ 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;
@@ -110,8 +110,10 @@ export class RoomService {
     await this.handleUserOnline(socket);
     await this.delayService.handleOnine(socket);
     let blockJoin = false;
-    const { RoomId, UserId } = socket.data.user;
+    const { RoomId, UserId, Role } = socket.data.user;
+
     if (RoomId?.length && UserId?.length) {
+
       //房主设置房间配置
       if (this._isLeader) {
         const isValid = await this.userService.isRoomMaster(RoomId, UserId);
@@ -132,6 +134,17 @@ export class RoomService {
         this._roomId,
         this._userId,
       );
+      const isMax = await this.userService.isMaxRoom(RoomId);
+      console.log('isMaxRoom', isMax);
+      if (isMax && Role !== 'leader' && !isExist) {
+        this.logger.warn(`308:room-maxing`, 'join-error');
+        socket.emit('manager-error', {
+          type: 'room-maxing',
+          code: 308,
+        });
+        socket.disconnect(true);
+        return;
+      }
 
       if (!blockJoin) {
         if (!isExist) {

+ 31 - 5
src/room/users/users.service.ts

@@ -20,6 +20,13 @@ export class UsersService {
 
     return res > 0 ? true : false;
   }
+  async isMaxRoom(RoomId: string, max?: number): Promise<boolean> {
+    const config = await this.getRoomConfig(RoomId);
+    const newMax = max ? max : config ? Number(config['limit']) : 50;
+    const res = await this.redis.hvals(`kankan:socket:rooms:${RoomId}`);
+    console.log('newMax', res.length);
+    return Promise.resolve(res.length >= newMax);
+  }
   async isValidRoom(RoomId: string): Promise<boolean> {
     const isRoomHasConfig = await this.redis.hexists(
       `kankan:socket:roomConfig`,
@@ -104,11 +111,21 @@ export class UsersService {
     RoomId: string,
     RoomConfig: RoomConfigType,
   ): Promise<void> {
-    this.redis.hset(
-      `kankan:socket:roomConfig`,
-      RoomId,
-      JSON.stringify(RoomConfig),
-    );
+    const isExist = await this.redis.hget(`kankan:socket:roomConfig`, RoomId);
+    // console.log('setRoomConfig', isExist);
+    if (isExist) {
+      const config = JSON.parse(isExist);
+      const obj = Object.assign({}, config, RoomConfig);
+      const updateString = JSON.stringify(obj);
+      // console.log('setRoomConfig-obj', RoomId, updateString);
+      await this.redis.hset(`kankan:socket:roomConfig`, RoomId, updateString);
+    } else {
+      await this.redis.hset(
+        `kankan:socket:roomConfig`,
+        RoomId,
+        JSON.stringify(RoomConfig),
+      );
+    }
   }
   async isRoomMaster(RoomId: string, userId: string): Promise<boolean> {
     try {
@@ -209,4 +226,13 @@ export class UsersService {
       return Promise.resolve(false);
     }
   }
+  async getRoomConfig(RoomId: string): Promise<JSONValue> {
+    const isExist = await this.redis.hget(`kankan:socket:roomConfig`, RoomId);
+    if (isExist) {
+      const updater = JSON.parse(isExist);
+      return Promise.resolve(updater);
+    } else {
+      return Promise.resolve(false);
+    }
+  }
 }