Пример #1
0
        public async Task <RoomInfo> GetRoom(long id, long blind)
        {
            //如果没有任何一个game启动
            if (_roomGroups.Count == 0)
            {
                throw new Exception("have no game started");
            }

            RoomInfo roomInfo = null;

            //先查看已经匹配的队列是否有合适的房间
            if (!_matchingQueue.TryGetValue(blind, out var matchingRooms) || matchingRooms.Count == 0)
            {
                //查找空闲房间
                var oneGroup = GetLeastGroup();

                roomInfo = oneGroup.Value.GetEmptyRoom(blind);
                if (roomInfo == null)
                {
                    //创建新房间
                    roomInfo = await CreateRoom(oneGroup.Key, blind);

                    oneGroup.Value.CreatRoom(roomInfo);
                    _allRoomId.Add(roomInfo.RoomId);
                    roomInfo.AddUserCount(1);
                }
            }
            else
            {
                roomInfo = matchingRooms.First <RoomInfo>();
                matchingRooms.Remove(roomInfo);
                if (roomInfo.IsFull() || roomInfo.IsEmpty())
                {
                    throw new Exception("matchingRooms matching queue error");
                }
                roomInfo.AddUserCount(1);
            }

            InsertNewInfo(_roomGroups[roomInfo.GameKey], roomInfo);
            return(roomInfo);
        }