예제 #1
0
        protected override async void Run(Gamer gamer, Actor_GamerDontPlay_Ntt message)
        {
            LandlordsRoom            room            = Game.Scene.GetComponent <LandlordsComponent>().GetGamingRoom(gamer);
            OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();

            if (orderController.CurrentAuthority == gamer.UserID)
            {
                //转发玩家不出牌消息
                Actor_GamerDontPlay_Ntt transpond = new Actor_GamerDontPlay_Ntt();
                transpond.UserID = gamer.UserID;
                room.Broadcast(transpond);

                //轮到下位玩家出牌
                orderController.Turn();

                //判断是否先手
                bool isFirst = orderController.CurrentAuthority == orderController.Biggest;
                if (isFirst)
                {
                    room.GetComponent <DeskCardsCacheComponent>().Clear();
                }
                room.Broadcast(new Actor_AuthorityPlayCard_Ntt()
                {
                    UserID = orderController.CurrentAuthority, IsFirst = isFirst
                });
            }
            await Task.CompletedTask;
        }
        /// <summary>
        /// 发地主牌
        /// </summary>
        /// <param name="self"></param>
        /// <param name="id"></param>
        public static void CardsOnTable(this GameControllerComponent self, long id)
        {
            LandlordsRoom            room            = self.GetParent <LandlordsRoom>();
            DeskCardsCacheComponent  deskCardsCache  = room.GetComponent <DeskCardsCacheComponent>();
            OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();
            HandCardsComponent       handCards       = room.GetGamerFromUserID(id).GetComponent <HandCardsComponent>();

            orderController.Start(id);

            for (int i = 0; i < 3; i++)
            {
                Card card = deskCardsCache.Deal();
                handCards.AddCard(card);
            }

            //更新玩家身份
            foreach (var gamer in room.gamers)
            {
                Identity gamerIdentity = gamer.UserID == id ? Identity.Landlord : Identity.Farmer;
                self.UpdateInIdentity(gamer, gamerIdentity);
            }

            //广播地主消息
            room.Broadcast(new Actor_SetLandlord_Ntt()
            {
                UserID = id, LordCards = To.RepeatedField(deskCardsCache.LordCards)
            });

            //广播地主先手出牌消息
            room.Broadcast(new Actor_AuthorityPlayCard_Ntt()
            {
                UserID = id, IsFirst = true
            });
        }
        /// <summary>
        /// 游戏继续
        /// </summary>
        /// <param name="self"></param>
        /// <param name="lastGamer"></param>
        public static void Continue(this GameControllerComponent self, Gamer lastGamer)
        {
            LandlordsRoom            room            = self.GetParent <LandlordsRoom>();
            OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();

            //是否结束,当前出牌者手牌数为0时游戏结束
            bool isEnd = lastGamer.GetComponent <HandCardsComponent>().CardsCount == 0;

            if (isEnd)
            {
                //当前最大出牌者为赢家
                Identity          winnerIdentity = room.GetGamerFromUserID(orderController.Biggest).GetComponent <HandCardsComponent>().AccessIdentity;
                List <GamerScore> gamersScore    = new List <GamerScore>();

                //游戏结束所有玩家摊牌
                foreach (var gamer in room.gamers)
                {
                    //取消托管
                    gamer.RemoveComponent <TrusteeshipComponent>();
                    //计算玩家积分
                    gamersScore.Add(new GamerScore()
                    {
                        UserID = gamer.UserID,
                        Score  = self.GetGamerScore(gamer, winnerIdentity)
                    });

                    if (gamer.UserID != lastGamer.UserID)
                    {
                        //剩余玩家摊牌
                        Card[] _gamerCards = gamer.GetComponent <HandCardsComponent>().GetAll();
                        room.Broadcast(new Actor_GamerPlayCard_Ntt()
                        {
                            UserID = gamer.UserID, Cards = To.RepeatedField(_gamerCards)
                        });
                    }
                }

                self.GameOver(gamersScore, winnerIdentity);
            }
            else
            {
                //轮到下位玩家出牌
                orderController.Biggest = lastGamer.UserID;
                orderController.Turn();
                room.Broadcast(new Actor_AuthorityPlayCard_Ntt()
                {
                    UserID = orderController.CurrentAuthority, IsFirst = false
                });
            }
        }
예제 #4
0
        protected override void Run(Gamer gamer, Actor_GamerReady_Landlords message)
        {
            LandlordsComponent landordsMatchComponent = Game.Scene.GetComponent <LandlordsComponent>();
            LandlordsRoom      room = landordsMatchComponent.GetWaitingRoom(gamer);

            if (room != null)
            {
                //找到玩家的座位顺序 设置其准备状态为真
                int seatIndex = room.GetGamerSeat(gamer.UserID);
                if (seatIndex >= 0)
                {
                    room.isReadys[seatIndex] = true;
                    //广播通知全房间玩家
                    room.Broadcast(new Actor_GamerReady_Landlords()
                    {
                        UserID = gamer.UserID
                    });
                    //检测开始游戏
                    room.CheckGameStart();
                }
                else
                {
                    Log.Error("玩家不在正确的座位上");
                }
            }
        }
        /// <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 });
                }
            }
        }
        /// <summary>
        /// 随机先手玩家
        /// </summary>
        public static void RandomFirstAuthority(this GameControllerComponent self)
        {
            LandlordsRoom            room            = self.GetParent <LandlordsRoom>();
            OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();

            Gamer[] gamers = room.gamers;

            int  index          = RandomHelper.RandomNumber(0, gamers.Length);
            long firstAuthority = gamers[index].UserID;

            orderController.Init(firstAuthority);

            //广播先手抢地主玩家
            room.Broadcast(new Actor_AuthorityGrabLandlord_Ntt()
            {
                UserID = firstAuthority
            });
        }
예제 #8
0
        protected override async ETTask Run(Gamer gamer, Actor_GamerPlayCard_Req message, Action <Actor_GamerPlayCard_Back> reply)
        {
            Actor_GamerPlayCard_Back response = new Actor_GamerPlayCard_Back();

            try
            {
                LandlordsRoom room = Game.Scene.GetComponent <LandlordsComponent>().GetGamingRoom(gamer);

                GameControllerComponent  gameController  = room.GetComponent <GameControllerComponent>();
                DeskCardsCacheComponent  deskCardsCache  = room.GetComponent <DeskCardsCacheComponent>();
                OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>();

                //检测是否符合出牌规则
                if (CardHelper.PopEnable(To.Array(message.Cards), out CardsType type))
                {
                    //当前出牌牌型是否比牌桌上牌型的权重更大
                    bool isWeightGreater = CardHelper.GetWeight(To.Array(message.Cards), type) > deskCardsCache.GetTotalWeight();
                    //当前出牌牌型是否和牌桌上牌型的数量一样
                    bool isSameCardsNum = message.Cards.Count == deskCardsCache.GetAll().Length;
                    //当前出牌玩家是否是上局最大出牌者
                    bool isBiggest = orderController.Biggest == orderController.CurrentAuthority;
                    //当前牌桌牌型是否是顺子
                    bool isStraight = deskCardsCache.Rule == CardsType.Straight || deskCardsCache.Rule == CardsType.DoubleStraight || deskCardsCache.Rule == CardsType.TripleStraight;
                    //当前出牌牌型是否和牌桌上牌型一样
                    bool isSameCardsType = type == deskCardsCache.Rule;

                    if (isBiggest ||                                                          //先手出牌玩家
                        type == CardsType.JokerBoom ||                                        //王炸
                        type == CardsType.Boom && isWeightGreater ||                          //更大的炸弹
                        isSameCardsType && isStraight && isSameCardsNum && isWeightGreater || //更大的顺子
                        isSameCardsType && isWeightGreater)                                   //更大的同类型牌
                    {
                        if (type == CardsType.JokerBoom)
                        {
                            //王炸翻4倍
                            gameController.Multiples *= 4;
                            room.Broadcast(new Actor_SetMultiples_Ntt()
                            {
                                Multiples = gameController.Multiples
                            });
                        }
                        else if (type == CardsType.Boom)
                        {
                            //炸弹翻2倍
                            gameController.Multiples *= 2;
                            room.Broadcast(new Actor_SetMultiples_Ntt()
                            {
                                Multiples = gameController.Multiples
                            });
                        }
                    }
                    else
                    {
                        response.Error = ErrorCode.ERR_PlayCardError;
                        reply(response);
                        return;
                    }
                }
                else
                {
                    response.Error = ErrorCode.ERR_PlayCardError;
                    reply(response);
                    return;
                }

                //如果符合将牌从手牌移到出牌缓存区
                deskCardsCache.Clear();
                deskCardsCache.Rule = type;
                HandCardsComponent handCards = gamer.GetComponent <HandCardsComponent>();
                foreach (var card in message.Cards)
                {
                    handCards.PopCard(card);
                    deskCardsCache.AddCard(card);
                }

                reply(response);

                //转发玩家出牌消息
                room.Broadcast(new Actor_GamerPlayCard_Ntt()
                {
                    UserID = gamer.UserID, Cards = message.Cards
                });

                //游戏控制器继续游戏
                gameController.Continue(gamer);

                await ETTask.CompletedTask;
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
예제 #9
0
        protected override async void Run(Gamer gamer, Actor_GamerGrabLandlordSelect_Ntt message)
        {
            LandlordsRoom               room                = Game.Scene.GetComponent <LandlordsComponent>().GetGamingRoom(gamer);
            OrderControllerComponent    orderController     = room.GetComponent <OrderControllerComponent>();
            GameControllerComponent     gameController      = room.GetComponent <GameControllerComponent>();
            ActorMessageSenderComponent actorProxyComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();

            if (orderController.CurrentAuthority == gamer.UserID)
            {
                //保存抢地主状态
                orderController.GamerLandlordState[gamer.UserID] = message.IsGrab;

                if (message.IsGrab)
                {
                    orderController.Biggest   = gamer.UserID;
                    gameController.Multiples *= 2;
                    room.Broadcast(new Actor_SetMultiples_Ntt()
                    {
                        Multiples = gameController.Multiples
                    });
                }

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

                if (orderController.SelectLordIndex >= room.Count)
                {
                    /*
                     * 地主:√ 农民1:× 农民2:×
                     * 地主:× 农民1:√ 农民2:√
                     * 地主:√ 农民1:√ 农民2:√ 地主:√
                     *
                     * */
                    if (orderController.Biggest == 0)
                    {
                        //没人抢地主则重新发牌
                        gameController.BackToDeck();
                        gameController.DealCards();

                        //发送玩家手牌
                        Gamer[]             gamers        = room.gamers;
                        List <GamerCardNum> gamersCardNum = new List <GamerCardNum>();
                        Array.ForEach(gamers, _gamer => gamersCardNum.Add(new GamerCardNum()
                        {
                            UserID = _gamer.UserID,
                            Num    = _gamer.GetComponent <HandCardsComponent>().GetAll().Length
                        }));
                        Array.ForEach(gamers, _gamer =>
                        {
                            ActorMessageSender actorProxy = actorProxyComponent.Get(_gamer.ActorIDofClient);
                            actorProxy.Send(new Actor_GameStart_Ntt()
                            {
                                HandCards     = To.RepeatedField(_gamer.GetComponent <HandCardsComponent>().GetAll()),
                                GamersCardNum = To.RepeatedField(gamersCardNum)
                            });
                        });

                        //随机先手玩家
                        gameController.RandomFirstAuthority();
                        return;
                    }
                    else if ((orderController.SelectLordIndex == room.Count &&
                              ((orderController.Biggest != orderController.FirstAuthority.Key && !orderController.FirstAuthority.Value) ||
                               orderController.Biggest == orderController.FirstAuthority.Key)) ||
                             orderController.SelectLordIndex > room.Count)
                    {
                        gameController.CardsOnTable(orderController.Biggest);
                        return;
                    }
                }

                //当所有玩家都抢地主时先手玩家还有一次抢地主的机会
                if (gamer.UserID == orderController.FirstAuthority.Key && message.IsGrab)
                {
                    orderController.FirstAuthority = new KeyValuePair <long, bool>(gamer.UserID, true);
                }

                orderController.Turn();
                orderController.SelectLordIndex++;
                room.Broadcast(new Actor_AuthorityGrabLandlord_Ntt()
                {
                    UserID = orderController.CurrentAuthority
                });
            }
            await Task.CompletedTask;
        }