/// <summary> /// 创建玩家结算信息 /// </summary> /// <returns></returns> public GameObject CreateGamerContent(Gamer gamer, Identity winnerIdentity, long baseScore, int multiples, long score) { GameObject newContent = UnityEngine.Object.Instantiate(contentPrefab); newContent.transform.SetParent(gamerContent.transform, false); Identity gamerIdentity = gamer.GetComponent <HandCardsComponent>().AccessIdentity; Sprite identitySprite = Resources.Load <GameObject>("UI").Get <GameObject>("Atlas").Get <Sprite>($"Identity_{Enum.GetName(typeof(Identity), gamerIdentity)}"); newContent.Get <GameObject>("Identity").GetComponent <Image>().sprite = identitySprite; string nickName = gamer.GetComponent <GamerUIComponent>().NickName; Text nickNameText = newContent.Get <GameObject>("NickName").GetComponent <Text>(); Text baseScoreText = newContent.Get <GameObject>("BaseScore").GetComponent <Text>(); Text multiplesText = newContent.Get <GameObject>("Multiples").GetComponent <Text>(); Text scoreText = newContent.Get <GameObject>("Score").GetComponent <Text>(); nickNameText.text = nickName; baseScoreText.text = baseScore.ToString(); multiplesText.text = multiples.ToString(); scoreText.text = score.ToString(); if (gamer.Id == this.Entity.Parent.GetComponent <GamerComponent>().LocalGamer.Id) { nickNameText.color = Color.red; baseScoreText.color = Color.red; multiplesText.color = Color.red; scoreText.color = Color.red; } return(newContent); }
protected override void Run(AuthorityPlayCard 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 (message.IsFirst) { gamer.GetComponent <HandCardsComponent>().ClearPlayCards(); } //显示出牌按钮 if (gamer.Id == gamerComponent.LocalGamer.Id) { InteractionComponent interaction = uiRoom.GetComponent <UIRoomComponent>().Interaction; interaction.IsFirst = message.IsFirst; interaction.StartPlay(); } } }
protected override void Run(Discard message) { UI uiRoom = Hotfix.Scene.GetComponent <UIComponent>().Get(UIType.Room); GamerComponent gamerComponent = uiRoom.GetComponent <GamerComponent>(); Gamer gamer = gamerComponent.Get(message.PlayerID); if (gamer != null) { if (gamer.Id == gamerComponent.LocalGamer.Id) { uiRoom.GetComponent <UIRoomComponent>().Interaction.EndPlay(); } gamer.GetComponent <HandCardsComponent>().ClearPlayCards(); gamer.GetComponent <GamerUIComponent>().SetDiscard(); } }
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); } }
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(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(); }
protected override void Run(PlayerReady message) { UI uiRoom = Hotfix.Scene.GetComponent <UIComponent>().Get(UIType.Room); GamerComponent gamerComponent = uiRoom.GetComponent <GamerComponent>(); Gamer gamer = gamerComponent.Get(message.PlayerID); gamer.GetComponent <GamerUIComponent>().SetReady(); //本地玩家准备,隐藏准备按钮 if (gamer.Id == gamerComponent.LocalGamer.Id) { uiRoom.GameObject.Get <GameObject>("ReadyButton").SetActive(false); } }
/// <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(GrabLordSelect message) { UI uiRoom = Hotfix.Scene.GetComponent <UIComponent>().Get(UIType.Room); GamerComponent gamerComponent = uiRoom.GetComponent <GamerComponent>(); Gamer gamer = gamerComponent.Get(message.PlayerID); if (gamer != null) { if (gamer.Id == gamerComponent.LocalGamer.Id) { uiRoom.GetComponent <UIRoomComponent>().Interaction.EndGrab(); } gamer.GetComponent <GamerUIComponent>().SetGrab(message.IsGrab); } }
protected override Task Run(Room entity, ChangeGameMode message) { Gamer gamer = entity.Get(message.PlayerId); if (gamer != null) { if (gamer.GetComponent <AutoPlayCardsComponent>() == null) { gamer.AddComponent <AutoPlayCardsComponent, Room>(entity); Log.Info($"玩家{gamer.Id}切换为自动模式"); } else { gamer.RemoveComponent <AutoPlayCardsComponent>(); Log.Info($"玩家{gamer.Id}切换为手动模式"); } } return(Task.CompletedTask); }
protected override Task Run(Room entity, PlayerQuit message) { Gamer gamer = entity.Get(message.PlayerID); if (gamer != null) { if (entity.State == RoomState.Game) { //玩家操作设置为自动 Log.Info($"游戏中,玩家{message.PlayerID}退出房间,切换为自动模式"); gamer.isOffline = true; if (gamer.GetComponent <AutoPlayCardsComponent>() == null) { gamer.AddComponent <AutoPlayCardsComponent, Room>(entity); } } else { //房间移除玩家 entity.Remove(gamer.Id); //同步匹配服务器移除玩家 MapHelper.SendMessage(new GamerQuitRoom() { PlayerID = message.PlayerID, RoomID = entity.Id }); //消息广播给其他人 entity.Broadcast(new GamerOut() { PlayerID = message.PlayerID }); Log.Info($"准备中,玩家{message.PlayerID}退出房间"); } } return(Task.CompletedTask); }
/// <summary> /// 更新身份 /// </summary> /// <param name="self"></param> /// <param name="id"></param> /// <param name="identity"></param> public static void UpdateInIdentity(this GameControllerComponent self, Gamer gamer, Identity identity) { gamer.GetComponent <HandCardsComponent>().AccessIdentity = identity; }
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); }