|
@@ -0,0 +1,129 @@
|
|
|
|
+import { pubClient } from "../connection/redis.js";
|
|
|
|
+import { ROLES, CODEMEG, EVENT } from "../enum/index.js";
|
|
|
|
+import { io } from "../core/io.js";
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 加入房间
|
|
|
|
+ * @param {*} clientRoom
|
|
|
|
+ * @param {*} socket
|
|
|
|
+ */
|
|
|
|
+const joinRoom = ({ clientRoom, userUniqueId, roomUniqueId, user }, socket) => {
|
|
|
|
+ console.log("加入房间:", userUniqueId);
|
|
|
|
+ socket.join(clientRoom);
|
|
|
|
+ socket.broadcast.emit(EVENT.roomIn, {
|
|
|
|
+ user,
|
|
|
|
+ // roomsPerson: sortRoomUser(roomsPerson),
|
|
|
|
+ // roomsConfig: this._roomsConfig[roomId],
|
|
|
|
+ });
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 离开房间
|
|
|
|
+ * @param {*} clientRoom
|
|
|
|
+ * @param {*} userUniqueId
|
|
|
|
+ * @param {*} roomUniqueId
|
|
|
|
+ * @param {*} socket
|
|
|
|
+ */
|
|
|
|
+const leaveRoom = async ({
|
|
|
|
+ clientRoom,
|
|
|
|
+ userUniqueId,
|
|
|
|
+ roomUniqueId,
|
|
|
|
+ socket,
|
|
|
|
+}) => {
|
|
|
|
+ try {
|
|
|
|
+ socket.leave(clientRoom);
|
|
|
|
+ const existUser = await pubClient.hGetAll(userUniqueId);
|
|
|
|
+ if (existUser) {
|
|
|
|
+ if (String(existUser.role).toLowerCase() === ROLES.LEADER) {
|
|
|
|
+ await pubClient.del(userUniqueId);
|
|
|
|
+ await pubClient.del(roomUniqueId);
|
|
|
|
+ io.emit(EVENT.roomClose, { code: 3002, msg: CODEMEG[3002] });
|
|
|
|
+ } else {
|
|
|
|
+ await pubClient.del(userUniqueId);
|
|
|
|
+ await pubClient.hDel(roomUniqueId, userUniqueId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ console.log("disconnect user" + userUniqueId);
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.log("error", error);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 检查房主存不存在
|
|
|
|
+ * @param {*} roomUniqueId
|
|
|
|
+ * @param {*} socket
|
|
|
|
+ */
|
|
|
|
+const watchRoomStatus = async (roomUniqueId, socket) => {
|
|
|
|
+ const watch = await pubClient.watch(roomUniqueId);
|
|
|
|
+ if (watch === "OK") {
|
|
|
|
+ checkRoomStatus(roomUniqueId, socket);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const checkRoomStatus = async (roomUniqueId, socket) => {
|
|
|
|
+ const values = await pubClient.hVals(roomUniqueId);
|
|
|
|
+ if (values) {
|
|
|
|
+ console.log("room watch", roomUniqueId, values);
|
|
|
|
+ const existMaster = values.find(
|
|
|
|
+ (item) => String(JSON.parse(item).role).toLowerCase() === ROLES.LEADER
|
|
|
|
+ );
|
|
|
|
+ if (!existMaster) {
|
|
|
|
+ console.log("不存在房主");
|
|
|
|
+ setTimeout(async () => {
|
|
|
|
+ socket.emit(EVENT.roomClose, { code: 3001, msg: CODEMEG[3001] });
|
|
|
|
+ await pubClient.del(roomUniqueId);
|
|
|
|
+ await pubClient.del(`type-${roomUniqueId}`);
|
|
|
|
+ }, 1000);
|
|
|
|
+ } else {
|
|
|
|
+ const hosts = JSON.parse(existMaster);
|
|
|
|
+ console.log("存在房主", hosts.roomType);
|
|
|
|
+ if (hosts.roomType === "1v1" && values.length > 2) {
|
|
|
|
+ console.log("1v1-房间人数已满", values.length);
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ socket.emit("roomInFail", {
|
|
|
|
+ type: "full",
|
|
|
|
+ msg: "房间人数已满",
|
|
|
|
+ });
|
|
|
|
+ }, 1000);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export async function roomController(socket) {
|
|
|
|
+ let user = socket.handshake.query;
|
|
|
|
+ const { roomId, userId, sceneNum, isClient, role, userLimitNum, roomType } =
|
|
|
|
+ user;
|
|
|
|
+ if (user) {
|
|
|
|
+ const webRoomId = `${roomId}_${sceneNum}_web`;
|
|
|
|
+ const clientRoom = `${userId}${roomId}`;
|
|
|
|
+ const userUniqueId = `user-${userId}`;
|
|
|
|
+ const roomUniqueId = `room-${webRoomId}`;
|
|
|
|
+ if ("roomId" in user && "userId" in user) {
|
|
|
|
+ await pubClient.hSet(userUniqueId, user);
|
|
|
|
+ await pubClient.hSet(
|
|
|
|
+ roomUniqueId,
|
|
|
|
+ userUniqueId,
|
|
|
|
+ JSON.stringify({
|
|
|
|
+ role: role,
|
|
|
|
+ roomType,
|
|
|
|
+ userLimitNum: userLimitNum || "",
|
|
|
|
+ })
|
|
|
|
+ );
|
|
|
|
+ joinRoom({ clientRoom, userUniqueId, roomUniqueId, user }, socket);
|
|
|
|
+ }
|
|
|
|
+ watchRoomStatus(roomUniqueId, socket);
|
|
|
|
+
|
|
|
|
+ socket.on("disconnect", () => {
|
|
|
|
+ leaveRoom({ clientRoom, userUniqueId, roomUniqueId, socket });
|
|
|
|
+ // watchRoomWithMaster(roomUniqueId, socket);
|
|
|
|
+ });
|
|
|
|
+ socket.on("reconnect", () => {
|
|
|
|
+ console.log("reconnect");
|
|
|
|
+ // leaveRoom({ clientRoom, userUniqueId, roomUniqueId, socket });
|
|
|
|
+ // watchRoomWithMaster(roomUniqueId, socket);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+}
|