HGHuangHuangComponentSystem.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. namespace ET.Server
  6. {
  7. [FriendOf(typeof (HGHuangHuangComponent))]
  8. public static class HGHuangHuangComponentSystem
  9. {
  10. [ObjectSystem]
  11. public class HGHuangHuangComponentAwakeSystem: AwakeSystem<HGHuangHuangComponent>
  12. {
  13. protected override void Awake(HGHuangHuangComponent self)
  14. {
  15. Room room = self.GetParent<Room>();
  16. if (room == null)
  17. {
  18. Log.Error($"黄冈晃晃主逻辑组件获取不到主实体...");
  19. return;
  20. }
  21. Log.Info($"创建黄冈晃晃主逻辑组件...");
  22. self.Flag = false;
  23. self.Time = 0;
  24. self.State = 0;
  25. self.Players = new Player[room.MaxNum];
  26. self.CurrentRound = 0;
  27. self.ZhuangPos = 0;
  28. self.Rand = new[] { 0, 0 };
  29. self.GangType = -1;
  30. self.AdmitDefeatList = new List<long>();
  31. self.CanPgIds = new List<long>();
  32. self.GangHuIds = new List<long>();
  33. self.CanHuIds = new List<long>();
  34. self.OperableList = new List<long>();
  35. self.ClickHuIds = new List<long>();
  36. self.HuResult = -1;
  37. self.CardList = new List<int>();
  38. self.UpdateTime = 0;
  39. // 初始化牌库
  40. for (int i = 0; i < 4; i++)
  41. {
  42. foreach (int value in HGHuangHuangConst.Values)
  43. {
  44. self.CardList.Add(value);
  45. }
  46. RandomGenerator.Shuffle(self.CardList);
  47. }
  48. }
  49. }
  50. [ObjectSystem]
  51. public class HGHuangHuangComponentDestroySystem: DestroySystem<HGHuangHuangComponent>
  52. {
  53. protected override void Destroy(HGHuangHuangComponent self)
  54. {
  55. Log.Info($"销毁黄冈晃晃主逻辑组件...");
  56. }
  57. }
  58. [ObjectSystem]
  59. [FriendOf(typeof(Room))]
  60. public class HGHuangHuangComponentUpdateSystem : UpdateSystem<HGHuangHuangComponent>
  61. {
  62. protected override void Update(HGHuangHuangComponent self)
  63. {
  64. long timeNow = TimeHelper.ClientNow();
  65. // 每秒执行一次
  66. if (timeNow - self.UpdateTime <= 1000)
  67. {
  68. return;
  69. }
  70. self.UpdateTime = timeNow;
  71. Room room = self.GetParent<Room>();
  72. if (room == null)
  73. {
  74. Log.Error($"黄冈晃晃主逻辑组件获取不到主实体...");
  75. return;
  76. }
  77. switch (self.State)
  78. {
  79. case 0:
  80. // 等待状态
  81. Log.Debug($"检测: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}, 状态:检测是否可开局...");
  82. if (self.CheckReadyStart(room))
  83. {
  84. self.State = 1;
  85. self.Flag = false;
  86. self.Time = 0;
  87. }
  88. // 120秒未开始直接解散
  89. if (self.Time >= 120)
  90. {
  91. room.Dispose();
  92. }
  93. break;
  94. case 1:
  95. // 开局
  96. if (!self.Flag)
  97. {
  98. if (self.Time >= 3)
  99. {
  100. self.Start(room);
  101. self.Flag = true;
  102. self.Time = 0;
  103. Log.Debug($"检测: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}, 状态:开局...VS动画...");
  104. }
  105. }
  106. else
  107. {
  108. if (self.Time >= 4)
  109. {
  110. self.State = 2;
  111. self.Flag = false;
  112. self.Time = 0;
  113. }
  114. }
  115. break;
  116. case 2:
  117. // 进行中
  118. if (!self.Flag)
  119. {
  120. self.Flag = true;
  121. self.Time = 0;
  122. // 摸牌
  123. self.DrawCard(room);
  124. }
  125. else
  126. {
  127. // 强制操作
  128. if (self.Time >= 15)
  129. {
  130. if (self.DrawCardPlayer.Id == self.CurrentPlayer.Id)
  131. {
  132. if (self.OperableList.Count > 0)
  133. {
  134. self.Guo(room, self.CurrentPlayer);
  135. }
  136. self.DisCard(room, self.CurrentPlayer, 0, true);
  137. }
  138. else
  139. {
  140. if (self.OperableList.Count > 0)
  141. {
  142. self.Guo(room, self.CurrentPlayer);
  143. }
  144. }
  145. self.Time = 15;
  146. }
  147. else
  148. {
  149. // todo 自动托管逻辑
  150. if (self.CurrentPlayer != null && self.CurrentPlayer.State != 3 && self.CurrentPlayer.IsAuto)
  151. {
  152. }
  153. }
  154. }
  155. break;
  156. case 3:
  157. // 已结束
  158. if (!self.Flag)
  159. {
  160. self.Flag = true;
  161. self.Time = 0;
  162. Log.Debug($"游戏结束: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}");
  163. }
  164. else
  165. {
  166. // todo 超时删除房间
  167. if (self.Time >= 60)
  168. {
  169. room.Dispose();
  170. }
  171. }
  172. break;
  173. }
  174. self.Time++;
  175. }
  176. }
  177. /// <summary>
  178. /// 检测是否可开始 3秒倒计时开始
  179. /// </summary>
  180. /// <param name="self"></param>
  181. /// <param name="room"></param>
  182. /// <returns></returns>
  183. private static bool CheckReadyStart(this HGHuangHuangComponent self, Room room)
  184. {
  185. if (!room.IsStart() || self.State != 0)
  186. {
  187. return false;
  188. }
  189. // 通知客户端3秒倒计时
  190. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  191. {
  192. MessageHelper.SendToClient(player, new G2C_ReadyStartPush(){ReadyStartTime = 3});
  193. }
  194. return true;
  195. }
  196. /// <summary>
  197. /// 发牌
  198. /// </summary>
  199. /// <param name="self"></param>
  200. /// <param name="room"></param>
  201. private static void SendCard(this HGHuangHuangComponent self, Room room)
  202. {
  203. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  204. {
  205. for (int i = 0; i < 13; i++)
  206. {
  207. int card = self.CardList[0];
  208. player.RemainCards = CardHelper.Add(player.RemainCards, card);
  209. self.CardList.RemoveAt(0);
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// 游戏开局
  215. /// </summary>
  216. /// <param name="self"></param>
  217. /// <param name="room"></param>
  218. private static void Start(this HGHuangHuangComponent self, Room room)
  219. {
  220. self.CurrentRound += 1;
  221. // 摇骰子
  222. int rand1 = RandomGenerator.RandomNumber(1, 7);
  223. int rand2 = RandomGenerator.RandomNumber(1, 7);
  224. self.Rand = new[] { rand1, rand2 };
  225. // 定庄
  226. // self.ZhuangPos = (rand1 + rand2) % 4;
  227. self.ZhuangPos = (rand1 + rand2) % room.MaxNum;
  228. // 设置当前操作玩家
  229. self.CurrentPlayer = self.Players[self.ZhuangPos];
  230. // 设置当前摸牌玩家
  231. self.DrawCardPlayer = self.Players[self.ZhuangPos];
  232. // 发牌
  233. self.SendCard(room);
  234. // 广播
  235. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  236. {
  237. player.State = 2;
  238. MessageHelper.SendToClient(player, new G2C_StartPush(){info = ProtoHelper.RoomToProto(room, player, null)});
  239. Log.Info($"游戏开局, 房间id={room.RoomId}, 玩家id={player.Id}, 玩家={player.Name}, 手牌信息={player.RemainCards}");
  240. }
  241. }
  242. /// <summary>
  243. /// 摸牌
  244. /// </summary>
  245. /// <param name="self"></param>
  246. /// <param name="room"></param>
  247. private static void DrawCard(this HGHuangHuangComponent self, Room room)
  248. {
  249. // 2个以上玩家认输或牌库没牌了直接结束
  250. if (self.AdmitDefeatList.Count > 2 || self.CardList.Count <= 0)
  251. {
  252. self.GameOver(room);
  253. return;
  254. }
  255. Player drawCardPlayer = self.DrawCardPlayer;
  256. if (drawCardPlayer == null)
  257. {
  258. Log.Error($"房间id={room.RoomId}, 摸牌玩家为空...");
  259. return;
  260. }
  261. // 摸牌
  262. int card = self.CardList[0];
  263. drawCardPlayer.RemainCards = CardHelper.Add(drawCardPlayer.RemainCards, card);
  264. self.CardList.RemoveAt(0);
  265. // 当前摸的牌
  266. self.DrawCard = card;
  267. // 吃,碰,杠,胡,过
  268. bool hasAct = false;
  269. // 检测摸牌人动作, 校验摸牌是否胡
  270. Struct.HuRes huRes = HGHuangHuangHelper.CheckHu(drawCardPlayer.RemainCards);
  271. if (huRes.Type != HGHuangHuangConst.HU_DEFAULT)
  272. {
  273. hasAct = true;
  274. drawCardPlayer.Act[3] = 1;
  275. self.CanHuIds.Add(drawCardPlayer.Id);
  276. if (!self.OperableList.Contains(drawCardPlayer.Id))
  277. {
  278. self.OperableList.Add(drawCardPlayer.Id);
  279. }
  280. }
  281. // todo 玩家听牌状态不允许杠
  282. // 检测摸牌人动作, 校验摸牌是否杠
  283. List<Struct.Kezi> gang = HGHuangHuangHelper.IsDrawGang(drawCardPlayer);
  284. if (gang is { Count: > 0 })
  285. {
  286. hasAct = true;
  287. drawCardPlayer.Act[2] = 1;
  288. if (!self.OperableList.Contains(drawCardPlayer.Id))
  289. {
  290. self.OperableList.Add(drawCardPlayer.Id);
  291. }
  292. if (!self.CanPgIds.Contains(drawCardPlayer.Id))
  293. {
  294. self.CanPgIds.Add(drawCardPlayer.Id);
  295. }
  296. }
  297. // 过牌
  298. drawCardPlayer.Act[4] = hasAct? 1 : 0;
  299. if (drawCardPlayer.HuCards.Any(_card => _card > 0 && _card == self.DrawCard))
  300. {
  301. drawCardPlayer.Act[4] = 0;
  302. }
  303. // 推送摸牌广播
  304. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  305. {
  306. MessageHelper.SendToClient(player, new G2C_DrawCardPush(){info = ProtoHelper.RoomToProto(room, player, drawCardPlayer)});
  307. Log.Info($"摸牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 摸牌玩家:{player.Id == drawCardPlayer.Id}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 摸的牌:{card}");
  308. }
  309. }
  310. /// <summary>
  311. /// 出牌
  312. /// </summary>
  313. /// <param name="self"></param>
  314. /// <param name="room"></param>
  315. /// <param name="player"></param>
  316. /// <param name="card">出的牌(0.当前摸牌玩家自动出牌 -1.非当前摸牌玩家自动出牌 >0.玩家手动出牌)</param>
  317. /// <param name="flag">是否自动出牌</param>
  318. public static async void DisCard(this HGHuangHuangComponent self, Room room, Player player, int card, bool flag)
  319. {
  320. if (player == null)
  321. {
  322. Log.Error($"出牌错误,player is null.");
  323. return;
  324. }
  325. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.PlayerOperation, player.Id))
  326. {
  327. if (self.CurrentPlayer.Id == player.Id && self.DrawCardPlayer.Id == player.Id)
  328. {
  329. Log.Info($"出牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息: {string.Join(", ", player.RemainCards)}");
  330. // 自动出牌
  331. if (flag)
  332. {
  333. if (self.PengPlayer != null && self.PengPlayer.Id == player.Id)
  334. {
  335. self.PengPlayer = null;
  336. int[] tmpCard = new int[player.RemainCards.Length];
  337. Array.Copy(player.RemainCards, 0, tmpCard, 0, player.RemainCards.Length);
  338. Array.Sort(tmpCard);
  339. card = tmpCard[^1];
  340. }
  341. else
  342. {
  343. card = player.RemainCards[^1];
  344. }
  345. // todo 真人托管状态
  346. }
  347. if (!player.RemainCards.Contains(card))
  348. {
  349. Log.Error($"出牌错误,玩家的牌不在手中...");
  350. return;
  351. }
  352. // 玩家出牌
  353. player.RemainCards = CardHelper.Remove(player.RemainCards, card);
  354. player.DisCards = CardHelper.Add(player.DisCards, card);
  355. player.DiscardCount += 1;
  356. self.DisCard = card;
  357. self.DisCardPlayer = player;
  358. self.PengPlayer = null;
  359. // 给出牌人广播
  360. MessageHelper.SendToClient(player, new G2C_DisCardPush(){info = ProtoHelper.RoomToProto(room, player, player)});
  361. self.CanHuIds.Clear();
  362. self.ClickHuIds.Clear();
  363. self.OperableList.Clear();
  364. self.CanPgIds.Clear();
  365. self.CleanUserAct();
  366. // 检测其它三家的动作
  367. int[] nextPos = room.GetSorcPos(player.Pos);
  368. foreach (int index in nextPos)
  369. {
  370. Player otherPlayer = self.Players[index];
  371. if (otherPlayer != null)
  372. {
  373. // 吃,碰,杠,胡,过
  374. int[] act = new int[5];
  375. List<Struct.Kezi> gangs = new List<Struct.Kezi>();
  376. List<Struct.Kezi> pengs = new List<Struct.Kezi>();
  377. List<Struct.Kezi> chis = new List<Struct.Kezi>();
  378. // todo 玩家听牌状态不允许有动作
  379. // 出牌是否杠
  380. gangs = HGHuangHuangHelper.IsDiscardGang(otherPlayer, card, player.Id);
  381. // 出牌是否碰
  382. pengs = HGHuangHuangHelper.IsDiscardPeng(otherPlayer, card, player.Id);
  383. // 出牌是否吃
  384. if (otherPlayer.Pos == nextPos[0])
  385. {
  386. chis = HGHuangHuangHelper.IsDiscardChi(otherPlayer, card, player.Id);
  387. }
  388. // 游戏中才会动作
  389. if (otherPlayer.State == 2)
  390. {
  391. bool hasAct = false;
  392. int[] tmpCards = new int[otherPlayer.RemainCards.Length];
  393. Array.Copy(otherPlayer.RemainCards, 0, tmpCards, 0, otherPlayer.RemainCards.Length);
  394. tmpCards = CardHelper.Add(tmpCards, card);
  395. // 校验胡牌
  396. Struct.HuRes huType = HGHuangHuangHelper.CheckHu(tmpCards);
  397. if (huType.Type != HGHuangHuangConst.HU_DEFAULT)
  398. {
  399. hasAct = true;
  400. act[3] = 1;
  401. // 放入可胡玩家列表
  402. if (!self.CanHuIds.Contains(otherPlayer.Id))
  403. {
  404. self.CanHuIds.Add(otherPlayer.Id);
  405. }
  406. // 加入可操作玩家list
  407. if (!self.OperableList.Contains(otherPlayer.Id))
  408. {
  409. self.OperableList.Add(otherPlayer.Id);
  410. }
  411. }
  412. // 是否杠
  413. if (gangs.Count > 0)
  414. {
  415. hasAct = true;
  416. act[2] = 1;
  417. }
  418. // 是否碰
  419. if (pengs.Count > 0)
  420. {
  421. hasAct = true;
  422. act[1] = 1;
  423. }
  424. // 是否吃
  425. if (chis.Count > 0)
  426. {
  427. hasAct = true;
  428. act[0] = 1;
  429. }
  430. // 是否过
  431. if (hasAct) {
  432. act[4] = 1;
  433. }
  434. if (act[2] == 1 || act[1] == 1) {
  435. // 加入可操作玩家list
  436. if (!self.OperableList.Contains(otherPlayer.Id)) {
  437. self.OperableList.Add(otherPlayer.Id);
  438. }
  439. // 加入可碰杠玩家集合
  440. if (!self.CanPgIds.Contains(otherPlayer.Id)) {
  441. self.CanPgIds.Add(otherPlayer.Id);
  442. }
  443. }
  444. // 设置玩家动作
  445. otherPlayer.Act = act;
  446. }
  447. // 广播其它三家
  448. MessageHelper.SendToClient(otherPlayer, new G2C_DisCardPush(){info = ProtoHelper.RoomToProto(room, otherPlayer, player)});
  449. }
  450. }
  451. if (self.CanHuIds.Count <= 0)
  452. {
  453. self.GangPlayer = null;
  454. }
  455. // 判断是否下家摸牌, 指定下家摸牌
  456. if (self.OperableList.Count == 0)
  457. {
  458. Player nextPlayer = null;
  459. foreach (int index in nextPos)
  460. {
  461. Player tempPlayer = self.Players[index];
  462. if (tempPlayer is { State: 2 } && tempPlayer.Id != player.Id)
  463. {
  464. nextPlayer = tempPlayer;
  465. break;
  466. }
  467. }
  468. // 有三家认输,就会有null
  469. if (nextPlayer == null)
  470. {
  471. self.GameOver(room);
  472. }
  473. else
  474. {
  475. // 设置摸牌玩家
  476. self.DrawCardPlayer = nextPlayer;
  477. // 设置操作玩家
  478. self.CurrentPlayer = nextPlayer;
  479. // 摸牌
  480. self.Flag = false;
  481. }
  482. }
  483. else
  484. {
  485. long id = 0;
  486. if (self.CanHuIds.Count > 0)
  487. {
  488. id = self.CanHuIds.First();
  489. } else if (self.CanPgIds.Count > 0)
  490. {
  491. id = self.CanPgIds.First();
  492. }
  493. else
  494. {
  495. id = self.OperableList.First();
  496. }
  497. Player tmpPlayer = room.GetPlayer(id);
  498. if (tmpPlayer != null)
  499. {
  500. self.CurrentPlayer = self.Players[tmpPlayer.Pos];
  501. }
  502. }
  503. }
  504. }
  505. }
  506. /// <summary>
  507. /// 玩家是否可以操作吃
  508. /// </summary>
  509. /// <param name="self"></param>
  510. /// <param name="player"></param>
  511. /// <returns></returns>
  512. private static bool IsCanChi(this HGHuangHuangComponent self, Player player)
  513. {
  514. if (self.ClickHuIds.Count > 0)
  515. {
  516. return false;
  517. }
  518. if (self.CanHuIds.Count > 1)
  519. {
  520. return false;
  521. }
  522. else
  523. {
  524. if (self.CanHuIds.Count == 1)
  525. {
  526. if (self.CanPgIds.Count > 0)
  527. {
  528. if (self.CanPgIds.Any(id => id > 0 && id != player.Id))
  529. {
  530. return false;
  531. }
  532. }
  533. return self.CanHuIds.Contains(player.Id);
  534. }
  535. else
  536. {
  537. if (self.CanPgIds.Count > 0)
  538. {
  539. return self.CanPgIds.Contains(player.Id);
  540. }
  541. else
  542. {
  543. return true;
  544. }
  545. }
  546. }
  547. }
  548. /// <summary>
  549. /// 玩家操作吃
  550. /// </summary>
  551. /// <param name="self"></param>
  552. /// <param name="room"></param>
  553. /// <param name="player"></param>
  554. /// <param name="card"></param>
  555. public static void Chi(this HGHuangHuangComponent self, Room room, Player player, int card)
  556. {
  557. if (self.IsCanChi(player))
  558. {
  559. return;
  560. }
  561. if (self.DisCard >= HGHuangHuangConst.DONG_FENG)
  562. {
  563. return;
  564. }
  565. // 删除出牌人打出的牌堆
  566. self.DisCardPlayer.DisCards = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  567. // 吃牌玩家的手牌
  568. int[] temp = CardHelper.Add(player.RemainCards, self.DisCard);
  569. for (int i = 0; i < 3; i++)
  570. {
  571. temp = CardHelper.Remove(temp, card + i);
  572. }
  573. player.RemainCards = temp;
  574. // 刻子
  575. player.KeZi.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card, self.DisCardPlayer.Id));
  576. // 重置摸牌人
  577. self.DrawCardPlayer = player;
  578. self.PengPlayer = player;
  579. self.OperableList.Clear();
  580. // 重置时间和流程标记
  581. self.Time = 15;
  582. self.Flag = false;
  583. Log.Info($"玩家吃牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 吃的牌:{card}");
  584. // 广播
  585. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  586. {
  587. RoomInfo info = ProtoHelper.RoomToProto(room, p, player);
  588. info.Time = 15;
  589. MessageHelper.SendToClient(p, new G2C_OperationPush(){info = info, OpType = 1});
  590. }
  591. }
  592. /// <summary>
  593. /// 玩家是否可以操作碰
  594. /// </summary>
  595. /// <param name="self"></param>
  596. /// <param name="player"></param>
  597. /// <returns></returns>
  598. private static bool IsCanPengGang(this HGHuangHuangComponent self, Player player)
  599. {
  600. if (!self.CanPgIds.Contains(player.Id))
  601. {
  602. return false;
  603. }
  604. if (self.ClickHuIds.Count > 0)
  605. {
  606. return false;
  607. }
  608. return self.CanHuIds.Count switch
  609. {
  610. > 1 => false,
  611. 1 => self.CanHuIds.Contains(player.Id),
  612. _ => true
  613. };
  614. }
  615. /// <summary>
  616. /// 玩家操作碰
  617. /// </summary>
  618. /// <param name="self"></param>
  619. /// <param name="room"></param>
  620. /// <param name="player"></param>
  621. public static void Peng(this HGHuangHuangComponent self, Room room, Player player)
  622. {
  623. if (!self.IsCanPengGang(player))
  624. {
  625. return;
  626. }
  627. int num = CardHelper.CountCardNum(player.RemainCards, self.DisCard);
  628. if (num < 2)
  629. {
  630. return;
  631. }
  632. // 删除出牌人打出的牌堆
  633. int[] temp = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  634. self.DisCardPlayer.DisCards = temp;
  635. // 碰牌玩家的手牌
  636. int[] remainCards = player.RemainCards;
  637. for (int i = 0; i < 2; i++)
  638. {
  639. remainCards = CardHelper.Remove(remainCards, self.DisCard);
  640. }
  641. player.RemainCards = remainCards;
  642. // 刻子
  643. player.KeZi.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.PENG, self.DisCard, self.DisCardPlayer.Id));
  644. // 重置摸牌人
  645. self.DrawCardPlayer = player;
  646. self.PengPlayer = player;
  647. self.OperableList.Clear();
  648. // 重置时间和流程标记
  649. self.Time = 15;
  650. self.Flag = false;
  651. Log.Info($"玩家碰牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 碰的牌:{self.DisCard}");
  652. // 广播
  653. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  654. {
  655. RoomInfo info = ProtoHelper.RoomToProto(room, p, player);
  656. info.Time = 15;
  657. MessageHelper.SendToClient(p, new G2C_OperationPush(){info = info, OpType = 2});
  658. }
  659. }
  660. /// <summary>
  661. /// 摸牌杠(暗杠或者回杠)
  662. /// </summary>
  663. /// <param name="self"></param>
  664. /// <param name="room"></param>
  665. /// <param name="player"></param>
  666. /// <param name="card"></param>
  667. /// <returns></returns>
  668. private static int GangPaiDraw(this HGHuangHuangComponent self, Room room, Player player, int card)
  669. {
  670. int type = (int)HGHuangHuangConst.KeziType.DEFAULT;
  671. int[] remainCards = player.RemainCards;
  672. int num = CardHelper.CountCardNum(remainCards, card);
  673. if (num == 4)
  674. {
  675. for (int i = 0; i < 4; i++)
  676. {
  677. remainCards = CardHelper.Remove(remainCards, card);
  678. }
  679. player.RemainCards = remainCards;
  680. player.KeZi.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.AN_GANG, card, player.Id));
  681. self.GangPlayer = player;
  682. self.GangPai = card;
  683. self.GangType = (int)HGHuangHuangConst.KeziType.AN_GANG;
  684. type = (int)HGHuangHuangConst.KeziType.AN_GANG;
  685. }
  686. else if (num == 1)
  687. {
  688. List<Struct.Kezi> templist = new List<Struct.Kezi>();
  689. if (player.KeZi.Count > 0)
  690. {
  691. templist = player.KeZi;
  692. }
  693. for (int i = 0; i < templist.Count; i++)
  694. {
  695. Struct.Kezi kz = templist[i];
  696. if (kz is not { Type: (int)HGHuangHuangConst.KeziType.PENG } || kz.Card != card)
  697. {
  698. continue;
  699. }
  700. player.KeZi.RemoveAt(i);
  701. remainCards = CardHelper.Remove(remainCards, card);
  702. player.KeZi.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.HUI_GANG, card, player.Id));
  703. }
  704. player.RemainCards = remainCards;
  705. self.GangPlayer = player;
  706. self.GangPai = card;
  707. self.GangType = (int)HGHuangHuangConst.KeziType.HUI_GANG;
  708. type = (int)HGHuangHuangConst.KeziType.HUI_GANG;
  709. }
  710. return type;
  711. }
  712. /// <summary>
  713. /// 出牌杠(明杠)
  714. /// </summary>
  715. /// <param name="self"></param>
  716. /// <param name="room"></param>
  717. /// <param name="player"></param>
  718. /// <param name="card"></param>
  719. /// <returns></returns>
  720. private static int GangPaiDisCard(this HGHuangHuangComponent self, Room room, Player player, int card)
  721. {
  722. const int type = (int)HGHuangHuangConst.KeziType.DEFAULT;
  723. if (self.DisCardPlayer == null)
  724. {
  725. return type;
  726. }
  727. self.DisCardPlayer.DisCards = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  728. for (int i = 0; i < 3; i++)
  729. {
  730. player.RemainCards = CardHelper.Remove(player.RemainCards, card);
  731. }
  732. player.KeZi.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.MING_GANG, card, self.DisCardPlayer.Id));
  733. self.GangPlayer = player;
  734. self.GangPai = card;
  735. self.GangType = (int)HGHuangHuangConst.KeziType.MING_GANG;
  736. return (int)HGHuangHuangConst.KeziType.MING_GANG;
  737. }
  738. /// <summary>
  739. /// 检测是否有抢杠胡
  740. /// </summary>
  741. /// <param name="self"></param>
  742. /// <param name="room"></param>
  743. /// <param name="player"></param>
  744. /// <param name="card">杠的牌</param>
  745. /// <returns></returns>
  746. private static bool CheckQangGangHu(this HGHuangHuangComponent self, Room room, Player player, int card)
  747. {
  748. bool flag = false;
  749. int[] nextPos = room.GetSorcPos(player.Pos);
  750. foreach (int index in nextPos) {
  751. Player p = self.Players[index];
  752. if (p == null)
  753. {
  754. continue;
  755. }
  756. // 玩家手牌
  757. int[] tmpCards = new int[p.RemainCards.Length];
  758. Array.Copy(p.RemainCards, 0, tmpCards, 0, p.RemainCards.Length);
  759. tmpCards = CardHelper.Add(tmpCards, card);
  760. Struct.HuRes result = HGHuangHuangHelper.CheckHu(tmpCards);
  761. if (result.Type != HGHuangHuangConst.HU_DEFAULT) {
  762. flag = true;
  763. if (!self.CanHuIds.Contains(p.Id)) {
  764. self.CanHuIds.Add(p.Id);
  765. }
  766. if (!self.OperableList.Contains(p.Id)) {
  767. self.OperableList.Add(p.Id);
  768. }
  769. if (!self.GangHuIds.Contains(p.Id)) {
  770. self.GangHuIds.Add(p.Id);
  771. }
  772. p.Act = new [] { 0, 0, 0, 1, 1 };
  773. }
  774. }
  775. return flag;
  776. }
  777. /// <summary>
  778. /// 玩家操作杠
  779. /// </summary>
  780. /// <param name="self"></param>
  781. /// <param name="room"></param>
  782. /// <param name="player"></param>
  783. /// <param name="card"></param>
  784. public static void Gang(this HGHuangHuangComponent self, Room room, Player player, int card)
  785. {
  786. if (card > 0 && self.IsCanPengGang(player))
  787. {
  788. int type = (int)HGHuangHuangConst.KeziType.DEFAULT;
  789. if (self.DrawCardPlayer.Id == player.Id)
  790. {
  791. type = self.GangPaiDraw(room, player, card);
  792. }
  793. else if (self.DrawCardPlayer.Id != player.Id && self.DisCard == card)
  794. {
  795. type = self.GangPaiDisCard(room, player, card);
  796. }
  797. if (type != (int)HGHuangHuangConst.KeziType.DEFAULT)
  798. {
  799. self.OperableList.Clear();
  800. // bool flag = true;
  801. if (self.GangType == (int)HGHuangHuangConst.KeziType.HUI_GANG)
  802. {
  803. if (self.CheckQangGangHu(room, self.GangPlayer, self.GangPai))
  804. {
  805. // 当前操作玩家
  806. Player tmpPlayer = room.GetPlayer(self.CanHuIds.First());
  807. if (tmpPlayer != null)
  808. {
  809. self.CurrentPlayer = self.Players[tmpPlayer.Pos];
  810. }
  811. self.Time = 15;
  812. // flag = false;
  813. }
  814. }
  815. // 广播
  816. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  817. {
  818. RoomInfo info = ProtoHelper.RoomToProto(room, p, player);
  819. info.Time = 15;
  820. MessageHelper.SendToClient(p, new G2C_OperationPush(){info = info, OpType = 3});
  821. }
  822. }
  823. }
  824. }
  825. /// <summary>
  826. /// 自摸胡牌(手动/自动)
  827. /// </summary>
  828. /// <param name="self"></param>
  829. /// <param name="room"></param>
  830. /// <param name="player"></param>
  831. private static void ZimoHuPai(this HGHuangHuangComponent self, Room room, Player player)
  832. {
  833. if (self.OperableList.Contains(player.Id) && self.CanHuIds.Contains(player.Id))
  834. {
  835. Struct.HuRes res = HGHuangHuangHelper.CheckHuType(player.KeZi, player.RemainCards, self.DrawCard);
  836. if (res.Type != HGHuangHuangConst.HU_DEFAULT)
  837. {
  838. self.OperableList.Remove(player.Id);
  839. self.CanHuIds.Remove(player.Id);
  840. self.ClickHuIds.Remove(player.Id);
  841. // 将摸的牌从手牌中移除
  842. player.RemainCards = CardHelper.Remove(player.RemainCards, self.DrawCard);
  843. self.HuResult = self.GangPlayer != null && player.Id == self.GangPlayer.Id ? (int)HGHuangHuangConst.Result.GANGKAI : (int)HGHuangHuangConst.Result.ZIMO;
  844. // 广播
  845. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  846. {
  847. RoomInfo info = ProtoHelper.RoomToProto(room, p, player);
  848. info.Time = 15;
  849. MessageHelper.SendToClient(p, new G2C_OperationPush(){info = info, OpType = 4});
  850. }
  851. // 自摸胡牌结算
  852. self.SettlementHu();
  853. }
  854. self.GameOver(room);
  855. }
  856. else
  857. {
  858. Log.Error($"自摸胡牌出错, playerId={player.Id}");
  859. }
  860. }
  861. /// <summary>
  862. /// 点炮胡牌广播
  863. /// </summary>
  864. /// <param name="self"></param>
  865. /// <param name="room"></param>
  866. /// <param name="player"></param>
  867. /// <param name="huType">牌型</param>
  868. /// <param name="isGangHu">是否抢杠胡</param>
  869. private static void DianPaoHuPaiBroadcast(this HGHuangHuangComponent self, Room room, Player player, int huType, bool isGangHu)
  870. {
  871. if (isGangHu)
  872. {
  873. // 移除抢杠胡集合玩家
  874. self.GangHuIds.Remove(player.Id);
  875. // 获取杠玩家
  876. Player gangPlayer = self.GangPlayer;
  877. if (gangPlayer == null)
  878. {
  879. return;
  880. }
  881. foreach (Struct.Kezi kezi in gangPlayer.KeZi.Where(kezi => kezi.Card == self.GangPai && kezi.Type == (int)HGHuangHuangConst.KeziType.HUI_GANG))
  882. {
  883. kezi.Type = (int)HGHuangHuangConst.KeziType.PENG;
  884. }
  885. self.HuResult = (int)HGHuangHuangConst.Result.QIANGGANG;
  886. }
  887. else
  888. {
  889. // 将玩家打出的牌从出牌集合中删除
  890. if (self.DisCardPlayer.DisCards.Contains(self.DisCard))
  891. {
  892. self.DisCardPlayer.DisCards = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  893. }
  894. self.HuResult = (int)HGHuangHuangConst.Result.DIANPAO;
  895. }
  896. player.HuCards.Add(self.GangPai);
  897. // 广播
  898. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  899. {
  900. RoomInfo info = ProtoHelper.RoomToProto(room, p, player);
  901. info.Time = 15;
  902. MessageHelper.SendToClient(p, new G2C_OperationPush(){info = info, OpType = 4});
  903. }
  904. }
  905. /// <summary>
  906. /// 点炮胡牌(手动/自动)
  907. /// </summary>
  908. /// <param name="self"></param>
  909. /// <param name="room"></param>
  910. /// <param name="player"></param>
  911. private static void DianPaoHuPai(this HGHuangHuangComponent self, Room room, Player player)
  912. {
  913. int card = 0;
  914. bool isGangHU = false;
  915. if (self.GangHuIds.Contains(player.Id))
  916. {
  917. card = self.GangPai;
  918. isGangHU = true;
  919. }
  920. else
  921. {
  922. card = self.DisCard;
  923. }
  924. if (self.CanHuIds.Contains(player.Id))
  925. {
  926. int[] userCards = player.RemainCards;
  927. int[] tmpCards = new int[userCards.Length];
  928. Array.Copy(userCards, 0, tmpCards, 0, userCards.Length);
  929. tmpCards = CardHelper.Add(tmpCards, card);
  930. Struct.HuRes res = HGHuangHuangHelper.CheckHuType(player.KeZi, tmpCards, card);
  931. if (res.Type != HGHuangHuangConst.HU_DEFAULT)
  932. {
  933. if (!self.ClickHuIds.Contains(player.Id))
  934. {
  935. self.ClickHuIds.Add(player.Id);
  936. }
  937. self.CanPgIds.Clear();
  938. self.OperableList.Remove(player.Id);
  939. self.CanHuIds.Remove(player.Id);
  940. // 广播
  941. self.DianPaoHuPaiBroadcast(room, player, res.Type, isGangHU);
  942. if (self.CanHuIds.Count == 0)
  943. {
  944. // 点炮胡结算
  945. self.SettlementHu();
  946. self.GameOver(room);
  947. }
  948. else
  949. {
  950. long id = self.CanHuIds.First();
  951. Player tmpPlayer = room.GetPlayer(id);
  952. if (tmpPlayer != null)
  953. {
  954. self.CurrentPlayer = tmpPlayer;
  955. }
  956. self.Time = 15;
  957. }
  958. }
  959. }
  960. }
  961. /// <summary>
  962. /// 玩家操作胡
  963. /// </summary>
  964. /// <param name="self"></param>
  965. /// <param name="room"></param>
  966. /// <param name="player"></param>
  967. public static void Hu(this HGHuangHuangComponent self, Room room, Player player)
  968. {
  969. int r = player.RemainCards.Length % 3;
  970. switch (r)
  971. {
  972. case 2:
  973. self.ZimoHuPai(room, player);
  974. break;
  975. case 1:
  976. self.DianPaoHuPai(room, player);
  977. break;
  978. }
  979. }
  980. /// <summary>
  981. /// 玩家操作过
  982. /// </summary>
  983. /// <param name="self"></param>
  984. /// <param name="room"></param>
  985. /// <param name="player"></param>
  986. public static void Guo(this HGHuangHuangComponent self, Room room, Player player)
  987. {
  988. // 玩家必须在可操作列表里
  989. if (!self.OperableList.Contains(player.Id))
  990. {
  991. return;
  992. }
  993. // 从碰杠集合中移除
  994. self.CanPgIds.Remove(player.Id);
  995. // 从可操作玩家集合中移除
  996. self.OperableList.Remove(player.Id);
  997. // 不是摸牌人,改time
  998. if (self.DrawCardPlayer.Id != player.Id)
  999. {
  1000. if (self.CanHuIds.Contains(player.Id))
  1001. {
  1002. self.CanHuIds.Remove(player.Id);
  1003. }
  1004. if (self.GangHuIds.Count > 0)
  1005. {
  1006. self.GangHuIds.Remove(player.Id);
  1007. if (self.GangHuIds.Count > 0)
  1008. {
  1009. // 设置操作动作玩家
  1010. if (self.CurrentPlayer.Id == player.Id)
  1011. {
  1012. self.CurrentPlayer = room.GetPlayer(self.GangHuIds.First());
  1013. self.Time = 15;
  1014. // 广播过
  1015. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  1016. {
  1017. RoomInfo info = ProtoHelper.RoomToProto(room, p, player);
  1018. info.Time = 15;
  1019. MessageHelper.SendToClient(p, new G2C_OperationPush(){info = info, OpType = 5});
  1020. }
  1021. }
  1022. }
  1023. else
  1024. {
  1025. if (self.ClickHuIds.Count == 0)
  1026. {
  1027. self.DrawCardPlayer = self.GangPlayer;
  1028. self.Flag = false;
  1029. }
  1030. else
  1031. {
  1032. // 没有其他人可以胡,且有人点击胡 结算胡 游戏结束
  1033. self.SettlementHu();
  1034. self.GameOver(room);
  1035. }
  1036. }
  1037. }
  1038. else
  1039. {
  1040. if (self.OperableList.Count == 0)
  1041. {
  1042. if (self.ClickHuIds.Count > 0)
  1043. {
  1044. // 没有其他人有胡 且有人点击胡 结算胡 游戏结束
  1045. self.SettlementHu();
  1046. self.GameOver(room);
  1047. }
  1048. else
  1049. {
  1050. self.DrawCardPlayer = self.GetNextPlayer(self.DisCardPlayer.Pos);
  1051. self.Flag = false;
  1052. }
  1053. }
  1054. else
  1055. {
  1056. if (self.CurrentPlayer.Id == player.Id)
  1057. {
  1058. long opId = 0;
  1059. if (self.CanHuIds.Count > 0)
  1060. {
  1061. self.CurrentPlayer = self.Players[room.GetPlayer(self.CanHuIds.First()).Pos];
  1062. opId = self.CanHuIds.First();
  1063. }
  1064. else if (self.CanPgIds.Count > 0)
  1065. {
  1066. self.CurrentPlayer = self.Players[room.GetPlayer(self.CanPgIds.First()).Pos];
  1067. opId = self.CanPgIds.First();
  1068. }
  1069. else
  1070. {
  1071. self.CurrentPlayer = self.Players[room.GetPlayer(self.OperableList.First()).Pos];
  1072. opId = self.OperableList.First();
  1073. }
  1074. self.Time = 15;
  1075. // 广播过
  1076. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  1077. {
  1078. RoomInfo info = ProtoHelper.RoomToProto(room, p, player);
  1079. info.Time = 15;
  1080. MessageHelper.SendToClient(p, new G2C_OperationPush(){info = info, OpType = 5});
  1081. }
  1082. }
  1083. }
  1084. if (self.CanHuIds.Count == 0)
  1085. {
  1086. self.GangPlayer = null;
  1087. }
  1088. }
  1089. }
  1090. else
  1091. {
  1092. if (self.CanHuIds.Contains(player.Id))
  1093. {
  1094. // 从胡牌集合中移除
  1095. self.CanHuIds.Remove(player.Id);
  1096. }
  1097. }
  1098. }
  1099. /// <summary>
  1100. /// 获取下个玩家
  1101. /// </summary>
  1102. /// <param name="self"></param>
  1103. /// <param name="curPos">当前玩家位置</param>
  1104. /// <returns></returns>
  1105. private static Player GetNextPlayer(this HGHuangHuangComponent self, int curPos)
  1106. {
  1107. return self.Players[(curPos + 1) % self.GetParent<Room>().MaxNum];
  1108. }
  1109. /// <summary>
  1110. /// 清空玩家可操作动作
  1111. /// </summary>
  1112. /// <param name="self"></param>
  1113. private static void CleanUserAct(this HGHuangHuangComponent self) {
  1114. foreach (Player player in self.Players)
  1115. {
  1116. if (player != null)
  1117. {
  1118. player.Act = new int[5];
  1119. }
  1120. }
  1121. }
  1122. /// <summary>
  1123. /// 胡牌结算
  1124. /// </summary>
  1125. /// <param name="self"></param>
  1126. private static void SettlementHu(this HGHuangHuangComponent self)
  1127. {
  1128. }
  1129. /// <summary>
  1130. /// 结束
  1131. /// </summary>
  1132. /// <param name="self"></param>
  1133. /// <param name="room"></param>
  1134. private static void GameOver(this HGHuangHuangComponent self, Room room)
  1135. {
  1136. self.State = 3;
  1137. self.Time = 60;
  1138. self.Flag = false;
  1139. foreach (Player player in self.Players)
  1140. {
  1141. if (player != null)
  1142. {
  1143. player.State = 3;
  1144. }
  1145. }
  1146. Log.Info($"牌局结束...roomId={room.RoomId}");
  1147. }
  1148. }
  1149. }