/// <summary> /// 场上的所有牌回到牌库中 /// </summary> /// <param name="self"></param> public static void BackToDeck(this GameControllerComponent self) { Room room = self.GetEntity <Room>(); DeckComponent deckComponent = room.GetComponent <DeckComponent>(); DeskCardsCacheComponent deskCardsCache = room.GetComponent <DeskCardsCacheComponent>(); //回收牌桌卡牌 deskCardsCache.Clear(); deskCardsCache.LordCards.Clear(); //回收玩家手牌 foreach (var gamer in room.GetAll()) { HandCardsComponent handCards = gamer.GetComponent <HandCardsComponent>(); while (handCards.CardsCount > 0) { Card card = handCards.Library[handCards.CardsCount - 1]; handCards.PopCard(card); deckComponent.AddCard(card); } } }
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); }