/// <summary> /// 抓牌 /// </summary> /// <param name="room"></param> /// <returns></returns> public static MahjongInfo GrabMahjong(this Room room) { try { OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>(); var currentGamer = room.Get(orderController.CurrentAuthority); HandCardsComponent cardsComponent = currentGamer.GetComponent <HandCardsComponent>(); DeskComponent deskComponent = room.GetComponent <DeskComponent>(); if (deskComponent.RestLibrary.Count == 0) { Log.Info("没牌了"); return(null); } MahjongInfo grabMahjong; if (room.NextGrabCard != null) { grabMahjong = new MahjongInfo() { m_weight = room.NextGrabCard.m_weight, weight = room.NextGrabCard.weight }; room.NextGrabCard = null; Log.Debug("发作弊牌:" + grabMahjong.m_weight); } else { int number = RandomHelper.RandomNumber(0, deskComponent.RestLibrary.Count); grabMahjong = deskComponent.RestLibrary[number]; deskComponent.RestLibrary.RemoveAt(number); Log.Info($"{currentGamer.UserID}发牌:" + grabMahjong.m_weight); } //花牌返回 if (grabMahjong.m_weight >= Consts.MahjongWeight.Hua_HongZhong) { return(grabMahjong); } //发牌 cardsComponent.AddCard(grabMahjong); room.my_lastMahjong = grabMahjong; cardsComponent.GrabCard = grabMahjong; Logic_NJMJ.getInstance().SortMahjong(cardsComponent.GetAll()); //发送抓牌消息 ActorMessageSenderComponent actorMessageSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>(); foreach (Gamer _gamer in room.gamers) { if (_gamer == null || _gamer.isOffline) { continue; } UnitGateComponent unitGateComponent = _gamer?.GetComponent <UnitGateComponent>(); Actor_GamerGrabCard actorGamerGrabCard; if (_gamer.UserID == orderController.CurrentAuthority) { actorGamerGrabCard = new Actor_GamerGrabCard() { Uid = currentGamer.UserID, weight = (int)grabMahjong.m_weight }; } else { actorGamerGrabCard = new Actor_GamerGrabCard() { Uid = currentGamer.UserID, }; } actorMessageSenderComponent.GetWithActorId(unitGateComponent.GateSessionActorId).Send(actorGamerGrabCard); } //发完牌判断是否胡牌 foreach (Gamer _gamer in room.gamers) { if (_gamer == null || _gamer.isOffline) { continue; } if (_gamer.UserID == orderController.CurrentAuthority) { HandCardsComponent handCardsComponent = _gamer.GetComponent <HandCardsComponent>(); //判断胡牌 if (Logic_NJMJ.getInstance().isHuPai(handCardsComponent.GetAll())) { _gamer.huPaiNeedData.my_lastMahjong = room.my_lastMahjong; _gamer.huPaiNeedData.restMahjongCount = deskComponent.RestLibrary.Count; _gamer.huPaiNeedData.isSelfZhuaPai = orderController.CurrentAuthority == _gamer.UserID; _gamer.huPaiNeedData.isZhuangJia = handCardsComponent.IsBanker; _gamer.huPaiNeedData.isGetYingHuaBuPai = _gamer.isGetYingHuaBuPai; _gamer.huPaiNeedData.isGangEndBuPai = _gamer.isGangEndBuPai; _gamer.huPaiNeedData.isGangFaWanPai = _gamer.isGangFaWanPai; _gamer.huPaiNeedData.isFaWanPaiTingPai = _gamer.isFaWanPaiTingPai; _gamer.huPaiNeedData.my_yingHuaList = handCardsComponent.FaceCards; _gamer.huPaiNeedData.my_gangList = handCardsComponent.GangCards; _gamer.huPaiNeedData.my_pengList = handCardsComponent.PengCards; List <List <MahjongInfo> > tempList = new List <List <MahjongInfo> >(); for (int i = 0; i < room.GetAll().Length; i++) { if (_gamer.UserID == room.GetAll()[i].UserID) { continue; } tempList.Add(room.GetAll()[i].GetComponent <HandCardsComponent>().PengCards); } _gamer.huPaiNeedData.other1_pengList = tempList[0]; _gamer.huPaiNeedData.other2_pengList = tempList[1]; _gamer.huPaiNeedData.other3_pengList = tempList[2]; List <Consts.HuPaiType> huPaiTypes = Logic_NJMJ.getInstance().getHuPaiType(handCardsComponent.GetAll(), _gamer.huPaiNeedData); Log.Info(JsonHelper.ToJson(_gamer.huPaiNeedData)); Log.Info(JsonHelper.ToJson(huPaiTypes)); if (huPaiTypes.Count > 0) { //判断小胡,4个花以上才能胡 if (huPaiTypes[0] == Consts.HuPaiType.Normal) { if (handCardsComponent.PengGangCards.Count > 0 || handCardsComponent.PengCards.Count > 0) { if (handCardsComponent.FaceCards.Count >= 4) { _gamer.IsCanHu = true; Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; canOperation.OperationType = 2; room.GamerBroadcast(_gamer, canOperation); } } } else { _gamer.IsCanHu = true; Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; canOperation.OperationType = 2; room.GamerBroadcast(_gamer, canOperation); } } } //暗杠 if (Logic_NJMJ.getInstance().IsAnGang(handCardsComponent.GetAll(), out var weight)) { _gamer.IsCanGang = true; Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; canOperation.OperationType = 4; room.GamerBroadcast(_gamer, canOperation); } //碰杠 else if (Logic_NJMJ.getInstance().IsPengGang(handCardsComponent.PengCards, handCardsComponent.GetAll(), out var weight2)) { _gamer.IsCanGang = true; Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; canOperation.OperationType = 5; room.GamerBroadcast(_gamer, canOperation); } } } return(grabMahjong); } catch (Exception e) { Log.Error(e); return(null); } }
protected override async void Run(Session session, G2M_JoinMatchRoom message, Action <M2G_JoinMatchRoom> reply) { M2G_JoinMatchRoom response = new M2G_JoinMatchRoom(); try { MatchRoomComponent roomComponent = Game.Scene.GetComponent <MatchRoomComponent>(); MatchRoom matchroom = roomComponent.Get(message.RoomId); Unit unit = matchroom.unitComponent.Get(message.UnitId); if (unit == null) { if (message.UnitId > 0) { unit = EntityFactory.CreateWithId <Unit>(message.UnitId); } else { unit = EntityFactory.Create <Unit>(); } unit.RoomID = matchroom.Id; matchroom.unitComponent.Add(unit); } UnitGateComponent unitGateComponent = unit.GetComponent <UnitGateComponent>(); if (unitGateComponent != null) { Game.Scene.GetComponent <ActorProxyComponent>().Remove(unitGateComponent.GateSessionId); unit.RemoveComponent <UnitGateComponent>(); ActorComponent actorComponent = unit.GetComponent <ActorComponent>(); await actorComponent.RemoveLocation(); unit.RemoveComponent <ActorComponent>(); } unit.AddComponent <UnitGateComponent, long>(message.GateSessionId); await unit.AddComponent <ActorComponent, IEntityActorHandler>(new MapUnitEntityActorHandler()).AddLocation(); //Game.Scene.GetComponent<UnitComponent>().Add(unit); Unit[] units = matchroom.unitComponent.GetAll(); response.UnitIds = new long[units.Length]; for (int i = 0; i < units.Length; i++) { response.UnitIds[i] = units[i].Id; } Response_RankList retRankList = new Response_RankList(); Actor_CreateUnits actorCreateUnits = new Actor_CreateUnits(); int tmpScore = 100; foreach (Unit u in units) { actorCreateUnits.Units.Add(new UnitInfo() { UnitId = u.Id, X = (int)(u.Position.X * 1000), Z = (int)(u.Position.Z * 1000) }); retRankList.Units.Add(new RankInfo() { Id = u.Id, name = "张三", score = tmpScore }); tmpScore -= 5; } Log.Debug($"{MongoHelper.ToJson(actorCreateUnits)}"); MessageHelper.Broadcast(actorCreateUnits, units); MessageHelper.Broadcast(retRankList, units); if (matchroom.Frame > 0) { response.AgoFrameMessage = matchroom.cacheFrameMessage; response.AgoFrameMessage.Frame = matchroom.Frame; } reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
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); }