Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 71.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Conquer_Online_Server.ServerBase;
  6. using Conquer_Online_Server.Network;
  7. using Conquer_Online_Server.Network.GamePackets;
  8.  
  9. namespace Conquer_Online_Server.Game
  10. {
  11. public class HandlePoker
  12. {
  13. public HandlePoker(byte[] packet, Client.GameClient client)
  14. {
  15. if (packet == null)
  16. return;
  17. if (client == null)
  18. return;
  19. ushort Length = BitConverter.ToUInt16(packet, 0);
  20. ushort ID = BitConverter.ToUInt16(packet, 2);
  21. ushort ID2 = BitConverter.ToUInt16(packet, 4);
  22. switch (ID)
  23. {
  24. #region 2171 Join table
  25. case 2171:
  26. {
  27. Game.Entity MyChar = client.Entity;
  28. uint TableId = BitConverter.ToUInt32(packet, 8);
  29. uint PlayerId = BitConverter.ToUInt32(packet, 12);
  30. byte Seat = packet[16]; byte Typ = packet[4];
  31. switch (Typ)
  32. {
  33. case 0://join table
  34. {
  35. if (Kernel.PokerTables.ContainsKey(TableId))
  36. {
  37. Game.PokerTable T = Kernel.PokerTables[TableId];
  38. if (T.Players.ContainsKey(client.Entity.UID)) T.RemovePlayer(MyChar.UID);
  39. if (T.FreeSeat(Seat))
  40. {
  41. T.AddNewPlayer(client.Entity, Seat, true);
  42. MyChar.PokerTable = T.Id;
  43. byte CurrentState = 1;
  44. if (!T.Players.ContainsKey(MyChar.UID))
  45. if (T.Watchers.ContainsKey(MyChar.UID)) CurrentState = T.Watchers[MyChar.UID].CurrentState;
  46. client.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, CurrentState, T.Nomber));
  47. foreach (Game.PokerPlayer P in T.Players.Values)
  48. {
  49. if (P.PlayerId == MyChar.UID) continue;
  50. client.Send(Game.PokerPackets.PokerPlayerInfo(P.Seat, P.PlayerId, P.CurrentState, T.Nomber));
  51. P.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, CurrentState, T.Nomber));
  52. }
  53. foreach (Game.PokerPlayer P in T.Watchers.Values)
  54. {
  55. if (P.PlayerId == MyChar.UID) continue;
  56. client.Send(Game.PokerPackets.PokerPlayerInfo(P.Seat, P.PlayerId, P.CurrentState, T.Nomber));
  57. P.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, CurrentState, T.Nomber));
  58. }
  59. if (T.Players.Count == 2 && T.Pot == 0) T.SetNewRound(10);
  60. }
  61. }
  62. break;
  63. }
  64. case 4://watch
  65. {
  66. if (Kernel.PokerTables.ContainsKey(TableId))
  67. {
  68. Game.PokerTable T = Kernel.PokerTables[TableId];
  69. if (T.Players.ContainsKey(MyChar.UID) || T.Watchers.ContainsKey(MyChar.UID)) return;
  70. if (T.FreeSeat(Seat))
  71. {
  72. T.AddNewPlayer(MyChar, Seat, false);
  73. T.Watchers[MyChar.UID].CurrentState = 2;
  74. MyChar.PokerTable = T.Id;
  75. client.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, 2, T.Nomber));
  76. foreach (Game.PokerPlayer P in T.Players.Values)
  77. {
  78. if (P.PlayerId == MyChar.UID) continue;
  79. client.Send(Game.PokerPackets.PokerPlayerInfo(P.Seat, P.PlayerId, P.CurrentState, T.Nomber));
  80. P.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, 2, T.Nomber));
  81. }
  82. foreach (Game.PokerPlayer P in T.Watchers.Values)
  83. {
  84. if (P.PlayerId == MyChar.UID) continue;
  85. client.Send(Game.PokerPackets.PokerPlayerInfo(P.Seat, P.PlayerId, P.CurrentState, T.Nomber));
  86. P.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, 2, T.Nomber));
  87. }
  88. }
  89. }
  90. break;
  91. }
  92. default:
  93. {
  94. break;
  95. }
  96. }
  97. break;
  98. }
  99. #endregion 2171 Join table
  100. #region 2093 Player move
  101. case 2093:
  102. {
  103. byte Typ = packet[6];
  104. Game.Entity MyChar = client.Entity;
  105. Game.PokerTable T = new Game.PokerTable();
  106. if (Kernel.PokerTables.ContainsKey(MyChar.PokerTable))
  107. T = MyChar.MyPokerTable;
  108. else return;
  109. switch (Typ)
  110. {
  111.  
  112. default:
  113. {
  114. T.NewPlayerMove(packet, MyChar.UID);
  115. break;
  116. }
  117. }
  118. break;
  119. }
  120. #endregion 2093 Player move
  121. #region 2096 Leave table
  122. case 2096:
  123. {
  124. Game.Entity MyChar = client.Entity;
  125. if (MyChar.MyPokerTable == null) return;
  126. if (MyChar.MyPokerTable.Players.ContainsKey(MyChar.UID) && MyChar.MyPokerTable.Pot > 1)
  127. {
  128. byte[] P = new byte[10];
  129. P[6] = 4; P[9] = 200;
  130. MyChar.MyPokerTable.NewPlayerMove(P, MyChar.UID);
  131. }
  132. else
  133. MyChar.MyPokerTable.RemovePlayer(MyChar.UID);
  134. client.Send(packet);
  135. break;
  136. }
  137. #endregion 2096 Leave table
  138. #region 2090
  139. case 2090:
  140. {
  141. byte Typ = packet[6];
  142. Game.Entity MyChar = client.Entity;
  143. switch (Typ)
  144. {
  145. case 1:
  146. {
  147. if (MyChar.PokerTable > 0)
  148. {
  149. if (Kernel.PokerTables.ContainsKey(MyChar.PokerTable))
  150. {
  151. byte Seat = packet[8];
  152. Game.PokerTable T = MyChar.MyPokerTable;
  153. if (T.Players.ContainsKey(client.Entity.UID)) return;
  154. if (T.FreeSeat(Seat))
  155. {
  156. T.AddNewPlayer(MyChar, Seat, true);
  157. byte CurrentState = 1;
  158. if (!T.Players.ContainsKey(MyChar.UID))
  159. if (T.Watchers.ContainsKey(MyChar.UID))
  160. {
  161. CurrentState = T.Watchers[MyChar.UID].CurrentState;
  162. T.Watchers.Remove(MyChar.UID);
  163. }
  164. client.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, CurrentState, T.Nomber));
  165. foreach (Game.PokerPlayer P in T.Players.Values)
  166. {
  167. if (P.PlayerId == MyChar.UID) continue;
  168. client.Send(Game.PokerPackets.PokerPlayerInfo(P.Seat, P.PlayerId, P.CurrentState, T.Nomber));
  169. P.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, CurrentState, T.Nomber));
  170. }
  171. foreach (Game.PokerPlayer P in T.Watchers.Values)
  172. {
  173. if (P.PlayerId == MyChar.UID) continue;
  174. client.Send(Game.PokerPackets.PokerPlayerInfo(P.Seat, P.PlayerId, P.CurrentState, T.Nomber));
  175. P.Send(Game.PokerPackets.PokerPlayerInfo(Seat, MyChar.UID, CurrentState, T.Nomber));
  176. }
  177. if (T.Players.Count == 2 && T.Pot == 0) T.SetNewRound(30);
  178. }
  179. }
  180. }
  181. break;
  182. }
  183. default:
  184. {
  185.  
  186. string D = "";
  187. for (int x = 0; x < packet.Length; x++)
  188. D += packet[x].ToString("X") + " ";
  189. client.Send(new Message("Unknown type: " + ID + " with length " + packet.Length + " :- " + D, System.Drawing.Color.CadetBlue, Message.Talk));
  190. break;
  191. }
  192. }
  193. break;
  194. }
  195. #endregion 2090
  196. }
  197.  
  198. }
  199. }
  200. public class PokerPackets
  201. {
  202. //2172
  203. public static byte[] PokerTable(PokerTable Table)
  204. {
  205. PacketBuilder P = new PacketBuilder(2172, 52 + (Table.Players.Count * 6));
  206. P.Long(Table.Id);
  207. P.Long(0);
  208. P.Long(0);
  209. P.Short(Table.X);//Table X coord
  210. P.Short(Table.Y);//Table Y Coord
  211. P.Long(7217967);//Fixed nomber don't know what it is
  212. P.Short(0);
  213. P.Long(Table.Nomber);//table nomber
  214. P.Int((Table.FreeBet ? 1 : 0));//Limited=0 Unlimited=1
  215. P.Int(0);
  216. P.Short(0);
  217. P.Long(Table.BetType);//table bet type 1=Silver 0=CPs
  218. P.Long(Table.MinLimit);
  219. P.Int((byte)Table.State);//table state 0=unopened 1=Pocket 2=flop 3=turn 4=river 5=showdown
  220. P.ULong(Table.Pot);//Pot
  221. P.Int(Table.Players.Count);//Players Count
  222. foreach (PokerPlayer Player in Table.Players.Values)
  223. {
  224. if (Player.MyChar == null) { P.Move(6); continue; }
  225. P.Long(Player.PlayerId);
  226. P.Int(Player.Seat);
  227. P.Int(Player.Connected ? 1 : 0);
  228. }
  229. return P.getFinal();
  230. }
  231. //2171
  232. public static byte[] PokerJoinAction(uint TableId, uint PlayerId, byte Req, byte Seat)
  233. {
  234. PacketBuilder P = new PacketBuilder(2171, 20);
  235. P.Long(Req);
  236. P.Long(TableId);
  237. P.Long(PlayerId);
  238. P.Long(Seat);
  239. return P.getFinal();
  240. }
  241. //2090
  242. public static byte[] PokerPlayerInfo(byte Seat, uint PlayerId, byte State, byte TableNo)
  243. {
  244.  
  245. PacketBuilder P = new PacketBuilder(2090, 20);
  246. P.Short(1);
  247. P.Short(State);
  248. P.Long(Seat);
  249. P.Long(TableNo);
  250. P.Long(PlayerId);
  251. return P.getFinal();
  252. }
  253. //2091
  254. public static byte[] PokerCards1Card(PokerTable T)
  255. {
  256. PacketBuilder P = new PacketBuilder(2091, 44 + T.Players.Count * 8);
  257. P.Short(7);
  258. P.Short(4);
  259. P.Long(0);
  260. P.Long(0);
  261. P.Long(0);
  262. P.Long(0);
  263. P.Long(0);
  264. P.Short(0);
  265. P.Short(T.Players.Count);
  266. P.Long(T._StartingPlayer);
  267. P.Long(0);
  268. P.Long(0);
  269. foreach (PokerPlayer Pl in T.Players.Values)
  270. {
  271. foreach (PokerCard C in Pl.MyCards.Values)
  272. {
  273. P.Short(C.Val);
  274. P.Short((byte)C.Typ);
  275. }
  276. P.Long(Pl.PlayerId);
  277. }
  278. return P.getFinal();
  279. }
  280. public static byte[] PokerCards2Cards(PokerTable T, Dictionary<byte, PokerCard> Cards)
  281. {
  282. PacketBuilder P = new PacketBuilder(2091, 44 + T.Players.Count * 8);
  283. P.Long(0);
  284. P.Short(2);//packet type players cards
  285. foreach (PokerCard C in Cards.Values)
  286. {
  287. P.Short(C.Val);
  288. }
  289. P.Short(0);
  290. P.Long(0);
  291. foreach (PokerCard C in Cards.Values)
  292. {
  293. P.Short((byte)C.Typ);
  294. }
  295. P.Short(0);
  296. P.Long(0);
  297. P.Short(T.Players.Count);//Players count
  298. P.Long(T._StartingPlayer);
  299. P.Long(T.GetLastPlayer());
  300. P.Long(T.GetNextPlayer());//get next player
  301. foreach (PokerPlayer Pl in T.Players.Values)
  302. {
  303. P.Short(13);
  304. P.Short(4);
  305. P.Long(Pl.PlayerId);
  306. }
  307. return P.getFinal();
  308. }
  309. public static byte[] PokerTableCards(Dictionary<byte, PokerCard> Cards, PokerTable T, byte RoundStage)
  310. {
  311. PacketBuilder P = new PacketBuilder(2091, 44);
  312. P.Short(0);
  313. P.Short(RoundStage);//packet type table cards
  314.  
  315. P.Short(Cards.Count);
  316. foreach (PokerCard C in Cards.Values)
  317. {
  318. P.Short(C.Val);
  319. }
  320. for (byte x = 0; x < 5 - Cards.Count; x++)
  321. P.Short(0);
  322. foreach (PokerCard C in Cards.Values)
  323. {
  324. P.Short((byte)C.Typ);
  325. }
  326. for (byte x = 0; x < 5 - Cards.Count; x++)
  327. P.Short(0);
  328. P.Short(0);
  329. P.Long(T._StartingPlayer);
  330. P.Long(T.GetLastPlayer());
  331. P.Long(T.GetNextPlayer());//get next player
  332. return P.getFinal();
  333. }
  334. //2092
  335. public static byte[] PokerPlayerTurn(uint Id1, uint LastBet, uint RequierdBet, byte Type, byte TimeDown)
  336. {
  337. PacketBuilder P = new PacketBuilder(2092, 20);
  338. P.Short(TimeDown);//timer count
  339. P.Short(Type);//
  340. P.Long(LastBet);//last bet
  341. P.Long(RequierdBet);//requierd bet
  342. P.Long(Id1);
  343. return P.getFinal();
  344. }
  345. //2093
  346. public static byte[] PokerPlayerMove(uint PlayerId, byte Typ, uint Bet, uint RequierdBet)
  347. {
  348. PacketBuilder P = new PacketBuilder(2093, 20);
  349. P.Short(0);
  350. P.Short(Typ);//move type 32 =all in one
  351. P.Long(Bet);//player bet
  352. P.Long(RequierdBet);//requierd bet
  353. P.Long(PlayerId);
  354. return P.getFinal();
  355. }
  356. //2094
  357. public static byte[] PokerShowAllCards(PokerTable T)
  358. {
  359. PacketBuilder P = new PacketBuilder(2094, 8 + T.Players.Count * 12);
  360. P.Short(0);//
  361. P.Short(T.Players.Count);
  362. foreach (PokerPlayer Pl in T.Players.Values)
  363. {
  364. byte Card1Val = 0, Card1Type = 0, Card2Val = 0, Card2Type = 0; byte Co = 0;
  365. foreach (PokerCard C in Pl.MyCards.Values)
  366. {
  367. Co++;
  368. if (Co == 1)
  369. {
  370. Card1Val = C.Val; Card1Type = (byte)C.Typ;
  371. }
  372. else if (Co == 2)
  373. {
  374. Card2Val = C.Val; Card2Type = (byte)C.Typ;
  375. }
  376. }
  377. P.Short(Card1Val);
  378. P.Short(Card2Val);
  379. P.Short(Card1Type);
  380. P.Short(Card2Type);
  381. P.Long(Pl.PlayerId);
  382. }
  383. return P.getFinal();
  384. }
  385. //2095
  386. public static byte[] PokerRoundResult(PokerTable T, uint WinnerId, uint MoneyWins)
  387. {
  388. PacketBuilder P = new PacketBuilder(2095, 8 + T.Players.Count * 15);
  389. P.Short(20);//Timer
  390. P.Short(T.Players.Count);
  391. P.Int(0);
  392. P.Int(0);
  393. P.Int(0);
  394. P.Long(WinnerId);
  395. P.Long(MoneyWins);
  396. P.Long(0);
  397. foreach (PokerPlayer Pl in T.Players.Values)
  398. {
  399. try
  400. {
  401. byte ContinuePlaying = 0;
  402. if (Pl.PlayerId == WinnerId) continue;
  403. if (T.BetType == 0)
  404. if (Pl.MyChar.Money >= T.MinLimit * 10)
  405. ContinuePlaying = 0;
  406. else ContinuePlaying = 1;
  407. else if (T.BetType == 1)
  408. if (Pl.MyChar.ConquerPoints >= T.MinLimit * 10)
  409. ContinuePlaying = 0;
  410. else ContinuePlaying = 1;
  411. if (ContinuePlaying == 0)
  412. P.Int(0);
  413. else
  414. {
  415. P.Int(1);
  416. Pl.CurrentState = 2;
  417. }
  418. P.Int(255);
  419. P.Int(0);
  420. P.Long(Pl.PlayerId);
  421. P.Long(0xffffffff - Pl.Bet);
  422. P.Short(0xffff);
  423. P.Short(0xffff);
  424. }
  425. catch
  426. {
  427. P.Int(0);
  428. P.Int(255);
  429. P.Int(0);
  430. P.Long(Pl.PlayerId);
  431. P.Long(0xffffffff - Pl.Bet);
  432. P.Short(0xffff);
  433. P.Short(0xffff);
  434. }
  435. }
  436. return P.getFinal();
  437. }
  438. //2096
  439. public static byte[] PokerLeaveTable(uint Id1)
  440. {
  441. PacketBuilder P = new PacketBuilder(2096, 16);
  442.  
  443. P.Long(1);//
  444. P.Long(0);
  445. P.Long(Id1);
  446. return P.getFinal();
  447. }
  448. //2098
  449. public static byte[] PokerTableState(PokerTable T, byte CountDown)
  450. {
  451. PacketBuilder P = new PacketBuilder(2098, 45 + T.Players.Count * 11);
  452. uint Id1 = 0, Id2 = 0, Id3 = 0, Id4 = 0, Id5 = 0, Id6 = 0; byte Counter = 0;
  453. foreach (PokerPlayer Pl in T.Players.Values)
  454. {
  455. Counter++;
  456. if (Counter == 1) Id1 = Pl.PlayerId;
  457. else if (Counter == 2) Id2 = Pl.PlayerId;
  458. else if (Counter == 3) Id3 = Pl.PlayerId;
  459. else if (Counter == 4) Id4 = Pl.PlayerId;
  460. else if (Counter == 5) Id5 = Pl.PlayerId;
  461. else if (Counter == 6) Id6 = Pl.PlayerId;
  462. }
  463. P.Short(0);
  464. P.Int(T.Players.Count);//Players Count
  465. P.Int(CountDown);//Time Count Down
  466. P.Int(0);
  467. P.Short(0);
  468. P.Long(0);
  469. P.Long(0);
  470. P.Long(Id1);
  471. P.Long(Id2);
  472.  
  473. P.Long(Id3);
  474. P.Long(Id4);
  475. P.Long(Id5);
  476. P.Long(Id6);
  477. P.Short(0);
  478. foreach (PokerPlayer Pl in T.Players.Values)
  479. {
  480. P.Int(2);
  481. P.Int(4);
  482. P.Int(13);
  483. P.Int(0);
  484. P.Int(4);
  485. P.Int(13);
  486. P.Int(0);
  487.  
  488. P.Long(Pl.PlayerId);
  489. }
  490. return P.getFinal();
  491. }
  492. }
  493. public class PokerTable
  494. {
  495. public uint Id = 0;
  496. public byte Nomber = 0;
  497. public uint Map = 0;
  498. public ushort X = 0;
  499. public ushort Y = 0;
  500. public uint MinLimit = 100000;
  501. public bool FreeBet = true;
  502. public byte _State = (byte)PokerTableState.UnOpened;
  503. public byte StartSeat = 0;
  504. public byte CurrentSeat = 0;
  505. public uint _StartingPlayer = 0;
  506. public byte RoundStage = 0;
  507. public uint RoundMaxBet = 0;
  508. public System.Timers.Timer RoundTimer = new System.Timers.Timer();
  509. public System.Timers.Timer MoveTimer = new System.Timers.Timer();
  510. public PokerTableState State
  511. {
  512. get
  513. {
  514. return (PokerTableState)_State;
  515. }
  516. set
  517. {
  518. _State = (byte)value;
  519. }
  520. }
  521. public uint Pot = 0;
  522. public byte BetType = 1;//Silver=0 CPs=1;
  523. public Dictionary<uint, PokerPlayer> Players = new Dictionary<uint, PokerPlayer>(10);
  524. public Dictionary<byte, PokerPlayer> Seats
  525. {
  526. get
  527. {
  528. Dictionary<byte, PokerPlayer> Ses = new Dictionary<byte, PokerPlayer>(10);
  529. foreach (PokerPlayer P in Players.Values)
  530. {
  531. if (P.CurrentState == 1) Ses.Add(P.Seat, P);
  532. }
  533. return Ses;
  534. }
  535. }
  536. public Dictionary<uint, PokerPlayer> Watchers = new Dictionary<uint, PokerPlayer>(10);
  537. public Dictionary<byte, PokerCard> Cards = new Dictionary<byte, PokerCard>(52);
  538. public Dictionary<byte, PokerCard> TableCards = new Dictionary<byte, PokerCard>(5);
  539. public uint PlayerBySeat(byte S)
  540. {
  541. uint I = 0;
  542. foreach (PokerPlayer P in Players.Values)
  543. if (P.Seat == S) I = P.PlayerId;
  544. return I;
  545. }
  546. public uint GetStartingPlayer()
  547. {
  548. if (Players.Count < 2) return 0;
  549. uint I = 0;
  550. StartSeat++;
  551. if (StartSeat > 9) StartSeat = 0;
  552. for (byte x = StartSeat; x < 10; x++)
  553. {
  554. I = PlayerBySeat(x);
  555. if (I > 0)
  556. {
  557. StartSeat = x;
  558. break;
  559. }
  560. }
  561. if (I == 0)
  562. {
  563. for (byte x = 0; x < StartSeat; x++)
  564. {
  565. I = PlayerBySeat(x);
  566. if (I > 0)
  567. {
  568. StartSeat = x;
  569. break;
  570. }
  571. }
  572. }
  573. return I;
  574. }
  575. public PokerPlayer StartingPlayer
  576. {
  577. get
  578. {
  579. if (Players.ContainsKey(_StartingPlayer))
  580. return Players[_StartingPlayer];
  581. else return null;
  582. }
  583. }
  584. public uint GetNextPlayer()
  585. {
  586. if (Players.Count < 2) return 0;
  587. uint I = 0; byte StartSe = StartSeat;
  588. if (StartSe > 9) StartSe = 0;
  589. for (byte x = StartSe; x < 10; x++)
  590. {
  591. I = PlayerBySeat(x);
  592. if (I > 0)
  593. {
  594. if (I == GetLastPlayer()) continue;
  595. break;
  596. }
  597. }
  598. if (I == 0)
  599. {
  600. for (byte x = 0; x < StartSe; x++)
  601. {
  602. I = PlayerBySeat(x);
  603. if (I > 0)
  604. {
  605. if (I == GetLastPlayer()) continue;
  606. break;
  607. }
  608. }
  609. }
  610. return I;
  611. }
  612. public uint GetLastPlayer()
  613. {
  614. if (Players.Count < 2) return 0;
  615. uint I = 0; byte CurrentSeat = StartSeat;
  616. if (CurrentSeat < 1) CurrentSeat = 9;
  617. for (byte x = CurrentSeat; x > 0; x--)
  618. {
  619. I = PlayerBySeat(x);
  620. if (I > 0)
  621. {
  622. break;
  623. }
  624. }
  625. if (I == 0)
  626. {
  627. if (PlayerBySeat(0) > 0) return PlayerBySeat(0);
  628. for (byte x = 9; x > StartSeat; x--)
  629. {
  630. I = PlayerBySeat(x);
  631. if (I > 0)
  632. {
  633. break;
  634. }
  635. }
  636. }
  637. return I;
  638. }
  639. public void SetNewRound(byte CountDown)
  640. {
  641. Dictionary<uint, PokerPlayer> NotConnectedAnymore = new Dictionary<uint, PokerPlayer>();
  642. foreach (PokerPlayer P in Players.Values)
  643. {
  644. if (!P.Connected) NotConnectedAnymore.Add(P.PlayerId, P);
  645. else if (P.CurrentState == 2)
  646. if (!Watchers.ContainsKey(P.PlayerId))
  647. Watchers.Add(P.PlayerId, P);
  648. }
  649. foreach (PokerPlayer P in NotConnectedAnymore.Values)
  650. RemovePlayer(P.PlayerId);
  651. foreach (PokerPlayer P in Watchers.Values)
  652. {
  653. if (P.CurrentState == 3)
  654. {
  655. if (!Players.ContainsKey(P.PlayerId))
  656. Players.Add(P.PlayerId, P);
  657. }
  658. else if (P.CurrentState == 2)
  659. {
  660. if (Players.ContainsKey(P.PlayerId))
  661. Players.Remove(P.PlayerId);
  662. }
  663. }
  664. foreach (PokerPlayer P in Players.Values)
  665. {
  666. if (Watchers.ContainsKey(P.PlayerId))
  667. Watchers.Remove(P.PlayerId);
  668. P.MyCards.Clear();
  669. P.CurrentState = 1;
  670. P.RoundState = 0;
  671. P.HandVals = "";
  672. P.Bet = MinLimit;
  673. if (BetType == 0)
  674. P.MyChar.Money -= MinLimit;
  675. else if (BetType == 1)
  676. {
  677. if (P.MyChar.ConquerPoints >= MinLimit)
  678. P.MyChar.ConquerPoints -= MinLimit;
  679. else P.MyChar.ConquerPoints = 0;
  680. }
  681. }
  682. Cards.Clear();
  683. TableCards.Clear();
  684. RoundStage = 0;
  685. for (byte Y = 0; Y < 4; Y++)
  686. {
  687. PokerCardsType T = PokerCardsType.Hearts;
  688. if (Y == 1) T = PokerCardsType.Spades;
  689. else if (Y == 2) T = PokerCardsType.Clubs;
  690. else if (Y == 3) T = PokerCardsType.Diamonds;
  691. for (byte x = 0; x < 13; x++)
  692. {
  693. PokerCard Pc = new PokerCard();
  694. Pc.Id = (byte)(x + (13 * Y));
  695. Pc.Typ = T;
  696. Pc.Val = x;
  697. Cards.Add(Pc.Id, Pc);
  698. }
  699. }
  700. if (RoundTimer != null && RoundTimer.Enabled)
  701. {
  702. RoundTimer.Stop();
  703. RoundTimer.Dispose();
  704. RoundTimer = null;
  705. RoundTimer = new System.Timers.Timer();
  706. }
  707. else if (RoundTimer == null)
  708. {
  709. RoundTimer = new System.Timers.Timer();
  710. }
  711. RoundTimer.Interval = CountDown * 1000;
  712. RoundTimer.Elapsed += delegate
  713. {
  714. if (Players.Count > 1 && Pot < 1)
  715. {
  716. DrawCards(1, false);
  717. Pot = MinLimit;
  718. RoundMaxBet = MinLimit;
  719. _StartingPlayer = GetStartingPlayer();
  720. SendToAll(PokerPackets.PokerCards1Card(this));
  721. DrawCards(1, false);
  722. foreach (PokerPlayer Pl in Players.Values)
  723. {
  724. Pl.Bet = MinLimit;
  725. Pl.Send(PokerPackets.PokerCards2Cards(this, Pl.MyCards));
  726. }
  727. SendToAll(PokerPackets.PokerPlayerTurn(_StartingPlayer, MinLimit, MinLimit * 2, 22, 30));
  728. StartMoveTimer(30, _StartingPlayer);
  729. RoundTimer.Stop();
  730. RoundTimer.Dispose();
  731. RoundTimer = null;
  732. }
  733. };
  734. RoundTimer.Start();
  735. SendToAll(PokerPackets.PokerPlayerTurn(0, 0, 0, 0, CountDown));
  736. Data D = new Data(true);
  737. D.ID = 234;
  738. D.UID = Id;
  739. D.dwParam = (uint)(MinLimit * Players.Count);
  740. SendToAll(D.ToArray());
  741. }
  742. public void StartMoveTimer(byte CountDown, uint PlayerId)
  743. {
  744. if (MoveTimer != null && MoveTimer.Enabled)
  745. {
  746. MoveTimer.Stop();
  747. MoveTimer.Dispose();
  748. MoveTimer = null;
  749. }
  750. MoveTimer = new System.Timers.Timer();
  751. MoveTimer.Interval = CountDown * 1000;
  752. MoveTimer.Elapsed += delegate
  753. {
  754. byte[] FoldMe = new byte[10];
  755. FoldMe[6] = 4;
  756. NewPlayerMove(FoldMe, PlayerId);
  757. MoveTimer.Stop();
  758. MoveTimer.Dispose();
  759. };
  760. MoveTimer.Start();
  761. }
  762.  
  763. public bool FreeSeat(byte Seat)
  764. {
  765. bool Free = true;
  766. foreach (PokerPlayer P in Players.Values)
  767. {
  768. if (P.Seat == Seat) Free = false;
  769. }
  770. foreach (PokerPlayer P in Watchers.Values)
  771. {
  772. if (P.CurrentState == 3)
  773. if (P.Seat == Seat) Free = false;
  774. }
  775. return Free;
  776. }
  777. public void AddNewPlayer(Entity P, byte Seat, bool Player)
  778. {
  779. P.PokerTable = this.Id;
  780. PokerPlayer Pl = new PokerPlayer();
  781. Pl.PlayerId = P.UID;
  782. Pl.TableId = Id;
  783. Pl.Seat = Seat;
  784. if (Player)
  785. {
  786. if (Pot > 0) Pl.RoundState = 4;
  787. if (!Players.ContainsKey(Pl.PlayerId))
  788. Players.Add(Pl.PlayerId, Pl);
  789. ToLocal(PokerPackets.PokerTable(this));
  790. }
  791. else
  792. {
  793. if (!Watchers.ContainsKey(Pl.PlayerId))
  794. Watchers.Add(Pl.PlayerId, Pl);
  795. }
  796. }
  797. public void RemovePlayer(uint Id)
  798. {
  799. if (Players.ContainsKey(Id))
  800. {
  801. try
  802. {
  803. lock (Players)
  804. {
  805. foreach (PokerPlayer P in Players.Values)
  806. {
  807. P.Send(PokerPackets.PokerLeaveTable(Id));
  808. }
  809. }
  810. lock (Watchers)
  811. {
  812. foreach (PokerPlayer P in Watchers.Values)
  813. {
  814. P.Send(PokerPackets.PokerLeaveTable(Id));
  815. }
  816. }
  817. if (Players[Id].MyChar != null)
  818. Players[Id].MyChar.PokerTable = 0;
  819. Players.Remove(Id);
  820. }
  821. catch { }
  822. }
  823. else if (Watchers.ContainsKey(Id))
  824. {
  825. lock (Players)
  826. {
  827. foreach (PokerPlayer P in Players.Values)
  828. P.Send(PokerPackets.PokerLeaveTable(Id));
  829. }
  830. lock (Watchers)
  831. {
  832. foreach (PokerPlayer P in Watchers.Values)
  833. P.Send(PokerPackets.PokerLeaveTable(Id));
  834. }
  835. if (Watchers[Id].MyChar != null)
  836. Watchers[Id].MyChar.PokerTable = 0;
  837. Watchers.Remove(Id);
  838. }
  839. ToLocal(PokerPackets.PokerLeaveTable(Id));
  840. ToLocal(PokerPackets.PokerTable(this));
  841. }
  842. public void SendToAll(byte[] P)
  843. {
  844. foreach (PokerPlayer Player in Players.Values)
  845. Player.Send(P);
  846. foreach (PokerPlayer Player in Watchers.Values)
  847. Player.Send(P);
  848. }
  849. public PokerCard GetNewCard()
  850. {
  851. Random Rand = new Random();
  852. PokerCard PC = new PokerCard();
  853. int Rnd = Rand.Next(52);
  854. PC.Id = (byte)Rnd;
  855. while (!Cards.ContainsKey(PC.Id))
  856. {
  857. PC.Id = (byte)Rand.Next(52);
  858. }
  859. PC = Cards[PC.Id];
  860. return PC;
  861. }
  862. public void DrawCards(byte Count, bool Table)
  863. {
  864. try
  865. {
  866. if (!Table)
  867. {
  868. for (byte x = 0; x < Count; x++)
  869. {
  870. foreach (PokerPlayer P in Players.Values)
  871. {
  872. if (!P.Connected) continue;
  873. if (P.CurrentState > 1) continue;
  874. PokerCard C = GetNewCard();
  875. C.PlayerId = P.PlayerId;
  876. P.MyCards.Add(C.Id, C);
  877. if (Cards.ContainsKey(C.Id)) Cards.Remove(C.Id);
  878. }
  879. }
  880. return;
  881. }
  882. byte Co = (byte)TableCards.Count;
  883. for (byte x = Co; x < (byte)(Count + Co); x++)
  884. {
  885. PokerCard C = GetNewCard();
  886. C.PlayerId = 0;
  887. TableCards.Add(x, C);
  888. if (Cards.ContainsKey(C.Id)) Cards.Remove(C.Id);
  889. }
  890. }
  891. catch (Exception xp) { Console.WriteLine(xp.ToString()); }
  892. }
  893. public void NewPlayerMove(byte[] P, uint PlayerId)
  894. {
  895. if (Pot == 0) return;
  896. try
  897. {
  898. if (Players.ContainsKey(PlayerId))
  899. {
  900. if (MoveTimer != null && MoveTimer.Enabled)
  901. {
  902. MoveTimer.Stop();
  903. MoveTimer.Dispose();
  904. MoveTimer = null;
  905. }
  906. PokerPlayer Pl = Players[PlayerId];
  907. byte Move = P[6]; byte CSeat = Pl.Seat;
  908. Pl.RoundState = Move;
  909. uint ReqPot = Pot;
  910. //Program.NewMsg("PokerMove " + Move);
  911. switch (Move)
  912. {
  913. #region Rise // Call
  914. case 2://call
  915. {
  916. Pot += MinLimit;
  917. Pl.Bet += MinLimit;
  918. if (Pl.Bet > RoundMaxBet) RoundMaxBet = Pl.Bet;
  919. if (BetType == 0)
  920. Pl.MyChar.Money -= MinLimit;
  921. else if (BetType == 1)
  922. {
  923. if (Pl.MyChar.ConquerPoints >= MinLimit)
  924. Pl.MyChar.ConquerPoints -= MinLimit;
  925. else Pl.MyChar.ConquerPoints = 0;
  926. }
  927. Data D = new Data(true);
  928. D.ID = 234;
  929. D.UID = Id;
  930. D.dwParam = Pot;
  931. SendToAll(D.ToArray());
  932. SendToAll(PokerPackets.PokerPlayerMove(PlayerId, Move, Pl.Bet, Pot));
  933. break;
  934. }
  935. case 8:
  936. {
  937.  
  938. break;
  939. }
  940. case 16://Rise
  941. {
  942. uint Botting = MinLimit + MinLimit;
  943. Pot += Botting;
  944. Pl.Bet += Botting;
  945. if (Pl.Bet > RoundMaxBet) RoundMaxBet = Pl.Bet;
  946. if (BetType == 0)
  947. Pl.MyChar.Money -= Botting;
  948. else if (BetType == 1)
  949. {
  950. if (Pl.MyChar.ConquerPoints >= Botting)
  951. Pl.MyChar.ConquerPoints -= Botting;
  952. else Pl.MyChar.ConquerPoints = 0;
  953. }
  954. Data D = new Data(true);
  955. D.ID = 234;
  956. D.UID = Id;
  957. D.dwParam = Pot;
  958. D.Data24_Uint = RoundMaxBet;
  959. SendToAll(D.ToArray());
  960. SendToAll(PokerPackets.PokerPlayerMove(PlayerId, Move, Pl.Bet, Pot));
  961. break;
  962. }
  963. #endregion
  964. #region Fold
  965. case 4:
  966. {
  967. if (P[9] == 200)
  968. {
  969. //RemoveThis = true;
  970. RemovePlayer(PlayerId);
  971. }
  972. else if (Players.ContainsKey(PlayerId))
  973. {
  974. SendToAll(PokerPackets.PokerPlayerMove(PlayerId, Move, MinLimit, Pot));
  975. Players[PlayerId].MyCards.Clear();
  976. Players[PlayerId].RoundState = 4;
  977. }
  978. break;
  979. }
  980. default: SendToAll(P); break;
  981. #endregion
  982. #region AllIn
  983. case 32:
  984. {
  985. uint Betting = 0;
  986. if (BetType == 0)
  987. {
  988. Betting = Pl.MyChar.Money;
  989. Pl.MyChar.Money = 0;
  990. }
  991. else if (BetType == 1)
  992. {
  993. Betting = Pl.MyChar.ConquerPoints;
  994. Pl.MyChar.ConquerPoints = 0;
  995. }
  996. Pot += Betting;
  997. Pl.Bet += Betting;
  998. if (Pl.Bet > RoundMaxBet) RoundMaxBet = Pl.Bet;
  999. Pl.RoundState = 4;
  1000. Data D = new Data(true);
  1001. D.ID = 234;
  1002. D.UID = Id;
  1003. D.dwParam = Pot;
  1004. D.Data24_Uint = RoundMaxBet;
  1005. SendToAll(D.ToArray());
  1006. SendToAll(PokerPackets.PokerPlayerMove(PlayerId, Move, Pl.Bet, Pot));
  1007. break;
  1008. }
  1009. #endregion
  1010. }
  1011. uint NextPlayer = GetNextSeat(CSeat, true);
  1012. #region No More Players available
  1013. if (NextPlayer == Pl.PlayerId)
  1014. {
  1015. EndRound(NextPlayer);
  1016. return;
  1017. }
  1018. else if (Players.ContainsKey(NextPlayer) && NextPlayer == GetNextSeat(Players[NextPlayer].Seat, false))
  1019. {
  1020. EndRound(NextPlayer);
  1021. return;
  1022. }
  1023. #endregion
  1024. switch (RoundStage)
  1025. {
  1026. #region Send First 3 table cards
  1027. case 1:
  1028. {
  1029. if (TableCards.Count < 3)
  1030. {
  1031. DrawCards(3, true);
  1032. Dictionary<byte, PokerCard> TC = new Dictionary<byte, PokerCard>(3);
  1033. TC.Add(0, TableCards[0]);
  1034. TC.Add(1, TableCards[1]);
  1035. TC.Add(2, TableCards[2]);
  1036. SendToAll(PokerPackets.PokerTableCards(TC, this, 1));
  1037. }
  1038. break;
  1039. }
  1040. #endregion
  1041. #region Send Forth table card
  1042. case 2:
  1043. {
  1044. if (TableCards.Count < 4)
  1045. {
  1046. DrawCards(1, true);
  1047. Dictionary<byte, PokerCard> TC = new Dictionary<byte, PokerCard>(1);
  1048. if (TableCards.ContainsKey(3))
  1049. TC.Add(3, TableCards[3]);
  1050. SendToAll(PokerPackets.PokerTableCards(TC, this, 2));
  1051. }
  1052. break;
  1053. }
  1054. #endregion
  1055. #region Send Fifth table cards
  1056. case 3:
  1057. {
  1058. if (TableCards.Count < 5)
  1059. {
  1060. DrawCards(1, true);
  1061. Dictionary<byte, PokerCard> TC = new Dictionary<byte, PokerCard>(1);
  1062. if (TableCards.ContainsKey(4))
  1063. TC.Add(4, TableCards[4]);
  1064. SendToAll(PokerPackets.PokerTableCards(TC, this, 3));
  1065. }
  1066. break;
  1067. }
  1068. #endregion
  1069. case 4:
  1070. {
  1071. EndRound(0);
  1072. break;
  1073. }
  1074. }
  1075. if (RoundStage == 4) { return; }
  1076. else
  1077. {
  1078. uint PlayerBet = 0;
  1079. byte CallType = (byte)PokerCallTypes.Fold;
  1080. PokerPlayer Playr = null;
  1081. bool JustAllin = false;
  1082. if (Players.ContainsKey(NextPlayer))
  1083. {
  1084. PlayerBet = Players[NextPlayer].Bet;
  1085. Playr = Players[NextPlayer];
  1086. if (RoundMaxBet < PlayerBet)
  1087. {
  1088. CallType += (byte)PokerCallTypes.Check;
  1089. }
  1090. else CallType += (byte)PokerCallTypes.Call;
  1091. if (BetType == 0)
  1092. {
  1093. if (Playr.MyChar.Money < RoundMaxBet) JustAllin = true;
  1094. else if (Playr.MyChar.Money < MinLimit) JustAllin = true;
  1095. if (Playr.MyChar.Money >= MinLimit * 2) CallType += (byte)PokerCallTypes.Rise;
  1096. }
  1097. else if (BetType == 1)
  1098. {
  1099. if (Playr.MyChar.ConquerPoints < RoundMaxBet) JustAllin = true;
  1100. else if (Playr.MyChar.ConquerPoints < MinLimit) JustAllin = true;
  1101. if (Playr.MyChar.ConquerPoints >= MinLimit * 2) CallType += (byte)PokerCallTypes.Rise;
  1102. }
  1103.  
  1104. }
  1105. if (JustAllin) CallType = (byte)PokerCallTypes.AllIn;
  1106. SendToAll(PokerPackets.PokerPlayerTurn(NextPlayer, MinLimit, RoundMaxBet, CallType, 30));
  1107. StartMoveTimer(30, NextPlayer);
  1108. }
  1109. }
  1110. }
  1111. catch (Exception xp) { Console.WriteLine(xp.ToString()); }
  1112. }
  1113. public void EndRound(uint WinnerId)
  1114. {
  1115. SendToAll(PokerPackets.PokerPlayerTurn(0, 0, 0, 0, 30));
  1116. try
  1117. {
  1118. if (MoveTimer != null || MoveTimer.Enabled)
  1119. {
  1120. MoveTimer.Stop();
  1121. MoveTimer.Dispose();
  1122. MoveTimer = null;
  1123. }
  1124. }
  1125. catch { }
  1126. uint WinsVal = Pot - (Pot / 10);
  1127. #region Check Winner
  1128. if (WinnerId == 0)
  1129. {
  1130. ushort HighestPower = 0;
  1131. byte HighestHandPower = 0;
  1132. SendToAll(PokerPackets.PokerShowAllCards(this));
  1133. foreach (PokerPlayer Pla in Players.Values)
  1134. {
  1135. if (Pla.RoundState == 4 || !Pla.Connected) continue;
  1136. Dictionary<byte, PokerCard> TC = new Dictionary<byte, PokerCard>(7);
  1137. byte Counter = 5;
  1138. foreach (byte x in TableCards.Keys)
  1139. {
  1140. TC.Add(x, TableCards[x]);
  1141. }
  1142. foreach (byte x in Pla.MyCards.Keys)
  1143. {
  1144. TC.Add(Counter, Pla.MyCards[x]);
  1145. Counter++;
  1146. }
  1147. byte HP = GetHandPower(TC, Pla);
  1148. if (HP > HighestHandPower)
  1149. {
  1150. HighestHandPower = HP;
  1151. WinnerId = Pla.PlayerId;
  1152. HighestPower = Pla.GetFullPower(TC);
  1153. }
  1154. else if (HP == HighestHandPower)
  1155. {
  1156. if (Pla.GetFullPower(TC) > HighestPower)
  1157. {
  1158. WinnerId = Pla.PlayerId;
  1159. HighestPower = Pla.GetFullPower(TC);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. #endregion
  1165. if (Players.ContainsKey(WinnerId))
  1166. {
  1167. if (BetType == 0)
  1168. Players[WinnerId].MyChar.Money += WinsVal;
  1169. else if (BetType == 1)
  1170. Players[WinnerId].MyChar.ConquerPoints += WinsVal;
  1171. }
  1172. SendToAll(PokerPackets.PokerRoundResult(this, WinnerId, WinsVal));
  1173. Pot = 0;
  1174. #region Start new round
  1175. if (Players.Count < 2) return;
  1176. else
  1177. {
  1178. Pot = 0;
  1179. SetNewRound(20);
  1180. }
  1181. #endregion
  1182. }
  1183. public void ToLocal(byte[] P)
  1184. {
  1185. Client.GameClient[] Locals = new Client.GameClient[Program.GamePool.Length];
  1186. Locals = Kernel.GamePool.Values.ToArray();
  1187. foreach (Client.GameClient client in Locals)
  1188. {
  1189. if (client != null)
  1190. {
  1191. if (client.Map.ID == Map)
  1192. {
  1193. if (Kernel.GetDistance(client.Entity.X, client.Entity.Y, X, Y) > 25)
  1194. {
  1195. continue;
  1196. }
  1197. client.Send(P);
  1198. }
  1199.  
  1200. }
  1201. }
  1202. }
  1203. public uint GetNextSeat(byte Seat, bool Next)
  1204. {
  1205. try
  1206. {
  1207. Dictionary<byte, PokerPlayer> Ses = Seats;
  1208. uint Id = 0;
  1209. byte Se = (byte)(Seat + 1);
  1210. bool Found = false;
  1211. while (!Found)
  1212. {
  1213. if (Ses.ContainsKey(Se))
  1214. {
  1215. if (Ses[Se].RoundState == 4 || !Ses[Se].Connected) { }
  1216. else
  1217. {
  1218. Found = true;
  1219. break;
  1220. }
  1221. }
  1222. Se++;
  1223. if (Se > 9) Se = 0;
  1224. }
  1225. Id = Ses[Se].PlayerId;
  1226. if (Id == _StartingPlayer && Next) RoundStage++;
  1227. return Id;
  1228. }
  1229. catch (Exception xp) { Console.WriteLine(xp.ToString()); return 0; }
  1230. }
  1231. public byte GetHandPower(Dictionary<byte, PokerCard> Hand, PokerPlayer Pl)
  1232. {
  1233. byte Power = 0; bool SameSuit = true;
  1234. string Vals = ""; byte Suit = 5;
  1235. byte Clubs = 0, Diamonds = 0, Hearts = 0, Spades = 0;
  1236. Dictionary<byte, PokerCard> Ha = new Dictionary<byte, PokerCard>();
  1237. #region Sort
  1238. for (byte x = 0; x < Hand.Count; x++)
  1239. {
  1240. byte Val = 53; byte Inde = 100;
  1241. foreach (byte C in Hand.Keys)
  1242. {
  1243. if (Suit == 5) Suit = (byte)Hand[C].Typ;
  1244. else if (Suit != (byte)Hand[C].Typ) SameSuit = false;
  1245. if (Hand[C].Typ == PokerCardsType.Clubs) Clubs++;
  1246. else if (Hand[C].Typ == PokerCardsType.Diamonds) Diamonds++;
  1247. else if (Hand[C].Typ == PokerCardsType.Hearts) Hearts++;
  1248. else if (Hand[C].Typ == PokerCardsType.Spades) Spades++;
  1249. if (Hand[C].Val < Val && !Ha.ContainsKey(C))
  1250. {
  1251. Inde = C; Val = Hand[C].Val;
  1252. }
  1253. }
  1254. if (Hand.ContainsKey(Inde))
  1255. {
  1256. Ha.Add(Inde, Hand[Inde]);
  1257. }
  1258. }
  1259. #endregion
  1260. foreach (byte x in Ha.Keys)
  1261. {
  1262. byte Val = Ha[x].Val;
  1263. if (Val == 10) Vals += "A";
  1264. else if (Val == 11) Vals += "B";
  1265. else if (Val == 12) Vals += "C";
  1266. else Vals += Val.ToString();
  1267. }
  1268. if (Clubs > 4 || Diamonds > 4 || Spades > 4 || Hearts > 4) SameSuit = true;
  1269. Pl.HandVals = Vals;
  1270. if (SameSuit && IsRoyal(Vals)) Power = (byte)PokerHandPower.RoyalFlush;
  1271. else if (SameSuit && IsStraight(Vals)) Power = (byte)PokerHandPower.StraightFlush;
  1272. else if (IsFourOfAKind(Vals)) Power = (byte)PokerHandPower.FourOfAKind;
  1273. else if (IsThreeOfAKind(Pl))
  1274. {
  1275. if (IsPair(Pl)) Power = (byte)PokerHandPower.FullHouse;
  1276. }
  1277. else if (SameSuit) Power = (byte)PokerHandPower.Flush;
  1278. else if (IsStraight(Vals)) Power = (byte)PokerHandPower.Straight;
  1279. else Pl.HandVals = Vals;
  1280. if (Power == 0)
  1281. {
  1282. if (IsPair(Pl))
  1283. { if (IsPair(Pl)) Power = (byte)PokerHandPower.TwoPairs; }
  1284. else Pl.HandVals = Vals;
  1285. if (IsThreeOfAKind(Pl)) Power = (byte)PokerHandPower.ThreeOfAKind;
  1286. else if (IsPair(Pl)) Power = (byte)PokerHandPower.Pair;
  1287. }
  1288. return Power;
  1289. }
  1290. public bool IsRoyal(string HandVals)
  1291. {
  1292. if (HandVals.Contains("89ABC")) return true;
  1293. else return false;
  1294. }
  1295. public bool IsStraight(string HandVals)
  1296. {
  1297. bool Straight = false;
  1298. string V = HandVals;
  1299. if (V.Contains("01234") || V.Contains("12345") || V.Contains("23456")) Straight = true;
  1300. else if (V.Contains("34567") || V.Contains("45678") || V.Contains("56789")) Straight = true;
  1301. else if (V.Contains("6789A") || V.Contains("789AB") || V.Contains("89ABC")) Straight = true;
  1302. else if (V.Contains("C0123")) Straight = true;
  1303. return Straight;
  1304. }
  1305. public bool IsFourOfAKind(string HandVals)
  1306. {
  1307. bool Yes = false;
  1308. string V = HandVals;
  1309. if (V.Contains("0123") || V.Contains("1234") || V.Contains("2345")) Yes = true;
  1310. else if (V.Contains("3456") || V.Contains("4567") || V.Contains("5678")) Yes = true;
  1311. else if (V.Contains("6789") || V.Contains("789A") || V.Contains("89AB")) Yes = true;
  1312. else if (V.Contains("9ABC") || V.Contains("C012")) Yes = true;
  1313. else if (V.Contains("0000") || V.Contains("1111") || V.Contains("2222")) Yes = true;
  1314. else if (V.Contains("3333") || V.Contains("4444") || V.Contains("5555")) Yes = true;
  1315. else if (V.Contains("6666") || V.Contains("7777") || V.Contains("8888")) Yes = true;
  1316. else if (V.Contains("9999") || V.Contains("AAAA") || V.Contains("BBBB")) Yes = true;
  1317. else if (V.Contains("CCCC")) Yes = true;
  1318. return Yes;
  1319. }
  1320. public bool IsThreeOfAKind(PokerPlayer Pl)
  1321. {
  1322. bool Yes = false;
  1323. string V = Pl.HandVals;
  1324. if (V.Contains("012") || V.Contains("123") || V.Contains("234")) Yes = true;
  1325. else if (V.Contains("345") || V.Contains("456") || V.Contains("567")) Yes = true;
  1326. else if (V.Contains("678") || V.Contains("789") || V.Contains("89A")) Yes = true;
  1327. else if (V.Contains("9AB") || V.Contains("ABC") || V.Contains("C01")) Yes = true;
  1328. else if (V.Contains("000") || V.Contains("111") || V.Contains("222")) Yes = true;
  1329. else if (V.Contains("333") || V.Contains("444") || V.Contains("555")) Yes = true;
  1330. else if (V.Contains("666") || V.Contains("777") || V.Contains("888")) Yes = true;
  1331. else if (V.Contains("999") || V.Contains("AAA") || V.Contains("BBB")) Yes = true;
  1332. else if (V.Contains("CCC")) Yes = true;
  1333. if (V.Contains("CCC")) V = V.Replace("CCC", "");
  1334. else if (V.Contains("BBB")) V = V.Replace("BBB", "");
  1335. else if (V.Contains("AAA")) V = V.Replace("AAA", "");
  1336. else if (V.Contains("999")) V = V.Replace("999", "");
  1337. else if (V.Contains("888")) V = V.Replace("888", "");
  1338. else if (V.Contains("777")) V = V.Replace("777", "");
  1339. else if (V.Contains("666")) V = V.Replace("666", "");
  1340. else if (V.Contains("555")) V = V.Replace("555", "");
  1341. else if (V.Contains("444")) V = V.Replace("444", "");
  1342. else if (V.Contains("333")) V = V.Replace("333", "");
  1343. else if (V.Contains("222")) V = V.Replace("222", "");
  1344. else if (V.Contains("111")) V = V.Replace("111", "");
  1345. else if (V.Contains("000")) V = V.Replace("000", "");
  1346. else if (V.Contains("ABC")) V = V.Replace("ABC", "");
  1347. else if (V.Contains("C01")) V = V.Replace("C01", "");
  1348. else if (V.Contains("9AB")) V = V.Replace("9AB", "");
  1349. else if (V.Contains("89A")) V = V.Replace("89A", "");
  1350. else if (V.Contains("789")) V = V.Replace("789", "");
  1351. else if (V.Contains("678")) V = V.Replace("678", "");
  1352. else if (V.Contains("567")) V = V.Replace("567", "");
  1353. else if (V.Contains("456")) V = V.Replace("456", "");
  1354. else if (V.Contains("345")) V = V.Replace("345", "");
  1355. else if (V.Contains("234")) V = V.Replace("234", "");
  1356. else if (V.Contains("123")) V = V.Replace("123", "");
  1357. else if (V.Contains("012")) V = V.Replace("012", "");
  1358. Pl.HandVals = V;
  1359. return Yes;
  1360. }
  1361. public bool IsPair(PokerPlayer Pl)
  1362. {
  1363. bool Yes = false;
  1364. string V = Pl.HandVals;
  1365. if (V.Contains("01") || V.Contains("12") || V.Contains("23")) Yes = true;
  1366. else if (V.Contains("34") || V.Contains("45") || V.Contains("56")) Yes = true;
  1367. else if (V.Contains("67") || V.Contains("78") || V.Contains("89")) Yes = true;
  1368. else if (V.Contains("9A") || V.Contains("AB") || V.Contains("BC")) Yes = true;
  1369. else if (V.Contains("C0")) Yes = true;
  1370. else if (V.Contains("00") || V.Contains("11") || V.Contains("22")) Yes = true;
  1371. else if (V.Contains("33") || V.Contains("44") || V.Contains("55")) Yes = true;
  1372. else if (V.Contains("66") || V.Contains("77") || V.Contains("88")) Yes = true;
  1373. else if (V.Contains("99") || V.Contains("AA") || V.Contains("BB")) Yes = true;
  1374. else if (V.Contains("CC")) Yes = true;
  1375. if (V.Contains("CC")) V = V.Replace("CC", "");
  1376. else if (V.Contains("BB")) V = V.Replace("BB", "");
  1377. else if (V.Contains("AA")) V = V.Replace("AA", "");
  1378. else if (V.Contains("99")) V = V.Replace("99", "");
  1379. else if (V.Contains("88")) V = V.Replace("88", "");
  1380. else if (V.Contains("77")) V = V.Replace("77", "");
  1381. else if (V.Contains("66")) V = V.Replace("66", "");
  1382. else if (V.Contains("55")) V = V.Replace("55", "");
  1383. else if (V.Contains("44")) V = V.Replace("44", "");
  1384. else if (V.Contains("33")) V = V.Replace("33", "");
  1385. else if (V.Contains("22")) V = V.Replace("22", "");
  1386. else if (V.Contains("11")) V = V.Replace("11", "");
  1387. else if (V.Contains("00")) V = V.Replace("00", "");
  1388. else if (V.Contains("BC")) V = V.Replace("BC", "");
  1389. else if (V.Contains("AB")) V = V.Replace("AB", "");
  1390. else if (V.Contains("C0")) V = V.Replace("C0", "");
  1391. else if (V.Contains("9A")) V = V.Replace("9A", "");
  1392. else if (V.Contains("89")) V = V.Replace("89", "");
  1393. else if (V.Contains("78")) V = V.Replace("78", "");
  1394. else if (V.Contains("67")) V = V.Replace("67", "");
  1395. else if (V.Contains("56")) V = V.Replace("56", "");
  1396. else if (V.Contains("45")) V = V.Replace("45", "");
  1397. else if (V.Contains("34")) V = V.Replace("34", "");
  1398. else if (V.Contains("23")) V = V.Replace("23", "");
  1399. else if (V.Contains("12")) V = V.Replace("12", "");
  1400. else if (V.Contains("01")) V = V.Replace("01", "");
  1401. Pl.HandVals = V;
  1402. return Yes;
  1403. }
  1404. }
  1405. public class PokerPlayer
  1406. {
  1407. public uint TableId = 0;
  1408. public uint PlayerId = 0;
  1409. public bool Connected = true;
  1410. public byte CurrentState = 1;
  1411. public byte RoundState = 0;
  1412. public uint Bet = 0;
  1413. public byte Seat = 0;
  1414. public byte CardsPower = 0;
  1415. public string HandVals = "";
  1416. public System.Timers.Timer MoveTimer = new System.Timers.Timer();
  1417. public ushort GetFullPower(Dictionary<byte, PokerCard> TCards)
  1418. {
  1419. ushort P = 0;
  1420. foreach (PokerCard C in MyCards.Values)
  1421. {
  1422. P += (ushort)(C.Id);
  1423. }
  1424. return P;
  1425. }
  1426. public Dictionary<byte, PokerCard> MyCards = new Dictionary<byte, PokerCard>(5);
  1427. public Entity MyChar
  1428. {
  1429. get
  1430. {
  1431. if (Kernel.GamePool.ContainsKey(PlayerId))
  1432. return Kernel.GamePool[PlayerId].Entity;
  1433. else { Connected = false; return null; }
  1434. }
  1435. set
  1436. {
  1437. PlayerId = value.UID;
  1438. }
  1439. }
  1440. public PokerTable MyTable
  1441. {
  1442. get
  1443. {
  1444. if (Kernel.PokerTables.ContainsKey(TableId)) return Kernel.PokerTables[TableId];
  1445. else return null;
  1446. }
  1447. set
  1448. {
  1449. TableId = value.Id;
  1450. }
  1451. }
  1452.  
  1453. public void Send(byte[] P)
  1454. {
  1455. if (!Connected)
  1456. {
  1457. if (Kernel.PokerTables.ContainsKey(TableId))
  1458. {
  1459. if (Kernel.PokerTables[TableId].Players.ContainsKey(PlayerId))
  1460. Kernel.PokerTables[TableId].Players[PlayerId].RoundState = 4;
  1461. return;
  1462. }
  1463. }
  1464. if (Kernel.GamePool.ContainsKey(PlayerId))
  1465. Kernel.GamePool[PlayerId].Send(P);
  1466. else
  1467. {
  1468. Connected = false;
  1469. if (Kernel.PokerTables.ContainsKey(TableId))
  1470. {
  1471. if (Kernel.PokerTables[TableId].Players.ContainsKey(PlayerId))
  1472. Kernel.PokerTables[TableId].Players[PlayerId].RoundState = 4;
  1473. }
  1474. }
  1475. }
  1476. }
  1477. public class PokerCard
  1478. {
  1479. public byte Id = 0;
  1480. public uint PlayerId;
  1481. public byte Val = 0;
  1482. public PokerCardsType Typ = PokerCardsType.Clubs;
  1483.  
  1484. }
  1485.  
  1486. public enum PokerTableState : byte
  1487. {
  1488. //table state 0=unopened 1=Pocket 2=flop 3=turn 4=river 5=showdown
  1489. UnOpened = 0,
  1490. Pocket = 1,
  1491. Flop = 2,
  1492. Turn = 3,
  1493. River = 4,
  1494. ShowDown = 5
  1495. }
  1496. public enum PokerJoinTableType : byte
  1497. {
  1498. Join = 0,
  1499. Leave = 1,
  1500. Watching = 2
  1501. }
  1502. public enum PokerCardsType : byte
  1503. {
  1504. Hearts = 0,
  1505. Spades = 1,
  1506. Clubs = 2,
  1507. Diamonds = 3
  1508. }
  1509. public enum PokerHandPower : byte
  1510. {
  1511. RoyalFlush = 10,//AKQJ10 of one suit
  1512. StraightFlush = 9,//five cards in sequence, all the same suit
  1513. FourOfAKind = 8,//four cards of the same rank (plus any fifth card)
  1514. FullHouse = 7,//three cards of one rank, plus a pair of another rank
  1515. Flush = 6,//five cards of the same suit
  1516. Straight = 5,//1-5
  1517. ThreeOfAKind = 4,// three cards of the same rank, plus two other unmatched cards
  1518. TwoPairs = 3,//two cards of the same rank, plus two other cards of a different rank, plus one unmatched card
  1519. Pair = 2,//two cards of the same rank, plus three other unmatched cards
  1520. Nothing = 1 // any hand not meeting the requirements of a pair or higher hand
  1521. }
  1522. public enum PokerCallTypes : byte
  1523. {
  1524. Bet = 1,
  1525. Call = 2,
  1526. Fold = 4,
  1527. Check = 8,
  1528. Rise = 16,
  1529. AllIn = 32,
  1530. CallFold = 6,
  1531. CheckFold = 12,
  1532. RiseCall = 18,
  1533. RiseFold = 20,
  1534. RiseCallFold = 22,
  1535. RiseCheck = 24,
  1536. AllInCall = 34,
  1537. AllInFold = 36,
  1538. AllInCallFold = 38,
  1539. }
  1540.  
  1541. public class PacketBuilder
  1542. {
  1543. protected byte[] _buffer = new byte[1024];
  1544. protected int Position = 0;
  1545. protected int Len = 0;
  1546. protected byte[] TQ_SERVER = Encoding.Default.GetBytes("TQServer");
  1547. public int GetPos()
  1548. {
  1549. return Position;
  1550. }
  1551. public void SetPosition(int Pos)
  1552. {
  1553. Position = Pos;
  1554. }
  1555. public PacketBuilder(int T, int L)
  1556. {
  1557. Len = L;
  1558. Length(L);
  1559. Type(T);
  1560. }
  1561.  
  1562. public void Short(int value)
  1563. {
  1564. _buffer[Position] = ((byte)(value & 0xff));
  1565. Position++;
  1566. _buffer[Position] = ((byte)((value >> 8) & 0xff));
  1567. Position++;
  1568. }
  1569. public void Short(uint value)
  1570. {
  1571. _buffer[Position] = ((byte)(value & 0xff));
  1572. Position++;
  1573. _buffer[Position] = ((byte)((value >> 8) & 0xff));
  1574. Position++;
  1575. }
  1576. public void Length(int value)
  1577. {
  1578. _buffer[Position] = ((byte)(value & 0xff));
  1579. Position++;
  1580. _buffer[Position] = ((byte)((value >> 8) & 0xff));
  1581. Position++;
  1582. }
  1583. public void Type(int value)
  1584. {
  1585. _buffer[Position] = ((byte)(value & 0xff));
  1586. Position++;
  1587. _buffer[Position] = ((byte)((value >> 8) & 0xff));
  1588. Position++;
  1589. }
  1590. public void Long(int value)
  1591. {
  1592. _buffer[Position] = ((byte)(value & 0xff));
  1593. Position++;
  1594. _buffer[Position] = ((byte)(value >> 8 & 0xff));
  1595. Position++;
  1596. _buffer[Position] = (byte)(value >> 16 & 0xff);
  1597. Position++;
  1598. _buffer[Position] = ((byte)(value >> 24 & 0xff));
  1599. Position++;
  1600. }
  1601. public void Long(ulong value)
  1602. {
  1603. _buffer[Position] = ((byte)((ulong)value & 0xffL));
  1604. Position++;
  1605. _buffer[Position] = ((byte)(value >> 8 & 0xff));
  1606. Position++;
  1607. _buffer[Position] = (byte)(value >> 16 & 0xff);
  1608. Position++;
  1609. _buffer[Position] = ((byte)(value >> 24 & 0xff));
  1610. Position++;
  1611. }
  1612. public void ULong(ulong value)
  1613. {
  1614. _buffer[Position] = (byte)(value);
  1615. Position++;
  1616. _buffer[Position] = (byte)(value >> 8);
  1617. Position++;
  1618. _buffer[Position] = (byte)(value >> 16);
  1619. Position++;
  1620. _buffer[Position] = (byte)(value >> 24);
  1621. Position++;
  1622. _buffer[Position] = (byte)(value >> 32);
  1623. Position++;
  1624. _buffer[Position] = (byte)(value >> 40);
  1625. Position++;
  1626. _buffer[Position] = (byte)(value >> 48);
  1627. Position++;
  1628. _buffer[Position] = (byte)(value >> 56);
  1629. Position++;
  1630. }
  1631. public void Int(int value)
  1632. {
  1633. _buffer[Position] = (Convert.ToByte(value & 0xff));
  1634. Position++;
  1635. }
  1636. public void Int(uint value)
  1637. {
  1638. _buffer[Position] = (Convert.ToByte(value & 0xff));
  1639. Position++;
  1640. }
  1641. public void Long(uint value)
  1642. {
  1643. _buffer[Position] = ((byte)(value & 0xff));
  1644. Position++;
  1645. _buffer[Position] = ((byte)(value >> 8 & 0xff));
  1646. Position++;
  1647. _buffer[Position] = (byte)(value >> 16 & 0xff);
  1648. Position++;
  1649. _buffer[Position] = ((byte)(value >> 24 & 0xff));
  1650. Position++;
  1651. }
  1652. public void Move(int value)
  1653. {
  1654. for (int x = 0; x < value; x++)
  1655. {
  1656. _buffer[Position] = 0;
  1657. Position++;
  1658. }
  1659. }
  1660.  
  1661. public void Text(string value)
  1662. {
  1663. byte[] nvalue = Encoding.Default.GetBytes(value);
  1664. Array.Copy(nvalue, 0, _buffer, Position, nvalue.Length);
  1665. Position += nvalue.Length;
  1666. }
  1667. protected void Seal()
  1668. {
  1669. Array.Copy(TQ_SERVER, 0, _buffer, Position, TQ_SERVER.Length);
  1670. Position += TQ_SERVER.Length + 1;
  1671. byte[] x = new byte[Position - 1];
  1672. Array.Copy(_buffer, x, Position - 1);
  1673. _buffer = new byte[x.Length];
  1674. Array.Copy(x, _buffer, x.Length);
  1675. x = null;
  1676. }
  1677. public byte[] getFinal()
  1678. {
  1679. Seal();
  1680. return _buffer;
  1681. }
  1682.  
  1683. internal void Fill(int End)
  1684. {
  1685. for (int x = Position; x < End; x++)
  1686. Int(0);
  1687. }
  1688. internal void PrintThis()
  1689. {
  1690. string Dat = "";
  1691. for (int x = 0; x < Position; x++)
  1692. Dat += _buffer[x].ToString("X") + " ";
  1693. Console.WriteLine(Dat);
  1694. }
  1695.  
  1696. #region Add from offset
  1697. public void Short(int value, int Offset)
  1698. {
  1699. _buffer[Offset] = ((byte)(value & 0xff));
  1700. _buffer[Offset + 1] = ((byte)((value >> 8) & 0xff));
  1701. }
  1702. public void Short(uint value, int Offset)
  1703. {
  1704. _buffer[Offset] = ((byte)(value & 0xff));
  1705. Offset++;
  1706. _buffer[Offset] = ((byte)((value >> 8) & 0xff));
  1707. }
  1708. public void Length(int value, int Offset)
  1709. {
  1710. _buffer[Offset] = ((byte)(value & 0xff));
  1711. Offset++;
  1712. _buffer[Offset] = ((byte)((value >> 8) & 0xff));
  1713. }
  1714. public void Type(int value, int Offset)
  1715. {
  1716. _buffer[Offset] = ((byte)(value & 0xff));
  1717. Offset++;
  1718. _buffer[Offset] = ((byte)((value >> 8) & 0xff));
  1719. }
  1720. public void Long(int value, int Offset)
  1721. {
  1722. _buffer[Offset] = ((byte)(value & 0xff));
  1723. Offset++;
  1724. _buffer[Offset] = ((byte)(value >> 8 & 0xff));
  1725. Offset++;
  1726. _buffer[Offset] = (byte)(value >> 16 & 0xff);
  1727. Offset++;
  1728. _buffer[Offset] = ((byte)(value >> 24 & 0xff));
  1729. }
  1730. public void Long(ulong value, int Offset)
  1731. {
  1732. _buffer[Offset] = ((byte)((ulong)value & 0xffL));
  1733. Offset++;
  1734. _buffer[Offset] = ((byte)(value >> 8 & 0xff));
  1735. Offset++;
  1736. _buffer[Offset] = (byte)(value >> 16 & 0xff);
  1737. Offset++;
  1738. _buffer[Offset] = ((byte)(value >> 24 & 0xff));
  1739. }
  1740. public void ULong(ulong value, int Offset)
  1741. {
  1742. _buffer[Offset] = (byte)(value);
  1743. Offset++;
  1744. _buffer[Offset] = (byte)(value >> 8);
  1745. Offset++;
  1746. _buffer[Offset] = (byte)(value >> 16);
  1747. Offset++;
  1748. _buffer[Offset] = (byte)(value >> 24);
  1749. Offset++;
  1750. _buffer[Offset] = (byte)(value >> 32);
  1751. Offset++;
  1752. _buffer[Offset] = (byte)(value >> 40);
  1753. Offset++;
  1754. _buffer[Offset] = (byte)(value >> 48);
  1755. Offset++;
  1756. _buffer[Offset] = (byte)(value >> 56);
  1757. }
  1758. public void Int(int value, int Offset)
  1759. {
  1760. _buffer[Offset] = (Convert.ToByte(value & 0xff));
  1761. Offset++;
  1762. }
  1763. public void Int(uint value, int Offset)
  1764. {
  1765. _buffer[Offset] = (Convert.ToByte(value & 0xff));
  1766. Offset++;
  1767. }
  1768. public void Long(uint value, int Offset)
  1769. {
  1770. _buffer[Offset] = ((byte)(value & 0xff));
  1771. Offset++;
  1772. _buffer[Offset] = ((byte)(value >> 8 & 0xff));
  1773. Offset++;
  1774. _buffer[Offset] = (byte)(value >> 16 & 0xff);
  1775. Offset++;
  1776. _buffer[Offset] = ((byte)(value >> 24 & 0xff));
  1777. Offset++;
  1778. }
  1779. #endregion
  1780. }
  1781. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement