public static async Task GamerReady(Gamer gamer, Actor_GamerReady message)
        {
            try
            {
                Log.Info($"收到玩家{gamer.UserID}准备");
                RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();
                Room          room          = roomComponent.Get(gamer.RoomID);

                if (room == null || gamer == null || gamer.IsReady || room.State == RoomState.Game)
                {
                    return;
                }

                gamer.IsReady = true;
                //消息广播给其他人
                room?.Broadcast(new Actor_GamerReady()
                {
                    Uid = gamer.UserID
                });

                Gamer[] gamers = room.GetAll();
                //房间内有4名玩家且全部准备则开始游戏
                if (room.Count == 4 && gamers.Where(g => g.IsReady).Count() == 4)
                {
                    room.State = RoomState.Game;
                    room.CurrentJuCount++;
                    room.IsGameOver = false;
                    room.RoomDispose();

                    #region 好友房扣钥匙

                    if (room.IsFriendRoom && room.CurrentJuCount == 1)
                    {
                        RoomConfig roomConfig = room.GetComponent <GameControllerComponent>().RoomConfig;
                        await DBCommonUtil.DeleteFriendKey(roomConfig.MasterUserId, roomConfig.KeyCount, "好友房房主扣除钥匙");
                    }
                    #endregion
                    //设置比下胡
                    if (room.LastBiXiaHu)
                    {
                        room.IsBiXiaHu = true;
                    }
                    else
                    {
                        room.IsBiXiaHu = false;
                    }
                    room.LastBiXiaHu = false;


                    if (roomComponent.gameRooms.TryGetValue(room.Id, out var itemRoom))
                    {
                        roomComponent.gameRooms.Remove(room.Id);
                    }

                    roomComponent.gameRooms.Add(room.Id, room);
                    roomComponent.idleRooms.Remove(room.Id);
                    //添加用户
                    room.UserIds.Clear();
                    //初始玩家开始状态
                    foreach (var _gamer in gamers)
                    {
                        room.UserIds.Add(_gamer.UserID);

                        if (_gamer.GetComponent <HandCardsComponent>() == null)
                        {
                            _gamer.AddComponent <HandCardsComponent>();
                        }

                        _gamer.IsReady = false;
                    }

                    Log.Info($"{room.Id}房间开始,玩家:{JsonHelper.ToJson(room.UserIds)}");


                    GameControllerComponent  gameController  = room.GetComponent <GameControllerComponent>();
                    OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();
                    DeskComponent            deskComponent   = room.GetComponent <DeskComponent>();

                    Gamer bankerGamer = null;
                    HandCardsComponent bankerHandCards = null;
                    if (room.IsLianZhuang)
                    {
                        if (room.huPaiUid != 0 && room.huPaiUid == room.BankerGamer?.UserID)
                        {
                            bankerGamer              = room.Get(room.huPaiUid);
                            bankerHandCards          = bankerGamer.GetComponent <HandCardsComponent>();
                            bankerHandCards.IsBanker = true;
                            room.BankerGamer         = bankerGamer;
                            //连庄
                            room.LastBiXiaHu = true;
                        }
                        else
                        {
                            int gamerSeat = room.GetGamerSeat(room.BankerGamer.UserID);
                            int currentSeat;
                            if (gamerSeat == 3)
                            {
                                currentSeat = 0;
                            }
                            else
                            {
                                currentSeat = ++gamerSeat;
                            }

                            bankerGamer              = room.gamers[currentSeat];
                            bankerHandCards          = bankerGamer.GetComponent <HandCardsComponent>();
                            bankerHandCards.IsBanker = true;
                            room.BankerGamer         = bankerGamer;
                        }
                    }
                    else
                    {
                        if (room.IsFriendRoom)
                        {
                            GameControllerComponent controllerComponent = room.GetComponent <GameControllerComponent>();
                            long masterUserId = controllerComponent.RoomConfig.MasterUserId;
                            bankerGamer = room.Get(masterUserId);
                        }
                        else
                        {
                            //随机庄家
                            int number = RandomHelper.RandomNumber(0, 12);
                            int i      = number % 4;
                            bankerGamer = room.gamers[i];
                        }
                        bankerHandCards          = bankerGamer.GetComponent <HandCardsComponent>();
                        bankerHandCards.IsBanker = true;
                        room.BankerGamer         = bankerGamer;
                    }

                    //发牌
                    gameController.DealCards();
                    orderController.Start(bankerGamer.UserID);

                    //去除花牌
                    foreach (var _gamer in gamers)
                    {
                        HandCardsComponent handCardsComponent = _gamer.GetComponent <HandCardsComponent>();
                        List <MahjongInfo> handCards          = handCardsComponent.GetAll();

                        for (int j = handCards.Count - 1; j >= 0; j--)
                        {
                            MahjongInfo mahjongInfo = handCards[j];

                            if (mahjongInfo.m_weight >= Consts.MahjongWeight.Hua_HongZhong)
                            {
                                handCards.RemoveAt(j);
                                mahjongInfo.weight = (byte)mahjongInfo.m_weight;
                                handCardsComponent.FaceCards.Add(mahjongInfo);
                                handCardsComponent.FaceGangCards.Add(mahjongInfo);
                            }
                        }

                        //加牌
                        int handCardsCount = handCards.Count;
                        for (int j = 0; j < 13 - handCardsCount; j++)
                        {
                            GetCardNotFace(deskComponent, handCardsComponent);
                        }
                    }

                    //庄家多发一张牌
                    GetCardNotFace(deskComponent, bankerHandCards);

//	                List<MahjongInfo> infos = bankerHandCards.GetAll();
//	                bankerHandCards.library = new List<MahjongInfo>(list);

                    //给客户端传送数据
                    Actor_StartGame actorStartGame = new Actor_StartGame();
                    foreach (var itemGame in gamers)
                    {
                        GamerData          gamerData          = new GamerData();
                        HandCardsComponent handCardsComponent = itemGame.GetComponent <HandCardsComponent>();

                        List <MahjongInfo> mahjongInfos = handCardsComponent.library;
                        foreach (var mahjongInfo in mahjongInfos)
                        {
                            mahjongInfo.weight = (byte)mahjongInfo.m_weight;
                        }

                        gamerData.UserID    = itemGame.UserID;
                        gamerData.handCards = mahjongInfos;
                        gamerData.faceCards = handCardsComponent.FaceCards;
                        if (handCardsComponent.IsBanker)
                        {
                            gamerData.IsBanker = true;
                        }

                        gamerData.SeatIndex     = room.seats[itemGame.UserID];
                        gamerData.OnlineSeconds = await DBCommonUtil.GetRestOnlineSeconds(itemGame.UserID);

                        actorStartGame.GamerDatas.Add(gamerData);
                    }

                    actorStartGame.restCount = deskComponent.RestLibrary.Count;
                    GameControllerComponent gameControllerComponent = room.GetComponent <GameControllerComponent>();

                    if (room.IsFriendRoom)
                    {
                        actorStartGame.RoomType       = 3;
                        actorStartGame.CurrentJuCount = room.CurrentJuCount;
                    }
                    else
                    {
                        actorStartGame.RoomType = (int)gameControllerComponent.RoomConfig.Id;
                    }
                    //发送消息
                    room.Broadcast(actorStartGame);
//	                Log.Debug("发送开始:" + JsonHelper.ToJson(actorStartGame));

                    //排序
                    var startTime = DateTime.Now;
                    foreach (var _gamer in gamers)
                    {
                        HandCardsComponent handCardsComponent = _gamer.GetComponent <HandCardsComponent>();
                        //排序
                        handCardsComponent.Sort();
                        handCardsComponent.GrabCard = handCardsComponent.GetAll()[handCardsComponent.GetAll().Count - 1];
                        //设置玩家在线开始时间
                        _gamer.StartTime = startTime;
                        //await DBCommonUtil.RecordGamerTime(startTime, true, _gamer.UserID);
                    }

                    foreach (var _gamer in room.GetAll())
                    {
                        HandCardsComponent handCardsComponent = _gamer.GetComponent <HandCardsComponent>();

                        List <MahjongInfo> cards = handCardsComponent.GetAll();

                        if (handCardsComponent.IsBanker)
                        {
                            if (Logic_NJMJ.getInstance().isHuPai(cards))
                            {
                                //ToDo 胡牌
                                Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation();
                                canOperation.Uid           = _gamer.UserID;
                                _gamer.IsCanHu             = true;
                                canOperation.OperationType = 2;
                                room.GamerBroadcast(_gamer, canOperation);
                                _gamer.isGangFaWanPai = true;
                            }
                        }
                        else
                        {
                            //检查听牌
                            List <MahjongInfo> checkTingPaiList = Logic_NJMJ.getInstance().checkTingPaiList(cards);
                            if (checkTingPaiList.Count > 0)
                            {
                                Log.Info($"{_gamer.UserID}听牌:");
                                _gamer.isFaWanPaiTingPai = true;
                            }
                        }
                    }

                    //等客户端掷骰子
                    //是否超时
                    await Game.Scene.GetComponent <TimerComponent>().WaitAsync(10 * 1000);

                    room.StartTime();
                    //扣服务费
                    if (!room.IsFriendRoom)
                    {
                        //不要动画
                        GameHelp.CostServiceCharge(room, false);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            await Task.CompletedTask;
        }
Example #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;
        }
        /// <summary>
        ///  胡牌
        /// </summary>
        /// <param name="gamer"></param>
        /// <param name="room"></param>
        /// <param name="mahjongInfos"></param>
        /// <param name="b"></param>
        private static int HuPai(Gamer gamer, Room room, List <MahjongInfo> mahjongInfos, bool isZimo)
        {
            room.IsZimo   = isZimo;
            room.huPaiUid = gamer.UserID;
            if (!isZimo)
            {
                gamer.isGangEndBuPai    = false;
                gamer.isGetYingHuaBuPai = false;
            }

            int huaCount = 0;

            DeskComponent            deskComponent   = room.GetComponent <DeskComponent>();
            OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();
            HandCardsComponent       handCards       = gamer.GetComponent <HandCardsComponent>();

            Actor_GamerHuPai actorGamerHuPai = new Actor_GamerHuPai();

            actorGamerHuPai.Uid = gamer.UserID;

            HuPaiNeedData huPaiNeedData = new HuPaiNeedData();

            huPaiNeedData.my_lastMahjong    = room.my_lastMahjong;
            huPaiNeedData.restMahjongCount  = deskComponent.RestLibrary.Count;
            huPaiNeedData.isSelfZhuaPai     = orderController.CurrentAuthority == gamer.UserID;
            huPaiNeedData.isZhuangJia       = handCards.IsBanker;
            huPaiNeedData.isGetYingHuaBuPai = gamer.isGetYingHuaBuPai;
            huPaiNeedData.isGangEndBuPai    = gamer.isGangEndBuPai;
            huPaiNeedData.isGangFaWanPai    = gamer.isGangFaWanPai;
            huPaiNeedData.isFaWanPaiTingPai = gamer.isFaWanPaiTingPai;
            huPaiNeedData.my_yingHuaList    = handCards.FaceCards;
            huPaiNeedData.my_gangList       = handCards.GangCards;
            huPaiNeedData.my_pengList       = handCards.PengCards;

            List <List <MahjongInfo> > temp = new List <List <MahjongInfo> >();

            foreach (var _gamer in room.GetAll())
            {
                _gamer.RemoveComponent <TrusteeshipComponent>();

                if (_gamer.UserID == gamer.UserID)
                {
                    continue;
                }
                HandCardsComponent handCardsComponent = _gamer.GetComponent <HandCardsComponent>();
                temp.Add(handCardsComponent.PengCards);

                //设置其他人的牌
                GamerData gamerData = new GamerData();
                gamerData.handCards = handCardsComponent.GetAll();
                gamerData.UserID    = _gamer.UserID;
                actorGamerHuPai.GamerDatas.Add(gamerData);
            }

            huPaiNeedData.other1_pengList = temp[0];
            huPaiNeedData.other2_pengList = temp[1];
            huPaiNeedData.other3_pengList = temp[2];

            //比下胡
//            if (room.IsLianZhuang)
//            {
//                if (room.BankerGamer.UserID == room.huPaiUid)
//                {
//                    room.LiangZhuangCount++;
//                    actorGamerHuPai.BixiaHuCount = room.LiangZhuangCount * 10;
//                }
//            }
            if (room.IsBiXiaHu)
            {
                actorGamerHuPai.BixiaHuCount = 20;
            }

            List <Consts.HuPaiType> huPaiTypes = Logic_NJMJ.getInstance().getHuPaiType(mahjongInfos, huPaiNeedData);

            foreach (var huPaiType in huPaiTypes)
            {
                if (huPaiType != Consts.HuPaiType.Normal)
                {
                    room.LastBiXiaHu = true;
                }
            }

            //自摸
            actorGamerHuPai.IsZiMo = isZimo;
            if (!isZimo)
            {
                actorGamerHuPai.FangPaoUid = orderController.CurrentAuthority;
                room.Get(orderController.CurrentAuthority).isFangPao = true;
            }
            else
            {
                gamer.isZimo = true;
            }

            //硬花
            actorGamerHuPai.YingHuaCount = handCards.FaceCards.Count;
            //硬花
            huaCount += handCards.FaceCards.Count;
            //软花
            foreach (var pengorbar in handCards.PengOrBars)
            {
                //东南西北风 碰牌
                if (pengorbar.OperateType == OperateType.Peng)
                {
                    if (pengorbar.Weight >= 31 && pengorbar.Weight <= 37)
                    {
                        actorGamerHuPai.RuanHuaCount += 1;
                    }
                }
                else
                {
                    if (pengorbar.BarType == BarType.DarkBar)
                    {
                        //风牌暗杠
                        if (pengorbar.Weight >= 31 && pengorbar.Weight <= 37)
                        {
                            actorGamerHuPai.RuanHuaCount += 3;
                        }
                        //万条筒暗杠
                        else
                        {
                            actorGamerHuPai.RuanHuaCount += 2;
                        }
                    }
                    else
                    {
                        //风牌明杠
                        if (pengorbar.Weight >= 31 && pengorbar.Weight <= 37)
                        {
                            actorGamerHuPai.RuanHuaCount += 2;
                        }
                        //万条筒明杠
                        else
                        {
                            actorGamerHuPai.RuanHuaCount += 1;
                        }
                    }
                }
            }

            int fengKeHuaShu = Logic_NJMJ.getInstance().getFengKeHuaShu(mahjongInfos);

            //将碰刚将入手牌
            foreach (var pengorbar in handCards.PengOrBars)
            {
                if (pengorbar.OperateType == OperateType.Peng)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        mahjongInfos.Add(new MahjongInfo()
                        {
                            m_weight = (Consts.MahjongWeight)pengorbar.Weight,
                            weight   = (byte)pengorbar.Weight
                        });
                    }
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                    {
                        mahjongInfos.Add(new MahjongInfo()
                        {
                            m_weight = (Consts.MahjongWeight)pengorbar.Weight,
                            weight   = (byte)pengorbar.Weight
                        });
                    }
                }
            }

            int queYiMenHuaShu = Logic_NJMJ.getInstance().getQueYiMenHuaShu(mahjongInfos);

            actorGamerHuPai.RuanHuaCount += queYiMenHuaShu;
            actorGamerHuPai.RuanHuaCount += fengKeHuaShu;

            huaCount += actorGamerHuPai.RuanHuaCount;
            //胡牌类型
            foreach (var type in huPaiTypes)
            {
                actorGamerHuPai.HuPaiTypes.Add((int)type);
            }

            room.Broadcast(actorGamerHuPai);

            room.IsGameOver = true;
            gamer.IsCanHu   = false;
            gamer.IsWinner  = true;

            foreach (var item in huPaiTypes)
            {
                Log.Info("有人胡牌:" + item.ToString());
            }

            Log.Info("huPaiNeedData:" + JsonHelper.ToJson(huPaiNeedData));

            //设置胡牌的花数
            for (int j = 0; j < huPaiTypes.Count; j++)
            {
                Consts.HuPaiType huPaiType = (Consts.HuPaiType)huPaiTypes[j];
                int count;
                Logic_NJMJ.getInstance().HuPaiHuaCount.TryGetValue(huPaiType, out count);
                //胡牌花
                huaCount += count;
            }

            //基数
            huaCount += 20;
            huaCount += actorGamerHuPai.BixiaHuCount;
            huaCount *= 2;

            return(huaCount);
        }