protected override async Task Run(Room room, Actor_PlayerEnterRoom_Ntt message)
        {
            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();
                Gamer            gamer   = room.Get(message.UserID);
                if (gamer == null)
                {
                    //创建房间玩家对象
                    gamer = GamerFactory.Create(message.PlayerID, message.UserID, room.RoomId, (ushort)room.Count);
                    await gamer.AddComponent <MailBoxComponent>().AddLocation();

                    gamer.AddComponent <UnitGateComponent, long>(message.SessionID);

                    //设置转接ID
                    Session session           = Game.Scene.GetComponent <NetOuterComponent>().Get(message.SessionID);
                    session.GetUser().ActorID = gamer.Id;

                    room.Add(gamer);

                    Actor_GamerEnterRoom_Ntt broadcastMessage = new Actor_GamerEnterRoom_Ntt();
                    foreach (Gamer item in room.GetAll())
                    {
                        if (item == null)
                        {
                            //添加空位
                            broadcastMessage.users.Add(null);
                            continue;
                        }
                        UserInfo userInfo = await dbProxy.Query <UserInfo>(item.UserID, false);

                        GamerInfo info = new GamerInfo()
                        {
                            ChairID = item.uChairID, NickName = userInfo.NickName, Loses = userInfo.Loses, Money = userInfo.Money, Wins = userInfo.Wins
                        };
                        broadcastMessage.users.Add(info);
                    }
                    room.Broadcast(broadcastMessage);

                    Log.Info($"玩家{message.UserID}进入房间");
                }
                else
                {
                    Log.Info("断线重连?????");
                }
            }
            catch (Exception e)
            {
                Log.Info($"创建房间错误:{e.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
            });
        }
Esempio n. 3
0
        /// <summary>
        /// 加入房间
        /// </summary>
        public static void JoinRoom(this LandMatchComponent self, Room 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上的User发送匹配成功
            //ActorMessageSender actorProxy = Game.Scene.GetComponent<ActorMessageSenderComponent>().Get(gamer.GActorID);
            //actorProxy.Send(new Actor_MatchSucess_M2G() { GamerID = gamer.InstanceId });
            Session GetGateSession = MapHelper.GetGateSession();

            GetGateSession.Send(new T_MatchSucess_M2G()
            {
                UserID = gamer.UserID, GamerID = gamer.InstanceId
            });
        }
        protected override async Task Run(Room room, Actor_PlayerEnterRoom_Req message, Action <Actor_PlayerEnterRoom_Ack> reply)
        {
            Actor_PlayerEnterRoom_Ack response = new Actor_PlayerEnterRoom_Ack();

            try
            {
                Gamer gamer = room.Get(message.UserID);
                if (gamer == null)
                {
                    //创建房间玩家对象
                    gamer = GamerFactory.Create(message.PlayerID, message.UserID);
                    await gamer.AddComponent <MailBoxComponent>().AddLocation();

                    gamer.AddComponent <UnitGateComponent, long>(message.SessionID);

                    //加入到房间
                    room.Add(gamer);

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

                        //添加玩家信息
                        GamerInfo info = new GamerInfo()
                        {
                            UserID = _gamer.UserID, IsReady = _gamer.IsReady
                        };
                        broadcastMessage.Gamers.Add(info);
                    }

                    //广播房间内玩家消息
                    room.Broadcast(broadcastMessage);

                    Log.Info($"玩家{message.UserID}进入房间");
                }
                else
                {
                    //玩家重连
                    gamer.isOffline = false;
                    gamer.PlayerID  = message.PlayerID;
                    gamer.GetComponent <UnitGateComponent>().GateSessionActorId = message.SessionID;

                    //玩家重连移除托管组件
                    gamer.RemoveComponent <TrusteeshipComponent>();

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

                        //添加玩家信息
                        GamerInfo info = new GamerInfo()
                        {
                            UserID = _gamer.UserID, IsReady = _gamer.IsReady
                        };
                        broadcastMessage.Gamers.Add(info);
                    }

                    //发送房间玩家信息
                    ActorMessageSender actorProxy = gamer.GetComponent <UnitGateComponent>().GetActorMessageSender();
                    actorProxy.Send(broadcastMessage);

                    List <GamerCardNum>      gamersCardNum   = new List <GamerCardNum>();
                    List <GamerState>        gamersState     = new List <GamerState>();
                    GameControllerComponent  gameController  = room.GetComponent <GameControllerComponent>();
                    OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();
                    DeskCardsCacheComponent  deskCardsCache  = room.GetComponent <DeskCardsCacheComponent>();

                    foreach (Gamer _gamer in room.GetAll())
                    {
                        HandCardsComponent handCards = _gamer.GetComponent <HandCardsComponent>();
                        gamersCardNum.Add(new GamerCardNum()
                        {
                            UserID = _gamer.UserID,
                            Num    = _gamer.GetComponent <HandCardsComponent>().GetAll().Length
                        });
                        gamersState.Add(new GamerState()
                        {
                            UserID            = _gamer.UserID,
                            Identity          = (byte)handCards.AccessIdentity,
                            GrabLandlordState = orderController.GamerLandlordState.ContainsKey(_gamer.UserID)
                            ? orderController.GamerLandlordState[_gamer.UserID]
                            : false
                        });
                    }

                    //发送游戏开始消息
                    Actor_GameStart_Ntt gameStartNotice = new Actor_GameStart_Ntt()
                    {
                        HandCards     = gamer.GetComponent <HandCardsComponent>().GetAll(),
                        GamersCardNum = gamersCardNum
                    };
                    actorProxy.Send(gameStartNotice);

                    Card[] lordCards = null;

                    if (gamer.GetComponent <HandCardsComponent>().AccessIdentity == Identity.None)
                    {
                        //广播先手玩家
                        actorProxy.Send(new Actor_AuthorityGrabLandlord_Ntt()
                        {
                            UserID = orderController.CurrentAuthority
                        });
                    }
                    else
                    {
                        if (gamer.UserID == orderController.CurrentAuthority)
                        {
                            //发送可以出牌消息
                            bool isFirst = gamer.UserID == orderController.Biggest;
                            actorProxy.Send(new Actor_AuthorityPlayCard_Ntt()
                            {
                                UserID = orderController.CurrentAuthority, IsFirst = isFirst
                            });
                        }
                        lordCards = deskCardsCache.LordCards.ToArray();
                    }
                    //发送重连数据
                    Actor_GamerReconnect_Ntt reconnectNotice = new Actor_GamerReconnect_Ntt()
                    {
                        Multiples   = room.GetComponent <GameControllerComponent>().Multiples,
                        GamersState = gamersState,
                        DeskCards   = new KeyValuePair <long, Card[]>(orderController.Biggest, deskCardsCache.library.ToArray()),
                        LordCards   = lordCards,
                    };
                    actorProxy.Send(reconnectNotice);

                    Log.Info($"玩家{message.UserID}重连");
                }

                response.GamerID = gamer.Id;

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }