HGHHComponentSystem.cs 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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. self.Difen = 1;
  40. // 初始化牌库
  41. self.InitCardList();
  42. }
  43. }
  44. [ObjectSystem]
  45. public class HGHHComponentDestroySystem: DestroySystem<HGHHComponent>
  46. {
  47. protected override void Destroy(HGHHComponent self)
  48. {
  49. Log.Info($"销毁黄冈晃晃主逻辑组件...");
  50. }
  51. }
  52. [ObjectSystem]
  53. [FriendOf(typeof(Room))]
  54. public class HGHuangHuangComponentUpdateSystem : UpdateSystem<HGHHComponent>
  55. {
  56. protected override void Update(HGHHComponent self)
  57. {
  58. long timeNow = TimeHelper.ClientNow();
  59. // 每秒执行一次
  60. if (timeNow - self.UpdateTime <= 1000)
  61. {
  62. return;
  63. }
  64. self.UpdateTime = timeNow;
  65. Room room = self.GetParent<Room>();
  66. if (room == null)
  67. {
  68. Log.Error($"黄冈晃晃主逻辑组件获取不到主实体...");
  69. return;
  70. }
  71. switch (self.State)
  72. {
  73. case 0:
  74. // 等待状态
  75. Log.Debug($"检测: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}, 当前局数:{self.CurrentRound}, 状态:检测是否可开局...");
  76. if (self.CheckReadyStart(room))
  77. {
  78. self.State = 1;
  79. self.Flag = false;
  80. self.Time = 0;
  81. }
  82. // 120秒未开始直接解散
  83. if (self.Time >= 120)
  84. {
  85. room.Dispose();
  86. }
  87. break;
  88. case 1:
  89. // 开局
  90. if (!self.Flag)
  91. {
  92. if (self.Time >= 3)
  93. {
  94. self.Start(room);
  95. self.Flag = true;
  96. self.Time = 0;
  97. Log.Debug($"检测: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}, 当前局数:{self.CurrentRound}, 状态:开局...VS动画...");
  98. }
  99. }
  100. else
  101. {
  102. if (self.Time >= 4)
  103. {
  104. self.State = 2;
  105. self.Flag = false;
  106. self.Time = 0;
  107. }
  108. }
  109. break;
  110. case 2:
  111. // 进行中
  112. if (!self.Flag)
  113. {
  114. self.Flag = true;
  115. self.Time = 0;
  116. // 摸牌
  117. self.DrawCard(room);
  118. }
  119. else
  120. {
  121. // 强制操作
  122. // if (self.Time >= 30)
  123. // {
  124. // if (self.DrawCardPlayer.Id == self.CurrentPlayer.Id)
  125. // {
  126. // if (self.OperableList.Count > 0)
  127. // {
  128. // self.Guo(room, self.CurrentPlayer);
  129. // }
  130. // self.DisCard(room, self.CurrentPlayer, 0, true);
  131. // }
  132. // else
  133. // {
  134. // if (self.OperableList.Count > 0)
  135. // {
  136. // self.Guo(room, self.CurrentPlayer);
  137. // }
  138. // }
  139. //
  140. // self.Time = 30;
  141. //
  142. // }
  143. // else
  144. // {
  145. // // todo 自动托管逻辑
  146. // if (self.CurrentPlayer != null && self.CurrentPlayer.State != 3 && self.CurrentPlayer.IsAuto)
  147. // {
  148. //
  149. // }
  150. // }
  151. }
  152. break;
  153. case 3:
  154. // 已结束
  155. if (!self.Flag)
  156. {
  157. self.Flag = true;
  158. self.Time = 0;
  159. Log.Debug($"游戏结束: 黄冈晃晃-房间号:{room.RoomId}, 房间人数:{room.Players.Count}, 当前局数:{self.CurrentRound}");
  160. }
  161. else
  162. {
  163. // 小于最大局数,切等待状态
  164. if (self.CurrentRound < room.MaxRound)
  165. {
  166. self.ClearRoom();
  167. self.State = 0;
  168. self.Flag = false;
  169. self.Time = 0;
  170. }
  171. else
  172. {
  173. // todo 超时删除房间
  174. if (self.Time >= 60)
  175. {
  176. room.Dispose();
  177. }
  178. }
  179. }
  180. break;
  181. }
  182. self.Time++;
  183. }
  184. }
  185. /// <summary>
  186. /// 初始化牌库
  187. /// </summary>
  188. /// <param name="self"></param>
  189. private static void InitCardList(this HGHHComponent self)
  190. {
  191. // 初始化牌库
  192. for (int i = 0; i < 4; i++)
  193. {
  194. foreach (int value in HGHHConst.Values)
  195. {
  196. self.CardList.Add(value);
  197. }
  198. RandomGenerator.Shuffle(self.CardList);
  199. }
  200. }
  201. /// <summary>
  202. /// 检测是否可开始 3秒倒计时开始
  203. /// </summary>
  204. /// <param name="self"></param>
  205. /// <param name="room"></param>
  206. /// <returns></returns>
  207. private static bool CheckReadyStart(this HGHHComponent self, Room room)
  208. {
  209. if (!room.IsStart() || self.State != 0)
  210. {
  211. return false;
  212. }
  213. // 通知客户端3秒倒计时
  214. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  215. {
  216. MessageHelper.SendToClient(player, new G2C_HGHHReadyStartPush(){ReadyStartTime = 3});
  217. }
  218. return true;
  219. }
  220. /// <summary>
  221. /// 发牌
  222. /// </summary>
  223. /// <param name="self"></param>
  224. /// <param name="room"></param>
  225. private static void SendCard(this HGHHComponent self, Room room)
  226. {
  227. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  228. {
  229. for (int i = 0; i < 13; i++)
  230. {
  231. int card = self.CardList[0];
  232. player.RemainCards = CardHelper.Add(player.RemainCards, card);
  233. self.CardList.RemoveAt(0);
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// 游戏开局
  239. /// </summary>
  240. /// <param name="self"></param>
  241. /// <param name="room"></param>
  242. private static void Start(this HGHHComponent self, Room room)
  243. {
  244. self.CurrentRound += 1;
  245. // 摇骰子
  246. int rand1 = RandomGenerator.RandomNumber(1, 7);
  247. int rand2 = RandomGenerator.RandomNumber(1, 7);
  248. self.Rand = new[] { rand1, rand2 };
  249. // 定庄
  250. // self.ZhuangPos = (rand1 + rand2) % 4;
  251. self.ZhuangPos = (rand1 + rand2) % room.MaxNum;
  252. // 设置当前操作玩家
  253. self.CurrentPlayer = self.Players[self.ZhuangPos];
  254. // 设置当前摸牌玩家
  255. self.DrawCardPlayer = self.Players[self.ZhuangPos];
  256. // 发牌
  257. self.SendCard(room);
  258. // 广播
  259. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  260. {
  261. player.State = 2;
  262. MessageHelper.SendToClient(player, new G2C_HGHHStartPush(){info = ProtoHelper.RoomToProto(room, player, self.CurrentPlayer)});
  263. Log.Info($"游戏开局, 房间id={room.RoomId}, 玩家id={player.Id}, 玩家={player.Name}, 手牌信息={string.Join(", ", player.RemainCards)}");
  264. }
  265. }
  266. /// <summary>
  267. /// 摸牌
  268. /// </summary>
  269. /// <param name="self"></param>
  270. /// <param name="room"></param>
  271. private static void DrawCard(this HGHHComponent self, Room room)
  272. {
  273. // 2个以上玩家认输或牌库没牌了直接结束
  274. if (self.AdmitDefeatList.Count > 2 || self.CardList.Count <= 0)
  275. {
  276. self.GameOver(room);
  277. return;
  278. }
  279. Player drawCardPlayer = self.DrawCardPlayer;
  280. if (drawCardPlayer == null)
  281. {
  282. Log.Error($"房间id={room.RoomId}, 摸牌玩家为空...");
  283. return;
  284. }
  285. // 摸牌
  286. int card = self.CardList[0];
  287. drawCardPlayer.RemainCards = CardHelper.Add(drawCardPlayer.RemainCards, card);
  288. self.CardList.RemoveAt(0);
  289. // 当前摸的牌
  290. self.DrawCard = card;
  291. drawCardPlayer.ActInfo.Clear();
  292. // 吃,碰,杠,胡,过
  293. int[] act = new int[5];
  294. bool hasAct = false;
  295. // 检测摸牌人动作, 校验摸牌是否胡
  296. Struct.HuRes huRes = HGHHHelper.CheckHu(drawCardPlayer.RemainCards);
  297. if (huRes.Type != HGHHConst.HU_DEFAULT)
  298. {
  299. hasAct = true;
  300. act[3] = 1;
  301. self.CanHuIds.Add(drawCardPlayer.Id);
  302. if (!self.OperableList.Contains(drawCardPlayer.Id))
  303. {
  304. self.OperableList.Add(drawCardPlayer.Id);
  305. }
  306. }
  307. // todo 玩家听牌状态不允许杠
  308. // 检测摸牌人动作, 校验摸牌是否杠
  309. List<Struct.Kezi> gangs = HGHHHelper.IsDrawGang(drawCardPlayer);
  310. if (gangs is { Count: > 0 })
  311. {
  312. hasAct = true;
  313. act[2] = 1;
  314. foreach (Struct.Kezi kezi in gangs.Where(kezi => kezi is { Card: >= 0 }))
  315. {
  316. drawCardPlayer.ActInfo.Add(new Struct.Kezi(kezi.Type, kezi.Card, kezi.PlayerId));
  317. }
  318. }
  319. // 过牌
  320. act[4] = hasAct? 1 : 0;
  321. if (drawCardPlayer.HuCards.Any(_card => _card > 0 && _card == self.DrawCard))
  322. {
  323. act[4] = 0;
  324. }
  325. if (act[2] == 1 || act[1] == 1 || act[0] == 1) {
  326. // 加入可操作玩家list
  327. if (!self.OperableList.Contains(drawCardPlayer.Id)) {
  328. self.OperableList.Add(drawCardPlayer.Id);
  329. }
  330. // 加入可碰杠玩家集合
  331. if (!self.CanPgIds.Contains(drawCardPlayer.Id)) {
  332. self.CanPgIds.Add(drawCardPlayer.Id);
  333. }
  334. }
  335. // 设置玩家动作
  336. drawCardPlayer.Act = act;
  337. // 设置当前操作玩家
  338. self.CurrentPlayer = drawCardPlayer;
  339. // 重置时间和流程标记
  340. self.Time = 0;
  341. self.Flag = true;
  342. Log.Info($"摸牌... 玩家ID:{drawCardPlayer.Id}, 位置:{drawCardPlayer.Pos}, 手牌大小:{drawCardPlayer.RemainCards.Length}, 手牌信息:{string.Join(", ", drawCardPlayer.RemainCards)}, 摸的牌:{card}");
  343. // 推送摸牌广播
  344. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null))
  345. {
  346. RoomInfo info = ProtoHelper.RoomToProto(room, player, self.CurrentPlayer);
  347. info.Time = 30;
  348. MessageHelper.SendToClient(player, new G2C_HGHHDrawCardPush(){info = info});
  349. }
  350. }
  351. /// <summary>
  352. /// 出牌
  353. /// </summary>
  354. /// <param name="self"></param>
  355. /// <param name="room"></param>
  356. /// <param name="player"></param>
  357. /// <param name="card">出的牌(0.当前摸牌玩家自动出牌 -1.非当前摸牌玩家自动出牌 >0.玩家手动出牌)</param>
  358. /// <param name="flag">是否自动出牌</param>
  359. public static async void DisCard(this HGHHComponent self, Room room, Player player, int card, bool flag)
  360. {
  361. if (player == null)
  362. {
  363. Log.Error($"出牌错误,player is null.");
  364. return;
  365. }
  366. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.PlayerOperation, player.Id))
  367. {
  368. if (self.CurrentPlayer.Id != player.Id || self.DrawCardPlayer.Id != player.Id)
  369. {
  370. return;
  371. }
  372. Log.Info($"出牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息: {string.Join(", ", player.RemainCards)}");
  373. // 自动出牌
  374. if (flag)
  375. {
  376. if (self.PengPlayer != null && self.PengPlayer.Id == player.Id)
  377. {
  378. self.PengPlayer = null;
  379. int[] tmpCard = new int[player.RemainCards.Length];
  380. Array.Copy(player.RemainCards, 0, tmpCard, 0, player.RemainCards.Length);
  381. Array.Sort(tmpCard);
  382. card = tmpCard[^1];
  383. }
  384. else
  385. {
  386. card = player.RemainCards[^1];
  387. }
  388. // todo 真人托管状态
  389. }
  390. if (!player.RemainCards.Contains(card))
  391. {
  392. Log.Error($"出牌错误,玩家的牌不在手中...");
  393. return;
  394. }
  395. // 玩家出牌
  396. player.RemainCards = CardHelper.Remove(player.RemainCards, card);
  397. player.DisCards = CardHelper.Add(player.DisCards, card);
  398. player.DiscardCount += 1;
  399. self.DisCard = card;
  400. self.DisCardPlayer = player;
  401. self.PengPlayer = null;
  402. self.CanHuIds.Clear();
  403. self.ClickHuIds.Clear();
  404. self.OperableList.Clear();
  405. self.CanPgIds.Clear();
  406. self.CleanUserAct();
  407. // 检测其它三家的动作
  408. int[] nextPos = room.GetSorcPos(player.Pos);
  409. foreach (int index in nextPos)
  410. {
  411. Player otherPlayer = self.Players[index];
  412. if (otherPlayer is not { State: 2 })
  413. {
  414. continue;
  415. }
  416. // 吃,碰,杠,胡,过
  417. int[] act = new int[5];
  418. List<Struct.Kezi> gangs = new List<Struct.Kezi>();
  419. List<Struct.Kezi> pengs = new List<Struct.Kezi>();
  420. List<Struct.Kezi> chis = new List<Struct.Kezi>();
  421. // todo 玩家听牌状态不允许有动作
  422. // 出牌是否杠
  423. gangs = HGHHHelper.IsDiscardGang(otherPlayer, card, player.Id);
  424. // 出牌是否碰
  425. pengs = HGHHHelper.IsDiscardPeng(otherPlayer, card, player.Id);
  426. // 出牌是否吃
  427. if (otherPlayer.Pos == nextPos[0])
  428. {
  429. chis = HGHHHelper.IsDiscardChi(otherPlayer, card, player.Id);
  430. }
  431. bool hasAct = false;
  432. int[] tmpCards = new int[otherPlayer.RemainCards.Length];
  433. Array.Copy(otherPlayer.RemainCards, 0, tmpCards, 0, otherPlayer.RemainCards.Length);
  434. tmpCards = CardHelper.Add(tmpCards, card);
  435. // 校验胡牌
  436. Struct.HuRes huType = HGHHHelper.CheckHu(tmpCards);
  437. if (huType.Type != HGHHConst.HU_DEFAULT)
  438. {
  439. hasAct = true;
  440. act[3] = 1;
  441. // 放入可胡玩家列表
  442. if (!self.CanHuIds.Contains(otherPlayer.Id))
  443. {
  444. self.CanHuIds.Add(otherPlayer.Id);
  445. }
  446. // 加入可操作玩家list
  447. if (!self.OperableList.Contains(otherPlayer.Id))
  448. {
  449. self.OperableList.Add(otherPlayer.Id);
  450. }
  451. }
  452. // 是否杠
  453. if (gangs.Count > 0)
  454. {
  455. hasAct = true;
  456. act[2] = 1;
  457. foreach (Struct.Kezi kezi in gangs.Where(kezi => kezi is { Card: >= 0 }))
  458. {
  459. otherPlayer.ActInfo.Add(new Struct.Kezi(kezi.Type, kezi.Card, kezi.PlayerId));
  460. }
  461. }
  462. // 是否碰
  463. if (pengs.Count > 0)
  464. {
  465. hasAct = true;
  466. act[1] = 1;
  467. foreach (Struct.Kezi kezi in pengs.Where(kezi => kezi is { Card: >= 0 }))
  468. {
  469. otherPlayer.ActInfo.Add(new Struct.Kezi(kezi.Type, kezi.Card, kezi.PlayerId));
  470. }
  471. }
  472. // 是否吃
  473. if (chis.Count > 0)
  474. {
  475. hasAct = true;
  476. act[0] = 1;
  477. foreach (Struct.Kezi kezi in chis.Where(kezi => kezi is { Card: >= 0 }))
  478. {
  479. otherPlayer.ActInfo.Add(new Struct.Kezi(kezi.Type, kezi.Card, kezi.PlayerId));
  480. }
  481. }
  482. // 是否过
  483. if (hasAct) {
  484. act[4] = 1;
  485. }
  486. if (act[2] == 1 || act[1] == 1 || act[0] == 1) {
  487. // 加入可操作玩家list
  488. if (!self.OperableList.Contains(otherPlayer.Id)) {
  489. self.OperableList.Add(otherPlayer.Id);
  490. }
  491. // 加入可碰杠玩家集合
  492. if (!self.CanPgIds.Contains(otherPlayer.Id)) {
  493. self.CanPgIds.Add(otherPlayer.Id);
  494. }
  495. }
  496. // 设置玩家动作
  497. otherPlayer.Act = act;
  498. }
  499. if (self.CanHuIds.Count <= 0)
  500. {
  501. self.GangPlayer = null;
  502. }
  503. // 判断是否下家摸牌, 指定下家摸牌
  504. if (self.OperableList.Count == 0)
  505. {
  506. Player nextPlayer = null;
  507. foreach (int index in nextPos)
  508. {
  509. Player tempPlayer = self.Players[index];
  510. if (tempPlayer is { State: 2 } && tempPlayer.Id != player.Id)
  511. {
  512. nextPlayer = tempPlayer;
  513. break;
  514. }
  515. }
  516. // 有三家认输,就会有null
  517. if (nextPlayer == null)
  518. {
  519. self.GameOver(room);
  520. }
  521. else
  522. {
  523. // 设置摸牌玩家
  524. self.DrawCardPlayer = nextPlayer;
  525. // 设置操作玩家
  526. self.CurrentPlayer = nextPlayer;
  527. // 摸牌
  528. self.Flag = false;
  529. }
  530. }
  531. else
  532. {
  533. long id = 0;
  534. if (self.CanHuIds.Count > 0)
  535. {
  536. id = self.CanHuIds.First();
  537. } else if (self.CanPgIds.Count > 0)
  538. {
  539. id = self.CanPgIds.First();
  540. }
  541. else
  542. {
  543. id = self.OperableList.First();
  544. }
  545. Player tmpPlayer = room.GetPlayer(id);
  546. if (tmpPlayer != null)
  547. {
  548. // 设置操作玩家
  549. self.CurrentPlayer = tmpPlayer;
  550. // 摸牌
  551. self.Flag = true;
  552. }
  553. }
  554. // 重置时间和流程标记
  555. self.Time = 30;
  556. // 给出牌人广播
  557. RoomInfo info = ProtoHelper.RoomToProto(room, player, self.CurrentPlayer);
  558. info.Time = 30;
  559. MessageHelper.SendToClient(player, new G2C_HGHHDisCardPush(){info = info});
  560. // 给其他三家广播
  561. foreach (int index in nextPos)
  562. {
  563. Player otherPlayer = self.Players[index];
  564. if (otherPlayer == null)
  565. {
  566. continue;
  567. }
  568. // 广播其它三家
  569. info = ProtoHelper.RoomToProto(room, otherPlayer, self.CurrentPlayer);
  570. info.Time = 30;
  571. MessageHelper.SendToClient(otherPlayer, new G2C_HGHHDisCardPush(){info = info});
  572. }
  573. }
  574. }
  575. /// <summary>
  576. /// 玩家是否可以操作吃
  577. /// </summary>
  578. /// <param name="self"></param>
  579. /// <param name="player"></param>
  580. /// <returns></returns>
  581. private static bool IsCanChi(this HGHHComponent self, Player player)
  582. {
  583. if (self.ClickHuIds.Count > 0)
  584. {
  585. return false;
  586. }
  587. if (self.CanHuIds.Count > 1)
  588. {
  589. return false;
  590. }
  591. else
  592. {
  593. if (self.CanHuIds.Count == 1)
  594. {
  595. if (self.CanPgIds.Count > 0)
  596. {
  597. if (self.CanPgIds.Any(id => id > 0 && id != player.Id))
  598. {
  599. return false;
  600. }
  601. }
  602. return self.CanHuIds.Contains(player.Id);
  603. }
  604. else
  605. {
  606. if (self.CanPgIds.Count > 0)
  607. {
  608. return self.CanPgIds.Contains(player.Id);
  609. }
  610. else
  611. {
  612. return true;
  613. }
  614. }
  615. }
  616. }
  617. /// <summary>
  618. /// 玩家操作吃
  619. /// </summary>
  620. /// <param name="self"></param>
  621. /// <param name="room"></param>
  622. /// <param name="player"></param>
  623. /// <param name="card"></param>
  624. public static void Chi(this HGHHComponent self, Room room, Player player, int card)
  625. {
  626. if (!self.IsCanChi(player))
  627. {
  628. return;
  629. }
  630. if (self.DisCard >= HGHHConst.DONG_FENG)
  631. {
  632. return;
  633. }
  634. // 删除出牌人打出的牌堆
  635. self.DisCardPlayer.DisCards = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  636. // 吃牌玩家的手牌
  637. int[] temp = CardHelper.Add(player.RemainCards, self.DisCard);
  638. for (int i = 0; i < 3; i++)
  639. {
  640. temp = CardHelper.Remove(temp, card + i);
  641. }
  642. player.RemainCards = temp;
  643. // 刻子
  644. player.KeZi.Add(new Struct.Kezi((int)HGHHConst.KeziType.CHI, card, self.DisCardPlayer.Id));
  645. // 重置摸牌人
  646. self.DrawCardPlayer = player;
  647. self.CurrentPlayer = player;
  648. self.PengPlayer = player;
  649. self.OperableList.Clear();
  650. // 重置时间和流程标记
  651. self.Time = 0;
  652. self.Flag = true;
  653. Log.Info($"玩家吃牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 吃的牌:{card}");
  654. // 广播
  655. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  656. {
  657. RoomInfo info = ProtoHelper.RoomToProto(room, p, self.CurrentPlayer);
  658. info.Time = 30;
  659. MessageHelper.SendToClient(p, new G2C_HGHHOperationPush(){info = info, OpType = (int)OperationType.CHI});
  660. }
  661. }
  662. /// <summary>
  663. /// 玩家是否可以操作碰
  664. /// </summary>
  665. /// <param name="self"></param>
  666. /// <param name="player"></param>
  667. /// <returns></returns>
  668. private static bool IsCanPengGang(this HGHHComponent self, Player player)
  669. {
  670. if (!self.CanPgIds.Contains(player.Id))
  671. {
  672. return false;
  673. }
  674. if (self.ClickHuIds.Count > 0)
  675. {
  676. return false;
  677. }
  678. return self.CanHuIds.Count switch
  679. {
  680. > 1 => false,
  681. 1 => self.CanHuIds.Contains(player.Id),
  682. _ => true
  683. };
  684. }
  685. /// <summary>
  686. /// 玩家操作碰
  687. /// </summary>
  688. /// <param name="self"></param>
  689. /// <param name="room"></param>
  690. /// <param name="player"></param>
  691. public static void Peng(this HGHHComponent self, Room room, Player player)
  692. {
  693. if (!self.IsCanPengGang(player))
  694. {
  695. return;
  696. }
  697. int num = CardHelper.CountCardNum(player.RemainCards, self.DisCard);
  698. if (num < 2)
  699. {
  700. return;
  701. }
  702. // 删除出牌人打出的牌堆
  703. int[] temp = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  704. self.DisCardPlayer.DisCards = temp;
  705. // 碰牌玩家的手牌
  706. int[] remainCards = player.RemainCards;
  707. for (int i = 0; i < 2; i++)
  708. {
  709. remainCards = CardHelper.Remove(remainCards, self.DisCard);
  710. }
  711. player.RemainCards = remainCards;
  712. // 刻子
  713. player.KeZi.Add(new Struct.Kezi((int)HGHHConst.KeziType.PENG, self.DisCard, self.DisCardPlayer.Id));
  714. // 重置摸牌人
  715. self.DrawCardPlayer = player;
  716. self.CurrentPlayer = player;
  717. self.PengPlayer = player;
  718. self.OperableList.Clear();
  719. // 重置时间和摸牌标记
  720. self.Time = 0;
  721. self.Flag = true;
  722. Log.Info($"玩家碰牌... 玩家ID:{player.Id}, 位置:{player.Pos}, 手牌大小:{player.RemainCards.Length}, 手牌信息:{string.Join(", ", player.RemainCards)}, 碰的牌:{self.DisCard}");
  723. // 广播
  724. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  725. {
  726. RoomInfo info = ProtoHelper.RoomToProto(room, p, self.CurrentPlayer);
  727. info.Time = 30;
  728. MessageHelper.SendToClient(p, new G2C_HGHHOperationPush(){info = info, OpType = (int)OperationType.PENG});
  729. }
  730. }
  731. /// <summary>
  732. /// 摸牌杠(暗杠或者回杠)
  733. /// </summary>
  734. /// <param name="self"></param>
  735. /// <param name="room"></param>
  736. /// <param name="player"></param>
  737. /// <param name="card"></param>
  738. /// <returns></returns>
  739. private static int GangPaiDraw(this HGHHComponent self, Room room, Player player, int card)
  740. {
  741. int type = (int)HGHHConst.KeziType.DEFAULT;
  742. int[] remainCards = player.RemainCards;
  743. int num = CardHelper.CountCardNum(remainCards, card);
  744. if (num == 4)
  745. {
  746. for (int i = 0; i < 4; i++)
  747. {
  748. remainCards = CardHelper.Remove(remainCards, card);
  749. }
  750. player.RemainCards = remainCards;
  751. player.KeZi.Add(new Struct.Kezi((int)HGHHConst.KeziType.AN_GANG, card, player.Id));
  752. self.GangPlayer = player;
  753. self.GangPai = card;
  754. self.GangType = (int)HGHHConst.KeziType.AN_GANG;
  755. type = (int)HGHHConst.KeziType.AN_GANG;
  756. }
  757. else if (num == 1)
  758. {
  759. List<Struct.Kezi> templist = new List<Struct.Kezi>();
  760. if (player.KeZi.Count > 0)
  761. {
  762. templist = player.KeZi;
  763. }
  764. for (int i = 0; i < templist.Count; i++)
  765. {
  766. Struct.Kezi kz = templist[i];
  767. if (kz is not { Type: (int)HGHHConst.KeziType.PENG } || kz.Card != card)
  768. {
  769. continue;
  770. }
  771. player.KeZi.RemoveAt(i);
  772. remainCards = CardHelper.Remove(remainCards, card);
  773. player.KeZi.Add(new Struct.Kezi((int)HGHHConst.KeziType.HUI_GANG, card, player.Id));
  774. }
  775. player.RemainCards = remainCards;
  776. self.GangPlayer = player;
  777. self.GangPai = card;
  778. self.GangType = (int)HGHHConst.KeziType.HUI_GANG;
  779. type = (int)HGHHConst.KeziType.HUI_GANG;
  780. }
  781. return type;
  782. }
  783. /// <summary>
  784. /// 出牌杠(明杠)
  785. /// </summary>
  786. /// <param name="self"></param>
  787. /// <param name="room"></param>
  788. /// <param name="player"></param>
  789. /// <param name="card"></param>
  790. /// <returns></returns>
  791. private static int GangPaiDisCard(this HGHHComponent self, Room room, Player player, int card)
  792. {
  793. const int type = (int)HGHHConst.KeziType.DEFAULT;
  794. if (self.DisCardPlayer == null)
  795. {
  796. return type;
  797. }
  798. self.DisCardPlayer.DisCards = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  799. for (int i = 0; i < 3; i++)
  800. {
  801. player.RemainCards = CardHelper.Remove(player.RemainCards, card);
  802. }
  803. player.KeZi.Add(new Struct.Kezi((int)HGHHConst.KeziType.MING_GANG, card, self.DisCardPlayer.Id));
  804. self.GangPlayer = player;
  805. self.GangPai = card;
  806. self.GangType = (int)HGHHConst.KeziType.MING_GANG;
  807. return (int)HGHHConst.KeziType.MING_GANG;
  808. }
  809. /// <summary>
  810. /// 检测是否有抢杠胡
  811. /// </summary>
  812. /// <param name="self"></param>
  813. /// <param name="room"></param>
  814. /// <param name="player"></param>
  815. /// <param name="card">杠的牌</param>
  816. /// <returns></returns>
  817. private static bool CheckQangGangHu(this HGHHComponent self, Room room, Player player, int card)
  818. {
  819. bool flag = false;
  820. int[] nextPos = room.GetSorcPos(player.Pos);
  821. foreach (int index in nextPos) {
  822. Player p = self.Players[index];
  823. if (p == null)
  824. {
  825. continue;
  826. }
  827. // 玩家手牌
  828. int[] tmpCards = new int[p.RemainCards.Length];
  829. Array.Copy(p.RemainCards, 0, tmpCards, 0, p.RemainCards.Length);
  830. tmpCards = CardHelper.Add(tmpCards, card);
  831. Struct.HuRes result = HGHHHelper.CheckHu(tmpCards);
  832. if (result.Type == HGHHConst.HU_DEFAULT)
  833. {
  834. continue;
  835. }
  836. flag = true;
  837. if (!self.CanHuIds.Contains(p.Id)) {
  838. self.CanHuIds.Add(p.Id);
  839. }
  840. if (!self.OperableList.Contains(p.Id)) {
  841. self.OperableList.Add(p.Id);
  842. }
  843. if (!self.GangHuIds.Contains(p.Id)) {
  844. self.GangHuIds.Add(p.Id);
  845. }
  846. p.Act = new [] { 0, 0, 0, 1, 1 };
  847. }
  848. return flag;
  849. }
  850. /// <summary>
  851. /// 玩家操作杠
  852. /// </summary>
  853. /// <param name="self"></param>
  854. /// <param name="room"></param>
  855. /// <param name="player"></param>
  856. /// <param name="card"></param>
  857. public static void Gang(this HGHHComponent self, Room room, Player player, int card)
  858. {
  859. if (card <= 0 || !self.IsCanPengGang(player))
  860. {
  861. return;
  862. }
  863. int type = (int)HGHHConst.KeziType.DEFAULT;
  864. if (self.DrawCardPlayer.Id == player.Id)
  865. {
  866. type = self.GangPaiDraw(room, player, card);
  867. }
  868. else if (self.DrawCardPlayer.Id != player.Id && self.DisCard == card)
  869. {
  870. type = self.GangPaiDisCard(room, player, card);
  871. }
  872. if (type == (int)HGHHConst.KeziType.DEFAULT)
  873. {
  874. return;
  875. }
  876. self.OperableList.Clear();
  877. // 重置时间和摸牌标记
  878. self.Time = 0;
  879. self.Flag = false;
  880. // bool flag = true;
  881. if (self.GangType == (int)HGHHConst.KeziType.HUI_GANG)
  882. {
  883. if (self.CheckQangGangHu(room, self.GangPlayer, self.GangPai))
  884. {
  885. // 当前操作玩家
  886. Player tmpPlayer = room.GetPlayer(self.CanHuIds.First());
  887. if (tmpPlayer != null)
  888. {
  889. self.CurrentPlayer = tmpPlayer;
  890. }
  891. // flag = false;
  892. }
  893. }
  894. // 广播
  895. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  896. {
  897. RoomInfo info = ProtoHelper.RoomToProto(room, p, self.CurrentPlayer);
  898. info.Time = 30;
  899. MessageHelper.SendToClient(p, new G2C_HGHHOperationPush(){info = info, OpType = (int)OperationType.GANG});
  900. }
  901. }
  902. /// <summary>
  903. /// 自摸胡牌(手动/自动)
  904. /// </summary>
  905. /// <param name="self"></param>
  906. /// <param name="room"></param>
  907. /// <param name="player"></param>
  908. private static void ZimoHuPai(this HGHHComponent self, Room room, Player player)
  909. {
  910. if (self.OperableList.Contains(player.Id) && self.CanHuIds.Contains(player.Id))
  911. {
  912. Struct.HuRes res = HGHHHelper.CheckHuType(player.KeZi, player.RemainCards, self.DrawCard);
  913. if (res.Type != HGHHConst.HU_DEFAULT)
  914. {
  915. if (!self.ClickHuIds.Contains(player.Id))
  916. {
  917. self.ClickHuIds.Add(player.Id);
  918. }
  919. // self.ClickHuIds.Remove(player.Id);
  920. self.OperableList.Remove(player.Id);
  921. self.CanHuIds.Remove(player.Id);
  922. // 将摸的牌从手牌中移除
  923. player.RemainCards = CardHelper.Remove(player.RemainCards, self.DrawCard);
  924. self.HuResult = self.GangPlayer != null && player.Id == self.GangPlayer.Id ? (int)HGHHConst.Result.GANGKAI : (int)HGHHConst.Result.ZIMO;
  925. // 广播
  926. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  927. {
  928. RoomInfo info = ProtoHelper.RoomToProto(room, p, self.CurrentPlayer);
  929. info.Time = 30;
  930. MessageHelper.SendToClient(p, new G2C_HGHHOperationPush(){info = info, OpType = (int)OperationType.HU});
  931. }
  932. // 自摸胡牌结算
  933. self.SettlementHu();
  934. }
  935. self.GameOver(room);
  936. }
  937. else
  938. {
  939. Log.Error($"自摸胡牌出错, playerId={player.Id}");
  940. }
  941. }
  942. /// <summary>
  943. /// 点炮胡牌广播
  944. /// </summary>
  945. /// <param name="self"></param>
  946. /// <param name="room"></param>
  947. /// <param name="player"></param>
  948. /// <param name="huType">牌型</param>
  949. /// <param name="isGangHu">是否抢杠胡</param>
  950. private static void DianPaoHuPaiBroadcast(this HGHHComponent self, Room room, Player player, int huType, bool isGangHu)
  951. {
  952. if (isGangHu)
  953. {
  954. // 移除抢杠胡集合玩家
  955. self.GangHuIds.Remove(player.Id);
  956. // 获取杠玩家
  957. Player gangPlayer = self.GangPlayer;
  958. if (gangPlayer == null)
  959. {
  960. return;
  961. }
  962. foreach (Struct.Kezi kezi in gangPlayer.KeZi.Where(kezi => kezi.Card == self.GangPai && kezi.Type == (int)HGHHConst.KeziType.HUI_GANG))
  963. {
  964. kezi.Type = (int)HGHHConst.KeziType.PENG;
  965. }
  966. self.HuResult = (int)HGHHConst.Result.QIANGGANG;
  967. }
  968. else
  969. {
  970. // 将玩家打出的牌从出牌集合中删除
  971. if (self.DisCardPlayer.DisCards.Contains(self.DisCard))
  972. {
  973. self.DisCardPlayer.DisCards = CardHelper.Remove(self.DisCardPlayer.DisCards, self.DisCard);
  974. }
  975. self.HuResult = (int)HGHHConst.Result.DIANPAO;
  976. }
  977. player.HuCards.Add(self.GangPai);
  978. // 广播
  979. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  980. {
  981. RoomInfo info = ProtoHelper.RoomToProto(room, p, self.CurrentPlayer);
  982. info.Time = 30;
  983. MessageHelper.SendToClient(p, new G2C_HGHHOperationPush(){info = info, OpType = (int)OperationType.HU});
  984. }
  985. }
  986. /// <summary>
  987. /// 点炮胡牌(手动/自动)
  988. /// </summary>
  989. /// <param name="self"></param>
  990. /// <param name="room"></param>
  991. /// <param name="player"></param>
  992. private static void DianPaoHuPai(this HGHHComponent self, Room room, Player player)
  993. {
  994. int card = 0;
  995. bool isGangHU = false;
  996. if (self.GangHuIds.Contains(player.Id))
  997. {
  998. card = self.GangPai;
  999. isGangHU = true;
  1000. }
  1001. else
  1002. {
  1003. card = self.DisCard;
  1004. }
  1005. if (!self.CanHuIds.Contains(player.Id))
  1006. {
  1007. return;
  1008. }
  1009. int[] userCards = player.RemainCards;
  1010. int[] tmpCards = new int[userCards.Length];
  1011. Array.Copy(userCards, 0, tmpCards, 0, userCards.Length);
  1012. tmpCards = CardHelper.Add(tmpCards, card);
  1013. Struct.HuRes res = HGHHHelper.CheckHuType(player.KeZi, tmpCards, card);
  1014. if (res.Type == HGHHConst.HU_DEFAULT)
  1015. {
  1016. return;
  1017. }
  1018. if (!self.ClickHuIds.Contains(player.Id))
  1019. {
  1020. self.ClickHuIds.Add(player.Id);
  1021. }
  1022. self.CanPgIds.Clear();
  1023. self.OperableList.Remove(player.Id);
  1024. self.CanHuIds.Remove(player.Id);
  1025. // 广播
  1026. self.DianPaoHuPaiBroadcast(room, player, res.Type, isGangHU);
  1027. if (self.CanHuIds.Count == 0)
  1028. {
  1029. // 点炮胡结算
  1030. self.SettlementHu();
  1031. self.GameOver(room);
  1032. }
  1033. else
  1034. {
  1035. long id = self.CanHuIds.First();
  1036. Player tmpPlayer = room.GetPlayer(id);
  1037. if (tmpPlayer != null)
  1038. {
  1039. self.CurrentPlayer = tmpPlayer;
  1040. }
  1041. self.Time = 0;
  1042. }
  1043. }
  1044. /// <summary>
  1045. /// 玩家操作胡
  1046. /// </summary>
  1047. /// <param name="self"></param>
  1048. /// <param name="room"></param>
  1049. /// <param name="player"></param>
  1050. public static void Hu(this HGHHComponent self, Room room, Player player)
  1051. {
  1052. int r = player.RemainCards.Length % 3;
  1053. switch (r)
  1054. {
  1055. case 2:
  1056. self.ZimoHuPai(room, player);
  1057. break;
  1058. case 1:
  1059. self.DianPaoHuPai(room, player);
  1060. break;
  1061. }
  1062. }
  1063. /// <summary>
  1064. /// 玩家操作过
  1065. /// </summary>
  1066. /// <param name="self"></param>
  1067. /// <param name="room"></param>
  1068. /// <param name="player"></param>
  1069. public static void Guo(this HGHHComponent self, Room room, Player player)
  1070. {
  1071. // 玩家必须在可操作列表里
  1072. if (!self.OperableList.Contains(player.Id))
  1073. {
  1074. return;
  1075. }
  1076. // 从碰杠集合中移除
  1077. self.CanPgIds.Remove(player.Id);
  1078. // 从可操作玩家集合中移除
  1079. self.OperableList.Remove(player.Id);
  1080. player.Act = new int[5];
  1081. player.ActInfo.Clear();
  1082. // 不是摸牌人,改time
  1083. if (self.DrawCardPlayer.Id != player.Id)
  1084. {
  1085. if (self.CanHuIds.Contains(player.Id))
  1086. {
  1087. self.CanHuIds.Remove(player.Id);
  1088. }
  1089. if (self.GangHuIds.Count > 0)
  1090. {
  1091. self.GangHuIds.Remove(player.Id);
  1092. if (self.GangHuIds.Count > 0)
  1093. {
  1094. // 设置操作动作玩家
  1095. if (self.CurrentPlayer.Id == player.Id)
  1096. {
  1097. self.CurrentPlayer = room.GetPlayer(self.GangHuIds.First());
  1098. self.Time = 0;
  1099. // 广播过
  1100. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  1101. {
  1102. RoomInfo info = ProtoHelper.RoomToProto(room, p, self.CurrentPlayer);
  1103. info.Time = 30;
  1104. MessageHelper.SendToClient(p, new G2C_HGHHOperationPush(){info = info, OpType = (int)OperationType.GUO});
  1105. }
  1106. }
  1107. }
  1108. else
  1109. {
  1110. if (self.ClickHuIds.Count == 0)
  1111. {
  1112. self.DrawCardPlayer = self.GangPlayer;
  1113. self.Flag = false;
  1114. }
  1115. else
  1116. {
  1117. // 没有其他人可以胡,且有人点击胡 结算胡 游戏结束
  1118. self.SettlementHu();
  1119. self.GameOver(room);
  1120. }
  1121. }
  1122. }
  1123. else
  1124. {
  1125. if (self.OperableList.Count == 0)
  1126. {
  1127. if (self.ClickHuIds.Count > 0)
  1128. {
  1129. // 没有其他人有胡 且有人点击胡 结算胡 游戏结束
  1130. self.SettlementHu();
  1131. self.GameOver(room);
  1132. }
  1133. else
  1134. {
  1135. self.DrawCardPlayer = self.GetNextPlayer(self.DisCardPlayer.Pos);
  1136. self.Flag = false;
  1137. }
  1138. }
  1139. else
  1140. {
  1141. if (self.CurrentPlayer.Id == player.Id)
  1142. {
  1143. if (self.CanHuIds.Count > 0)
  1144. {
  1145. self.CurrentPlayer = self.Players[room.GetPlayer(self.CanHuIds.First()).Pos];
  1146. }
  1147. else if (self.CanPgIds.Count > 0)
  1148. {
  1149. self.CurrentPlayer = self.Players[room.GetPlayer(self.CanPgIds.First()).Pos];
  1150. }
  1151. else
  1152. {
  1153. self.CurrentPlayer = self.Players[room.GetPlayer(self.OperableList.First()).Pos];
  1154. }
  1155. self.Time = 0;
  1156. // 广播过
  1157. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  1158. {
  1159. RoomInfo info = ProtoHelper.RoomToProto(room, p, self.CurrentPlayer);
  1160. info.Time = 30;
  1161. MessageHelper.SendToClient(p, new G2C_HGHHOperationPush(){info = info, OpType = (int)OperationType.GUO});
  1162. }
  1163. }
  1164. }
  1165. if (self.CanHuIds.Count == 0)
  1166. {
  1167. self.GangPlayer = null;
  1168. }
  1169. }
  1170. }
  1171. else
  1172. {
  1173. if (self.CanHuIds.Contains(player.Id))
  1174. {
  1175. // 从胡牌集合中移除
  1176. self.CanHuIds.Remove(player.Id);
  1177. }
  1178. }
  1179. }
  1180. /// <summary>
  1181. /// 获取下个玩家
  1182. /// </summary>
  1183. /// <param name="self"></param>
  1184. /// <param name="curPos">当前玩家位置</param>
  1185. /// <returns></returns>
  1186. private static Player GetNextPlayer(this HGHHComponent self, int curPos)
  1187. {
  1188. return self.Players[(curPos + 1) % self.GetParent<Room>().MaxNum];
  1189. }
  1190. /// <summary>
  1191. /// 清空玩家可操作动作
  1192. /// </summary>
  1193. /// <param name="self"></param>
  1194. private static void CleanUserAct(this HGHHComponent self) {
  1195. foreach (Player player in self.Players)
  1196. {
  1197. if (player != null)
  1198. {
  1199. player.Act = new int[5];
  1200. }
  1201. }
  1202. }
  1203. /// <summary>
  1204. /// 胡牌结算
  1205. /// </summary>
  1206. /// <param name="self"></param>
  1207. private static void SettlementHu(this HGHHComponent self)
  1208. {
  1209. Room room = self.GetParent<Room>();
  1210. if (room == null)
  1211. {
  1212. return;
  1213. }
  1214. G2C_HGHHSettlementPush push = new ();
  1215. push.CurRound = self.CurrentRound;
  1216. push.PlayerInfo = new List<PlayerInfo>();
  1217. switch (self.HuResult)
  1218. {
  1219. case (int)HGHHConst.Result.DIANPAO:
  1220. if (self.ClickHuIds.Count > 0)
  1221. {
  1222. long loseFen = 0;
  1223. foreach (long winId in self.ClickHuIds)
  1224. {
  1225. Player winPlayer = room.GetPlayer(winId);
  1226. if (winPlayer == null)
  1227. {
  1228. continue;
  1229. }
  1230. int multiple = HGHHConst.ScoreType[winPlayer.HuType];
  1231. if (self.ZhuangPos == self.DisCardPlayer.Pos && self.DisCardPlayer.DiscardCount == 1)
  1232. {
  1233. multiple *= 64;
  1234. }
  1235. int n = 0;
  1236. long fen = 0;
  1237. if (multiple * (long) Math.Pow(2, n) < 64) {
  1238. fen = self.Difen * multiple * (long) Math.Pow(2, n);
  1239. } else {
  1240. fen = self.Difen * 64;
  1241. }
  1242. // todo 结算赢家
  1243. PlayerInfo playerInfo = self.SettlementWin(winPlayer, fen);
  1244. if (playerInfo != null)
  1245. {
  1246. push.PlayerInfo.Add(playerInfo);
  1247. }
  1248. // 输家分
  1249. loseFen += fen;
  1250. }
  1251. // todo 结算输家
  1252. PlayerInfo loseInfo = self.SettlementLose(self.DisCardPlayer, loseFen);
  1253. if (loseInfo != null)
  1254. {
  1255. push.PlayerInfo.Add(loseInfo);
  1256. }
  1257. }
  1258. break;
  1259. case (int)HGHHConst.Result.ZIMO:
  1260. if (self.ClickHuIds.Count > 0)
  1261. // if (self.ClickHuIds.Count > 0 && self.OperableList.Count <= 0)
  1262. {
  1263. Player winPlayer = room.GetAllPlayers()[self.ClickHuIds.First()];
  1264. if (winPlayer != null)
  1265. {
  1266. long winFen = 0;
  1267. int multiple = 0;
  1268. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null && player.Id != winPlayer.Id))
  1269. {
  1270. multiple = HGHHConst.ScoreType[winPlayer.HuType];
  1271. if (winPlayer.DiscardCount > 0)
  1272. {
  1273. continue;
  1274. }
  1275. if (winPlayer.Pos == self.ZhuangPos)
  1276. {
  1277. Log.Debug($"天湖结算...");
  1278. }
  1279. else
  1280. {
  1281. Log.Debug($"地胡结算...");
  1282. }
  1283. multiple *= 64;
  1284. int n = 1;
  1285. long fen = 0;
  1286. if (multiple * (long) Math.Pow(2, n) < 64) {
  1287. fen = self.Difen * multiple * (long) Math.Pow(2, n);
  1288. } else {
  1289. fen = self.Difen * 64;
  1290. }
  1291. // todo 结算输家
  1292. PlayerInfo loseInfo = self.SettlementLose(player, fen);
  1293. if (loseInfo != null)
  1294. {
  1295. push.PlayerInfo.Add(loseInfo);
  1296. }
  1297. winFen += fen;
  1298. }
  1299. // todo 结算赢家
  1300. PlayerInfo playerInfo = self.SettlementWin(winPlayer, winFen);
  1301. if (playerInfo != null)
  1302. {
  1303. push.PlayerInfo.Add(playerInfo);
  1304. }
  1305. }
  1306. }
  1307. break;
  1308. case (int)HGHHConst.Result.QIANGGANG:
  1309. // 牌型<8倍,按8倍计算; >=8倍,按实际牌型计算
  1310. if (self.ClickHuIds.Count > 0 && self.OperableList.Count <= 0)
  1311. {
  1312. long loseFen = 0;
  1313. int multiple = 0;
  1314. foreach (long winId in self.ClickHuIds)
  1315. {
  1316. Player winPlayer = room.GetPlayer(winId);
  1317. if (winPlayer == null)
  1318. {
  1319. continue;
  1320. }
  1321. multiple = HGHHConst.ScoreType[winPlayer.HuType];
  1322. if (multiple < 8) {
  1323. multiple = 8;
  1324. }
  1325. int n = 0;
  1326. long fen = 0;
  1327. if (multiple * (long) Math.Pow(2, n) < 64) {
  1328. fen = self.Difen * multiple * (long) Math.Pow(2, n);
  1329. } else {
  1330. fen = self.Difen * 64;
  1331. }
  1332. // todo 结算赢家
  1333. PlayerInfo playerInfo = self.SettlementWin(winPlayer, fen);
  1334. if (playerInfo != null)
  1335. {
  1336. push.PlayerInfo.Add(playerInfo);
  1337. }
  1338. loseFen += fen;
  1339. }
  1340. // todo 结算输家
  1341. PlayerInfo loseInfo = self.SettlementLose(self.GangPlayer, loseFen);
  1342. if (loseInfo != null)
  1343. {
  1344. push.PlayerInfo.Add(loseInfo);
  1345. }
  1346. }
  1347. break;
  1348. case (int)HGHHConst.Result.GANGKAI:
  1349. // 牌型<8倍,按8倍计算; >=8倍,按实际牌型计算
  1350. if (self.ClickHuIds.Count > 0 && self.OperableList.Count <= 0)
  1351. {
  1352. Player winPlayer = room.GetAllPlayers()[self.ClickHuIds.First()];
  1353. if (winPlayer != null)
  1354. {
  1355. long winFen = 0;
  1356. int multiple = 0;
  1357. foreach (Player player in room.GetAllPlayers().Values.Where(player => player != null && player.Id != winPlayer.Id))
  1358. {
  1359. multiple = HGHHConst.ScoreType[winPlayer.HuType];
  1360. if (multiple < 8) {
  1361. multiple = 8;
  1362. }
  1363. int n = 1;
  1364. long fen = 0;
  1365. if (multiple * (long) Math.Pow(2, n) < 64) {
  1366. fen = self.Difen * multiple * (long) Math.Pow(2, n);
  1367. } else {
  1368. fen = self.Difen * 64;
  1369. }
  1370. // todo 结算输家
  1371. PlayerInfo loseInfo = self.SettlementLose(player, fen);
  1372. if (loseInfo != null)
  1373. {
  1374. push.PlayerInfo.Add(loseInfo);
  1375. }
  1376. winFen += fen;
  1377. }
  1378. // todo 结算赢家
  1379. PlayerInfo playerInfo = self.SettlementWin(winPlayer, winFen);
  1380. if (playerInfo != null)
  1381. {
  1382. push.PlayerInfo.Add(playerInfo);
  1383. }
  1384. }
  1385. }
  1386. break;
  1387. }
  1388. // 广播
  1389. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
  1390. {
  1391. MessageHelper.SendToClient(p, push);
  1392. }
  1393. }
  1394. /// <summary>
  1395. /// 结算赢家主方法
  1396. /// </summary>
  1397. /// <param name="self"></param>
  1398. /// <param name="winPlayer">赢家</param>
  1399. /// <param name="winFen">赢的分数</param>
  1400. private static PlayerInfo SettlementWin(this HGHHComponent self, Player winPlayer, long winFen) {
  1401. Room room = self.GetParent<Room>();
  1402. if (room == null)
  1403. {
  1404. return null;
  1405. }
  1406. winPlayer.Diamond += winFen;
  1407. return ProtoHelper.PlayerInfoToProto(room, winPlayer, true);
  1408. }
  1409. /// <summary>
  1410. /// 结算输家主方法
  1411. /// </summary>
  1412. /// <param name="self"></param>
  1413. /// <param name="losePlayer">输家</param>
  1414. /// <param name="loseFen">输的分数</param>
  1415. /// <returns></returns>
  1416. private static PlayerInfo SettlementLose(this HGHHComponent self, Player losePlayer, long loseFen)
  1417. {
  1418. Room room = self.GetParent<Room>();
  1419. if (room == null)
  1420. {
  1421. return null;
  1422. }
  1423. if (losePlayer.Diamond < loseFen)
  1424. {
  1425. losePlayer.Diamond = 0L;
  1426. } else {
  1427. losePlayer.Diamond -= loseFen;
  1428. }
  1429. // 不够底分2倍,强制认输
  1430. if (losePlayer.Diamond <= self.Difen * 2)
  1431. {
  1432. Log.Debug($"输家不够底分2倍,强制认输...");
  1433. }
  1434. return ProtoHelper.PlayerInfoToProto(room, losePlayer, true);
  1435. }
  1436. /// <summary>
  1437. /// 结束
  1438. /// </summary>
  1439. /// <param name="self"></param>
  1440. /// <param name="room"></param>
  1441. private static void GameOver(this HGHHComponent self, Room room)
  1442. {
  1443. self.State = 3;
  1444. self.Time = 60;
  1445. self.Flag = false;
  1446. foreach (Player player in self.Players)
  1447. {
  1448. if (player != null)
  1449. {
  1450. player.State = 3;
  1451. }
  1452. }
  1453. Log.Info($"牌局结束...roomId={room.RoomId}");
  1454. }
  1455. /// <summary>
  1456. /// 清理桌子信息
  1457. /// </summary>
  1458. /// <param name="self"></param>
  1459. private static void ClearRoom(this HGHHComponent self)
  1460. {
  1461. foreach (Player player in self.Players)
  1462. {
  1463. player.State = 0;
  1464. player.RemainCards = Array.Empty<int>();
  1465. player.DisCards = Array.Empty<int>();
  1466. player.KeZi.Clear();
  1467. player.Act = new int[5];;
  1468. player.ActInfo.Clear();
  1469. player.HuCards.Clear();
  1470. }
  1471. Log.Debug($"清理玩家数据...");
  1472. self.ZhuangPos = 0;
  1473. self.GangType = -1;
  1474. self.AdmitDefeatList.Clear();
  1475. self.CanPgIds.Clear();
  1476. self.GangHuIds.Clear();
  1477. self.CanHuIds.Clear();
  1478. self.OperableList.Clear();
  1479. self.ClickHuIds.Clear();
  1480. self.HuResult = -1;
  1481. self.CardList.Clear();
  1482. self.UpdateTime = 0;
  1483. Log.Debug($"清理桌子数据...");
  1484. // 初始化牌库
  1485. self.InitCardList();
  1486. }
  1487. }
  1488. }