Example #1
0
        private static void Add(this ActorMessageSender self, ActorTask task)
        {
            if (self.IsDisposed)
            {
                throw new Exception("ActorProxy Disposed! dont hold actorproxy");
            }

            self.WaitingTasks.Enqueue(task);
            // failtimes > 0表示正在重试,这时候不能加到正在发送队列
            if (self.FailTimes == 0)
            {
                self.AllowGet();
            }
        }
Example #2
0
        private static void AllowGet(this ActorMessageSender self)
        {
            if (self.Tcs == null || self.WaitingTasks.Count <= 0)
            {
                return;
            }

            ActorTask task = self.WaitingTasks.Peek();

            var t = self.Tcs;

            self.Tcs = null;
            t.SetResult(task);
        }
 /// <summary>
 /// 广播消息
 /// </summary>
 /// <param name="message"></param>
 public static void Broadcast(this LandlordsRoom self, IActorMessage message)
 {
     foreach (Gamer gamer in self.gamers)
     {
         //如果玩家不存在或者不在线
         if (gamer == null || gamer.isOffline)
         {
             continue;
         }
         ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
         ActorMessageSender          actorProxy          = actorProxyComponent.Get(gamer.ActorIDofClient);
         actorProxy.Send(message);
     }
 }
Example #4
0
        /// <summary>
        /// 匹配队列广播
        /// </summary>
        /// <param name="self"></param>
        /// <param name="message"></param>
        public static void Broadcast(this Moba5V5Component self, IActorMessage message)
        {
            foreach (Gamer gamer in self.MatchingQueue)
            {
                if (gamer.UserID == 0)
                {
                    continue;
                }

                //像Gate服务器中User绑定的Seesion发送Actor消息
                ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
                ActorMessageSender          actorProxy          = actorProxyComponent.Get(gamer.ActorIDofClient);
                actorProxy.Send(message);
            }
        }
        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);
            }
        }
Example #6
0
        protected override async ETTask Run(Unit unit, Actor_PlayerToUnitSubHealthRequest request, Actor_PlayerToUnitSubHealthResponse response, Action reply)
        {
            //是否攻击了已经死亡的玩家
            response.AttackDiePlayer = false;

            if (unit.Die)
            {
                response.AttackDiePlayer = true;
            }
            else
            {
                int newHealth = unit.SubHealth(request.SubHealth);

                response.UnitHealth = newHealth;
                response.Die        = unit.Die;

                //这个玩家被打死了需要广播给其它玩家
                if (unit.Die)
                {
                    List <Unit> units = Game.Scene.GetComponent <UnitComponent>().getCountUnits(0);
                    if (units != null)
                    {
                        ActorMessageSenderComponent actorSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
                        for (int i = 0; i < units.Count; i++)
                        {
                            if (units[i].Account != unit.Account)
                            {
                                ActorMessageSender actorMessageSender = actorSenderComponent.Get(units[i].GateInstanceId);
                                actorMessageSender.Send(new Actor_OtherPlayerDie()
                                {
                                    DiePlayerAccount = unit.Account
                                });
                            }
                        }
                    }

                    //需要开启复活倒计时
                    UpDateNetSync(3000, unit.Account).Coroutine();

                    Log.Info("玩家:" + request.KillerAccount + " 打死了玩家 " + unit.Account);

                    RecordKillDataSendPackge(request.KillerAccount, unit.Account);
                }
            }

            reply();
            await ETTask.CompletedTask;
        }
Example #7
0
 /// <summary>
 /// 广播消息
 /// </summary>
 /// <param name="message"></param>
 public static void Broadcast(this Room self, IActorMessage message)
 {
     foreach (KeyValuePair <long, Gamer> kp in self.gamers)
     {
         Gamer gamer = kp.Value;
         //如果玩家不存在或者不在线
         if (gamer == null || gamer.isOffline)
         {
             continue;
         }
         //向客户端User发送Actor消息
         ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
         ActorMessageSender          actorProxy          = actorProxyComponent.Get(gamer.CActorId);
         actorProxy.Send(message);
     }
 }
        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="self"></param>
        /// <param name="room"></param>
        /// <param name="matcher"></param>
        public static void JoinRoom(this LandlordsComponent self, LandlordsRoom room, Gamer gamer)
        {
            //玩家可能掉线
            if (gamer == null)
            {
                return;
            }

            //玩家加入房间 成为已经进入房间的玩家
            //绑定玩家与房间 以后可以通过玩家UserID找到所在房间
            self.Waiting[gamer.UserID] = room;
            //为玩家添加座位
            room.Add(gamer);
            //房间广播
            Log.Info($"玩家{gamer.UserID}进入房间");
            Actor_GamerEnterRoom_Ntt broadcastMessage = new Actor_GamerEnterRoom_Ntt();

            foreach (Gamer _gamer in room.gamers)
            {
                if (_gamer == null)
                {
                    //添加空位
                    broadcastMessage.Gamers.Add(new GamerInfo());
                    continue;
                }

                //添加玩家信息
                //GamerInfo info = new GamerInfo() { UserID = _gamer.UserID, IsReady = room.IsGamerReady(gamer) };
                GamerInfo info = new GamerInfo()
                {
                    UserID = _gamer.UserID
                };
                broadcastMessage.Gamers.Add(info);
            }
            //广播房间内玩家消息 每次有人进入房间都会收到一次广播
            room.Broadcast(broadcastMessage);

            //通知Gate服务器玩家匹配成功 参数:Gamer的InstanceId
            //Gate服务器将Actor类消息转发给Gamer
            ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
            ActorMessageSender          gamerActorProxy     = actorProxyComponent.Get(gamer.ActorIDofUser);

            gamerActorProxy.Send(new Actor_LandlordsMatchSucess()
            {
                ActorIDofGamer = gamer.InstanceId
            });
        }
        /// <summary>
        /// 游戏结束
        /// </summary>
        /// <param name="self"></param>
        public static async void GameOver(this GameControllerComponent self, List <GamerScore> gamersScore, Identity winnerIdentity)
        {
            LandlordsRoom room = self.GetParent <LandlordsRoom>();

            Gamer[] gamers = room.gamers;

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

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

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

                gamersMoney[gamer.UserID] = gamerMoney;
            }

            //广播游戏结束消息
            room.Broadcast(new Actor_Gameover_Ntt()
            {
                Winner            = (byte)winnerIdentity,
                BasePointPerMatch = self.BasePointPerMatch,
                Multiples         = self.Multiples,
                GamersScore       = To.RepeatedField(gamersScore)
            });

            //清理玩家
            foreach (var _gamer in gamers)
            {
                //踢出离线玩家
                if (_gamer.isOffline)
                {
                    ActorMessageSender actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(_gamer.Id);
                    //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 });
                }
            }
        }
Example #10
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());
            }
        }
Example #11
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));
        }
Example #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;
            }
        }
Example #13
0
        public static void Broadcast(IActorMessage message)
        {
            Unit[] units = Game.Scene.GetComponent <UnitComponent>().GetAll();
            ActorMessageSenderComponent actorLocationSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

            foreach (Unit unit in units)
            {
                UnitGateComponent unitGateComponent = unit.GetComponent <UnitGateComponent>();
                if (unitGateComponent.IsDisconnect)
                {
                    continue;
                }

                ActorMessageSender actorMessageSender = actorLocationSenderComponent.Get(unitGateComponent.GateSessionActorId);
                actorMessageSender.Send(message);
            }
        }
        public 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.Id;
                ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(unitId);
                // 这里设置了帧消息的id,防止客户端伪造
                iFrameMessage.Id = unitId;
                return;
            }

            case IActorMessage iActorMessage:     // gate session收到actor消息直接转发给actor自己去处理
            {
                session.GetComponent <SessionPlayerComponent>().Player.GetComponent <MailBoxComponent>().Add(new ActorMessageInfo()
                    {
                        Session = session, Message = iActorMessage
                    });
                return;
            }
            }

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(packet.Opcode, message));
        }
Example #15
0
        public static void Broadcast(this Tank self, IActorMessage message)
        {
            Tank[] tanks = self.Battle.GetAll();
            ActorMessageSenderComponent actorLocationSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

            foreach (Tank tank in tanks)
            {
                TankGateComponent tankGateComponent = tank.GetComponent <TankGateComponent>();
                if (tankGateComponent.IsDisconnect)
                {
                    continue;
                }

                ActorMessageSender actorMessageSender = actorLocationSenderComponent.Get(tankGateComponent.GateSessionActorId);


                actorMessageSender.Send(message);
            }
        }
Example #16
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);
        }
Example #17
0
        protected override async void Run(Session session, G2M_EnterMatch_Landords message)
        {
            //Log.Debug("Map服务器收到第一条消息");
            LandlordsComponent matchComponent = Game.Scene.GetComponent <LandlordsComponent>();

            //玩家是否已经开始游戏
            if (matchComponent.Playing.ContainsKey(message.UserID))
            {
                LandlordsRoom room;
                matchComponent.Playing.TryGetValue(message.UserID, out room);
                Gamer gamer = room.GetGamerFromUserID(message.UserID);

                //更新玩家的属性
                gamer.ActorIDofUser   = message.ActorIDofUser;
                gamer.ActorIDofClient = message.ActorIDofClient;

                //帮助玩家恢复牌桌



                //向Gate上的User发送匹配成功消息 使User更新绑定的ActorID
                //Map上的Gamer需要保存User的InstanceID给其发消息
                //生成Gamer的时候 需要设置ActorIDofUser
                ActorMessageSender actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(gamer.ActorIDofUser);
                actorProxy.Send(new Actor_LandlordsMatchSucess()
                {
                    ActorIDofGamer = gamer.InstanceId
                });
            }
            else
            {
                //新建玩家
                Gamer newgamer = ComponentFactory.Create <Gamer, long>(message.UserID);
                newgamer.ActorIDofUser   = message.ActorIDofUser;
                newgamer.ActorIDofClient = message.ActorIDofClient;

                //为Gamer添加组件
                await newgamer.AddComponent <MailBoxComponent>().AddLocation();

                //添加玩家到匹配队列 广播一遍正在匹配中的玩家
                matchComponent.AddGamerToMatchingQueue(newgamer);
            }
        }
Example #18
0
        /// <summary>
        /// 广播
        /// </summary>
        /// <param name="message"></param>
        public static void Broadcast(IActorMessage message)
        {
            //获得角色管理组件中所有的角色
            Unit[] units = Game.Scene.GetComponent <UnitComponent>().GetAll();
            //获得Actor会话管理组件
            ActorMessageSenderComponent actorLocationSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

            foreach (Unit unit in units)                                                       //遍历所有的角色
            {
                UnitGateComponent unitGateComponent = unit.GetComponent <UnitGateComponent>(); //获取每个角色的会话通道
                if (unitGateComponent.IsDisconnect)
                {
                    continue;
                }
                //发送消息
                ActorMessageSender actorMessageSender = actorLocationSenderComponent.Get(unitGateComponent.GateSessionActorId);
                actorMessageSender.Send(message);
            }
        }
        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 Task Run(Gamer gamer, Actor_Trusteeship_Ntt message)
        {
            Room room = Game.Scene.GetComponent <RoomComponent>().Get(gamer.RoomID);
            //是否已经托管
            bool isTrusteeship = gamer.GetComponent <TrusteeshipComponent>() != null;

            if (message.isTrusteeship && !isTrusteeship)
            {
                gamer.AddComponent <TrusteeshipComponent>();
                Log.Info($"玩家{gamer.UserID}切换为自动模式");
            }
            else if (isTrusteeship)
            {
                gamer.RemoveComponent <TrusteeshipComponent>();
                Log.Info($"玩家{gamer.UserID}切换为手动模式");
            }

            //这里由服务端设置消息UserID用于转发
            Actor_Trusteeship_Ntt transpond = new Actor_Trusteeship_Ntt();

            transpond.isTrusteeship = message.isTrusteeship;
            transpond.UserID        = gamer.UserID;
            //转发消息
            room.Broadcast(transpond);

            if (isTrusteeship)
            {
                OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();
                if (gamer.UserID == orderController.CurrentAuthority)
                {
                    bool isFirst = gamer.UserID == orderController.Biggest;
                    ActorMessageSender actorProxy = gamer.GetComponent <UnitGateComponent>().GetActorMessageSender();
                    actorProxy.Send(new Actor_AuthorityPlayCard_Ntt()
                    {
                        UserID = orderController.CurrentAuthority, IsFirst = isFirst
                    });
                }
            }

            await Task.CompletedTask;
        }
Example #21
0
        protected override async ETTask Run(Session session, C2G_ReadyLand_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>();

            //通知Map服务器玩家准备游戏
            if (user.ActorID != 0)
            {
                ActorMessageSender actorProxy = actorProxyComponent.Get(user.ActorID);
                actorProxy.Send(new Actor_GamerReady_Landlords());

                user.ActorID = 0;
            }
            await ETTask.CompletedTask;
        }
Example #22
0
        public static async void UpdateAsync(this ActorMessageSender self)
        {
            try
            {
                while (true)
                {
                    ActorTask actorTask = await self.GetAsync();

                    if (self.IsDisposed)
                    {
                        return;
                    }

                    await self.RunTask(actorTask);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Example #23
0
        protected override 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>();

            //通知Map服务器玩家离开房间
            if (user.ActorIDforClient != 0)
            {
                ActorMessageSender actorProxy = actorProxyComponent.Get(user.ActorIDforClient);
                actorProxy.Send(new Actor_PlayerExitRoom());

                user.ActorIDforClient = 0;
            }
        }
Example #24
0
        protected override async ETTask Run(Session session, EnterMatchs_G2M message)
        {
            //Log.Debug("Map服务器收到第一条消息");
            LandMatchComponent matchComponent = Game.Scene.GetComponent <LandMatchComponent>();

            //玩家是否在房间中待机
            if (matchComponent.Waiting.ContainsKey(message.UserID))
            {
                Room room;
                //通过UserID获取matchComponent中相对应的键,即找到此User所在的Room
                matchComponent.Waiting.TryGetValue(message.UserID, out room);
                //房间中的玩家对象,获取获取其UserID的座位索引
                Gamer gamer = room.GetGamerFromUserID(message.UserID);

                //设置GateActorID,ClientActorID
                gamer.GActorID = message.GActorID;
                gamer.CActorID = message.CActorID;

                //向Gate发送消息更新Gate上user的ActorID
                //这样不论玩家到了哪个地图服务器,Gate上的user都持有所在地图服务器上gamer的InstanceId
                ActorMessageSender actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(gamer.GActorID);
                actorProxy.Send(new Actor_MatchSucess_M2G()
                {
                    GamerID = gamer.InstanceId
                });
            }
            else
            {
                //新建玩家,用UserID构建Gamer
                Gamer newgamer = ComponentFactory.Create <Gamer, long>(message.UserID);
                newgamer.GActorID = message.GActorID;
                newgamer.CActorID = message.CActorID;

                //为Gamer添加MailBoxComponent组件,可以通过MailBox进行actor通信
                await newgamer.AddComponent <MailBoxComponent>().AddLocation();

                //添加玩家到匹配队列 广播一遍正在匹配中的玩家
                matchComponent.AddGamerToMatchingQueue(newgamer);
            }
        }
        // 每10s扫描一次过期的actorproxy进行回收,过期时间是1分钟
        public override async void Start(ActorMessageSenderComponent self)
        {
            List <long> timeoutActorProxyIds = new List <long>();

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

                if (self.IsDisposed)
                {
                    return;
                }

                timeoutActorProxyIds.Clear();

                long timeNow = TimeHelper.Now();
                foreach (long id in self.ActorMessageSenders.Keys)
                {
                    ActorMessageSender actorMessageSender = self.Get(id);
                    if (actorMessageSender == null)
                    {
                        continue;
                    }

                    if (timeNow < actorMessageSender.LastSendTime + 60 * 1000)
                    {
                        continue;
                    }

                    actorMessageSender.Error = ErrorCode.ERR_ActorTimeOut;
                    timeoutActorProxyIds.Add(id);
                }

                foreach (long id in timeoutActorProxyIds)
                {
                    self.Remove(id);
                }
            }
        }
        private static void OnSimulateAfter(this UnitSimulateComponent self)
        {
            ActorMessageSenderComponent actorLocationSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
            UnitGateComponent           unitGateComponent            = self.Entity.GetComponent <UnitGateComponent>();
            ActorMessageSender          actorMessageSender           = actorLocationSenderComponent.Get(unitGateComponent.GateSessionActorId);

            if (unitGateComponent.IsDisconnect)
            {
                return;
            }

            if (self.ExecuteQueue.Count > 0)
            {
                Actor_ServerCommond serverCommond = new Actor_ServerCommond();
                while (self.ExecuteQueue.Count > 0)
                {
                    StateCommand stateCommand = self.ExecuteQueue.Dequeue();
                    serverCommond.Result.Add(stateCommand.ToCommand());
                }

                actorMessageSender.Send(serverCommond);
            }

            if (self.Frame % UnitStateComponent.SyncFrame == 1)
            {
                Actor_StateSync stateSync = new Actor_StateSync();

                Unit[] units = Game.Scene.GetComponent <UnitComponent>().GetAll();
                foreach (Unit unit in units)
                {
                    UnitStateComponent unitStateComponent = unit.GetComponent <UnitStateComponent>();
                    UnitState          state = unitStateComponent.State;
                    UnitStateInfo      info  = state.Pack(unit.Id, state.Frame);
                    stateSync.States.Add(info);
                }

                actorMessageSender.Send(stateSync);
            }
        }
Example #27
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;
        }
Example #28
0
        public static void BroadcastTarget(IActorMessage message, List <long> targetUids)
        {
            ActorMessageSenderComponent actorLocationSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

            for (int i = 0; i < targetUids?.Count; i++)
            {
                if (targetUids[i] <= 0)
                {
                    Log.Error($"BroadcastTargetg失敗! targetUid<=0 {message.ToString()}");
                    continue;
                }

                Player player = CacheHelper.GetFromCache <Player>(targetUids[i]);
                if (player == null || !player.isOnline || player.gateSessionActorId == 0)
                {
                    Log.Trace($"Don't broadcast player who have disconnected!");
                    continue;
                }

                ActorMessageSender actorMessageSender = actorLocationSenderComponent.Get(player.gateSessionActorId);
                actorMessageSender.Send(message);
            }
        }
        protected override async void Run(Gamer gamer, Actor_Trusteeship_Ntt message)
        {
            LandlordsRoom room = Game.Scene.GetComponent <LandlordsComponent>().GetGamingRoom(gamer);

            //是否已经托管
            bool isTrusteeship = gamer.GetComponent <TrusteeshipComponent>() != null;

            if (message.IsTrusteeship && !isTrusteeship)
            {
                gamer.AddComponent <TrusteeshipComponent>();
                Log.Info($"玩家{gamer.UserID}切换为自动模式");
            }
            else if (isTrusteeship)
            {
                gamer.RemoveComponent <TrusteeshipComponent>();
                Log.Info($"玩家{gamer.UserID}切换为手动模式");
            }

            //当玩家切换为手动模式时 补发出牌权
            if (isTrusteeship)
            {
                OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();
                if (gamer.UserID == orderController.CurrentAuthority)
                {
                    bool isFirst = gamer.UserID == orderController.Biggest;

                    //向客户端发送出牌权消息
                    ActorMessageSender actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(gamer.ActorIDofClient);
                    actorProxy.Send(new Actor_AuthorityPlayCard_Ntt()
                    {
                        UserID = orderController.CurrentAuthority, IsFirst = isFirst
                    });
                }
            }

            await Task.CompletedTask;
        }
Example #30
0
        public override async void Destroy(SessionUserComponent self)
        {
            //释放User对象时将User对象从管理组件中移除
            Game.Scene.GetComponent <UserComponent>()?.Remove(self.User.UserID);

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

            //正在匹配中发送玩家退出匹配请求
            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 = this.User.UserID });
            }

            //正在游戏中发送玩家退出房间请求
            if (self.User.ActorID != 0)
            {
                Log.Info($"session释放,玩家MapId:{self.User.ActorID}");

                ActorMessageSenderComponent actorMessageSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
                ActorMessageSender          actorMessageSender          = actorMessageSenderComponent.Get(self.User.ActorID);

                actorMessageSender.Send(new Actor_GamerExitRoom()
                {
                    IsFromClient = false,
                });
            }

            //向登录服务器发送玩家下线消息
//            IPEndPoint realmIPEndPoint = config.RealmConfig.GetComponent<InnerConfig>().IPEndPoint;
//            Session realmSession = Game.Scene.GetComponent<NetInnerComponent>().Get(realmIPEndPoint);
//            realmSession.Send(new Actor_ForceOffline());

            self.User.Dispose();
            self.User = null;
        }