//开始游戏 并创建房间记录 返回房间ID
        private static void RoomStartGame(this MatchRoomComponent matchRoomComponent, long toyGameId, M2S_StartGame message)
        {
            try
            {
                //记录玩家信息
                MatchRoom matchRoom = MatchRoomFactory.CreateRandomMatchRoom(toyGameId, matchRoomComponent.RandomRoomId(), message.MatchPlayerInfos, message.RoomConfig);
                matchRoomComponent.MatchRoomDic[matchRoom.RoomId] = matchRoom;
                for (int i = 0; i < message.MatchPlayerInfos.count; i++)
                {
                    if (message.MatchPlayerInfos[i].IsAI)
                    {
                        continue;//如果是AI不记录
                    }
                    matchRoomComponent.UserIdInRoomIdDic[message.MatchPlayerInfos[i].User.UserId] = matchRoom;
                }

                message.RoomType = RoomType.Match;
                Session toyGameSession = Game.Scene.GetComponent <NetInnerSessionComponent>().GetGameServerSession(toyGameId);
                message.RoomId = matchRoom.RoomId;
                toyGameSession.Send(message);
            }
            catch (Exception e)
            {
                Log.Error(e);
                throw;
            }
        }
Example #2
0
        protected override void Run(Session session, MP2MH_PlayerExitRoom_Req message, Action <MH2MP_PlayerExitRoom_Ack> reply)
        {
            MH2MP_PlayerExitRoom_Ack response = new MH2MP_PlayerExitRoom_Ack();

            try
            {
                MatchRoomComponent matchRoomComponent = Game.Scene.GetComponent <MatchRoomComponent>();
                Room room = matchRoomComponent.Get(message.RoomID);

                //移除玩家对象
                Gamer gamer = room.Remove(message.UserID);
                Game.Scene.GetComponent <MatchComponent>().Playing.Remove(gamer.UserID);
                gamer.Dispose();

                Log.Info($"Match:同步玩家{message.UserID}退出房间");

                if (room.Count == 0)
                {
                    //当房间中没有玩家时回收
                    matchRoomComponent.Recycle(room.Id);
                    Log.Info($"回收房间{room.Id}");
                }

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
        /// <summary>
        /// 回收匹配房间
        /// </summary>
        /// <param name="self"></param>
        /// <param name="id"></param>
        public static void Recycle(this MatchRoomComponent self, long id)
        {
            Room room = self.readyRooms[id];

            self.readyRooms.Remove(room.Id);
            self.idleRooms.Enqueue(room);
        }
        public async Task Handle(Session session, Entity entity, ActorRequest message)
        {
            if (message.AMessage is AFrameMessage aFrameMessage)
            {
                // 客户端发送不需要设置Frame消息的id,在这里统一设置,防止客户端被破解发个假的id过来
                aFrameMessage.Id = entity.Id;
                Unit unit = entity as Unit;

                MatchRoomComponent roomComponent = Game.Scene.GetComponent <MatchRoomComponent>();
                MatchRoom          matchroom     = roomComponent.Get(unit.RoomID);
                if (matchroom != null)
                {
                    matchroom.Add(aFrameMessage);
                }

                //Game.Scene.GetComponent<ServerFrameComponent>().Add(aFrameMessage);
                ActorResponse response = new ActorResponse
                {
                    RpcId = message.RpcId
                };
                session.Reply(response);
                return;
            }
            await Game.Scene.GetComponent <ActorMessageDispatherComponent>().Handle(session, entity, message);
        }
 public static int JoinMatchQueue(this MatchRoomComponent matchRoomComponent, long matchRoomId, User user, IResponse iResponse)
 {
     try
     {
         if (matchRoomComponent.mUserInQueue.ContainsKey(user.UserId))
         {
             iResponse.Message = "用户已经在队列里面";
             return(1);//用户已经在队列里面
         }
         else
         {
             if (!matchRoomComponent.mAllQueueDics.ContainsKey(matchRoomId))
             {
                 iResponse.Message = "要加入的匹配房间不存在";
                 return(2);//要加入的匹配房间不存在
             }
             MatchRoomConfig matchRoomConfig = Game.Scene.GetComponent <GameMatchRoomConfigComponent>().GetMatachRoomInfo(matchRoomId);
             if (user.Beans < matchRoomConfig.BesansLowest)
             {
                 iResponse.Message = "豆子不足";
                 return(2);//豆子数不足
             }
             matchRoomComponent.mUserInQueue.Add(user.UserId, matchRoomId);
             matchRoomComponent.mAllQueueDics[matchRoomId].Add(user);
             iResponse.Message = string.Empty;
             return(0);//成功加入
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
         throw;
     }
 }
 //玩家下线
 public static void PlayerOffline(this MatchRoomComponent matchRoomComponent, long userId)
 {
     if (matchRoomComponent.UserIdInRoomIdDic.ContainsKey(userId))
     {
         matchRoomComponent.UserIdInRoomIdDic[userId].PlayerOffline(userId);
     }
 }
 public static int JoinMatchQueue(this MatchRoomComponent matchRoomCom, long matchRoomId, User user, IResponse response)
 {
     try
     {
         if (matchRoomCom.mUserQueue.ContainsKey(user.UserId))
         {
             response.Message = "用户已经在队列中";
             return(1);
         }
         else
         {
             if (!matchRoomCom.mAllQueue.ContainsKey(matchRoomId))
             {
                 response.Message = "要加入的匹配房间不存在";
                 return(2);
             }
             matchRoomCom.mUserQueue.Add(user.UserId, matchRoomId);
             matchRoomCom.mAllQueue[matchRoomId].Add(user);
             response.Message = string.Empty;
             return(0);
         }
     }
     catch (System.Exception ex)
     {
         Log.Error(ex);
         throw;
     }
 }
        /// <summary>
        /// 匹配房间结束游戏
        /// </summary>
        /// <param name="self"></param>
        /// <param name="id"></param>
        public static void RoomEndGame(this MatchRoomComponent self, long id)
        {
            Room room = self.gameRooms[id];

            self.gameRooms.Remove(id);
            self.readyRooms.Add(room.Id, room);
        }
        //玩家加入房间
        public static void JoinRoom(this MatchRoomComponent matchRoomComponent, int roomId, User user, long sessionActorId, IResponse iResponse)
        {
            if (matchRoomComponent.UserIdInRoomIdDic.ContainsKey(user.UserId))
            {
                //玩家已经在其他房间中
                iResponse.Message = "正在其他房间游戏中";
                return;
            }

            MatchRoom matchRoom = matchRoomComponent.GetRoom(roomId);

            if (matchRoom != null)
            {
                if (matchRoom.UserJoinRoom(user, sessionActorId))
                {
                    //玩家成功加入
                    matchRoomComponent.UserIdInRoomIdDic[user.UserId]      = matchRoom;
                    user.AddComponent <UserGateActorIdComponent>().ActorId = sessionActorId;
                    iResponse.Message = string.Empty;
                    return;
                }
                else
                {
                    //玩家加入失败 房间已满
                    iResponse.Message = "房间已满";
                    return;
                }
            }
            //房间不存在
            iResponse.Message = "房间不存在";
        }
Example #10
0
        /// <summary>
        /// 获取匹配房间
        /// </summary>
        /// <param name="self"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Room Get(this MatchRoomComponent self, long id)
        {
            Room room;

            self.rooms.TryGetValue(id, out room);
            return(room);
        }
 //一个用户退出匹配队列
 public static void RmoveQueueUser(this MatchRoomComponent matchRoomComponent, long userId)
 {
     try
     {
         if (matchRoomComponent.mUserInQueue.ContainsKey(userId))
         {
             User        userIdUser = null;
             List <User> userLists  = matchRoomComponent.mAllQueueDics[matchRoomComponent.mUserInQueue[userId]];
             foreach (var user in userLists)
             {
                 if (user.UserId == userId)
                 {
                     userIdUser = user;
                 }
             }
             if (userIdUser != null)
             {
                 userLists.Remove(userIdUser);
             }
             matchRoomComponent.mUserInQueue.Remove(userId);
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
         throw;
     }
 }
        //房间投票计时
        public static async void RoomVoteOvertime(this MatchRoomComponent self)
        {
            while (true)
            {
                await Game.Scene.GetComponent <TimerComponent>().WaitAsync(1000);

                VoteOverTimeFinshRooms.Clear();
                for (int i = 0; i < self.VoteInRooms.Count; i++)
                {
                    self.VoteInRooms[i].VoteTimeResidue--;
                    if (self.VoteInRooms[i].VoteTimeResidue <= 0)
                    {
                        self.VoteInRooms[i].RoomVoteOverTime();
                        VoteOverTimeFinshRooms.Add(self.VoteInRooms[i]);
                    }
                }
                for (int i = 0; i < VoteOverTimeFinshRooms.Count; i++)
                {
                    if (self.VoteInRooms.Contains(VoteOverTimeFinshRooms[i]))
                    {
                        self.VoteInRooms.Remove(VoteOverTimeFinshRooms[i]);
                    }
                }
            }
        }
Example #13
0
 //玩家聊天
 public static bool UserChat(this MatchRoomComponent matchRoomComponent, long userId, ChatInfo chatInfo)
 {
     if (matchRoomComponent.UserIdInRoomIdDic.ContainsKey(userId))
     {
         return(matchRoomComponent.UserIdInRoomIdDic[userId].UserChat(userId, chatInfo));//广播聊天信息
     }
     return(false);
 }
        //得到房间
        public static MatchRoom GetRoom(this MatchRoomComponent matchRoomComponent, int roomId)
        {
            MatchRoom matchRoom;

            if (matchRoomComponent.MatchRoomDic.TryGetValue(roomId, out matchRoom))
            {
            }
            return(matchRoom);
        }
        //得到房间
        public static MatchRoom GetRoomUserIdIn(this MatchRoomComponent matchRoomComponent, long userId)
        {
            MatchRoom matchRoom;

            if (matchRoomComponent.UserIdInRoomIdDic.TryGetValue(userId, out matchRoom))
            {
            }
            return(matchRoom);
        }
        //检测一个房间是否能开始游戏
        public static void DetetionRoomStartGame(this MatchRoomComponent matchRoomComponent, int roomId)
        {
            MatchRoom matchRoom = matchRoomComponent.GetRoom(roomId);

            if (matchRoom != null && !matchRoom.IsGameBeing && matchRoom.DetetionMayStartGame())
            {
                matchRoom.StartGame();//开始游戏
            }
        }
        //随机一个房间号 不与之前的重复
        public static int RandomRoomId(this MatchRoomComponent matchRoomComponent)
        {
            int roomId = RandomTool.Random(10000, 99999);

            while (matchRoomComponent.MatchRoomDic.ContainsKey(roomId))
            {
                roomId = RandomTool.Random(10000, 99999);
                Log.Info("随机生成房间号:" + roomId);
            }
            return(roomId);
        }
 public static bool IsUserInGame(this MatchRoomComponent matchRoomCom, long userId, long sessionActorId)
 {
     if (matchRoomCom.UserIdInRoomDic.ContainsKey(userId))
     {
         ActorHelp.SendActor(sessionActorId, new Actor_BeingInGame()
         {
             IsGameBeing = true
         });
         return(true);
     }
     return(false);
 }
        //创建房间
        public static MatchRoom CreateRoom(this MatchRoomComponent matchRoomComponent, RepeatedField <int> roomConfigLists, int friendsCircleId, long toyGameId, long userJewelNum, IResponse iResponse)
        {
            MatchRoom matchRoom = MatchRoomFactory.Create(roomConfigLists, toyGameId, matchRoomComponent.RandomRoomId(), friendsCircleId, userJewelNum, iResponse);

            if (matchRoom == null)
            {
                return(null);//为空表示创建房间失败
            }
            matchRoomComponent.MatchRoomDic.Add(matchRoom.RoomId, matchRoom);
            matchRoomComponent.AddFriendsCircleRoom(matchRoom);
            return(matchRoom);
        }
 //判断用户在不在游戏 如果在 就通知客户端玩家在游戏中
 public static bool JudgeUserIsGameIn(this MatchRoomComponent matchRoomComponent, long userId, long userSessionActorId)
 {
     if (matchRoomComponent.UserIdInRoomIdDic.ContainsKey(userId))
     {
         ActorHelp.SendeActor(userSessionActorId, new Actor_BeingInGame()
         {
             IsGameBeing = true
         });                                                                                      //通知客户端 用户在游戏中
         return(true);
     }
     return(false);
 }
 //亲友房间结束
 public static void DestroyFriendsCircleRoom(this MatchRoomComponent matchRoomComponent, MatchRoom matchRoom)
 {
     if (matchRoom.FriendsCircleId != 0)
     {
         if (matchRoomComponent.FriendsCircleInMatchRoomDic.ContainsKey(matchRoom.FriendsCircleId))
         {
             if (matchRoomComponent.FriendsCircleInMatchRoomDic[matchRoom.FriendsCircleId].Contains(matchRoom))
             {
                 matchRoomComponent.FriendsCircleInMatchRoomDic[matchRoom.FriendsCircleId].Remove(matchRoom);
             }
         }
     }
 }
Example #22
0
 /// <summary>
 /// 获取空闲的匹配房间
 /// </summary>
 /// <param name="self"></param>
 /// <returns></returns>
 public static Room GetIdleRoom(this MatchRoomComponent self)
 {
     if (self.IdleRoomCount > 0)
     {
         Room room = self.idleRooms.Dequeue();
         self.readyRooms.Add(room.Id, room);
         return(room);
     }
     else
     {
         return(null);
     }
 }
        protected override async void Run(Session session, C2M_CreateRoom message, Action <M2C_CreateRoom> reply)
        {
            M2C_CreateRoom response = new M2C_CreateRoom();

            try
            {
                MatchRoomComponent matchRoomComponent = Game.Scene.GetComponent <MatchRoomComponent>();
                //判断玩家 是否在其他游戏中
                if (matchRoomComponent.JudgeUserIsGameIn(message.UserId, message.SessionActorId))
                {
                    return;
                }
                int  needJeweNumCount  = 0;
                long userJewel         = message.User.Jewel;
                User friendsCreateUser = null;
                if (message.FriendsCircleId > 0)
                {
                    friendsCreateUser = await  GetFriendsCircleCreateUser(message.FriendsCircleId, response);

                    if (friendsCreateUser == null)
                    {
                        reply(response);
                        return;
                    }
                    userJewel = friendsCreateUser.Jewel;
                }
                MatchRoom room = matchRoomComponent.CreateRoom(message.RoomConfigLists, message.FriendsCircleId, message.ToyGameId, userJewel, response);
                if (room == null)
                {
                    reply(response);
                    return;
                }
                if (friendsCreateUser != null)
                {
                    //记录亲友圈 主人的id
                    room.FriendsCreateUserId = friendsCreateUser.UserId;
                }

                //创建者加入房间
                matchRoomComponent.JoinRoom(room.RoomId, message.User, message.SessionActorId, response);
                response.RoomInfo = RoomInfoFactory.Creator(matchRoomComponent.GetRoomUserIdIn(message.UserId));
                reply(response);
                RoomInfoFactory.Destroy(response.RoomInfo);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
        protected override async void Run(Session session, G2M_PlayerEnterMatch message, Action <M2G_PlayerEnterMatch> reply)
        {
            M2G_PlayerEnterMatch response = new M2G_PlayerEnterMatch();

            try
            {
                MatchComponent      matchComponent      = Game.Scene.GetComponent <MatchComponent>();
                ActorProxyComponent actorProxyComponent = Game.Scene.GetComponent <ActorProxyComponent>();

                if (matchComponent.Playing.ContainsKey(message.UserID))
                {
                    //todo 重连逻辑
                    MatchRoomComponent matchRoomComponent = Game.Scene.GetComponent <MatchRoomComponent>();
                    long  roomId = matchComponent.Playing[message.UserID];
                    Room  room   = matchRoomComponent.Get(roomId);
                    Gamer gamer  = room.Get(message.UserID);

                    //重置GateActorID
                    gamer.PlayerID = message.PlayerID;

                    //重连房间
                    ActorProxy actorProxy = actorProxyComponent.Get(roomId);
                    await actorProxy.Call(new MH2MP_PlayerEnterRoom()
                    {
                        PlayerID  = message.PlayerID,
                        UserID    = message.UserID,
                        SessionID = message.SessionID
                    });

                    //向玩家发送匹配成功消息
                    ActorProxy gamerActorProxy = actorProxyComponent.Get(gamer.PlayerID);
                    gamerActorProxy.Send(new M2G_MatchSucess()
                    {
                        GamerID = gamer.Id
                    });
                }
                else
                {
                    //创建匹配玩家
                    Matcher matcher = MatcherFactory.Create(message.PlayerID, message.UserID, message.SessionID);
                }

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Example #25
0
        protected override void Run(Session session, C2M_OutRoom message, Action <M2C_OutRoom> reply)
        {
            M2C_OutRoom response = new M2C_OutRoom();

            try
            {
                MatchRoomComponent matchRoomComponent = Game.Scene.GetComponent <MatchRoomComponent>();
                matchRoomComponent.OutRoom(message.UserId, response);
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
 //移除对应的人都是从数组开头开始移除
 private static void PlayerStartGame(this MatchRoomComponent matchRoomComponent, long matchRoomId, int number)
 {
     try
     {
         for (int i = 0; i < number; i++)
         {
             matchRoomComponent.mUserInQueue.Remove(matchRoomComponent.mAllQueueDics[matchRoomId][0].UserId);
             matchRoomComponent.mAllQueueDics[matchRoomId].RemoveAt(0);
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
         throw;
     }
 }
 private static void MatchStartGame(this MatchRoomComponent matchRoomCom, long roomId, int number)
 {
     try
     {
         for (int i = 0; i < number; i++)
         {
             matchRoomCom.mUserQueue.Remove(matchRoomCom.mAllQueue[roomId][0].UserId);
             matchRoomCom.mAllQueue[roomId].RemoveAt(0);
         }
     }
     catch (System.Exception ex)
     {
         Log.Error(ex);
         throw;
     }
 }
        public static void Update(this MatchComponent self)
        {
            while (true)
            {
                MatcherComponent   matcherComponent = Game.Scene.GetComponent <MatcherComponent>();
                Queue <Matcher>    matchers         = new Queue <Matcher>(matcherComponent.GetAll());
                MatchRoomComponent roomManager      = Game.Scene.GetComponent <MatchRoomComponent>();
                Room room = roomManager.GetReadyRoom();

                if (matchers.Count == 0)
                {
                    //当没有匹配玩家时直接结束
                    break;
                }

                if (room == null && matchers.Count >= 3)
                {
                    //当还有一桌匹配玩家且没有可加入房间时使用空房间
                    room = roomManager.GetIdleRoom();
                }

                if (room != null)
                {
                    //当有准备状态房间且房间还有空位时匹配玩家直接加入填补空位
                    while (matchers.Count > 0 && room.Count < 3)
                    {
                        self.JoinRoom(room, matcherComponent.Remove(matchers.Dequeue().UserID));
                    }
                }
                else if (matchers.Count >= 3)
                {
                    //当还有一桌匹配玩家且没有空房间时创建新房间
                    self.CreateRoom();
                    break;
                }
                else
                {
                    break;
                }

                //移除匹配成功玩家
                while (self.MatchSuccessQueue.Count > 0)
                {
                    matcherComponent.Remove(self.MatchSuccessQueue.Dequeue().UserID);
                }
            }
        }
 //添加亲友圈房间房间
 public static void AddFriendsCircleRoom(this MatchRoomComponent matchRoomComponent, MatchRoom matchRoom)
 {
     if (matchRoom.FriendsCircleId != 0)
     {
         if (matchRoomComponent.FriendsCircleInMatchRoomDic.ContainsKey(matchRoom.FriendsCircleId))
         {
             matchRoomComponent.FriendsCircleInMatchRoomDic[matchRoom.FriendsCircleId].Add(matchRoom);
         }
         else
         {
             matchRoomComponent.FriendsCircleInMatchRoomDic[matchRoom.FriendsCircleId] = new List <MatchRoom>()
             {
                 matchRoom
             };
         }
     }
 }
        protected override void Run(Session session, MP2MH_SyncRoomState message)
        {
            MatchRoomComponent matchRoomComponent = Game.Scene.GetComponent <MatchRoomComponent>();
            Room room = matchRoomComponent.Get(message.RoomID);

            //同步房间状态
            switch (message.State)
            {
            case RoomState.Game:
                matchRoomComponent.RoomStartGame(room.Id);
                Log.Info($"房间{room.Id}切换为游戏状态");
                break;

            case RoomState.Ready:
                Log.Info($"房间{room.Id}切换为准备状态");
                matchRoomComponent.RoomEndGame(room.Id);
                break;
            }
        }