HGHHComponentSystem.cs 50 KB

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