Ejemplo n.º 1
0
        /// <summary>
        /// 进入匹配队列 进入匹配房间
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public MatchRoom Enter(int userId, ClientPeer client)
        {
            int roomId;

            //遍历一下等待的房间 看一下有没有正在等待的 如果有 我们就把这玩家加进去
            foreach (MatchRoom mr in idModelDict.Values)
            {
                //房间满了 继续
                if (mr.IsFull())
                {
                    continue;
                }
                //没满的话
                mr.Enter(userId, client);
                uidRoomIdDict.Add(userId, mr.Id);
                return(mr);
            }
            //如果调用到这里 代表没进去 ,因为没有等待的房间了
            //自己开个房
            MatchRoom room = null;

            //判断一下是否有重用的房间
            if (roomQueue.Count > 0)
            {
                room = roomQueue.Dequeue();
                room.Enter(userId, client);
                idModelDict.Add(room.Id, room);
                uidRoomIdDict.Add(userId, room.Id);
                //1.给房间添加计时    2.计时到时前10秒的警告
                timer.AddTimeEvent(500000000,
                                   delegate
                {
                    room.Brocast(OpCode.MATCH, MatchCode.MESSAGE_BRO, null);
                });
                timer.AddTimeEvent(600000000,
                                   delegate
                {
                    if (room == null)
                    {
                        return;
                    }
                    room.Brocast(OpCode.MATCH, MatchCode.BACK_BRO, null);
                    Destroy(room);
                }
                                   );
            }
            else
            {
                room   = new MatchRoom(id.Add_Get());
                roomId = room.Id;
                room.Enter(userId, client);
                idModelDict.Add(room.Id, room);
                uidRoomIdDict.Add(userId, room.Id);
                //1.给房间添加计时    2.计时到时前10秒的警告
                timer.AddTimeEvent(500000000,
                                   delegate
                {
                    room.Brocast(OpCode.MATCH, MatchCode.MESSAGE_BRO, null);
                });
                timer.AddTimeEvent(600000000,
                                   delegate
                {
                    if (room == null)
                    {
                        return;
                    }
                    room.Brocast(OpCode.MATCH, MatchCode.BACK_BRO, null);
                    Destroy(room);
                }
                                   );
            }

            return(room);
        }