Browse Source

细节调整

gemercheung 3 years ago
parent
commit
d6ed3a75ff

+ 60 - 7
src/controller/room/assistant.js

@@ -52,7 +52,7 @@ export class RoomAssistant {
    * @param {*} roomId
    * @param {*} roomId
    * @param {*} userId
    * @param {*} userId
    */
    */
-  async setAssistant(roomId, userId, callback) {
+  async setAssistant(roomId, userId, cancel) {
     try {
     try {
       const userRes = await getCurrentUser(roomId, userId, FROMTYPE.MiniAPP);
       const userRes = await getCurrentUser(roomId, userId, FROMTYPE.MiniAPP);
       const user = JSON.parse(userRes);
       const user = JSON.parse(userRes);
@@ -61,19 +61,70 @@ export class RoomAssistant {
         console.log("不能设置自己为助理!");
         console.log("不能设置自己为助理!");
         return;
         return;
       }
       }
-
-      //  0是助手,1不是助手
-      const userObj = Object.assign({}, user, { isAssistant: 0 });
+      const role = cancel ? "customer" : "assistant";
+      //  assistant是助手,customer是普通角色,操作role会好些
+      const userObj = Object.assign({}, user, { role: role });
       const roomObj = Object.assign({}, roomConfigRes, { assistantId: user.userId });
       const roomObj = Object.assign({}, roomConfigRes, { assistantId: user.userId });
       // console.log("setAssistant", userObj, roomObj);
       // console.log("setAssistant", userObj, roomObj);
       // console.error("roomObj", roomObj);
       // console.error("roomObj", roomObj);
       await updateRoomUser(roomId, userId, userObj);
       await updateRoomUser(roomId, userId, userObj);
       // // 更新roomConfig 设置助手id
       // // 更新roomConfig 设置助手id
       await setRoomConfig(roomId, roomObj);
       await setRoomConfig(roomId, roomObj);
+      const AllRoomUsers = await getAllRoomUsers(roomId);
+
+      // 已存在的设置为false
+      Array.from(AllRoomUsers)
+        .filter((i) => i.role !== "leader")
+        .forEach(async (assistant) => {
+          if (assistant.userId !== userObj.userId) {
+            const unsetUserObj = Object.assign({}, assistant, { role: "customer" });
+            await updateRoomUser(roomId, userId, unsetUserObj);
+          }
+        });
+      // console.log("AllRoomUsers", AllRoomUsers);
       // callback(user);
       // callback(user);
-      this.room.notify.notifyBeAssistant(roomId, userId, user);
+      this.room.notify.notifyBeAssistant(roomId, userObj, this.room.userId);
+    } catch (error) {
+      this.room.logger.error("setAssistant:error", error);
+    }
+  }
+  /**
+   * 设置MIC权 LEADER(权限) 房主或助手
+   * 主要
+   * @param {*} roomId
+   * @param {*} userId
+   */
+  async setMicRight(roomId, userId, isAllowMic) {
+    try {
+      const userRes = await getCurrentUser(roomId, userId, FROMTYPE.MiniAPP);
+      const user = JSON.parse(userRes);
+      const roomConfigRes = await getRoomConfig(roomId);
+      if (this.room.userId == userId) {
+        console.log("不用设置自己的MIC!");
+        return;
+      }
+
+      const reveseMic = Number(isAllowMic) === 0 ? 1 : 0;
+      console.log("设置MIC权当前用户:: %s 新MIC权", user, reveseMic);
+      const userObj = Object.assign({}, user, { isAllowMic: reveseMic });
+      const roomObj = Object.assign({}, roomConfigRes, { allowMicId: user.userId });
+
+      await updateRoomUser(roomId, userId, userObj);
+      await setRoomConfig(roomId, roomObj);
+      const AllRoomUsers = await getAllRoomUsers(roomId);
+      // 已存在的设置为false
+      Array.from(AllRoomUsers)
+        .filter((i) => i.role !== "leader")
+        .forEach(async (assistant) => {
+          if (assistant.userId !== userObj.userId) {
+            const unsetUserObj = Object.assign({}, assistant, { isAllowMic: 0 });
+            await updateRoomUser(roomId, userId, unsetUserObj);
+          }
+        });
+
+      this.room.notify.notifyBeHasMic(roomId, userObj, this.room.userId);
     } catch (error) {
     } catch (error) {
-      this.room.logger.error("setAssistant", error);
+      this.room.logger.error("setMicRight::error", error);
     }
     }
   }
   }
 
 
@@ -199,7 +250,9 @@ export class RoomAssistant {
           if (checkIsRoomMaster) {
           if (checkIsRoomMaster) {
             this.room.logger.info("房主已存在房间 :", { roomId, userId, from: user.from });
             this.room.logger.info("房主已存在房间 :", { roomId, userId, from: user.from });
             // await this.joinRoom(roomId, userId, user);
             // await this.joinRoom(roomId, userId, user);
-            this.notifyUserJitter(roomId);
+            // this.notifyUserJitter(roomId);
+          } else {
+            this.room.logger.error("存在非法房主", userId);
           }
           }
         }
         }
       }
       }

+ 29 - 15
src/controller/room/index.js

@@ -28,7 +28,7 @@ export class RoomController extends BasicController {
       role: null,
       role: null,
       userLimitNum: null,
       userLimitNum: null,
       sceneNumber: null,
       sceneNumber: null,
-      roomType: null,
+      // roomType: null,
       from: null,
       from: null,
       onlineStatus: false,
       onlineStatus: false,
       voiceStatus: 0,
       voiceStatus: 0,
@@ -68,7 +68,7 @@ export class RoomController extends BasicController {
     this.logger.info("init-user-query:", this.socket.handshake.query);
     this.logger.info("init-user-query:", this.socket.handshake.query);
     if (user) {
     if (user) {
       this.user = Object.assign({}, user, {
       this.user = Object.assign({}, user, {
-        roomType: user.roomType || "",
+        // roomType: user.roomType || "",
       });
       });
       this.user.sig = this.getSig(this.user.userId);
       this.user.sig = this.getSig(this.user.userId);
       const oneSceneNum = this.user.sceneNumber || this.user.sceneNum;
       const oneSceneNum = this.user.sceneNumber || this.user.sceneNum;
@@ -84,14 +84,17 @@ export class RoomController extends BasicController {
           const roomConfig = getRoomConfig(this.roomId);
           const roomConfig = getRoomConfig(this.roomId);
           const checkoutMaster = await isRoomMaster(this.roomId, this.userId);
           const checkoutMaster = await isRoomMaster(this.roomId, this.userId);
           const isNoExistMaster = "masterId" in roomConfig;
           const isNoExistMaster = "masterId" in roomConfig;
-          console.log("checkoutMaster-isNoExistMaster", checkoutMaster, !isNoExistMaster);
+          // console.log("checkoutMaster-isNoExistMaster", checkoutMaster, !isNoExistMaster);
 
 
           if (checkoutMaster || !isNoExistMaster) {
           if (checkoutMaster || !isNoExistMaster) {
-            await setRoomConfig(this.roomId, {
+            console.log("房主进入记录信息::: checkoutMaster: %s, isNoExistMaster: %s ,roomId %s", checkoutMaster, !isNoExistMaster, this.roomId);
+            const roomConfig = {
               masterId: this.userId,
               masterId: this.userId,
               userLimitNum: this.user.userLimitNum,
               userLimitNum: this.user.userLimitNum,
               enableTalk: this.user.enableTalk === "true" ? true : false,
               enableTalk: this.user.enableTalk === "true" ? true : false,
-            });
+            };
+            // console.log("roomConfig", roomConfig);
+            await setRoomConfig(this.roomId, roomConfig);
           }
           }
         }
         }
       }
       }
@@ -166,7 +169,7 @@ export class RoomController extends BasicController {
         const roomConfig = await getRoomConfig(this.roomId);
         const roomConfig = await getRoomConfig(this.roomId);
         roomConfig.enableTalk = data;
         roomConfig.enableTalk = data;
         await setRoomConfig(this.roomId, roomConfig);
         await setRoomConfig(this.roomId, roomConfig);
-        console.log("setRoomConfig", data, roomConfig);
+        this.logger.info("changeRoomEnableTalk", JSON.stringify(data));
         this.socket.broadcast.to(this.roomId).emit(EVENT.changeRoomEnableTalk, roomConfig);
         this.socket.broadcast.to(this.roomId).emit(EVENT.changeRoomEnableTalk, roomConfig);
       } catch (error) {
       } catch (error) {
         this.logger.error("event:changeRoomEnableTalk", error);
         this.logger.error("event:changeRoomEnableTalk", error);
@@ -230,14 +233,24 @@ export class RoomController extends BasicController {
         console.error("error", error);
         console.error("error", error);
       }
       }
     });
     });
-
+    /**
+     * 设置助手
+     */
     this.socket.on("setAssistant", async ({ data, from }) => {
     this.socket.on("setAssistant", async ({ data, from }) => {
       const userId = `user:${data.userId}`;
       const userId = `user:${data.userId}`;
       const roomId = `room:${data.sceneNumber}:${data.roomId}`;
       const roomId = `room:${data.sceneNumber}:${data.roomId}`;
       console.log("设置助手", userId, roomId);
       console.log("设置助手", userId, roomId);
 
 
-      this.roomAssistant.setAssistant(roomId, userId);
-      // this.notify.notifyBeAssistant(roomId, userId, user);
+      this.roomAssistant.setAssistant(roomId, userId, data.cancel);
+    });
+    /**
+     * 设置MIC权
+     */
+    this.socket.on("setUserhasMic", async ({ data, from, isAllowMic }) => {
+      const userId = `user:${data.userId}`;
+      const roomId = `room:${data.sceneNumber}:${data.roomId}`;
+      console.log("设置MIC权", userId, roomId, data.isAllowMic);
+      this.roomAssistant.setMicRight(roomId, userId, data.isAllowMic);
     });
     });
 
 
     if (this.debugger) {
     if (this.debugger) {
@@ -266,12 +279,13 @@ export class RoomController extends BasicController {
             roomId: this.user.roomId,
             roomId: this.user.roomId,
           });
           });
         }
         }
-        setTimeout(() => {
-          this.socket.emit("someOneInRoom", {
-            user: this.user,
-            roomsPerson: AllRoomUsers,
-          });
-        });
+        //TODO someOneInRoom是用来触发语音与更新房间状态
+        // setTimeout(() => {
+        //   this.socket.emit("someOneInRoom", {
+        //     user: this.user,
+        //     roomsPerson: AllRoomUsers,
+        //   });
+        // });
       }
       }
     } catch (error) {
     } catch (error) {
       this.logger.error("room::roomMasterAutoRejoin", error);
       this.logger.error("room::roomMasterAutoRejoin", error);

+ 38 - 7
src/controller/room/notify.js

@@ -12,12 +12,43 @@ export class Notify {
     this.room = room;
     this.room = room;
   }
   }
 
 
-  notifyBeAssistant(userId, roomId, user) {
-    this.socket.emit(EVENT.beAssistant, {
-      user: user,
-    });
-    this.socket.broadcast.to(roomId).emit(EVENT.beAssistant, {
-      user: user,
-    });
+  async notifyBeAssistant(roomId, user, operator) {
+    try {
+      const AllRoomUsers = await getAllRoomUsers(roomId);
+      const realOperator = String(operator).split(":")[1];
+      console.log("notifyBeAssistant", realOperator);
+      this.socket.emit(EVENT.beAssistant, {
+        user: user,
+        roomPersons: AllRoomUsers,
+        operator: realOperator,
+      });
+      this.socket.broadcast.to(roomId).emit(EVENT.beAssistant, {
+        user: user,
+        roomPersons: AllRoomUsers,
+        operator: realOperator,
+      });
+    } catch (error) {
+      this.room.logger.error("notifyBeAssistant::error", error);
+    }
+  }
+
+  async notifyBeHasMic(roomId, user, operator) {
+    try {
+      const AllRoomUsers = await getAllRoomUsers(roomId);
+      const realOperator = String(operator).split(":")[1];
+      console.log("beHasMic", realOperator);
+      this.socket.emit(EVENT.beHasMic, {
+        user: user,
+        roomPersons: AllRoomUsers,
+        operator: realOperator,
+      });
+      this.socket.broadcast.to(roomId).emit(EVENT.beHasMic, {
+        user: user,
+        roomPersons: AllRoomUsers,
+        operator: realOperator,
+      });
+    } catch (error) {
+      this.room.logger.error("notifyBeHasMic::error", error);
+    }
   }
   }
 }
 }

+ 1 - 0
src/enum/event.js

@@ -20,6 +20,7 @@ const EVENT = {
   roomStatus: "roomStatus", // 房间状态
   roomStatus: "roomStatus", // 房间状态
   beKicked: "beKicked", // 通知被踢者
   beKicked: "beKicked", // 通知被踢者
   beAssistant: "beAssistant", // 通知成为助手
   beAssistant: "beAssistant", // 通知成为助手
+  beHasMic: "beHasMic", // 通知拥有MIC权
 };
 };
 
 
 export { EVENT };
 export { EVENT };

+ 6 - 1
src/service/roomConfigService.js

@@ -2,7 +2,12 @@ import { pubClient } from "../connection/redis.js";
 
 
 const setRoomConfig = (roomId, config) => {
 const setRoomConfig = (roomId, config) => {
   const roomConfigKey = `config:${roomId}`;
   const roomConfigKey = `config:${roomId}`;
-  return pubClient.hSet(roomConfigKey, config);
+  // console.log("roomConfigKey", roomConfigKey);
+  // Object.keys(config).forEach((item) => {
+  //   console.log("item", item, config[item]);
+  //   // pubClient.set(roomConfigKey, item, config[item]);// 旧版
+  // });
+  pubClient.hSet(roomConfigKey, config); // 5.0 可以用hash set
 };
 };
 const removeRoomConfig = (roomId, config) => {
 const removeRoomConfig = (roomId, config) => {
   const roomConfigKey = `config:${roomId}`;
   const roomConfigKey = `config:${roomId}`;