Ejemplo n.º 1
0
        void create(SelectModel[] teamOne, SelectModel[] teamTwo)
        {
            FightRoom room;

            if (!cache.TryPop(out room))
            {
                room = new FightRoom();
                room.SetArea(index.GetAdd());
            }
            //房间数据初始化
            room.init(teamOne, teamTwo);
            //绑定映射关系
            foreach (SelectModel item in teamOne)
            {
                userRoom.TryAdd(item.userID, room.Area);
            }
            foreach (SelectModel item in teamTwo)
            {
                userRoom.TryAdd(item.userID, room.Area);
            }
            roomMap.TryAdd(room.Area, room);
        }
Ejemplo n.º 2
0
        public void create(List <int> teamOne, List <int> teamTwo)
        {
            SelectRoom room;

            //如果缓存中没有房间对象
            if (!cache.TryPop(out room))
            {
                room = new SelectRoom();
                //添加唯一标识码
                room.SetArea(index.GetAdd());
            }
            //内部初始化房间
            room.init(teamOne, teamTwo);
            //item 队伍中角色id号
            foreach (int item in teamOne)
            {
                userRoom.TryAdd(item, room.Area);
            }
            foreach (int item in teamTwo)
            {
                userRoom.TryAdd(item, room.Area);
            }
            roomMap.TryAdd(room.Area, room);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 进入房间逻辑
        /// </summary>
        /// <param name="token"></param>
        private void enter(UserToken token)
        {
            int userId = getUserID(token);

            //Console.WriteLine("客户端开始匹配"+userId);
            //判断玩家是否在匹配队伍中
            if (!userRoom.ContainsKey(userId))//玩家没有在队伍内
            {
                MatchRoom room = null;
                //判断队伍等待队列中是否有匹配队列
                if (roomMap.Count > 0)
                {
                    //Console.WriteLine("存在匹配房间...");
                    foreach (MatchRoom item in roomMap.Values)//遍历所有的匹配房间
                    {
                        //匹配队伍人员未满
                        if (item.teamMax * 2 > item.teamOne.Count + item.teamTwo.Count)
                        {
                            room = item;
                            if (room.teamOne.Count < room.teamMax)
                            {
                                room.teamOne.Add(userId);
                            }
                            else if (room.teamTwo.Count < room.teamMax)
                            {
                                room.teamTwo.Add(userId);
                                //Console.WriteLine("加入队伍2....");
                            }
                            //匹配到队伍
                            userRoom.TryAdd(userId, room.id);
                            break;
                        }
                    }
                    //等待房间已经满员
                    if (!userRoom.ContainsKey(userId))
                    {
                        if (cache.Count > 0)
                        {
                            cache.TryPop(out room);
                            room.teamOne.Add(userId);
                            userRoom.TryAdd(userId, room.id);
                            roomMap.TryAdd(room.id, room);
                        }
                        else
                        {
                            //Console.WriteLine("客户端创建房间" + userId);
                            room    = new MatchRoom();
                            room.id = index.GetAdd();
                            room.teamOne.Add(userId);
                            userRoom.TryAdd(userId, room.id);
                            roomMap.TryAdd(room.id, room);
                        }
                    }
                }
                else
                {
                    //等待中的房间已经全部满员
                    if (cache.Count > 0)
                    {
                        cache.TryPop(out room);
                        room.teamOne.Add(userId);
                        userRoom.TryAdd(userId, room.id);
                        roomMap.TryAdd(room.id, room);
                    }
                    else
                    {
                        //Console.WriteLine("客户端创建房间" + userId);
                        room    = new MatchRoom();
                        room.id = index.GetAdd();
                        room.teamOne.Add(userId);
                        userRoom.TryAdd(userId, room.id);
                        roomMap.TryAdd(room.id, room);
                    }
                }

                //进入房间成功 判断房间是否满员 满员则将房间丢进缓存队列
                if (room.teamOne.Count == room.teamMax && room.teamTwo.Count == room.teamMax)
                {
                    //Console.WriteLine("匹配队伍满员...."+getUserName(room.teamOne[0])+" : "+getUserName(room.teamTwo[0]));
                    //匹配队伍满员后  在服务器端对选择房间进行初始化
                    EventUtil.createSelect(room.teamOne, room.teamTwo);
                    //通知队伍类所有玩家进行选人
                    Console.WriteLine("可以进入选人界面..");
                    writeToUsers(room.teamOne.ToArray(), Type, 0, MatchProtocol.ENTER_SELECT_BRO, null);
                    writeToUsers(room.teamTwo.ToArray(), Type, 0, MatchProtocol.ENTER_SELECT_BRO, null);

                    //移除玩家与房间的映射
                    foreach (int item in room.teamOne)
                    {
                        int id;
                        userRoom.TryRemove(item, out id);
                    }
                    foreach (int item in room.teamTwo)
                    {
                        int id;
                        userRoom.TryRemove(item, out id);
                    }
                    room.teamOne.Clear();
                    room.teamTwo.Clear();
                    roomMap.TryRemove(room.id, out room);
                    cache.Push(room);
                }
            }
        }