Пример #1
0
        public Gamer Remove(long id)
        {
            int seatIndex = GetGamerSeat(id);

            if (seatIndex >= 0)
            {
                Gamer gamer = gamers[seatIndex];
                gamers.Remove(seatIndex);
                seats.Remove(id);
                gamer.GetComponent <UICowCow_GamerInfoComponent>().Dispose();
                gamer.RemoveComponent <UICowCow_GamerInfoComponent>();
                return(gamer);
            }
            return(null);
        }
Пример #2
0
        protected override async void Run(Session session, G2M_PlayerEnterRoom message, Action <M2G_PlayerEnterRoom> reply)
        {
            M2G_PlayerEnterRoom response = new M2G_PlayerEnterRoom();

            //Log.Info("G2M_GamerEnterRoomHandler" + JsonHelper.ToJson(message));

            try
            {
                RoomComponent roomCompnent = Game.Scene.GetComponent <RoomComponent>();
                Gamer         gamer        = null;
                Room          room         = null;

                foreach (var _room in roomCompnent.rooms.Values)
                {
                    room  = _room;
                    gamer = room.Get(message.UserId);
                    if (gamer != null)
                    {
                        Log.Info("找到房间:" + _room.Id);
                        break;
                    }
                }

                //断线重连
                if (gamer != null)
                {
                    //在空闲房间内
                    if (room.State == RoomState.Idle)
                    {
                        response.Message = "已经进入房间";
                        response.Error   = ErrorCode.ERR_Common;
                        Log.Error("玩家多次进入空闲房间");
                        room.Remove(gamer.UserID);
                        reply(response);
                        return;
                    }
                    DeskComponent deskComponent = room.GetComponent <DeskComponent>();

                    //重新更新actor
                    gamer.PlayerID = message.PlayerId;
                    gamer.GetComponent <UnitGateComponent>().GateSessionActorId = message.SessionId;

                    //短线重连
                    Actor_GamerReconnet reconnet = new Actor_GamerReconnet();
                    foreach (var _gamer in room.GetAll())
                    {
                        if (_gamer == null)
                        {
                            Log.Error($"断线重连后玩家为空");
                            continue;
                        }
                        GamerData gamerData = new GamerData();

                        HandCardsComponent handCardsComponent = _gamer.GetComponent <HandCardsComponent>();
                        if (handCardsComponent == null)
                        {
                            Log.Error($"{_gamer.UserID}断线重连后玩家的手牌为空,移除玩家");
                            continue;
//                            room.Remove(_gamer.UserID);
//			                //房间没人就释放
//			                if (room.seats.Count == 0)
//			                {
//                                roomCompnent.RemoveRoom(room);
//			                    room.Dispose();
//			                }
//                            return;
                        }
                        List <MahjongInfo> handCards = handCardsComponent.GetAll();

                        gamerData.handCards = handCards;
                        gamerData.faceCards = handCardsComponent.FaceCards;
                        gamerData.playCards = handCardsComponent.PlayCards;

                        //添加碰刚的uid
                        foreach (var pengOrBar in handCardsComponent.PengOrBars)
                        {
                            //碰
                            if (pengOrBar.OperateType == OperateType.Peng)
                            {
                                gamerData.pengCards.Add(new MahjongInfo()
                                {
                                    weight = (byte)pengOrBar.Weight
                                });
                                gamerData.OperatedPengUserIds.Add(pengOrBar.UserId);
                            }
                            //杠
                            else
                            {
                                gamerData.gangCards.Add(new MahjongInfo()
                                {
                                    weight = (byte)pengOrBar.Weight
                                });
                                gamerData.OperatedGangUserIds.Add(pengOrBar.UserId);
                            }
                        }

                        gamerData.IsBanker      = handCardsComponent.IsBanker;
                        gamerData.UserID        = _gamer.UserID;
                        gamerData.SeatIndex     = room.GetGamerSeat(_gamer.UserID);
                        gamerData.OnlineSeconds = await DBCommonUtil.GetRestOnlineSeconds(_gamer.UserID);

                        gamerData.IsTrusteeship = gamer.IsTrusteeship;
                        PlayerBaseInfo playerBaseInfo = await DBCommonUtil.getPlayerBaseInfo(_gamer.UserID);

                        PlayerInfo playerInfo = PlayerInfoFactory.Create(playerBaseInfo);

                        gamerData.playerInfo = playerInfo;

                        reconnet.Gamers.Add(gamerData);
                    }

                    reconnet.RestCount = deskComponent.RestLibrary.Count;
                    reconnet.RoomType  = (int)room.GetComponent <GameControllerComponent>().RoomConfig.Id;
                    if (room.IsFriendRoom)
                    {
                        GameControllerComponent gameControllerComponent = room.GetComponent <GameControllerComponent>();
                        reconnet.RoomId         = gameControllerComponent.RoomConfig.FriendRoomId;
                        reconnet.MasterUserId   = gameControllerComponent.RoomConfig.MasterUserId;
                        reconnet.JuCount        = gameControllerComponent.RoomConfig.JuCount;
                        reconnet.Multiples      = gameControllerComponent.RoomConfig.Multiples;
                        reconnet.CurrentJuCount = room.CurrentJuCount;
                    }
                    room.GamerReconnect(gamer, reconnet);

                    gamer.isOffline = false;
                    gamer.RemoveComponent <TrusteeshipComponent>();
                    Log.Info($"玩家{message.UserId}断线重连");
                    gamer.StartTime = DateTime.Now;
                }
                else
                {
                    Log.Info($"{message.UserId}进入房间");

                    gamer = await GamerFactory.Create(message.PlayerId, message.UserId);

                    await gamer.AddComponent <MailBoxComponent>().AddLocation();

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

                    RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();
                    //获得空闲的房间
                    Room idleRoom;

                    if (message.RoomType == 3)
                    {
                        idleRoom = roomComponent.GetFriendRoomById(message.RoomId);
                        if (idleRoom == null)
                        {
                            response.Error   = ErrorCode.ERR_Common;
                            response.Message = "房间号不存在";
                            reply(response);
                            return;
                        }

                        if (idleRoom.Count == 4)
                        {
                            response.Error   = ErrorCode.ERR_Common;
                            response.Message = "房间人数已满";
                            reply(response);
                            return;
                        }
                    }
                    else
                    {
                        idleRoom = roomComponent.GetIdleRoomById(message.RoomType);
                        if (idleRoom == null)
                        {
                            idleRoom = RoomFactory.Create(message.RoomType);
                            roomComponent.Add(idleRoom);
                        }
                    }
                    idleRoom.Add(gamer);
                    await idleRoom.BroadGamerEnter(gamer.UserID);
                }
                response.GameId = gamer.Id;
                reply(response);

                if (message.RoomType == 3)
                {
                    await Actor_GamerReadyHandler.GamerReady(gamer, new Actor_GamerReady()
                    {
                    });
                }
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }

            await Task.CompletedTask;
        }
        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);
            }
        }
Пример #4
0
        protected override async Task Run(Room room, MH2MP_PlayerEnterRoom message, Action <MP2MH_PlayerEnterRoom> reply)
        {
            MP2MH_PlayerEnterRoom response = new MP2MH_PlayerEnterRoom();

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

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

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

                    M2C_GamerEnterRoom broadcastMessage = new M2C_GamerEnterRoom();
                    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
                {
                    //玩家重连 todo处理牛牛玩家重连逻辑
                    gamer.isOffline = false;
                    gamer.PlayerID  = message.PlayerID;
                    gamer.GetComponent <UnitGateComponent>().GateSessionId = message.SessionID;

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

                    M2C_GamerEnterRoom broadcastMessage = new M2C_GamerEnterRoom();
                    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);
                    }

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

                    Dictionary <long, Identity> gamersIdentity  = new Dictionary <long, Identity>();
                    Dictionary <long, int>      gamersCardsNum  = new Dictionary <long, int>();
                    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>();
                        gamersCardsNum.Add(_gamer.UserID, handCards.CardsCount);
                        gamersIdentity.Add(_gamer.UserID, handCards.AccessIdentity);
                    }

                    //发送游戏开始消息
                    M2C_GameStart gameStartNotice = new M2C_GameStart()
                    {
                        GamerCards    = gamer.GetComponent <HandCardsComponent>().GetAll(),
                        GamerCardsNum = gamersCardsNum
                    };
                    actorProxy.Send(gameStartNotice);

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

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

                response.GamerID = gamer.Id;

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