示例#1
0
        public static async void Start(this TrusteeshipComponent self)
        {
            //玩家所在房间
            Room room = Game.Scene.GetComponent <RoomComponent>().Get(self.GetParent <Gamer>().RoomID);
            OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();
            Gamer gamer           = self.GetParent <Gamer>();
            bool  isStartPlayCard = false;

            while (true)
            {
                await Game.Scene.GetComponent <TimerComponent>().WaitAsync(1000);

                if (self.IsDisposed)
                {
                    return;
                }

                if (gamer.UserID != orderController?.CurrentAuthority)
                {
                    continue;
                }

                //自动出牌开关,用于托管延迟出牌
                isStartPlayCard = !isStartPlayCard;
                if (isStartPlayCard)
                {
                    continue;
                }

                ActorMessageSender actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(gamer.Id);
                //当还没抢地主时随机抢地主
                if (gamer.GetComponent <HandCardsComponent>().AccessIdentity == Identity.None)
                {
                    int randomSelect = RandomHelper.RandomNumber(0, 2);
                    actorProxy.Send(new Actor_GamerGrabLandlordSelect_Ntt()
                    {
                        IsGrab = randomSelect == 0
                    });
                    self.Playing = false;
                    continue;
                }

                //自动提示出牌
                Actor_GamerPrompt_Ack response = await actorProxy.Call(new Actor_GamerPrompt_Req()) as Actor_GamerPrompt_Ack;

                if (response.Error > 0 || response.Cards == null)
                {
                    actorProxy.Send(new Actor_GamerDontPlay_Ntt());
                }
                else
                {
                    await actorProxy.Call(new Actor_GamerPlayCard_Req()
                    {
                        Cards = response.Cards
                    });
                }
            }
        }
        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="self"></param>
        /// <param name="room"></param>
        /// <param name="matcher"></param>
        public static async void JoinRoom(this MatchComponent self, Room room, Matcher matcher)
        {
            //玩家加入房间,移除匹配队列
            self.Playing[matcher.UserID] = room.Id;
            self.MatchSuccessQueue.Enqueue(matcher);

            //向房间服务器发送玩家进入请求
            ActorMessageSender        actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(room.Id);
            Actor_PlayerEnterRoom_Ack actor_PlayerEnterRoom_Ack = await actorProxy.Call(new Actor_PlayerEnterRoom_Req()
            {
                PlayerID  = matcher.PlayerID,
                UserID    = matcher.UserID,
                SessionID = matcher.GateSessionID
            }) as Actor_PlayerEnterRoom_Ack;

            Gamer gamer = GamerFactory.Create(matcher.PlayerID, matcher.UserID, actor_PlayerEnterRoom_Ack.GamerID);

            room.Add(gamer);

            //向玩家发送匹配成功消息
            ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
            ActorMessageSender          gamerActorProxy     = actorProxyComponent.Get(gamer.PlayerID);

            gamerActorProxy.Send(new Actor_MatchSucess_Ntt()
            {
                GamerID = gamer.Id
            });
        }
        protected override async void Run(Session session, C2G_ReturnLobby_Ntt message)
        {
            //验证Session
            if (!GateHelper.SignSession(session))
            {
                return;
            }

            User user = session.GetComponent <SessionUserComponent>().User;
            //StartConfigComponent config = Game.Scene.GetComponent<StartConfigComponent>();
            ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

            //正在匹配中发送玩家退出匹配请求
            //if (user.IsMatching)
            //{
            //    IPEndPoint matchIPEndPoint = config.MatchConfig.GetComponent<InnerConfig>().IPEndPoint;
            //    Session matchSession = Game.Scene.GetComponent<NetInnerComponent>().Get(matchIPEndPoint);
            //    await matchSession.Call(new G2M_PlayerExitMatch_Req() { UserID = user.UserID });

            //    user.IsMatching = false;
            //}

            //正在游戏中发送玩家退出房间请求
            if (user.ActorID != 0)
            {
                ActorMessageSender actorProxy = actorProxyComponent.Get(user.ActorID);
                await actorProxy.Call(new Actor_PlayerExitRoom_Req()
                {
                    UserID = user.UserID
                });

                user.ActorID = 0;
            }
        }
        /// <summary>
        /// 游戏结束
        /// </summary>
        /// <param name="self"></param>
        public static async void GameOver(this GameControllerComponent self, List <GamerScore> gamersScore, Identity winnerIdentity)
        {
            Room room = self.GetParent <Room>();

            Gamer[] gamers = room.GetAll();

            //清理所有卡牌
            self.BackToDeck();
            room.GetComponent <DeskCardsCacheComponent>().Clear();

            //同步匹配服务器结束游戏
            room.State = RoomState.Ready;
            MapHelper.SendMessage(new MP2MH_SyncRoomState_Ntt()
            {
                RoomID = room.InstanceId, State = room.State
            });

            Dictionary <long, long> gamersMoney = new Dictionary <long, long>();

            foreach (GamerScore gamerScore in gamersScore)
            {
                //结算玩家余额
                Gamer gamer      = room.Get(gamerScore.UserID);
                long  gamerMoney = await self.StatisticalIntegral(gamer, gamerScore.Score);

                gamersMoney[gamer.UserID] = gamerMoney;
            }

            //广播游戏结束消息
            Actor_Gameover_Ntt gameoverMessage = new Actor_Gameover_Ntt()
            {
                Winner            = winnerIdentity,
                BasePointPerMatch = self.BasePointPerMatch,
                Multiples         = self.Multiples
            };

            gameoverMessage.GamersScore.AddRange(gamersScore);
            room.Broadcast(gameoverMessage);

            //清理玩家
            foreach (var _gamer in gamers)
            {
                //踢出离线玩家
                if (_gamer.isOffline)
                {
                    ActorMessageSender actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(_gamer.InstanceId);
                    await actorProxy.Call(new Actor_PlayerExitRoom_Req());
                }
                //踢出余额不足玩家
                else if (gamersMoney[_gamer.UserID] < self.MinThreshold)
                {
                    ActorMessageSender actorProxy = _gamer.GetComponent <UnitGateComponent>().GetActorMessageSender();
                    actorProxy.Send(new Actor_GamerMoneyLess_Ntt()
                    {
                        UserID = _gamer.UserID
                    });
                }
            }
        }
示例#5
0
        /// <summary>
        /// 处理发送消息
        /// </summary>
        /// <param name="self"></param>
        /// <param name="task"></param>
        /// <returns></returns>
        private static async Task RunTask(this ActorLocationSender self, ActorTask task)
        {
            //获得和其他服务器通讯的会话
            ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(self.ActorId);
            //发送一个rpc消息
            IActorResponse response = await actorMessageSender.Call(task.ActorRequest);

            // 发送成功
            switch (response.Error)
            {
            case ErrorCode.ERR_NotFoundActor:
                // 如果没找到Actor,重试
                ++self.FailTimes;

                // 失败MaxFailTimes次则清空actor发送队列,返回失败
                if (self.FailTimes > ActorLocationSender.MaxFailTimes)
                {
                    // 失败直接删除actorproxy
                    Log.Info($"actor send message fail, actorid: {self.Id}");
                    self.RunError(response.Error);
                    self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);                           //从字典中移除这个会话通道
                    return;
                }

                // 等待0.5s再发送
                await Game.Scene.GetComponent <TimerComponent>().WaitAsync(500);

                self.ActorId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(self.Id);                        //重新获得map服务器地址

                self.Address = StartConfigComponent.Instance
                               .Get(IdGenerater.GetAppIdFromId(self.ActorId))
                               .GetComponent <InnerConfig>().IPEndPoint;                         //得到IP
                self.AllowGet();
                return;

            case ErrorCode.ERR_ActorNoMailBoxComponent:
                self.RunError(response.Error);
                self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                return;

            default:                                  //发送成功
                self.LastSendTime = TimeHelper.Now(); //记录最后发送时间
                self.FailTimes    = 0;
                self.WaitingTasks.Dequeue();          //出栈

                if (task.Tcs == null)
                {
                    return;
                }

                IActorLocationResponse actorLocationResponse = response as IActorLocationResponse;
                if (actorLocationResponse == null)
                {
                    task.Tcs.SetException(new Exception($"actor location respose is not IActorLocationResponse, but is: {response.GetType().Name}"));
                }
                task.Tcs.SetResult(actorLocationResponse);                          //返回PRC消息回应
                return;
            }
        }
        public async void Dispatch(Session session, ushort opcode, object message)
        {
            try
            {
                switch (message)
                {
                case IActorLocationRequest actorLocationRequest:     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
                {
                    long actorId = session.GetComponent <SessionUserComponent>().User.ActorID;
                    ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(actorId);

                    int       rpcId    = actorLocationRequest.RpcId; // 这里要保存客户端的rpcId
                    IResponse response = await actorLocationSender.Call(actorLocationRequest);

                    response.RpcId = rpcId;

                    session.Reply(response);
                    return;
                }

                case IActorLocationMessage actorLocationMessage:
                {
                    long actorId = session.GetComponent <SessionUserComponent>().User.ActorID;
                    ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(actorId);
                    actorLocationSender.Send(actorLocationMessage);
                    return;
                }

                case IActorRequest iActorRequest:
                {
                    long actorId = session.GetComponent <SessionUserComponent>().User.ActorID;
                    ActorMessageSender actorSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(actorId);

                    int       rpcId    = iActorRequest.RpcId; // 这里要保存客户端的rpcId
                    IResponse response = await actorSender.Call(iActorRequest);

                    response.RpcId = rpcId;

                    session.Reply(response);
                    return;
                }

                case IActorMessage iactorMessage:
                {
                    long actorId = session.GetComponent <SessionUserComponent>().User.ActorID;
                    ActorMessageSender actorSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(actorId);
                    actorSender.Send(iactorMessage);
                    return;
                }
                }

                Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(opcode, message));
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
示例#7
0
        public async void Dispatch(Session session, Packet packet)
        {
            ushort opcode      = packet.Opcode;
            Type   messageType = session.Network.Entity.GetComponent <OpcodeTypeComponent>().GetType(opcode);
            object message     = session.Network.MessagePacker.DeserializeFrom(messageType, packet.Bytes, Packet.Index, packet.Length - Packet.Index);

            //Log.Debug($"recv: {JsonHelper.ToJson(message)}");

            switch (message)
            {
            case IFrameMessage iFrameMessage:                     // 如果是帧消息,构造成OneFrameMessage发给对应的unit
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                // 这里设置了帧消息的id,防止客户端伪造
                iFrameMessage.Id = unitId;

                OneFrameMessage oneFrameMessage = new OneFrameMessage
                {
                    Op       = opcode,
                    AMessage = session.Network.MessagePacker.SerializeToByteArray(iFrameMessage)
                };
                actorMessageSender.Send(oneFrameMessage);
                return;
            }

            case IActorRequest iActorRequest:                     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                int       rpcId    = iActorRequest.RpcId;                // 这里要保存客户端的rpcId
                IResponse response = await actorMessageSender.Call(iActorRequest);

                response.RpcId = rpcId;

                session.Reply(response);
                return;
            }

            case IActorMessage iActorMessage:                     // gate session收到actor消息直接转发给actor自己去处理
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);
                actorMessageSender.Send(iActorMessage);
                return;
            }
            }

            if (message != null)
            {
                Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(opcode, message));
                return;
            }

            throw new Exception($"message type error: {message.GetType().FullName}");
        }
示例#8
0
        private static async ETTask RunTask(this ActorLocationSender self, ActorTask task)
        {
            ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(self.ActorId);
            IActorResponse     response           = await actorMessageSender.Call(task.ActorRequest);

            // 发送成功
            switch (response.Error)
            {
            case ErrorCode.ERR_NotFoundActor:
                // 如果没找到Actor,重试
                ++self.FailTimes;

                // 失败MaxFailTimes次则清空actor发送队列,返回失败
                if (self.FailTimes > ActorLocationSender.MaxFailTimes)
                {
                    // 失败直接删除actorproxy
                    Log.Info($"actor send message fail, actorid: {self.Id}");
                    self.RunError(response.Error);
                    self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                    return;
                }

                // 等待0.5s再发送
                await Game.Scene.GetComponent <TimerComponent>().WaitAsync(500);

                self.ActorId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(self.Id);

                self.AllowGet();
                return;

            case ErrorCode.ERR_ActorNoMailBoxComponent:
                self.RunError(response.Error);
                self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                return;

            default:
                self.LastRecvTime = TimeHelper.Now();
                self.FailTimes    = 0;
                self.WaitingTasks.Dequeue();

                // 如果所有的发送消息都得到了返回,发送任务完成,那么删除这个ActorLocationSender,及时回收发送对象
                if (self.WaitingTasks.Count == 0)
                {
                    self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                }

                if (task.Tcs != null)
                {
                    IActorLocationResponse actorLocationResponse = response as IActorLocationResponse;
                    if (actorLocationResponse == null)
                    {
                        task.Tcs.SetException(new Exception($"actor location respose is not IActorLocationResponse, but is: {response.GetType().Name}"));
                    }
                    task.Tcs.SetResult(actorLocationResponse);
                }
                return;
            }
        }
        public static async void Start(this TrusteeshipComponent self)
        {
            //找到玩家所在房间
            LandlordsComponent landordsMatchComponent = Game.Scene.GetComponent <LandlordsComponent>();
            Gamer                    gamer            = self.GetParent <Gamer>();
            LandlordsRoom            room             = landordsMatchComponent.GetGamingRoom(self.GetParent <Gamer>());
            ActorMessageSender       actorProxy       = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(gamer.InstanceId);
            OrderControllerComponent orderController  = room.GetComponent <OrderControllerComponent>();

            //这个托管组件是通过定时器实现的
            while (true)
            {
                //延迟1秒
                await Game.Scene.GetComponent <TimerComponent>().WaitAsync(3000);

                if (self.IsDisposed)
                {
                    return;
                }

                if (gamer.UserID != orderController?.CurrentAuthority)
                {
                    continue;
                }

                //给Map上的Gamer发送Actor消息
                //自动提示出牌
                Actor_GamerPrompt_Back response = (Actor_GamerPrompt_Back)await actorProxy.Call(new Actor_GamerPrompt_Req());

                if (response.Error > 0 || response.Cards.Count == 0)
                {
                    actorProxy.Send(new Actor_GamerDontPlay_Ntt());
                }
                else
                {
                    await actorProxy.Call(new Actor_GamerPlayCard_Req()
                    {
                        Cards = response.Cards
                    });
                }
            }
        }
        protected override async void Run(Session session, G2M_PlayerEnterMatch_Req message, Action <M2G_PlayerEnterMatch_Ack> reply)
        {
            M2G_PlayerEnterMatch_Ack response = new M2G_PlayerEnterMatch_Ack();

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

                if (matchComponent.Playing.ContainsKey(message.UserID))
                {
                    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;

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

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

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
示例#11
0
        public override async void Destroy(SessionUserComponent self)
        {
            try
            {
                //释放User对象时将User对象从管理组件中移除
                Game.Scene.GetComponent <UserComponent>()?.Remove(self.User.UserID);

                StartConfigComponent        config = Game.Scene.GetComponent <StartConfigComponent>();
                ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

                //正在匹配中发送玩家退出匹配请求
                if (self.User.IsMatching)
                {
                    IPEndPoint matchIPEndPoint = config.MatchConfig.GetComponent <InnerConfig>().IPEndPoint;
                    Session    matchSession    = Game.Scene.GetComponent <NetInnerComponent>().Get(matchIPEndPoint);
                    await matchSession.Call(new G2M_PlayerExitMatch_Req()
                    {
                        UserID = self.User.UserID
                    });
                }

                //正在游戏中发送玩家退出房间请求
                if (self.User.ActorID != 0)
                {
                    ActorMessageSender actorProxy = actorProxyComponent.Get(self.User.ActorID);
                    await actorProxy.Call(new Actor_PlayerExitRoom_Req()
                    {
                        UserID = self.User.UserID
                    });
                }

                //向登录服务器发送玩家下线消息
                IPEndPoint realmIPEndPoint = config.RealmConfig.GetComponent <InnerConfig>().IPEndPoint;
                Session    realmSession    = Game.Scene.GetComponent <NetInnerComponent>().Get(realmIPEndPoint);
                await realmSession.Call(new G2R_PlayerOffline_Req()
                {
                    UserID = self.User.UserID
                });

                self.User.Dispose();
                self.User = null;
            }
            catch (System.Exception e)
            {
                Log.Trace(e.ToString());
            }
        }
示例#12
0
        private static async Task RunTask(this ActorLocationSender self, ActorTask task)
        {
            ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(self.ActorId);
            IActorResponse     response           = await actorMessageSender.Call(task.ActorRequest);

            // 发送成功
            switch (response.Error)
            {
            case ErrorCode.ERR_NotFoundActor:
                // 如果没找到Actor,重试
                ++self.FailTimes;

                // 失败MaxFailTimes次则清空actor发送队列,返回失败
                if (self.FailTimes > ActorLocationSender.MaxFailTimes)
                {
                    // 失败直接删除actorproxy
                    Log.Info($"actor send message fail, actorid: {self.Id}");
                    self.RunError(response.Error);
                    self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                    return;
                }

                // 等待0.5s再发送
                await Game.Scene.GetComponent <TimerComponent>().WaitAsync(500);

                self.ActorId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(self.Id);

                self.Address = StartConfigComponent.Instance
                               .Get(IdGenerater.GetAppIdFromId(self.ActorId))
                               .GetComponent <InnerConfig>().IPEndPoint;
                self.AllowGet();
                return;

            case ErrorCode.ERR_ActorNoMailBoxComponent:
                self.RunError(response.Error);
                self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                return;

            default:
                self.LastSendTime = TimeHelper.Now();
                self.FailTimes    = 0;
                self.WaitingTasks.Dequeue();
                task.Tcs?.SetResult(response);
                return;
            }
        }
示例#13
0
        public async void Dispatch(Session session, ushort opcode, object message)
        {
            switch (message)
            {
            case IFrameMessage iFrameMessage:                     // 如果是帧消息,构造成OneFrameMessage发给对应的unit
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                // 这里设置了帧消息的id,防止客户端伪造
                iFrameMessage.Id = unitId;

                OneFrameMessage oneFrameMessage = new OneFrameMessage
                {
                    Op       = opcode,
                    AMessage = ByteString.CopyFrom(session.Network.MessagePacker.SerializeTo(iFrameMessage))
                };
                actorMessageSender.Send(oneFrameMessage);
                return;
            }

            case IActorRequest iActorRequest:                     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                int       rpcId    = iActorRequest.RpcId;                // 这里要保存客户端的rpcId
                IResponse response = await actorMessageSender.Call(iActorRequest);

                response.RpcId = rpcId;

                session.Reply(response);
                return;
            }

            case IActorMessage iActorMessage:                     // gate session收到actor消息直接转发给actor自己去处理
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);
                actorMessageSender.Send(iActorMessage);
                return;
            }
            }

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(opcode, message));
        }
示例#14
0
        public override async void Destroy(SessionPlayerComponent self)
        {
            // 发送断线消息
            //ActorMessageSender actorMessageSender = Game.Scene.GetComponent<ActorMessageSenderComponent>().Get(self.Player.UnitId);
            //actorMessageSender.Send(new G2M_SessionDisconnect());
            Log.Debug("断开连接:" + self.Player.Id);
            if (self.Player.UnitId != 0) //需要通知map清理掉unit
            {
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(self.Player.UnitId);
                var ret = await actorMessageSender.Call(new G2M_UnitDispose()
                {
                    ActorId = self.Player.UnitId
                });

                Log.Debug("G2M_UnitDispose:" + ret);
                Game.Scene.GetComponent <ActorMessageSenderComponent>().Remove(self.Player.UnitId);
            }
            Game.Scene.GetComponent <PlayerManagerComponent>()?.Remove(self.Player.Id);
        }
示例#15
0
        public async ETVoid DispatchAsync(Session session, ushort opcode, object message)
        {
            // 根据消息接口判断是不是Actor消息,不同的接口做不同的处理
            switch (message)
            {
            case IFrameMessage iFrameMessage:                     // 如果是帧消息,构造成OneFrameMessage发给对应的unit
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(unitId);

                // 这里设置了帧消息的id,防止客户端伪造
                iFrameMessage.Id = unitId;

                OneFrameMessage oneFrameMessage = new OneFrameMessage
                {
                    Op       = opcode,
                    AMessage = ByteString.CopyFrom(session.Network.MessagePacker.SerializeTo(iFrameMessage))
                };
                //actorLocationSender.Send(oneFrameMessage);
                Game.Scene.GetComponent <ServerFrameComponent>().Add(oneFrameMessage);
                return;
            }

            case IActorLocationRequest actorLocationRequest:                     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(unitId);

                int       rpcId      = actorLocationRequest.RpcId;                      // 这里要保存客户端的rpcId
                long      instanceId = session.InstanceId;
                IResponse response   = await actorLocationSender.Call(actorLocationRequest);

                response.RpcId = rpcId;

                // session可能已经断开了,所以这里需要判断
                if (session.InstanceId == instanceId)
                {
                    session.Reply(response);
                }

                break;
            }

            case IActorLocationMessage actorLocationMessage:
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(unitId);
                actorLocationSender.Send(actorLocationMessage);
                break;
            }

            case IActorRequest iActorRequest:                     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long actorId = session.GetComponent <SessionUserComponent>().User.ActorID;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(actorId);

                int       rpcId    = iActorRequest.RpcId;                        // 这里要保存客户端的rpcId
                IResponse response = await actorMessageSender.Call(iActorRequest);

                response.RpcId = rpcId;

                session.Reply(response);
                return;
            }

            case IActorMessage iActorMessage:                     // gate session收到actor消息直接转发给actor自己去处理
            {
                long actorId = session.GetComponent <SessionUserComponent>().User.ActorID;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(actorId);
                actorMessageSender.Send(iActorMessage);
                return;
            }

            default:
            {
                // 非Actor消息
                Game.Scene.GetComponent <MessageDispatcherComponent>().Handle(session, new MessageInfo(opcode, message));
                break;
            }
            }
        }
        public override async void Destroy(SessionUserComponent self)
        {
            try
            {
                //释放User对象时将User对象从管理组件中移除
                Game.Scene.GetComponent <UserComponent>()?.Remove(self.User.UserID);

                StartConfigComponent        config = Game.Scene.GetComponent <StartConfigComponent>();
                ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

                //从房间移除掉线的玩家
                //TODO  后续做断线重连需要重新改

                //移除在房间的玩家
                if (self.User.ActorID != 0)
                {
                    ActorMessageSender actorProxy = actorProxyComponent.Get(self.User.ActorID);
                    await actorProxy.Call(new Actor_PlayerExitRoom_Req()
                    {
                        UserID = self.User.UserID
                    });
                }

                Room room = RoomHelp.GetRoom(self.User.RoomID);
                if (room?.State == RoomState.Idle)
                {
                    if (room.Count == 0)
                    {
                        room.Dispose();
                    }
                }

                //正在匹配中发送玩家退出匹配请求
                //if (self.User.IsMatching)
                //{
                //    IPEndPoint matchIPEndPoint = config.MatchConfig.GetComponent<InnerConfig>().IPEndPoint;
                //    Session matchSession = Game.Scene.GetComponent<NetInnerComponent>().Get(matchIPEndPoint);
                //    await matchSession.Call(new G2M_PlayerExitMatch_Req() { UserID = self.User.UserID });
                //}

                //正在游戏中发送玩家退出房间请求
                //if (self.User.ActorID != 0)
                //{
                //    ActorMessageSender actorProxy = actorProxyComponent.Get(self.User.ActorID);
                //    await actorProxy.Call(new Actor_PlayerExitRoom_Req() { UserID = self.User.UserID });
                //}

                //向登录服务器发送玩家下线消息
                IPEndPoint realmIPEndPoint = config.RealmConfig.GetComponent <InnerConfig>().IPEndPoint;
                Session    realmSession    = Game.Scene.GetComponent <NetInnerComponent>().Get(realmIPEndPoint);
                await realmSession.Call(new G2R_PlayerOffline_Req()
                {
                    UserID = self.User.UserID
                });

                self.User.Dispose();
                self.User = null;
            }
            catch (System.Exception e)
            {
                Log.Trace(e.ToString());
            }
        }
        protected override async ETTask Run(Session session, C2G_RequestEnterMap request, G2C_RequestEnterMap response, Action reply)
        {
            Log.Info("玩家:" + request.Account + "请求进入Map");

            //获取玩家
            PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>();
            Player          player          = playerComponent.getPlayerByAccount(request.Account);

            if (player != null)
            {
                //获取内网发送组件
                IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent <InnerConfig>().IPEndPoint;
                Session    mapSession = Game.Scene.GetComponent <NetInnerComponent>().Get(mapAddress);

                M2G_EnterWorld m2GEnterWorld = (M2G_EnterWorld)await mapSession.Call(new G2M_EnterWorld()
                {
                    Account = request.Account, PlayerGateInstanceId = player.InstanceId
                });

                player.MapInstanceId = m2GEnterWorld.PlayerMapInstanceId;

                //获取角色出生位置
                ActorMessageSenderComponent actorSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
                ActorMessageSender          actorMessageSender   = actorSenderComponent.Get(player.MapInstanceId);

                Actor_PlayerInitPositionResponse actor_PlayerInitPositionResponse = (Actor_PlayerInitPositionResponse)await actorMessageSender.Call(new Actor_PlayerInitPositionRequest());

                Log.Info("获取角色初始位置:" + actor_PlayerInitPositionResponse.PositionX + " || " +
                         actor_PlayerInitPositionResponse.PositionY + " || " + actor_PlayerInitPositionResponse.PositionZ);

                response.PositionX = actor_PlayerInitPositionResponse.PositionX;
                response.PositionY = actor_PlayerInitPositionResponse.PositionY;
                response.PositionZ = actor_PlayerInitPositionResponse.PositionZ;

                reply();

                Log.Info("同意进入Map");

                //向其它玩家发送自己登录的信息
                List <Player> players = playerComponent.getOtherPlayerIgnoreAccount(request.Account);
                if (players != null)
                {
                    for (int i = 0; i < players.Count; i++)
                    {
                        players[i].session.Send(new G2C_OtherPlayerEnterMap()
                        {
                            Account   = request.Account,
                            PositionX = actor_PlayerInitPositionResponse.PositionX,
                            PositionY = actor_PlayerInitPositionResponse.PositionY,
                            PositionZ = actor_PlayerInitPositionResponse.PositionZ
                        });
                    }
                }
            }
            else
            {
                Log.Info("玩家超时:" + request.Account);
                session.Send(new G2C_PlayerBackLogin()
                {
                    NetMessage = "游戏超时: " + request.Account
                });
            }

            await ETTask.CompletedTask;
        }
示例#18
0
        protected override async ETTask Run(Session session, C2G_GetOtherPlayer message)
        {
            //获取内网发送组件
            IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent <InnerConfig>().IPEndPoint;
            Session    mapSession = Game.Scene.GetComponent <NetInnerComponent>().Get(mapAddress);

            M2G_GetAllMapUnitExcept m2GGetAllMapUnitExcept = (M2G_GetAllMapUnitExcept)await mapSession.Call(new G2M_GetAllMapUnitExcept()
            {
                Account = message.Account
            });

            if (m2GGetAllMapUnitExcept.Accounts.Count > 0)
            {
                PlayerComponent             playerComponent      = Game.Scene.GetComponent <PlayerComponent>();
                ActorMessageSenderComponent actorSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

                for (int i = 0; i < m2GGetAllMapUnitExcept.Accounts.Count; i++)
                {
                    if (playerComponent.AccountHaveBeCreated(m2GGetAllMapUnitExcept.Accounts[i]))
                    {
                        Player player = playerComponent.getPlayerByAccount(m2GGetAllMapUnitExcept.Accounts[i]);

                        ActorMessageSender actorMessageSender = actorSenderComponent.Get(player.MapInstanceId);
                        Actor_PlayerInitPositionResponse actor_PlayerInitPositionResponse = (Actor_PlayerInitPositionResponse)await actorMessageSender.Call(new Actor_PlayerInitPositionRequest());

                        session.Send(new G2C_OtherPlayerEnterMap()
                        {
                            Account   = player.Account,
                            PositionX = actor_PlayerInitPositionResponse.PositionX,
                            PositionY = actor_PlayerInitPositionResponse.PositionY,
                            PositionZ = actor_PlayerInitPositionResponse.PositionZ
                        });
                    }
                }
            }

            await ETTask.CompletedTask;
        }
示例#19
0
        public async ETVoid DispatchAsync(Session session, ushort opcode, object message)
        {
            // 根据消息接口判断是不是Actor消息,不同的接口做不同的处理
            switch (message)
            {
            case IFrameMessage iFrameMessage:                     // 如果是帧消息,构造成OneFrameMessage发给对应的unit
            {
                // ...
                return;
            }

            case IActorLocationRequest actorLocationRequest:                     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long unitId = session.GetComponent <SessionUserComponent>().User.UnitId;
                ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(unitId);

                int       rpcId      = actorLocationRequest.RpcId;              // 这里要保存客户端的rpcId
                long      instanceId = session.InstanceId;
                IResponse response   = await actorLocationSender.Call(actorLocationRequest);

                response.RpcId = rpcId;

                // session可能已经断开了,所以这里需要判断
                if (session.InstanceId == instanceId)
                {
                    session.Reply(response);
                }

                break;
            }

            case IActorLocationMessage actorLocationMessage:
            {
                long unitId = session.GetComponent <SessionUserComponent>().User.UnitId;
                ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(unitId);
                actorLocationSender.Send(actorLocationMessage);
                break;
            }

            case IActorRequest iActorRequest:
            {
                long actorId = session.GetComponent <SessionUserComponent>().User.ActorId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(actorId);

                int       rpcId    = iActorRequest.RpcId;                // 这里要保存客户端的rpcId
                IResponse response = await actorMessageSender.Call(iActorRequest);

                response.RpcId = rpcId;

                session.Reply(response);
                return;
            }

            case IActorMessage iActorMessage:
            {
                long actorId = session.GetComponent <SessionUserComponent>().User.ActorId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(actorId);
                actorMessageSender.Send(iActorMessage);
                return;
            }

            default:
            {
                // 非Actor消息
                Game.Scene.GetComponent <MessageDispatcherComponent>().Handle(session, new MessageInfo(opcode, message));
                break;
            }
            }
        }
示例#20
0
        public async void Dispatch(Session session, Packet packet)
        {
            object message;

            try
            {
                OpcodeTypeComponent opcodeTypeComponent = session.Network.Entity.GetComponent <OpcodeTypeComponent>();
                object instance = opcodeTypeComponent.GetInstance(packet.Opcode);
                message = session.Network.MessagePacker.DeserializeFrom(instance, packet.Stream);
            }
            catch (Exception e)
            {
                // 出现任何异常都要断开Session,防止客户端伪造消息
                Log.Error(e);
                session.Error = ErrorCode.ERR_PacketParserError;
                session.Network.Remove(session.Id);
                return;
            }

            //Log.Debug($"recv: {JsonHelper.ToJson(message)}");

            switch (message)
            {
            case IFrameMessage iFrameMessage:                     // 如果是帧消息,构造成OneFrameMessage发给对应的unit
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                // 这里设置了帧消息的id,防止客户端伪造
                iFrameMessage.Id = unitId;

                OneFrameMessage oneFrameMessage = new OneFrameMessage
                {
                    Op       = packet.Opcode,
                    AMessage = ByteString.CopyFrom(session.Network.MessagePacker.SerializeTo(iFrameMessage))
                };
                actorMessageSender.Send(oneFrameMessage);
                return;
            }

            case IActorRequest iActorRequest:                     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                int       rpcId    = iActorRequest.RpcId;                // 这里要保存客户端的rpcId
                IResponse response = await actorMessageSender.Call(iActorRequest);

                response.RpcId = rpcId;

                session.Reply(response);
                return;
            }

            case IActorMessage iActorMessage:                     // gate session收到actor消息直接转发给actor自己去处理
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);
                actorMessageSender.Send(iActorMessage);
                return;
            }
            }

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(packet.Opcode, message));
        }
示例#21
0
        public async void Dispatch(Session session, Packet packet)
        {
            Log.Debug("分配消息" + packet.Opcode);
            IMessage message;

            try
            {
                message = session.Network.Entity.GetComponent <OpcodeTypeComponent>().GetNewMessage(packet.Opcode);
                message.MergeFrom(packet.Bytes, packet.Offset, packet.Length);
                //message = session.Network.MessagePacker.DeserializeFrom(messageType, packet.Bytes, Packet.Index, packet.Length - Packet.Index);
            }
            catch (Exception e)
            {
                // 出现任何异常都要断开Session,防止客户端伪造消息
                Log.Error(e);
                session.Error = ErrorCode.ERR_PacketParserError;
                session.Network.Remove(session.Id);
                return;
            }

            //Log.Debug($"recv: {JsonHelper.ToJson(message)}");

            switch (message)
            {
            case IFrameMessage iFrameMessage:     // 如果是帧消息,构造成OneFrameMessage发给对应的unit
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                // 这里设置了帧消息的id,防止客户端伪造
                iFrameMessage.Id = unitId;

                //OneFrameMessage oneFrameMessage = new OneFrameMessage
                //{
                //	Op = packet.Opcode,
                //	AMessage = session.Network.MessagePacker.SerializeToByteArray(iFrameMessage)
                //};
                //actorMessageSender.Send(oneFrameMessage);
                return;
            }

            case IActorRequest iActorRequest:     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);

                int       rpcId    = iActorRequest.RpcId; // 这里要保存客户端的rpcId
                IResponse response = await actorMessageSender.Call(iActorRequest);

                response.RpcId = rpcId;

                session.Reply(response);
                return;
            }

            case IActorMessage iActorMessage:     // gate session收到actor消息直接转发给actor自己去处理
            {
                long unitId = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);
                actorMessageSender.Send(iActorMessage);
                return;
            }
            }

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(packet.Opcode, message));
        }