lib/MatchMaker.js
/**
     * Create or joins the client into a particular room
     *
     * The client doesn't join instantly because this method is called from the
     * match-making process. The client will request a new WebSocket connection
     * to effectively join into the room created/joined by this method.
     */
Create or joins the client into a particular room
The client doesn't join instantly because this method is called from the
match-making process. The client will request a new WebSocket connection
to effectively join into the room created/joined by this method.
Create or joins the client into a particular room The client doesn ' t join instantly because this method is called from the match - making process . The client will request a new WebSocket connection to effectively join into the room created / joined by this method .
onJoinRoomRequest(client, roomToJoin, clientOptions) {
        return __awaiter(this, void 0, void 0, function* () {
            const hasHandler = this.hasHandler(roomToJoin);
            let roomId;
            let isReconnect;
            if (!hasHandler && index_1.isValidId(roomToJoin)) {
                roomId = roomToJoin;
            }
            if (!hasHandler && !index_1.isValidId(roomToJoin)) {
                throw new Errors_1.MatchMakeError('join_request_fail');
            }
            if (clientOptions.sessionId) {
                isReconnect = yield this.presence.get(clientOptions.sessionId);
                if (isReconnect) {
                    roomId = isReconnect;
                }
            }
            if (!roomId || !clientOptions.sessionId) {
                clientOptions.sessionId = index_1.generateId();
                isReconnect = false;
                yield this.awaitRoomAvailable(roomToJoin);
                if (hasHandler) {
                    const bestRoomByScore = (yield this.getAvailableRoomByScore(roomToJoin, clientOptions))[0];
                    if (bestRoomByScore && bestRoomByScore.roomId) {
                        roomId = bestRoomByScore.roomId;
                    }
                }
            }
            if (index_1.isValidId(roomId)) {
                roomId = yield this.joinById(roomId, clientOptions, isReconnect);
            }
            if (!roomId && hasHandler) {
                roomId = this.create(roomToJoin, clientOptions);
            }
            if (roomId) {
                yield this.remoteRoomCall(roomId, '_reserveSeat', [{
                        id: client.id,
                        sessionId: clientOptions.sessionId,
                    }]);
            }
            else {
                throw new Errors_1.MatchMakeError(`Failed to join invalid room "${roomToJoin}"`);
            }
            return roomId;
        });
    }
onJoinRoomRequest ( client , roomToJoin , clientOptions ) { return __awaiter ( this , void 0 , void 0 , function * ( ) { const hasHandler = this . hasHandler ( roomToJoin ) ; let roomId ; let isReconnect ; if ( ! hasHandler && index_1 . isValidId ( roomToJoin ) ) { roomId = roomToJoin ; } if ( ! hasHandler && ! index_1 . isValidId ( roomToJoin ) ) { throw new Errors_1 . MatchMakeError ( 'join_request_fail' ) ; } if ( clientOptions . sessionId ) { isReconnect = yield this . presence . get ( clientOptions . sessionId ) ; if ( isReconnect ) { roomId = isReconnect ; } } if ( ! roomId || ! clientOptions . sessionId ) { clientOptions . sessionId = index_1 . generateId ( ) ; isReconnect = false ; yield this . awaitRoomAvailable ( roomToJoin ) ; if ( hasHandler ) { const bestRoomByScore = ( yield this . getAvailableRoomByScore ( roomToJoin , clientOptions ) ) [ 0 ] ; if ( bestRoomByScore && bestRoomByScore . roomId ) { roomId = bestRoomByScore . roomId ; } } } if ( index_1 . isValidId ( roomId ) ) { roomId = yield this . joinById ( roomId , clientOptions , isReconnect ) ; } if ( ! roomId && hasHandler ) { roomId = this . create ( roomToJoin , clientOptions ) ; } if ( roomId ) { yield this . remoteRoomCall ( roomId , '_reserveSeat' , [ { id : client . id , sessionId : clientOptions . sessionId , } ] ) ; } else { throw new Errors_1 . MatchMakeError ( ` ${ roomToJoin } ` ) ; } return roomId ; } ) ; }
Create or joins the client into a particular room
 The client doesn't join instantly because this method is called from the
 match-making process.
Create or joins the client into a particular room The client doesn ' t join instantly because this method is called from the match - making process .
// when multiple clients request to create a room simultaneously, we need // to wait for the first room to be created to prevent creating multiple of them // check if there's an existing room with provided name available to join // if couldn't join a room by its id, let's try to create a new one // reserve seat for client on selected room