/// <summary>
        /// Creates room and adds to active rooms
        /// </summary>
        private static RoomInstance CreateNewRoomInstance(long id)
        {
            RoomInstance room = new RoomInstance(id);

            ActiveRooms.Add(id, room);
            Log.WriteLine("Created a new room. Id: " + id, typeof(RoomManager));
            return(room);
        }
        public static void CoverCardOnTable(long connectionId, string cardCodeOnTable, string cardCodeDropped)
        {
            RoomInstance room = GetRoomForPlayer(connectionId);

            if (room.ContainsPlayer(connectionId))
            {
                room.CoverCardOnTable(connectionId, cardCodeOnTable, cardCodeDropped);
            }
        }
        public static void Pass(long connectionId)
        {
            RoomInstance room = GetRoomForPlayer(connectionId);

            if (room.ContainsPlayer(connectionId))
            {
                room.Pass(connectionId);
            }
        }
        public static void GetNotReady(long connectionId)
        {
            RoomInstance room = GetRoomForPlayer(connectionId);

            if (room.ContainsPlayer(connectionId))
            {
                room.GetNotReady(connectionId);
            }
        }
        /// <summary>
        /// If client's connection was destroyed we need make other players to wait him.
        /// This is different situation to when client itself wants to quit or give up.
        /// </summary>
        public static void OnClientDisconnectedSuddenly(long connectionId)
        {
            RoomInstance room = GetRoomForPlayer(connectionId);

            if (room != null)
            {
                room.OnClientDisconnectedSuddenly(connectionId);
            }
        }
        /// <summary>
        /// Called by client who leaves a game
        /// </summary>
        /// <param name="connectionId">Player's connection id</param>
        public static void LeaveRoom(long connectionId)
        {
            RoomInstance room = GetRoomForPlayer(connectionId);

            if (room == null)
            {
                return;
            }

            if (room.ContainsPlayer(connectionId))
            {
                room.LeaveRoom(connectionId);
            }
        }
        private static RoomInstance CreateRandomRoom(long id)
        {
            // randomize room parameters
            Random r = new Random();
            int    randomMaxPlayers = r.Next(2, 5);           // 2 - 4 players in random room
            int    randomDeckSize   = 24 + 16 * r.Next(0, 3); //24-36-52 cards

            // create room
            RoomInstance room = CreateNewRoomInstance(id);

            room.MaxPlayers = randomMaxPlayers;
            room.DeckSize   = randomDeckSize;

            Log.WriteLine("Created a random room. Id: " + id, typeof(RoomManager));
            return(room);
        }
        /// <summary>
        /// Sent by player how wants to create a new room
        /// </summary>
        /// <param name="connectionId">Room creator connection id</param>
        /// <param name="maxPlayers">Max players in room</param>
        /// <param name="deckSize">Deck size in room</param>
        public static void CreateRoom(long connectionId, int maxPlayers, int deckSize, double bet)
        {
            var client = ClientManager.GetConnectedClient(connectionId);

            Log.WriteLine("[" + client + "] wants to create room.", typeof(RoomManager));

            if (client.IsInRoom)
            {
                Log.WriteLine("[" + client + "] is already in room. Abort.", typeof(RoomManager));
                return;
            }

            //Validate
            if (maxPlayers < 2 || maxPlayers > 6 ||
                !(deckSize == 24 || deckSize == 36 || deckSize == 52))
            {
                //Send incorrect room
                return;
            }

            RoomInstance room = CreateNewRoomInstance(connectionId);

            room.MaxPlayers = maxPlayers;
            room.DeckSize   = deckSize;
#if TEST_MODE_TWO_CARDS
            room.DeckSize = 4;
#endif
            room.HostId = connectionId;
            room.Bet    = bet;


            //Client joins random room
            if (room.JoinRoom(connectionId))
            {
                //Send 'OK' if room has free slots
                ServerSendPackets.Send_JoinRoomOk(connectionId, room.RoomId);
            }
        }
        /// <summary>
        /// Adds player to room
        /// </summary>
        public static bool JoinRoom(long connectionId, long roomId)
        {
            //Getting not-full rooms
            var availableRooms = GetAvailableRooms().ToArray();

            RoomInstance roomToJoin = availableRooms.Single(room => room.RoomId == roomId);

            //If this room is not present
            if (roomToJoin == null)
            {
                //todo send fail to join
                //ServerSendPackets.Send_FaliToJoin(connectionId);
                return(false);
            }

            if (roomToJoin.JoinRoom(connectionId))
            {
                //Send 'OK' if room has free slots
                ServerSendPackets.Send_JoinRoomOk(connectionId, roomToJoin.RoomId);
                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Delete room from list and dispose
 /// </summary>
 public static void DeleteRoom(RoomInstance room)
 {
     ActiveRooms.Remove(room.RoomId);
     room.Dispose();
 }