/// <summary> /// 提示 /// </summary> private async void OnPrompt() { long playerId = ClientComponent.Instance.LocalPlayer.Id; PromptRe promptRE = await SessionComponent.Instance.Session.Call <PromptRe>(new PromptRt() { PlayerId = playerId }); HandCardsComponent handCards = this.Entity.Parent.GetComponent <GamerComponent>().LocalGamer.GetComponent <HandCardsComponent>(); //清空当前选中 while (currentSelectCards.Count > 0) { Card selectCard = currentSelectCards[currentSelectCards.Count - 1]; handCards.GetSprite(selectCard).GetComponent <HandCardSprite>().OnClick(null); } //自动选中提示出牌 if (promptRE.Cards != null) { for (int i = 0; i < promptRE.Cards.Length; i++) { handCards.GetSprite(promptRE.Cards[i]).GetComponent <HandCardSprite>().OnClick(null); } } }
/// <summary> /// 发地主牌 /// </summary> /// <param name="self"></param> /// <param name="id"></param> public static void CardsOnTable(this GameControllerComponent self, long id) { Room room = self.GetEntity <Room>(); DeskCardsCacheComponent deskCardsCache = room.GetComponent <DeskCardsCacheComponent>(); OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>(); HandCardsComponent handCards = room.Get(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.GetAll()) { Identity gamerIdentity = gamer.Id == id ? Identity.Landlord : Identity.Farmer; self.UpdateInIdentity(gamer, gamerIdentity); } //广播地主消息 room.Broadcast(new SelectLord() { PlayerId = id, LordCards = deskCardsCache.LordCards.ToArray() }); //广播地主先手出牌消息 room.Broadcast(new AuthorityPlayCard() { PlayerId = id, IsFirst = true }); }
protected override void Run(GameStart message) { UI uiRoom = Hotfix.Scene.GetComponent <UIComponent>().Get(UIType.Room); GamerComponent gamerComponent = uiRoom.GetComponent <GamerComponent>(); //初始化玩家UI foreach (var gamer in gamerComponent.GetAll()) { GamerUIComponent gamerUI = gamer.GetComponent <GamerUIComponent>(); gamerUI.GameStart(); HandCardsComponent handCards = gamer.GetComponent <HandCardsComponent>(); if (handCards != null) { handCards.Reset(); } else { handCards = gamer.AddComponent <HandCardsComponent, GameObject>(gamerUI.Panel); } handCards.Appear(); if (gamer.Id == gamerComponent.LocalGamer.Id) { //本地玩家添加手牌 handCards.AddCards(message.GamerCards); } else { //设置其他玩家手牌数 handCards.SetHandCardsNum(message.GamerCardsNum[gamer.Id]); } } //显示牌桌UI GameObject desk = uiRoom.GameObject.Get <GameObject>("Desk"); desk.SetActive(true); GameObject lordPokers = desk.Get <GameObject>("LordPokers"); //重置地主牌 Sprite lordSprite = Resources.Load <GameObject>("UI").Get <GameObject>("Atlas").Get <Sprite>("None"); for (int i = 0; i < lordPokers.transform.childCount; i++) { lordPokers.transform.GetChild(i).GetComponent <Image>().sprite = lordSprite; } UIRoomComponent uiRoomComponent = uiRoom.GetComponent <UIRoomComponent>(); //清空选中牌 uiRoom.GetComponent <UIRoomComponent>().Interaction.Clear(); //设置初始倍率 uiRoomComponent.SetMultiples(1); }
protected override void Run(SelectLord message) { UI uiRoom = Hotfix.Scene.GetComponent <UIComponent>().Get(UIType.Room); GamerComponent gamerComponent = uiRoom.GetComponent <GamerComponent>(); Gamer gamer = gamerComponent.Get(message.PlayerID); if (gamer != null) { HandCardsComponent handCards = gamer.GetComponent <HandCardsComponent>(); if (gamer.Id == gamerComponent.LocalGamer.Id) { //本地玩家添加手牌 handCards.AddCards(message.LordCards); } else { //其他玩家设置手牌数 handCards.SetHandCardsNum(20); } } foreach (var _gamer in gamerComponent.GetAll()) { if (_gamer.Id == message.PlayerID) { _gamer.GetComponent <HandCardsComponent>().AccessIdentity = Identity.Landlord; _gamer.GetComponent <GamerUIComponent>().SetIdentity(Identity.Landlord); } else { _gamer.GetComponent <HandCardsComponent>().AccessIdentity = Identity.Farmer; _gamer.GetComponent <GamerUIComponent>().SetIdentity(Identity.Farmer); } } //重置玩家UI提示 foreach (var _gamer in gamerComponent.GetAll()) { _gamer.GetComponent <GamerUIComponent>().ResetPrompt(); } //切换地主牌精灵 GameObject lordPokers = uiRoom.GameObject.Get <GameObject>("Desk").Get <GameObject>("LordPokers"); for (int i = 0; i < lordPokers.transform.childCount; i++) { Sprite lordCardSprite = Resources.Load <GameObject>("UI").Get <GameObject>("Atlas").Get <Sprite>(message.LordCards[i].GetName()); lordPokers.transform.GetChild(i).GetComponent <Image>().sprite = lordCardSprite; } //显示切换游戏模式按钮 uiRoom.GetComponent <UIRoomComponent>().Interaction.GameStart(); }
/// <summary> /// 计算玩家积分 /// </summary> /// <param name="self"></param> /// <param name="gamer"></param> /// <param name="winnerIdentity"></param> /// <returns></returns> public static long GetScore(this GameControllerComponent self, Gamer gamer, Identity winnerIdentity) { HandCardsComponent handCards = gamer.GetComponent <HandCardsComponent>(); //积分计算公式:全场底分 * 全场倍率 * 身份倍率 long integration = self.BasePointPerMatch * self.Multiples * (int)handCards.AccessIdentity; //当玩家不是胜者,结算积分为负 if (handCards.AccessIdentity != winnerIdentity) { integration = -integration; } return(integration); }
protected override void Run(GamerPlayCards message) { UI uiRoom = Hotfix.Scene.GetComponent <UIComponent>().Get(UIType.Room); GamerComponent gamerComponent = uiRoom.GetComponent <GamerComponent>(); Gamer gamer = gamerComponent.Get(message.PlayerID); if (gamer != null) { gamer.GetComponent <GamerUIComponent>().ResetPrompt(); if (gamer.Id == gamerComponent.LocalGamer.Id) { InteractionComponent interaction = uiRoom.GetComponent <UIRoomComponent>().Interaction; interaction.Clear(); interaction.EndPlay(); } HandCardsComponent handCards = gamer.GetComponent <HandCardsComponent>(); handCards.PopCards(message.Cards); } }
protected override void Run(GamerReconnect message) { UI uiRoom = Hotfix.Scene.GetComponent <UIComponent>().Get(UIType.Room); GamerComponent gamerComponent = uiRoom.GetComponent <GamerComponent>(); if (message.PlayerID == gamerComponent.LocalGamer.Id) { uiRoom.GameObject.Get <GameObject>("ReadyButton").SetActive(false); foreach (var gamer in gamerComponent.GetAll()) { //初始化玩家身份 Identity gamerIdentity = message.GamersIdentity[gamer.Id]; HandCardsComponent gamerHandCards = gamer.GetComponent <HandCardsComponent>(); gamerHandCards.AccessIdentity = gamerIdentity; gamer.GetComponent <GamerUIComponent>().SetIdentity(gamerIdentity); //初始化出牌 if (message.DeskCards.Key == gamer.Id && gamerIdentity != Identity.None) { gamerHandCards.PopCards(message.DeskCards.Value); } } } //初始化界面 UIRoomComponent uiRoomComponent = uiRoom.GetComponent <UIRoomComponent>(); uiRoomComponent.SetMultiples(message.Multiples); uiRoomComponent.Interaction.GameStart(); //初始化地主牌 if (message.LordCards != null) { GameObject lordPokers = uiRoom.GameObject.Get <GameObject>("Desk").Get <GameObject>("LordPokers"); for (int i = 0; i < lordPokers.transform.childCount; i++) { Sprite lordCardSprite = Resources.Load <GameObject>("UI").Get <GameObject>("Atlas").Get <Sprite>(message.LordCards[i].GetName()); lordPokers.transform.GetChild(i).GetComponent <Image>().sprite = lordCardSprite; } } }
/// <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 entity, PlayerReady message) { Gamer gamer = entity.Get(message.PlayerID); if (gamer != null) { gamer.IsReady = true; Gamer[] gamers = entity.GetAll(); //转发玩家准备消息 entity.Broadcast(message); Log.Info($"玩家{gamer.Id}准备"); //房间内有3名玩家且全部准备则开始游戏 if (entity.Count == 3 && gamers.Where(g => g.IsReady).Count() == 3) { //同步匹配服务器开始游戏 entity.State = RoomState.Game; MapHelper.SendMessage(new SyncRoomState() { RoomID = entity.Id, State = entity.State }); //初始玩家开始状态 foreach (var _gamer in gamers) { if (_gamer.GetComponent <HandCardsComponent>() == null) { _gamer.AddComponent <HandCardsComponent>(); } _gamer.IsReady = false; } GameControllerComponent gameController = entity.GetComponent <GameControllerComponent>(); //洗牌发牌 gameController.DealCards(); Dictionary <long, int> gamerCardsNum = new Dictionary <long, int>(); Array.ForEach(gamers, (g) => { HandCardsComponent handCards = g.GetComponent <HandCardsComponent>(); //重置玩家身份 handCards.AccessIdentity = Identity.None; //记录玩家手牌数 gamerCardsNum.Add(g.Id, handCards.CardsCount); }); //发送玩家手牌和其他玩家手牌数 foreach (var _gamer in gamers) { ActorProxy actorProxy = _gamer.GetComponent <UnitGateComponent>().GetActorProxy(); actorProxy.Send(new GameStart() { GamerCards = _gamer.GetComponent <HandCardsComponent>().GetAll(), GamerCardsNum = gamerCardsNum }); } //随机先手玩家 gameController.RandomFirstAuthority(); Log.Info($"房间{entity.Id}开始游戏"); } } return(Task.CompletedTask); }
public static Card[] GetAll(this HandCardsComponent self) { return(self.Library.ToArray()); }
/// <summary> /// 手牌排序 /// </summary> /// <param name="self"></param> public static void Sort(this HandCardsComponent self) { CardsHelper.SortCards(self.Library); }
/// <summary> /// 出牌 /// </summary> /// <param name="self"></param> /// <param name="card"></param> public static void PopCard(this HandCardsComponent self, Card card) { self.Library.Remove(card); }
/// <summary> /// 向牌库中添加牌 /// </summary> /// <param name="card"></param> public static void AddCard(this HandCardsComponent self, Card card) { self.Library.Add(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); }
protected override Task Run(Room entity, PlayerReconnect message) { Gamer gamer = entity.GetAll().Where(g => g.UserId == message.UserId).FirstOrDefault(); if (gamer != null) { long pastId = gamer.Id; gamer.Id = message.PlayerId; gamer.isOffline = false; entity.Replace(pastId, gamer); gamer.RemoveComponent <AutoPlayCardsComponent>(); UnitGateComponent unitGateComponent = gamer.GetComponent <UnitGateComponent>(); unitGateComponent.GateSessionId = message.GateSessionId; ActorProxy actorProxy = unitGateComponent.GetActorProxy(); OrderControllerComponent orderController = entity.GetComponent <OrderControllerComponent>(); DeskCardsCacheComponent deskCardsCache = entity.GetComponent <DeskCardsCacheComponent>(); GameControllerComponent gameController = entity.GetComponent <GameControllerComponent>(); //替换过期玩家ID if (orderController.FirstAuthority.Key == pastId) { orderController.FirstAuthority = new KeyValuePair <long, bool>(gamer.Id, orderController.FirstAuthority.Value); } if (orderController.Biggest == pastId) { orderController.Biggest = gamer.Id; } if (orderController.CurrentAuthority == pastId) { orderController.CurrentAuthority = gamer.Id; } entity.Broadcast(new GamerReenter() { PastId = pastId, NewId = gamer.Id }); //发送房间玩家信息 Gamer[] gamers = entity.GetAll(); GamerInfo[] gamersInfo = new GamerInfo[gamers.Length]; for (int i = 0; i < gamers.Length; i++) { gamersInfo[i] = new GamerInfo(); gamersInfo[i].PlayerId = gamers[i].Id; gamersInfo[i].UserId = gamers[i].UserId; gamersInfo[i].IsReady = gamers[i].IsReady; } actorProxy.Send(new GamerEnter() { RoomId = entity.Id, GamersInfo = gamersInfo }); Dictionary <long, int> gamerCardsNum = new Dictionary <long, int>(); Dictionary <long, Identity> gamersIdentity = new Dictionary <long, Identity>(); Array.ForEach(gamers, (g) => { HandCardsComponent handCards = g.GetComponent <HandCardsComponent>(); gamerCardsNum.Add(g.Id, handCards.CardsCount); gamersIdentity.Add(g.Id, handCards.AccessIdentity); }); //发送玩家手牌 actorProxy.Send(new GameStart() { GamerCards = gamer.GetComponent <HandCardsComponent>().GetAll(), GamerCardsNum = gamerCardsNum }); Card[] lordCards = null; if (gamer.GetComponent <HandCardsComponent>().AccessIdentity == Identity.None) { //广播先手玩家 entity.Broadcast(new SelectAuthority() { PlayerId = orderController.CurrentAuthority }); } else { lordCards = deskCardsCache.LordCards.ToArray(); } //发送重连消息 actorProxy.Send(new GamerReconnect() { PlayerId = gamer.Id, Multiples = gameController.Multiples, GamersIdentity = gamersIdentity, LordCards = lordCards, DeskCards = new KeyValuePair <long, Card[]>(orderController.Biggest, deskCardsCache.library.ToArray()) }); //发送当前出牌者消息 bool isFirst = orderController.Biggest == orderController.CurrentAuthority; actorProxy.Send(new AuthorityPlayCard() { PlayerId = orderController.CurrentAuthority, IsFirst = isFirst }); Log.Info($"玩家{gamer.Id}重连"); } return(Task.CompletedTask); }