import client from './redisConnection' ; import Database from '../@types/databaseName' ; const setSocket = ( roomCode , socketId , socketInfo ) => { return new Promise < number > ( ( resolve , reject ) => { client . select ( Database . SOCKETS_IN_ROOM , ( ) => { client . hset ( [ roomCode , socketId , socketInfo ] , ( err , res ) => { if ( err ) reject ( err ) ; return resolve ( res ) ; } ) ; } ) ; } ) ; } ; const getSockets = ( roomCode ) => { return new Promise < { [ key ] : string ; } > ( ( resolve , reject ) => { client . select ( Database . SOCKETS_IN_ROOM , ( ) => { client . hgetall ( roomCode , ( err , res ) => { if ( err ) return reject ( err ) ; return resolve ( res ) ; } ) ; } ) ; } ) ; } ; const removeSocket = ( roomCode , socketId ) => { return new Promise < number > ( ( resolve , reject ) => { client . select ( Database . SOCKETS_IN_ROOM , ( ) => { client . hdel ( roomCode , socketId , ( err , res ) => { if ( err ) return reject ( err ) ; return resolve ( res ) ; } ) ; } ) ; } ) ; } ; const isRoomEmpty = ( roomCode ) => { return new Promise < boolean > ( ( resolve , reject ) => { client . select ( Database . SOCKETS_IN_ROOM , ( ) => { client . keys ( '*' , ( err , res ) => { if ( err ) return reject ( err ) ; if ( res . includes ( roomCode ) ) return resolve ( false ) ; return resolve ( true ) ; } ) ; } ) ; } ) ; } ; const getSocketCountInRoom = ( roomCode ) => { return new Promise < number > ( ( resolve , reject ) => { client . select ( Database . SOCKETS_IN_ROOM , ( ) => { client . hlen ( roomCode , ( err , res ) => { if ( err ) return reject ( err ) ; return resolve ( res ) ; } ) ; } ) ; } ) ; } ; const getSocket = ( roomCode , socketId ) => { return new Promise < string > ( ( resolve , reject ) => { client . select ( Database . SOCKETS_IN_ROOM , ( ) => { client . hget ( roomCode , socketId , ( err , res ) => { if ( err ) return reject ( err ) ; return resolve ( res ) ; } ) ; } ) ; } ) ; } ; const flushAll = ( ) => { return new Promise < string > ( ( resolve , reject ) => { client . select ( Database . SOCKETS_IN_ROOM , ( ) => { client . flushall ( ( err , res ) => { if ( err ) return reject ( err ) ; return resolve ( res ) ; } ) ; } ) ; } ) ; } ; const socketsInRoom = { setSocket , getSockets , removeSocket , isRoomEmpty , getSocketCountInRoom , getSocket , flushAll , } ; export default socketsInRoom ;