/// <summary> /// 改变玩家财富 /// </summary> /// <param name="self"></param> /// <param name="huaCount"></param> /// <param name="room"></param> /// <returns></returns> private static async Task ChangeWeath(GameControllerComponent self, int huaCount, Room room) { int amount = huaCount * self.RoomConfig.Multiples; if (huaCount > 0) { //改变财富 foreach (var gamer in room.GetAll()) { //自摸 if (room.IsZimo) { if (gamer.UserID == room.huPaiUid) { GameHelp.ChangeGamerGold(room, gamer, amount * 3, self.RoomConfig.Name + "结算"); gamer.ChangeGold = amount * 3; // await DBCommonUtil.ChangeWealth(gamer.UserID, 1, amount * 3,self.RoomConfig.Name + "结算"); UpdateTask(gamer, amount * 3); } else { // Log.Debug($"玩家:{gamer.UserID} 输了{amount}"); // await DBCommonUtil.ChangeWealth(gamer.UserID, 1, -amount, self.RoomConfig.Name + "结算"); GameHelp.ChangeGamerGold(room, gamer, -amount, self.RoomConfig.Name + "结算"); gamer.ChangeGold = -amount; } } else { if (gamer.UserID == room.huPaiUid) { // await DBCommonUtil.ChangeWealth(gamer.UserID, 1, amount, self.RoomConfig.Name + "结算"); GameHelp.ChangeGamerGold(room, gamer, amount, self.RoomConfig.Name + "结算"); gamer.ChangeGold = amount; UpdateTask(gamer, amount); } else { if (gamer.UserID == room.fangPaoUid) { // Log.Debug($"玩家:{gamer.UserID} 输了{amount}"); // await DBCommonUtil.ChangeWealth(gamer.UserID, 1, -amount, self.RoomConfig.Name + "结算"); GameHelp.ChangeGamerGold(room, gamer, -amount, self.RoomConfig.Name + "结算"); gamer.ChangeGold = -amount; } } } } } }
/// <summary> /// 给当前玩家发一张不是花牌的牌 /// </summary> /// <param name="room"></param> /// <param name="currentGamer"></param> /// <returns></returns> public static async Task <MahjongInfo> GrabMahjongNoHua(this Room room, Gamer currentGamer) { GameControllerComponent gameController = room.GetComponent <GameControllerComponent>(); HandCardsComponent cardsComponent = currentGamer.GetComponent <HandCardsComponent>(); var grabMahjong = GrabMahjong(room); if (grabMahjong == null) { Log.Info("没牌流局了"); await gameController.GameOver(0); return(null); } while (grabMahjong.m_weight >= Consts.MahjongWeight.Hua_HongZhong) { Actor_GamerBuHua actorGamerBuHua = new Actor_GamerBuHua() { Uid = currentGamer.UserID, weight = grabMahjong.weight }; room.Broadcast(actorGamerBuHua); //从手牌中删除花牌 Log.Info($"{currentGamer.UserID}补花,{grabMahjong.m_weight}"); cardsComponent.FaceCards.Add(grabMahjong); cardsComponent.FaceGangCards.Add(grabMahjong); #region 花杠 int temp = 0; foreach (var faceCard in cardsComponent.FaceCards) { if (faceCard.m_weight == grabMahjong.m_weight) { temp++; } } Logic_NJMJ.getInstance().SortMahjong(cardsComponent.FaceCards); //春夏秋冬 for (int i = 0; i < cardsComponent.FaceGangCards.Count - 3; i++) { if (cardsComponent.FaceCards[i + 3].m_weight - cardsComponent.FaceCards[i + 2].m_weight == 2 && cardsComponent.FaceCards[i + 2].m_weight - cardsComponent.FaceCards[i + 1].m_weight == 2 && cardsComponent.FaceCards[i + 1].m_weight - cardsComponent.FaceCards[i].m_weight == 2) { temp = 4; cardsComponent.FaceGangCards.RemoveAt(i + 3); cardsComponent.FaceGangCards.RemoveAt(i + 2); cardsComponent.FaceGangCards.RemoveAt(i + 1); cardsComponent.FaceGangCards.RemoveAt(i); } } if (temp == 4) { foreach (var _gamer in room.GetAll()) { if (_gamer.UserID == currentGamer.UserID) { GameHelp.ChangeGamerGold(room, _gamer, 20 * gameController.RoomConfig.Multiples * 3); } else { GameHelp.ChangeGamerGold(room, _gamer, -20 * gameController.RoomConfig.Multiples); } } room.LastBiXiaHu = true; } #endregion currentGamer.isGangEndBuPai = false; currentGamer.isGetYingHuaBuPai = true; grabMahjong = GrabMahjong(room); if (grabMahjong == null) { Log.Info("没牌流局了"); await gameController.GameOver(0); return(null);; } } room.StartTime(); return(grabMahjong); }
protected override async Task Run(Gamer gamer, Actor_GamerOperation message) { try { RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>(); Room room = roomComponent.Get(gamer.RoomID); room.IsOperate = true; room.IsPlayingCard = false; room.IsNeedWaitOperate = false; if (room.IsGameOver) { return; } DeskComponent deskComponent = room.GetComponent <DeskComponent>(); OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>(); GameControllerComponent gameController = room.GetComponent <GameControllerComponent>(); Gamer currentGamer = room.Get(orderController.CurrentAuthority); HandCardsComponent handCards = gamer.GetComponent <HandCardsComponent>(); if (handCards == null) { return; } List <MahjongInfo> mahjongInfos = handCards.GetAll(); Log.Info($"玩家{gamer.UserID}碰或刚{message.OperationType},当前出牌:{deskComponent.CurrentCard.m_weight}"); //胡牌 int huaCount = 0; if (message.OperationType == 2) { //自摸 bool isFinish = false; if (orderController.CurrentAuthority == gamer.UserID) { if (Logic_NJMJ.getInstance().isHuPai(mahjongInfos)) { huaCount = HuPai(gamer, room, mahjongInfos, true); room.huPaiUid = gamer.UserID; isFinish = true; } } //放炮 else { if (room.CanHu(deskComponent.CurrentCard, mahjongInfos)) { List <MahjongInfo> temp = new List <MahjongInfo>(mahjongInfos); temp.Add(deskComponent.CurrentCard); huaCount = HuPai(gamer, room, temp, false); isFinish = true; room.huPaiUid = gamer.UserID; room.fangPaoUid = orderController.CurrentAuthority; } } if (isFinish) { //游戏结束结算 await gameController.GameOver(huaCount); } gamer.IsCanHu = false; gamer.IsCanPeng = false; gamer.IsCanGang = false; } //放弃 else if (message.OperationType == 3) { Log.Debug("放弃:" + gamer.UserID); gamer.IsCanHu = false; gamer.IsCanPeng = false; gamer.IsCanGang = false; if (orderController.CurrentAuthority == gamer.UserID) { //room.tokenSource.Cancel(); } else { room.tokenSource.Cancel(); Log.Info($"{gamer.UserID}取消,发牌"); room.GamerGrabCard(); } } else { Actor_GamerOperation gamerOperation = new Actor_GamerOperation(); gamerOperation.Uid = gamer.UserID; gamerOperation.weight = deskComponent.CurrentCard.weight; //有没有人胡牌 while (true) { if (!GetCanHu(room, gamer)) { break; } await Game.Scene.GetComponent <TimerComponent>().WaitAsync(1000); } //游戏结束 if (room.IsGameOver) { Log.Warning("游戏结束不能碰刚"); return; } // 碰 if (message.OperationType == 0) { if (Logic_NJMJ.getInstance().isCanPeng(deskComponent.CurrentCard, mahjongInfos)) { room.tokenSource.Cancel(); //Log.Info("11111"); gamerOperation.OperationType = 0; gamerOperation.OperatedUid = deskComponent.CurrentAuthority; room.Broadcast(gamerOperation); //更新手牌 for (int i = 0; i < 2; i++) { int index = Logic_NJMJ.getInstance().GetIndex(mahjongInfos, deskComponent.CurrentCard); mahjongInfos.RemoveAt(index); } //Log.Info("222222"); handCards.PengCards.Add(deskComponent.CurrentCard); currentGamer.GetComponent <HandCardsComponent>().PlayCards.Remove(deskComponent.CurrentCard); //添加碰的人 PengOrBar pengOrBar = ComponentFactory.Create <PengOrBar>(); pengOrBar.OperateType = OperateType.Peng; pengOrBar.Weight = deskComponent.CurrentCard.weight; pengOrBar.UserId = deskComponent.CurrentAuthority; pengOrBar.BarType = BarType.None; handCards.PengOrBars.Add(pengOrBar); Log.Debug("PengOrBars:" + handCards.PengOrBars.Count); //碰完当前玩家出牌 orderController.CurrentAuthority = gamer.UserID; room.StartTime(); } } // 杠 else { HandCardsComponent handCardsComponent = gamer.GetComponent <HandCardsComponent>(); gamerOperation.OperationType = message.OperationType; bool isSuccess = false; //明杠 if (Logic_NJMJ.getInstance().isCanGang(deskComponent.CurrentCard, mahjongInfos)) { isSuccess = true; //更新手牌 for (int i = 0; i < 3; i++) { int index = Logic_NJMJ.getInstance().GetIndex(mahjongInfos, deskComponent.CurrentCard); mahjongInfos.RemoveAt(index); } handCards.GangCards.Add(deskComponent.CurrentCard); currentGamer.GetComponent <HandCardsComponent>().PlayCards.Remove(deskComponent.CurrentCard); //杠扣钱 GameHelp.ChangeGamerGold(room, gamer, 20 * gameController.RoomConfig.Multiples); GameHelp.ChangeGamerGold(room, currentGamer, -20 * gameController.RoomConfig.Multiples); //添加明杠 gamerOperation.OperatedUid = deskComponent.CurrentAuthority; PengOrBar pengOrBar = ComponentFactory.Create <PengOrBar>(); pengOrBar.OperateType = OperateType.Bar; pengOrBar.Weight = deskComponent.CurrentCard.weight; pengOrBar.UserId = deskComponent.CurrentAuthority; pengOrBar.BarType = BarType.LightBar; handCards.PengOrBars.Add(pengOrBar); } //暗杠 else if (Logic_NJMJ.getInstance().IsAnGang(handCardsComponent.GetAll(), out var weight)) { isSuccess = true; gamerOperation.weight = (int)weight; gamerOperation.OperationType = 4; MahjongInfo info = new MahjongInfo() { weight = (byte)weight, m_weight = (Consts.MahjongWeight)weight }; //更新手牌 for (int i = 0; i < 4; i++) { int index = Logic_NJMJ.getInstance().GetIndex(mahjongInfos, info); mahjongInfos.RemoveAt(index); } handCards.GangCards.Add(info); //杠扣钱 foreach (var _gamer in room.GetAll()) { if (_gamer.UserID == gamer.UserID) { GameHelp.ChangeGamerGold(room, _gamer, 10 * gameController.RoomConfig.Multiples * 3); } else { GameHelp.ChangeGamerGold(room, _gamer, -10 * gameController.RoomConfig.Multiples); } } //添加暗杠 gamerOperation.OperatedUid = 0; PengOrBar pengOrBar = ComponentFactory.Create <PengOrBar>(); pengOrBar.OperateType = OperateType.Bar; pengOrBar.Weight = (int)weight; pengOrBar.UserId = 0; pengOrBar.BarType = BarType.DarkBar; handCards.PengOrBars.Add(pengOrBar); } //碰杠 else if (Logic_NJMJ.getInstance().IsPengGang(handCardsComponent.PengCards, handCardsComponent.GetAll(), out var weight1)) { isSuccess = true; gamerOperation.weight = weight1; gamerOperation.OperationType = 5; MahjongInfo info = new MahjongInfo() { weight = (byte)weight1, m_weight = (Consts.MahjongWeight)weight1 }; //更新手牌 for (int i = 0; i < 1; i++) { int index = Logic_NJMJ.getInstance().GetIndex(mahjongInfos, info); mahjongInfos.RemoveAt(index); } handCardsComponent.PengCards.Remove(info); handCards.GangCards.Add(info); handCards.PengGangCards.Add(info); //添加碰杠 PengOrBar pengOrBar = null; foreach (var item in handCardsComponent.PengOrBars) { if (item.Weight == weight1 && item.OperateType == OperateType.Peng) { pengOrBar = item; } } if (pengOrBar == null) { Log.Error("碰刚的牌没有碰过"); return; } pengOrBar.OperateType = OperateType.Bar; pengOrBar.BarType = BarType.PengBar; gamerOperation.OperatedUid = pengOrBar.UserId; //杠扣钱 foreach (var _gamer in room.GetAll()) { if (_gamer.UserID == gamer.UserID) { GameHelp.ChangeGamerGold(room, _gamer, 20 * gameController.RoomConfig.Multiples); } else if (_gamer.UserID == pengOrBar.UserId) { GameHelp.ChangeGamerGold(room, _gamer, -20 * gameController.RoomConfig.Multiples); } } } if (isSuccess) { room.Broadcast(gamerOperation); //杠完之后不能 gamer.isFaWanPaiTingPai = false; gamer.isGangEndBuPai = true; gamer.isGetYingHuaBuPai = false; //杠完后是本人出牌 orderController.CurrentAuthority = gamer.UserID; //杠完之后抓牌 await room.GrabMahjongNoHua(gamer); #region 比下胡 累计2个暗杠或3个明杠(同一个玩家) int lightCount = handCards.PengOrBars.Count(c => c.BarType == BarType.LightBar); int darkCount = handCards.PengOrBars.Count(c => c.BarType == BarType.DarkBar); if (lightCount >= 3 || darkCount >= 2) { room.LastBiXiaHu = true; } #endregion } else { Log.Debug("不能杠"); } } } } catch (Exception e) { Log.Error(e); } await Task.CompletedTask; }
public static async Task PlayCard(Gamer gamer, Actor_GamerPlayCard message) { RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>(); Room room = roomComponent.Get(gamer.RoomID); if (room == null || room.IsGameOver) { return; } try { MahjongInfo mahjongInfo = new MahjongInfo() { weight = (byte)message.weight, m_weight = (Consts.MahjongWeight)message.weight }; //加锁 if (room.IsPlayingCard) { Log.Warning("当前正在出牌,不能再出牌了"); return; } room.IsPlayingCard = true; GameControllerComponent gameController = room.GetComponent <GameControllerComponent>(); OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>(); DeskComponent deskComponent = room.GetComponent <DeskComponent>(); HandCardsComponent handCardsComponent = gamer.GetComponent <HandCardsComponent>(); if (handCardsComponent == null) { return; } List <MahjongInfo> mahjongInfos = handCardsComponent.GetAll(); if (orderController.CurrentAuthority != gamer.UserID) { Log.Warning("没有轮到当前玩家出牌:" + gamer.UserID); Log.Warning("当前出牌玩家:" + orderController.CurrentAuthority); room.IsPlayingCard = false; return; } int index = -1; for (int i = 0; i < mahjongInfos.Count; i++) { if (mahjongInfos[i].m_weight == mahjongInfo.m_weight) { index = i; break; } } if (index >= 0) { //停止倒计时 room?.tokenSource?.Cancel(); Log.Info($"玩家{gamer.UserID}出牌:" + mahjongInfo.m_weight); //当前出的牌 deskComponent.CurrentCard = mahjongInfo; deskComponent.CurrentAuthority = gamer.UserID; handCardsComponent.PlayCards.Add(mahjongInfo); mahjongInfos.RemoveAt(index); room.my_lastMahjong = mahjongInfo; Actor_GamerPlayCard actorGamerPlayCard = new Actor_GamerPlayCard() { weight = message.weight, Uid = gamer.UserID, index = message.index }; room.Broadcast(actorGamerPlayCard); gamer.IsCanHu = false; gamer.IsCanPeng = false; gamer.IsCanGang = false; #region 4个人连续出同样的牌,第一个出牌的人立即支付其他三人 List <MahjongInfo> list = new List <MahjongInfo>(); foreach (var _gamer in room.GetAll()) { HandCardsComponent cardsComponent = _gamer.GetComponent <HandCardsComponent>(); if (cardsComponent.PlayCards.Count > 0) { list.Add(cardsComponent.PlayCards[cardsComponent.PlayCards.Count - 1]); } } if (list.Count == 4) { bool flag = true; for (int i = 1; i < list.Count; i++) { if (list[0].m_weight != list[i].m_weight) { flag = false; break; } } if (flag) { Gamer nextGamer = orderController.FindNextGamer(gamer.UserID); //罚分 foreach (var _gamer in room.GetAll()) { if (_gamer.UserID == nextGamer.UserID) { GameHelp.ChangeGamerGold(room, _gamer, -10 * gameController.RoomConfig.Multiples * 3); } else { GameHelp.ChangeGamerGold(room, _gamer, 10 * gameController.RoomConfig.Multiples); } room.GamerBroadcast(_gamer, new Actor_ShowAnimType() { Type = 3, Count = 0 }); } room.LastBiXiaHu = true; } } #endregion #region 一个人出4张一样的牌 //4个人出一样的牌 int temp = 0; foreach (var playCard in handCardsComponent.PlayCards) { if (playCard.m_weight == mahjongInfo.m_weight) { temp++; } } if (temp == 4) { //罚分 foreach (var _gamer in room.GetAll()) { if (_gamer.UserID == gamer.UserID) { GameHelp.ChangeGamerGold(room, _gamer, -10 * gameController.RoomConfig.Multiples * 3); } else { GameHelp.ChangeGamerGold(room, _gamer, 10 * gameController.RoomConfig.Multiples); } room.GamerBroadcast(_gamer, new Actor_ShowAnimType() { Type = 3, Count = 0 }); } room.LastBiXiaHu = true; } #endregion #region 一人前四次出牌打出东南西北(不必按顺序),则其他每名玩家立即支付给该玩家 if (handCardsComponent.PlayCards.Count == 4) { int Feng_Dong = handCardsComponent.PlayCards.Count(a => a.m_weight == Consts.MahjongWeight.Feng_Dong); int Feng_Nan = handCardsComponent.PlayCards.Count(a => a.m_weight == Consts.MahjongWeight.Feng_Nan); int Feng_Xi = handCardsComponent.PlayCards.Count(a => a.m_weight == Consts.MahjongWeight.Feng_Xi); int Feng_Bei = handCardsComponent.PlayCards.Count(a => a.m_weight == Consts.MahjongWeight.Feng_Bei); if (Feng_Dong == 1 && Feng_Nan == 1 && Feng_Xi == 1 && Feng_Bei == 1) { //四连风 foreach (var _gamer in room.GetAll()) { if (_gamer.UserID == gamer.UserID) { GameHelp.ChangeGamerGold(room, _gamer, 5 * gameController.RoomConfig.Multiples * 3); } else { GameHelp.ChangeGamerGold(room, _gamer, -5 * gameController.RoomConfig.Multiples); } room.GamerBroadcast(_gamer, new Actor_ShowAnimType() { Type = 2, Count = 0 }); } } } #endregion #region 等待客户端有没有人碰杠胡 //等待客户端有没有人碰 bool isNeedWait = false; foreach (var _gamer in room.GetAll()) { if (_gamer == null) { continue; } if (_gamer.UserID == gamer.UserID) { continue; } HandCardsComponent currentCards = _gamer.GetComponent <HandCardsComponent>(); List <MahjongInfo> cards = _gamer.GetComponent <HandCardsComponent>().GetAll(); if (Logic_NJMJ.getInstance().isCanPeng(mahjongInfo, cards)) { Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; _gamer.IsCanPeng = true; isNeedWait = true; canOperation.OperationType = 0; //Log.Info($"{_gamer.UserID}可碰:"+JsonHelper.ToJson(canOperation)); room.GamerBroadcast(_gamer, canOperation); } //明杠 if (Logic_NJMJ.getInstance().isCanGang(mahjongInfo, cards)) { Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; _gamer.IsCanGang = true; isNeedWait = true; canOperation.OperationType = 1; //Log.Info($"{_gamer.UserID}可杠" + JsonHelper.ToJson(canOperation)); room.GamerBroadcast(_gamer, canOperation); } if (room.CanHu(mahjongInfo, cards)) { _gamer.huPaiNeedData.my_lastMahjong = room.my_lastMahjong; _gamer.huPaiNeedData.restMahjongCount = deskComponent.RestLibrary.Count; _gamer.huPaiNeedData.isSelfZhuaPai = orderController.CurrentAuthority == _gamer.UserID; _gamer.huPaiNeedData.isZhuangJia = currentCards.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 = currentCards.FaceCards; _gamer.huPaiNeedData.my_gangList = currentCards.GangCards; _gamer.huPaiNeedData.my_pengList = currentCards.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 <MahjongInfo> infos = new List <MahjongInfo>(cards); infos.Add(mahjongInfo); List <Consts.HuPaiType> huPaiTypes = Logic_NJMJ.getInstance().getHuPaiType(infos, _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 (currentCards.PengGangCards.Count > 0 || currentCards.PengCards.Count > 0) { if (currentCards.FaceCards.Count >= 4) { _gamer.IsCanHu = true; isNeedWait = true; Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; canOperation.OperationType = 2; room.GamerBroadcast(_gamer, canOperation); } } } else { _gamer.IsCanHu = true; isNeedWait = true; Actor_GamerCanOperation canOperation = new Actor_GamerCanOperation(); canOperation.Uid = _gamer.UserID; canOperation.OperationType = 2; room.GamerBroadcast(_gamer, canOperation); } } } } #endregion if (isNeedWait) { room.IsNeedWaitOperate = true; room.StartOperateTime(); } //没人可以操作就直接发牌 else { room.IsNeedWaitOperate = false; //发牌 room.GamerGrabCard(); room.IsPlayingCard = false; } } else { Log.Warning("玩家出牌不存在:" + message.weight); room.IsPlayingCard = false; } } catch (Exception e) { Log.Error(e); room.IsPlayingCard = false; } await Task.CompletedTask; }