protected override async Task Run(Room unit, Prompt_RT message, Action <Prompt_RE> reply)
        {
            Prompt_RE response = new Prompt_RE();

            try
            {
                Gamer gamer = unit.Get(message.PlayerID);
                if (gamer != null)
                {
                    List <Card> handCards = new List <Card>(gamer.GetComponent <HandCardsComponent>().GetAll());
                    CardsHelper.SortCards(handCards);
                    if (gamer.Id == unit.GetComponent <OrderControllerComponent>().Biggest)
                    {
                        response.Cards = handCards.Where(card => card.CardWeight == handCards[handCards.Count - 1].CardWeight).ToArray();
                    }
                    else
                    {
                        DeskCardsCacheComponent deskCardsCache = unit.GetComponent <DeskCardsCacheComponent>();
                        List <Card[]>           result         = await CardsHelper.GetPrompt(handCards, deskCardsCache, deskCardsCache.Rule);

                        if (result.Count > 0)
                        {
                            response.Cards = result[RandomHelper.RandomNumber(0, result.Count)];
                        }
                    }
                }
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Beispiel #2
0
 /// <summary>
 /// 手牌排序
 /// </summary>
 /// <param name="self"></param>
 public static void Sort(this DeskCardsCacheComponent self)
 {
     CardsHelper.SortCards(self.library);
 }
Beispiel #3
0
 /// <summary>
 /// 获取总权值
 /// </summary>
 /// <param name="self"></param>
 /// <returns></returns>
 public static int GetTotalWeight(this DeskCardsCacheComponent self)
 {
     return(CardsHelper.GetWeight(self.library.ToArray(), self.Rule));
 }
Beispiel #4
0
 /// <summary>
 /// 手牌排序
 /// </summary>
 /// <param name="self"></param>
 public static void Sort(this HandCardsComponent self)
 {
     CardsHelper.SortCards(self.Library);
 }
        protected override Task Run(Room unit, PlayCards_RT message, Action <PlayCards_RE> reply)
        {
            PlayCards_RE response = new PlayCards_RE();

            try
            {
                Gamer gamer = unit.Get(message.PlayerID);
                if (gamer == null)
                {
                    response.Error = ErrorCode.ERR_PlayCardError;
                    reply(response);
                    return(Task.CompletedTask);
                }

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

                //检测是否符合出牌规则
                if (CardsHelper.PopEnable(message.Cards, out CardsType type))
                {
                    if (orderController.Biggest == orderController.CurrentAuthority ||
                        type == CardsType.JokerBoom ||
                        type == CardsType.Boom && CardsHelper.GetWeight(message.Cards, type) > deskCardsCache.GetTotalWeight() ||
                        (deskCardsCache.Rule == CardsType.Straight || deskCardsCache.Rule == CardsType.DoubleStraight || deskCardsCache.Rule == CardsType.TripleStraight) && type == deskCardsCache.Rule && message.Cards.Length == deskCardsCache.GetAll().Length&& CardsHelper.GetWeight(message.Cards, type) > deskCardsCache.GetTotalWeight() ||
                        type == deskCardsCache.Rule && CardsHelper.GetWeight(message.Cards, type) > deskCardsCache.GetTotalWeight())
                    {
                        if (type == CardsType.JokerBoom)
                        {
                            gameController.Multiples *= 4;
                            unit.Broadcast(new GameMultiples()
                            {
                                Multiples = gameController.Multiples
                            });
                        }
                        else if (type == CardsType.Boom)
                        {
                            gameController.Multiples *= 2;
                            unit.Broadcast(new GameMultiples()
                            {
                                Multiples = gameController.Multiples
                            });
                        }
                    }
                    else
                    {
                        response.Error = ErrorCode.ERR_PlayCardError;
                        reply(response);
                        return(Task.CompletedTask);
                    }
                }
                else
                {
                    response.Error = ErrorCode.ERR_PlayCardError;
                    reply(response);
                    return(Task.CompletedTask);
                }

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

                //转发玩家出牌消息
                unit.Broadcast(new GamerPlayCards()
                {
                    PlayerID = gamer.Id, Cards = message.Cards
                });

                if (handCards.CardsCount == 0)
                {
                    Identity winnerIdentity             = unit.Get(orderController.Biggest).GetComponent <HandCardsComponent>().AccessIdentity;
                    Dictionary <long, long> gamersScore = new Dictionary <long, long>();

                    foreach (var _gamer in unit.GetAll())
                    {
                        _gamer.RemoveComponent <AutoPlayCardsComponent>();
                        gamersScore.Add(_gamer.Id, gameController.GetScore(_gamer, winnerIdentity));
                        //玩家剩余出牌
                        if (_gamer.Id != message.PlayerID)
                        {
                            Card[] _gamerCards = _gamer.GetComponent <HandCardsComponent>().GetAll();
                            unit.Broadcast(new GamerPlayCards()
                            {
                                PlayerID = _gamer.Id, Cards = _gamerCards
                            });
                        }
                    }

                    //游戏结束结算
                    gameController.GameOver(gamersScore);

                    //广播游戏结束消息
                    unit.Broadcast(new Gameover()
                    {
                        Winner            = winnerIdentity,
                        BasePointPerMatch = gameController.BasePointPerMatch,
                        Multiples         = gameController.Multiples,
                        GamersScore       = gamersScore
                    });
                }
                else
                {
                    //轮到下位玩家出牌
                    orderController.Biggest = gamer.Id;
                    orderController.Turn();
                    unit.Broadcast(new AuthorityPlayCard()
                    {
                        PlayerID = orderController.CurrentAuthority, IsFirst = false
                    });
                }
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
            return(Task.CompletedTask);
        }