123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ET.Server
- {
- [FriendOf(typeof (HGHuangHuangComponent))]
- public static class HGHuangHuangComponentSystem
- {
- [ObjectSystem]
- public class HGHuangHuangComponentAwakeSystem: AwakeSystem<HGHuangHuangComponent>
- {
- protected override void Awake(HGHuangHuangComponent self)
- {
- Room room = self.GetParent<Room>();
- if (room == null)
- {
- Log.Error($"黄冈晃晃主逻辑组件获取不到主实体...");
- return;
- }
-
- Log.Info($"创建黄冈晃晃主逻辑组件...");
- self.Flag = false;
- self.Time = 0;
- self.State = 0;
- self.Players = new Player[room.MaxNum];
- self.CurrentRound = 0;
- self.ZhuangPos = 0;
- self.Rand = new[] { 0, 0 };
- self.GangType = -1;
- self.AdmitDefeatList = new List<long>();
- self.CanPgIds = new List<long>();
- self.CanHuIds = new List<long>();
- self.OperableList = new List<long>();
- self.ClickHuIds = new List<Player>();
- self.CardList = new List<int>();
- self.UpdateTime = 0;
-
- // 初始化牌库
- for (int i = 0; i < 4; i++)
- {
- foreach (int value in HGHuangHuangConst.Values)
- {
- self.CardList.Add(value);
- }
- RandomGenerator.Shuffle(self.CardList);
- }
- }
- }
-
- [ObjectSystem]
- public class HGHuangHuangComponentDestroySystem: DestroySystem<HGHuangHuangComponent>
- {
- protected override void Destroy(HGHuangHuangComponent self)
- {
- Log.Info($"销毁黄冈晃晃主逻辑组件...");
- }
- }
- [ObjectSystem]
- [FriendOf(typeof(Room))]
- public class HGHuangHuangComponentUpdateSystem : UpdateSystem<HGHuangHuangComponent>
- {
- protected override void Update(HGHuangHuangComponent self)
- {
- long timeNow = TimeHelper.ClientNow();
- // 每秒执行一次
- if (timeNow - self.UpdateTime <= 1000)
- {
- return;
- }
- self.UpdateTime = timeNow;
- Room room = self.GetParent<Room>();
- if (room == null)
- {
- Log.Error($"黄冈晃晃主逻辑组件获取不到主实体...");
- return;
- }
- switch (self.State)
- {
- case 0:
- // 等待状态
- Log.Debug($"检测: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}, 状态:检测是否可开局...");
- if (self.CheckReadyStart(room))
- {
- self.State = 1;
- self.Flag = false;
- self.Time = 0;
- }
-
- // 120秒未开始直接解散
- if (self.Time >= 120)
- {
- room.Dispose();
- }
- break;
- case 1:
- // 开局
- if (!self.Flag)
- {
- if (self.Time >= 3)
- {
- self.Start(room);
- self.Flag = true;
- self.Time = 0;
- Log.Debug($"检测: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}, 状态:开局...VS动画...");
- }
- }
- else
- {
- if (self.Time >= 4)
- {
- self.State = 2;
- self.Flag = false;
- self.Time = 0;
- }
- }
- break;
- case 2:
- // 进行中
- if (!self.Flag)
- {
- self.Flag = true;
- self.Time = 0;
- // 摸牌
- self.DrawCard(room);
- }
- else
- {
- // 强制操作
- if (self.Time >= 15)
- {
- if (self.OperableList != null && self.OperableList.Count > 0)
- {
- // 过
-
- }
- if (self.DrawCardPlayer.Id == self.CurrentPlayer.Id)
- {
- // 出牌
- self.DisCard(room, self.CurrentPlayer, 0, true);
- }
- }
- }
- break;
- case 3:
- // 已结束
- break;
- }
- self.Time++;
- }
- }
- /// <summary>
- /// 检测是否可开始 3秒倒计时开始
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- /// <returns></returns>
- private static bool CheckReadyStart(this HGHuangHuangComponent self, Room room)
- {
- if (!room.IsStart() || self.State != 0)
- {
- return false;
- }
-
- // 通知客户端3秒倒计时
- foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
- {
- MessageHelper.SendToClient(player, new G2C_ReadyStartPush(){ReadyStartTime = 3});
- }
- return true;
- }
-
- /// <summary>
- /// 发牌
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- private static void SendCard(this HGHuangHuangComponent self, Room room)
- {
- foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
- {
- for (int i = 0; i < 13; i++)
- {
- int card = self.CardList[0];
- player.RemainCards = CardHelper.Add(player.RemainCards, card);
- self.CardList.RemoveAt(0);
- }
- }
- }
- /// <summary>
- /// 游戏开局
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- private static void Start(this HGHuangHuangComponent self, Room room)
- {
- self.CurrentRound += 1;
- // 摇骰子
- int rand1 = RandomGenerator.RandomNumber(1, 7);
- int rand2 = RandomGenerator.RandomNumber(1, 7);
- self.Rand = new[] { rand1, rand2 };
- // 定庄
- // self.ZhuangPos = (rand1 + rand2) % 4;
- self.ZhuangPos = (rand1 + rand2) % room.MaxNum;
- // 设置当前操作玩家
- self.CurrentPlayer = self.Players[self.ZhuangPos];
- // 设置当前摸牌玩家
- self.DrawCardPlayer = self.Players[self.ZhuangPos];
- // 发牌
- self.SendCard(room);
- // 广播
- foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
- {
- player.State = 2;
-
- MessageHelper.SendToClient(player, new G2C_StartPush(){info = ProtoHelper.RoomToProto(room, player, null)});
-
- Log.Info($"游戏开局, 房间id={room.RoomId}, 玩家id={player.Id}, 玩家={player.Name}, 手牌信息={player.RemainCards}");
- }
- }
-
- /// <summary>
- /// 摸牌
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- private static void DrawCard(this HGHuangHuangComponent self, Room room)
- {
- // 2个以上玩家认输或牌库没牌了直接结束
- if (self.AdmitDefeatList.Count > 2 || self.CardList.Count <= 0)
- {
- self.GameOver(room);
- return;
- }
-
- Player drawCardPlayer = self.DrawCardPlayer;
- if (drawCardPlayer == null)
- {
- Log.Error($"房间id={room.RoomId}, 摸牌玩家为空...");
- return;
- }
- // 摸牌
- int card = self.CardList[0];
- drawCardPlayer.RemainCards = CardHelper.Add(drawCardPlayer.RemainCards, card);
- self.CardList.RemoveAt(0);
- // 当前摸的牌
- self.DrawCard = card;
- // 吃,碰,杠,胡,过
- bool hasAct = false;
- // 检测摸牌人动作, 校验摸牌是否胡
- Struct.HuRes huRes = HGHuangHuangHelper.CheckHu(drawCardPlayer.RemainCards);
- if (huRes.Type != HGHuangHuangConst.HU_DEFAULT)
- {
- hasAct = true;
- drawCardPlayer.Act[3] = 1;
- self.CanHuIds.Add(drawCardPlayer.Id);
- if (!self.OperableList.Contains(drawCardPlayer.Id))
- {
- self.OperableList.Add(drawCardPlayer.Id);
- }
- }
- // todo 玩家听牌状态不允许杠
- // 检测摸牌人动作, 校验摸牌是否杠
- List<Struct.Kezi> gang = HGHuangHuangHelper.IsDrawGang(drawCardPlayer);
- if (gang is { Count: > 0 })
- {
- hasAct = true;
- drawCardPlayer.Act[2] = 1;
- if (!self.OperableList.Contains(drawCardPlayer.Id))
- {
- self.OperableList.Add(drawCardPlayer.Id);
- }
- if (!self.CanPgIds.Contains(drawCardPlayer.Id))
- {
- self.CanPgIds.Add(drawCardPlayer.Id);
- }
- }
- // 过牌
- drawCardPlayer.Act[4] = hasAct? 1 : 0;
- if (drawCardPlayer.HuCards.Any(_card => _card > 0 && _card == self.DrawCard))
- {
- drawCardPlayer.Act[4] = 0;
- }
- // 推送摸牌广播
- foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
- {
- MessageHelper.SendToClient(player, new G2C_DrawCardPush(){info = ProtoHelper.RoomToProto(room, player, drawCardPlayer)});
-
- Log.Info($"摸牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 摸牌玩家:{player.Id == drawCardPlayer.Id}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 摸的牌:{card}");
- }
- }
- /// <summary>
- /// 出牌
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- /// <param name="player"></param>
- /// <param name="card">出的牌(0.当前摸牌玩家自动出牌 -1.非当前摸牌玩家自动出牌 >0.玩家手动出牌)</param>
- /// <param name="flag">是否自动出牌</param>
- public static async void DisCard(this HGHuangHuangComponent self, Room room, Player player, int card, bool flag)
- {
- if (player == null)
- {
- Log.Error($"出牌错误,player is null.");
- return;
- }
- using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.PlayerOperation, player.Id))
- {
- if (self.CurrentPlayer.Id == player.Id && self.DrawCardPlayer.Id == player.Id)
- {
- Log.Info($"出牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息: {string.Join(", ", player.RemainCards)}");
-
- // 自动出牌
- if (flag)
- {
- if (self.PengPlayer != null && self.PengPlayer.Id == player.Id)
- {
- self.PengPlayer = null;
- int[] tmpCard = new int[player.RemainCards.Length];
- Array.Copy(player.RemainCards, 0, tmpCard, 0, player.RemainCards.Length);
- Array.Sort(tmpCard);
- card = tmpCard[^1];
- }
- else
- {
- card = player.RemainCards[^1];
- }
- // todo 真人托管状态
- }
-
- if (!player.RemainCards.Contains(card))
- {
- Log.Error($"出牌错误,玩家的牌不在手中...");
- return;
- }
-
- // 玩家出牌
- player.RemainCards = CardHelper.Remove(player.RemainCards, card);
- player.DisCards = CardHelper.Add(player.DisCards, card);
- player.DiscardCount += 1;
- self.DisCard = card;
- self.DisCardPlayer = player;
- self.PengPlayer = null;
-
- // 给出牌人广播
- MessageHelper.SendToClient(player, new G2C_DisCardPush(){info = ProtoHelper.RoomToProto(room, player, player)});
-
- self.CanHuIds.Clear();
- self.ClickHuIds.Clear();
- self.OperableList.Clear();
- self.CanPgIds.Clear();
- self.CleanUserAct();
-
- // 检测其它三家的动作
- int[] nextPos = room.GetSorcPos(player.Pos);
- foreach (int index in nextPos)
- {
- Player otherPlayer = self.Players[index];
- if (otherPlayer != null)
- {
- // 吃,碰,杠,胡,过
- int[] act = new int[5];
- List<Struct.Kezi> gangs = new List<Struct.Kezi>();
- List<Struct.Kezi> pengs = new List<Struct.Kezi>();
- List<Struct.Kezi> chis = new List<Struct.Kezi>();
- // todo 玩家听牌状态不允许有动作
- // 出牌是否杠
- gangs = HGHuangHuangHelper.IsDiscardGang(otherPlayer, card, player.Id);
- // 出牌是否碰
- pengs = HGHuangHuangHelper.IsDiscardPeng(otherPlayer, card, player.Id);
- // 出牌是否吃
- if (otherPlayer.Pos == nextPos[0])
- {
- chis = HGHuangHuangHelper.IsDiscardChi(otherPlayer, card, player.Id);
- }
- // 游戏中才会动作
- if (otherPlayer.State == 2)
- {
- bool hasAct = false;
-
- int[] tmpCards = new int[otherPlayer.RemainCards.Length];
- Array.Copy(otherPlayer.RemainCards, 0, tmpCards, 0, otherPlayer.RemainCards.Length);
- tmpCards = CardHelper.Add(tmpCards, card);
- // 校验胡牌
- Struct.HuRes huType = HGHuangHuangHelper.CheckHu(tmpCards);
- if (huType.Type != HGHuangHuangConst.HU_DEFAULT)
- {
- hasAct = true;
- act[3] = 1;
- // 放入可胡玩家列表
- if (!self.CanHuIds.Contains(otherPlayer.Id))
- {
- self.CanHuIds.Add(otherPlayer.Id);
- }
- // 加入可操作玩家list
- if (!self.OperableList.Contains(otherPlayer.Id))
- {
- self.OperableList.Add(otherPlayer.Id);
- }
- }
- // 是否杠
- if (gangs.Count > 0)
- {
- hasAct = true;
- act[2] = 1;
- }
- // 是否碰
- if (pengs.Count > 0)
- {
- hasAct = true;
- act[1] = 1;
- }
- // 是否吃
- if (chis.Count > 0)
- {
- hasAct = true;
- act[0] = 1;
- }
- // 是否过
- if (hasAct) {
- act[4] = 1;
- }
- if (act[2] == 1 || act[1] == 1) {
- // 加入可操作玩家list
- if (!self.OperableList.Contains(otherPlayer.Id)) {
- self.OperableList.Add(otherPlayer.Id);
- }
- // 加入可碰杠玩家集合
- if (!self.CanPgIds.Contains(otherPlayer.Id)) {
- self.CanPgIds.Add(otherPlayer.Id);
- }
- }
- // 设置玩家动作
- otherPlayer.Act = act;
- }
-
- // 广播其它三家
- MessageHelper.SendToClient(otherPlayer, new G2C_DisCardPush(){info = ProtoHelper.RoomToProto(room, otherPlayer, player)});
- }
- }
- if (self.CanHuIds.Count <= 0)
- {
- self.GangPlayer = null;
- }
- // 判断是否下家摸牌, 指定下家摸牌
- if (self.OperableList.Count == 0)
- {
- Player nextPlayer = null;
- foreach (int index in nextPos)
- {
- Player tempPlayer = self.Players[index];
- if (tempPlayer is { State: 2 } && tempPlayer.Id != player.Id)
- {
- nextPlayer = tempPlayer;
- break;
- }
- }
- // 有三家认输,就会有null
- if (nextPlayer == null)
- {
- self.GameOver(room);
- }
- else
- {
- // 设置摸牌玩家
- self.DrawCardPlayer = nextPlayer;
- // 设置操作玩家
- self.CurrentPlayer = nextPlayer;
- // 摸牌
- self.Flag = false;
- }
- }
- else
- {
- long id = 0;
-
- if (self.CanHuIds.Count > 0)
- {
- id = self.CanHuIds.First();
- } else if (self.CanPgIds.Count > 0)
- {
- id = self.CanPgIds.First();
- }
- else
- {
- id = self.OperableList.First();
- }
-
- Player tmpPlayer = room.GetPlayer(id);
- if (tmpPlayer != null)
- {
- self.CurrentPlayer = self.Players[tmpPlayer.Pos];
- }
- }
- }
- }
- }
- /// <summary>
- /// 玩家是否可以操作吃
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- /// <returns></returns>
- private static bool IsCanChi(this HGHuangHuangComponent self, Player player)
- {
- if (self.ClickHuIds.Count > 0)
- {
- return false;
- }
- if (self.CanHuIds.Count > 1)
- {
- return false;
- }
- else
- {
- if (self.CanHuIds.Count == 1)
- {
- if (self.CanPgIds.Count > 0)
- {
- if (self.CanPgIds.Any(id => id > 0 && id != player.Id))
- {
- return false;
- }
- }
- return self.CanHuIds.Contains(player.Id);
- }
- else
- {
- if (self.CanPgIds.Count > 0)
- {
- return self.CanPgIds.Contains(player.Id);
- }
- else
- {
- return true;
- }
- }
- }
- }
- /// <summary>
- /// 玩家操作吃
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- /// <param name="player"></param>
- /// <param name="card"></param>
- public static void Chi(this HGHuangHuangComponent self, Room room, Player player, int card)
- {
- if (self.IsCanChi(player))
- {
- return;
- }
- if (self.DisCard >= HGHuangHuangConst.DONG_FENG)
- {
- return;
- }
- // 删除出牌人打出的牌堆
- self.DisCardPlayer.DisCards = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
- // 吃牌玩家的手牌
- int[] temp = CardHelper.Add(player.RemainCards, self.DisCard);
- for (int i = 0; i < 3; i++)
- {
- temp = CardHelper.Remove(temp, card + i);
- }
- player.RemainCards = temp;
- // 刻子
- player.KeZi.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card, self.DisCardPlayer.Id));
-
- // 重置摸牌人
- self.DrawCardPlayer = player;
- self.PengPlayer = player;
-
- self.OperableList.Clear();
-
- // 重置时间和流程标记
- self.Time = 15;
- self.Flag = false;
-
- Log.Info($"玩家吃牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 吃的牌:{card}");
-
- // 广播
- foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
- {
- MessageHelper.SendToClient(p, new G2C_OperationPush(){info = ProtoHelper.RoomToProto(room, p, p)});
- }
- }
- /// <summary>
- /// 玩家是否可以操作碰
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- /// <returns></returns>
- private static bool IsCanPengGang(this HGHuangHuangComponent self, Player player)
- {
- if (!self.CanPgIds.Contains(player.Id))
- {
- return false;
- }
- if (self.ClickHuIds.Count > 0)
- {
- return false;
- }
- return self.CanHuIds.Count switch
- {
- > 1 => false,
- 1 => self.CanHuIds.Contains(player.Id),
- _ => true
- };
- }
- /// <summary>
- /// 玩家操作碰
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- /// <param name="player"></param>
- public static void Peng(this HGHuangHuangComponent self, Room room, Player player)
- {
- if (!self.IsCanPengGang(player))
- {
- return;
- }
-
- int num = CardHelper.CountCardNum(player.RemainCards, self.DisCard);
- if (num < 2)
- {
- return;
- }
- // 删除出牌人打出的牌堆
- int[] temp = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
- self.DisCardPlayer.DisCards = temp;
-
- // 碰牌玩家的手牌
- int[] remainCards = player.RemainCards;
- for (int i = 0; i < 2; i++)
- {
- remainCards = CardHelper.Remove(remainCards, self.DisCard);
- }
- player.RemainCards = remainCards;
-
- // 刻子
- player.KeZi.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.PENG, self.DisCard, self.DisCardPlayer.Id));
-
- // 重置摸牌人
- self.DrawCardPlayer = player;
- self.PengPlayer = player;
-
- self.OperableList.Clear();
-
- // 重置时间和流程标记
- self.Time = 15;
- self.Flag = false;
-
- Log.Info($"玩家碰牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 碰的牌:{self.DisCard}");
-
- // 广播
- foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
- {
- MessageHelper.SendToClient(p, new G2C_OperationPush(){info = ProtoHelper.RoomToProto(room, p, player)});
- }
- }
-
- /// <summary>
- /// 玩家操作杠
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- /// <param name="player"></param>
- /// <param name="card"></param>
- public static void Gang(this HGHuangHuangComponent self, Room room, Player player, int card)
- {
-
- }
-
- /// <summary>
- /// 玩家操作胡
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- /// <param name="player"></param>
- public static void Hu(this HGHuangHuangComponent self, Room room, Player player)
- {
-
- }
-
- /// <summary>
- /// 玩家操作过
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- /// <param name="player"></param>
- public static void Guo(this HGHuangHuangComponent self, Room room, Player player)
- {
-
- }
- /// <summary>
- /// 清空玩家可操作动作
- /// </summary>
- /// <param name="self"></param>
- private static void CleanUserAct(this HGHuangHuangComponent self) {
- foreach (Player player in self.Players)
- {
- if (player != null)
- {
- player.Act = new int[5];
- }
- }
- }
-
- /// <summary>
- /// 结束
- /// </summary>
- /// <param name="self"></param>
- /// <param name="room"></param>
- private static void GameOver(this HGHuangHuangComponent self, Room room)
- {
-
- }
- }
- }
|