protected override async void Run(Session session, G2M_CreateFriendRoom message, Action <M2G_CreateFriendRoom> reply)
        {
            M2G_CreateFriendRoom response = new M2G_CreateFriendRoom();

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

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

                if (gamer == null)
                {
                    Room friendRoom = RoomFactory.CreateFriendRoom(message);
                    roomComponent.Add(friendRoom);

                    GameControllerComponent gameControllerComponent = friendRoom.GetComponent <GameControllerComponent>();
                    response.RoomId = gameControllerComponent.RoomConfig.FriendRoomId;
                }
                else
                {
                    response.Error   = ErrorCode.ERR_RoomNoExist;
                    response.Message = "正在游戏内,无法创建房间";
                }

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

            await Task.CompletedTask;
        }
Beispiel #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;
        }
Beispiel #3
0
        protected async ETVoid RunAsync(Session session, C2G_CreateRoom message, Action <G2C_CreateRoom> reply)
        {
            G2C_CreateRoom response = new G2C_CreateRoom();

            try
            {
                Player player = session.GetComponent <SessionPlayerComponent>().Player;

                if (!this.CheckRoomName(message.RoomNam))
                {
                    response.Error = ErrorCode.ERR_RpcFail;

                    response.Message = Message.Get(player, 1044);

                    reply(response);

                    A2C_PopMessage msg = new A2C_PopMessage();

                    msg.Text = response.Message;

                    msg.Type = PopMessageType.Float;

                    session.Send(msg);

                    return;
                }

                RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();


                Room room = ComponentFactory.CreateWithId <Room>(IdGenerater.GenerateId());

                room.SerialNumber = roomComponent.GetSerialNum();

                room.PeopleNum = message.PeopleNum;

                room.MapTableId = message.MapId;

                room.BigModel = (BigModel)message.BigModel;

                room.SmallMode = message.SmallModel;

                room.RoomName = message.RoomNam;

                room.OwnerId = player.Id;

                //room.AddComponent<PlayerComponent>().Add(player);

                room.Add(player);

                room.BroadcastRoomDetailInfo();

                //player.RoomId = room.Id;

                roomComponent.Add(room);

                reply(response);

                BroadcastMessage.Send_G2C_Rooms();

                await ETTask.CompletedTask;
            }
            catch (Exception e)
            {
                Log.Error(e);
                ReplyError(response, e, reply);
            }
        }
        private static async Task ChangeTable(Gamer gamer)
        {
            try
            {
//                Log.Info("收到换桌:" + JsonHelper.ToJson(message));
                RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();
                Room          gamerRoom     = roomComponent.Get(gamer.RoomID);
                if (gamerRoom == null || gamerRoom.IsFriendRoom)
                {
                    return;
                }
                if (gamerRoom.State == RoomState.Game)
                {
                    Log.Warning($"玩家{gamer.UserID}打牌过程中不能换桌");
                    return;
                }

                GameControllerComponent gameControllerComponent = gamerRoom.GetComponent <GameControllerComponent>();
                long roomType = gameControllerComponent.RoomConfig.Id;

                Room idleRoom = null;
                //获得空闲的房间
                foreach (var room in roomComponent.idleRooms.Values)
                {
                    if (room.Id != gamerRoom?.Id && room.Count < 4)
                    {
                        GameControllerComponent controllerComponent = room.GetComponent <GameControllerComponent>();
                        if (controllerComponent.RoomConfig.Id == roomType)
                        {
                            idleRoom = room;
                            break;
                        }
                    }
                }

                if (idleRoom == null)
                {
                    idleRoom = RoomFactory.Create((int)roomType);
                    roomComponent.Add(idleRoom);
                }

                Log.Info($"收到玩家{gamer.UserID}换桌前:" + gamer.RoomID);
                //房间移除玩家
                gamerRoom?.Remove(gamer.UserID);
                //房间没人就释放
                if (gamerRoom?.seats.Count == 0)
                {
                    roomComponent.RemoveRoom(gamerRoom);
                    gamerRoom?.Dispose();
                }

                gamer.IsReady      = false;
                gamer.ReadyTimeOut = 0;
                idleRoom.Add(gamer);
                await idleRoom.BroadGamerEnter(gamer.UserID);

                Log.Info($"收到玩家{gamer.UserID}换桌后:" + gamer.RoomID);

                gamerRoom?.Broadcast(new Actor_GamerExitRoom()
                {
                    Uid = gamer.UserID
                });
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            await Task.CompletedTask;
        }