gemercheung 3 years ago
parent
commit
d888da0758

+ 3 - 0
src/controller/basicController.js

@@ -16,5 +16,8 @@ export class BasicController {
   isHoster = (role) => {
     return String(role).toLowerCase() === ROLES.LEADER;
   };
+  issAssistant = (role) => {
+    return String(role).toLowerCase() === ROLES.ASSISTANT;
+  };
   run() {}
 }

+ 25 - 21
src/controller/room/assistant.js

@@ -48,6 +48,31 @@ export class RoomAssistant {
   }
 
   /**
+   * 设置助手 LEADER(权限) 房主或助手
+   * @param {*} roomId
+   * @param {*} userId
+   */
+  async setAssistant(roomId, userId) {
+    try {
+      const userRes = await getCurrentUser(roomId, userId, FROMTYPE.MiniAPP);
+      const user = JSON.parse(userRes);
+      const roomConfigRes = await getRoomConfig(roomId);
+      // const roomConfig = JSON.parse(roomConfigRes);
+      // roomConfig.assistantId = user.userId;
+
+      //  0是助手,1不是助手
+      const userObj = Object.assign({}, { isAssistant: 0 }, user);
+      const roomObj = Object.assign({}, { assistantId: user.userId }, roomConfigRes);
+      console.log("setAssistant", userObj, roomObj);
+      await updateRoomUser(roomId, userId, userObj);
+      // // 更新roomConfig 设置助手id
+      await setRoomConfig(roomId, roomObj);
+    } catch (error) {
+      this.room.logger.error("setAssistant", error);
+    }
+  }
+
+  /**
    * 创建房间  LEADER or assistant 房主或助手
    * @param {*string} roomId
    * @param {*string} userId
@@ -70,27 +95,6 @@ export class RoomAssistant {
     this.room.logger.info("removeRoom", { roomId });
     await this.redis.del(roomId);
   }
-  //   /**
-  //    * 加入房间
-  //    * @param {*} roomId
-  //    * @param {*} userId
-  //    * @param {*} user
-  //    */
-  //   async joinRoom(roomId, userId, user) {
-  //     this.room.logger.info("joinRoom", { roomId });
-  //     try {
-  //       const hasRoom = await this.redis.exists(roomId);
-  //       if (hasRoom) {
-  //         await this.redis.hSet(roomId, userId, JSON.stringify(user));
-  //       } else {
-  //         this.room.logger.info("no room join");
-  //       }
-  //       return Promise.resolve();
-  //     } catch (error) {
-  //       this.room.logger.error(error);
-  //       return Promise.reject(error);
-  //     }
-  //   }
 
   /**
    *  加入房间

+ 0 - 206
src/controller/room/bocker.js

@@ -11,210 +11,4 @@ export class RoomBlocker {
     this.room = room;
   }
 
-  async prepearRoom(roomSessionId, roomId) {
-    const uRoomId = await this.redis.get(roomSessionId);
-    const mergeRoomId = uRoomId || roomId;
-    this.roomId = mergeRoomId;
-    console.log("prepearRoom", roomSessionId, this.roomId);
-    await this.redis.set(roomSessionId, mergeRoomId);
-    return Promise.resolve(this.roomId);
-  }
-
-  async destoryRoom(roomSessionId, roomConfigId) {
-    console.log("destoryRoom", roomSessionId, roomConfigId);
-    await this.redis.del(roomSessionId);
-    await this.redis.del(roomConfigId);
-    this.disconnect();
-    return Promise.resolve(true);
-  }
-
-  /**
-   *
-   */
-  kickPersion() {}
-
-  /**
-   * 创建房间  LEADER or assistant 房主或助手
-   * @param {*} roomId
-   * @param {*} userId
-   * @param {*} user
-   */
-
-  async buildRoom(roomId, userId, user) {
-    const hasJoin = await this.redis.HVALS(roomId, userId);
-    if (hasJoin.length === 0) {
-      this.room.logger.info("buildRoom", { roomId });
-      await this.redis.hSet(roomId, userId, JSON.stringify(user));
-    }
-  }
-
-  /**
-   * 关闭房间
-   * @param {*} roomId
-   */
-
-  async removeRoom(roomId) {
-    this.room.logger.info("removeRoom", { roomId });
-    await this.redis.del(roomId);
-  }
-
-  /**
-   *  加入房间
-   * @param {*} roomId
-   * @param {*} userId
-   * @param {*} user
-   */
-  async joinRoom(roomId, userId, user) {
-    const hasRoom = await this.redis.exists(roomId);
-    const isJoinRoom = await this.redis.hExists(roomId, userId);
-    if (hasRoom) {
-      await this.redis.hSet(roomId, userId, JSON.stringify(user));
-    } else {
-      this.room.logger.error("不存在房间", roomId);
-    }
-    if (!isJoinRoom) {
-      this.room.logger.info("加入房间 :", { userId, roomId, user });
-      this.socket.join(roomId);
-      const AllRoomUsers = await getAllRoomUsers(roomId);
-      const roomConfig = await getRoomConfig(roomId);
-      this.socket.broadcast.emit(EVENT.roomIn, {
-        user,
-        roomsPerson: AllRoomUsers,
-        roomsConfig: roomConfig,
-      });
-    } else {
-      this.room.logger.info(`已加入房间 :`, { userId });
-    }
-  }
-
-  /**
-   * 离开房间
-   * @param {*} roomId
-   * @param {*} userId
-   * @param {*} user
-   */
-  async leaveRoom(roomId, userId, user) {
-    try {
-      await this.redis.hDel(roomId, userId);
-      this.room.logger.info("离开房间", userId, AllRoomUsers);
-      const AllRoomUsers = await getAllRoomUsers(roomId);
-      const roomConfig = await getRoomConfig(roomId);
-      this.socket.broadcast.to(roomId).emit(EVENT.roomOut, {
-        user,
-        roomsPerson: AllRoomUsers,
-        roomsConfig: roomConfig,
-      });
-
-      this.socket.broadcast.to(roomId).emit(EVENT.someOneLeaveRoom, {
-        user,
-        roomsPerson: AllRoomUsers,
-      });
-      await this.socket.leave(roomId);
-    } catch (error) {
-      console.log("leaveRoom::error", error);
-    }
-  }
-
-  /**
-   * 房主关闭房间
-   * @param {*} clientRoom
-   * @param {*} userUniqueId
-   * @param {*} roomUniqueId
-   */
-  async closeRoom(roomId, userId, user) {
-    try {
-      this.room.logger.info("房主关闭房间", userId);
-      console.log("isInRoom", this.socket.rooms.has(roomId));
-      this.socket.broadcast.to(roomId).emit(EVENT.roomClose, { code: 3002, msg: CODEMEG[3002] });
-      await removeRoomAllUsers(roomId);
-      this.socket.leave(roomId);
-    } catch (error) {
-      this.room.logger.error("RoomAssistant::closeRoom", error);
-    }
-  }
-
-  /**
-   * 呼叫房间
-   * @param {*} roomId
-   * @param {*} userId
-   * @param {*} user
-   */
-  async startCall(roomId, userId, user) {
-    try {
-      if (!this.room.isHoster(user.role)) {
-        this.room.logger.info("不是房主", JSON.stringify(user));
-        await this.joinRoom(roomId, userId, user);
-      } else {
-        const hasRoom = await this.redis.hVals(roomId);
-        if (hasRoom.length === 0) {
-          this.room.logger.info("房主主动创建房间 :", { roomId, userId });
-          await this.buildRoom(roomId, userId, user);
-        } else {
-          this.room.logger.info("房主已存在房间 :", { roomId, userId });
-        }
-      }
-      user.isInRoom = true;
-      const AllRoomUsers = await getAllRoomUsers(roomId);
-      const roomConfig = await getRoomConfig(roomId);
-      await updateRoomUser(roomId, userId, user);
-      this.room.logger.info("roomId", roomId);
-      this.room.logger.info("AllRoomUsers", AllRoomUsers.length);
-
-      this.socket.emit(EVENT.roomIn, {
-        user,
-        roomsPerson: AllRoomUsers,
-        roomsConfig: roomConfig,
-      });
-      this.socket.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
-        user,
-        roomsPerson: AllRoomUsers,
-      });
-    } catch (error) {
-      this.room.logger.error("assistant::startCall:", error);
-    }
-  }
-
-  /**
-   * 关闭呼叫房间
-   * @param {*} roomId
-   * @param {*} userId
-   * @param {*} user
-   */
-
-  stopCall(roomId, userId, user) {
-    if (!this.room.isHoster(user.role)) {
-      this.leaveRoom(roomId, userId, user);
-    } else {
-      this.closeRoom(roomId, userId, user);
-    }
-  }
-  // 主动断开
-  async disconnect() {
-    const syncId = this.room.syncId;
-    const roomId = this.room.roomId;
-    const userId = this.room.userId;
-    this.socket.leave(syncId);
-    this.socket.leave(roomId);
-    await this.redis.del(syncId);
-    await this.redis.del(userId);
-  }
-  // RoomSessionId 房间有效时间
-  setRoomUnlimit(roomSessionId) {
-    return this.redis.expire(roomSessionId, -1);
-  }
-  setRoomAvailableBySeconds(roomSessionId, seconds) {
-    return this.redis.expire(roomSessionId, seconds);
-  }
-  setRoomAvailableByHours(roomSessionId, hours) {
-    return this.redis.expire(roomSessionId, 60 * 60 * hours);
-  }
-  watchRoomExpired(callback) {
-    subClient.subscribe("__keyevent@0__:expired", this.watchRoomExpiredFn);
-  }
-  async watchRoomExpiredFn(key) {
-    console.log("key=> ", key);
-  }
-  unWatchRoomExpired() {
-    subClient.unsubscribe("__keyevent@0__:expired", this.watchRoomExpiredFn);
-  }
 }

+ 38 - 19
src/controller/room/index.js

@@ -4,12 +4,14 @@ import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, update
 import { setRoomConfig, getRoomConfig, updateRoomConfigByKey } from "../../service/roomConfigService.js";
 
 import { RoomAssistant } from "./assistant.js";
+import { Notify } from "./notify.js";
 import { BasicController } from "../basicController.js";
 
 export class RoomController extends BasicController {
   constructor(...args) {
     super(...args);
     this.roomAssistant = new RoomAssistant(this.socket, this.redisCli, this);
+    this.notify = new Notify(this.socket, this.redisCli, this);
     this.roomId = null;
     this.sessionId = null;
     this.userId = null;
@@ -178,36 +180,53 @@ export class RoomController extends BasicController {
         this.logger.error("event:changeOnlineStatus", error);
       }
     });
+    /**
+     * 踢人管理
+     */
 
     this.socket.on(EVENT.kickUser, async ({ data, from }) => {
       try {
         const userId = `user:${data.userId}`;
         const roomId = `room:${data.sceneNumber}:${data.roomId}`;
-        
-        const isKick = await this.roomAssistant.kickPersion(roomId, userId);
-        if (isKick) {
-          const AllRoomUsers = await getAllRoomUsers(roomId);
-          const currentUser = await getCurrentUser(userId, FROMTYPE.MiniAPP);
-          console.log("kickUser-AllRoomUsers", AllRoomUsers.length);
-          this.logger.info("kickUser", currentUser, userId, roomId);
-          // 通知管理
-          this.socket.emit(EVENT.someOneLeaveRoom, {
-            user: currentUser,
-            roomsPerson: AllRoomUsers,
-          });
-           // 通知房间人员变动
-          this.socket.broadcast.to(roomId).emit(EVENT.someOneLeaveRoom, {
-            user: currentUser,
-            roomsPerson: AllRoomUsers,
-          });
-          // 如果踢人后 如何通知?
-          this.socket.broadcast.to(roomId).emit(EVENT.beKicked, data);
+        const currentUser = await getCurrentUser(userId, FROMTYPE.MiniAPP);
+        const user = JSON.parse(currentUser);
+        // 被踢人不能是hoster
+        console.log("user", JSON.stringify(user));
+        if (user && !this.isHoster(user.role)) {
+          console.log("currentUser.role", user.role);
+          const isKick = await this.roomAssistant.kickPersion(roomId, userId);
+          if (isKick) {
+            const AllRoomUsers = await getAllRoomUsers(roomId);
+            console.log("kickUser-AllRoomUsers", AllRoomUsers.length);
+            this.logger.info("kickUser", currentUser, userId, roomId);
+            // 通知管理
+            this.socket.emit(EVENT.someOneLeaveRoom, {
+              user: user,
+              roomsPerson: AllRoomUsers,
+            });
+            // 通知房间人员变动
+            this.socket.broadcast.to(roomId).emit(EVENT.someOneLeaveRoom, {
+              user: user,
+              roomsPerson: AllRoomUsers,
+            });
+            // 如果踢人后 如何通知?
+            this.socket.broadcast.to(roomId).emit(EVENT.beKicked, data);
+          }
+        } else {
+          this.logger.warn(user.nickname || "" + " is hoster,cant be out of room");
         }
       } catch (error) {
         console.error("error", error);
       }
     });
 
+    this.socket.on("setAssistant", async ({ data, from }) => {
+      const userId = `user:${data.userId}`;
+      const roomId = `room:${data.sceneNumber}:${data.roomId}`;
+      console.log("设置助手", userId, roomId);
+      this.roomAssistant.setAssistant(roomId, userId);
+    });
+
     if (this.debugger) {
       this.socket.onAny((event, data) => {
         if (event !== "webSyncAction") {

+ 21 - 0
src/controller/room/notify.js

@@ -0,0 +1,21 @@
+// 房间拦截行为助手
+import { EVENT, CODEMEG } from "../../enum/index.js";
+import { updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
+import { setRoomConfig, getRoomConfig } from "../../service/roomConfigService.js";
+import { subClient } from "../../connection/redis.js";
+
+export class Notify {
+  constructor(socket, redis, room) {
+    this.socket = socket;
+    this.redis = redis;
+    this.roomId = null;
+    this.room = room;
+  }
+
+  notifyBeAssistant(userId, roomId) {
+    const user = {};
+    this.socket.broadcast.to(roomId).emit(EVENT.beAssistant, {
+      user: user
+    });
+  }
+}

+ 2 - 1
src/enum/event.js

@@ -18,7 +18,8 @@ const EVENT = {
   setUserhasMic: "setUserhasMic", //
   kickUser: "kickUser", // 踢人
   roomStatus: "roomStatus", // 房间状态
-  beKicked:"beKicked" // 通知被踢者
+  beKicked: "beKicked", // 通知被踢者
+  beAssistant: "beAssistant", // 通知成为助手
 };
 
 export { EVENT };

+ 1 - 1
src/enum/role.js

@@ -1,7 +1,7 @@
 const ROLES = {
   LEADER: "leader", // 房主
-  AGENT: "agent", // 房客
   CUSTOMER: "customer", // 房客
+  ASSISTANT: "assistant", // 助手 
 };
 
 export { ROLES };