diff --git a/console/src/ConsoleCallback.cs b/console/src/ConsoleCallback.cs index 8c65caf8..f2154f3d 100644 --- a/console/src/ConsoleCallback.cs +++ b/console/src/ConsoleCallback.cs @@ -48,53 +48,71 @@ namespace pokerth_console Console.WriteLine("Game was started. Players: {0}.", outPlayers); } - public void HandStarted(pokerth_lib.Hand h) + public void HandStarted(int[] cards) { - Console.WriteLine("New hand. Your cards: {0} {1}. Your money: {2}.", - Log.CardToString(h.Players[h.MyPlayerId].Cards[0]), - Log.CardToString(h.Players[h.MyPlayerId].Cards[1]), - h.Players[h.MyPlayerId].Money); + Console.WriteLine("\nNew hand. Your cards: {0}, {1}.", + Log.CardToString(cards[0]), + Log.CardToString(cards[1])); } - public void MyTurn(Hand.State state) + public void SmallBlind(string name, uint blind) { - Console.WriteLine("--> {0}: Your turn.", - Log.StateToString(state)); + Console.WriteLine("{0} posts small blind (${1}).", name, blind); + } + + public void BigBlind(string name, uint blind) + { + Console.WriteLine("{0} posts big blind (${1}).", name, blind); + } + + public void MyTurn(Hand.State state, uint highestSet, uint minimumRaise, uint money) + { + Console.WriteLine("\n--> {0}: Your turn. Highest set: ${1}. Minimum raise: ${2}. Cash: ${3}.", + Log.StateToString(state), highestSet, minimumRaise, money); } public void PlayersTurn(Hand.State state, string player) { - Console.WriteLine("{0}: {1}'s turn.", + Console.WriteLine("\n{0}: {1}'s turn.", Log.StateToString(state), player); } - public void ActionDone(string name, Hand.Action action, uint totalBet, uint money, uint highestSet, uint minimumRaise) + public void ActionDone(string name, Hand.Action action, uint curBet) { - Console.WriteLine("{0} acts: {1}. Total bet: {2}. Money left: {3}.", - name, Log.ActionToString(action), totalBet, money); - Console.WriteLine("Highest set: {0}. Minimum raise: {1}.", - highestSet, minimumRaise); + Console.WriteLine("{0} {1}.", name, Log.ActionToString(action, curBet)); } - public void ShowFlopCards(int firstFlopCard, int secondFlopCard, int thirdFlopCard) + public void ShowFlopCards(int[] cards) { - Console.WriteLine("Flop cards: {0} {1} {2}.", - Log.CardToString(firstFlopCard), - Log.CardToString(secondFlopCard), - Log.CardToString(thirdFlopCard)); + Console.WriteLine("\n--- Flop --- [{0}, {1}, {2}]", + Log.CardToString(cards[0]), + Log.CardToString(cards[1]), + Log.CardToString(cards[2])); } - public void ShowTurnCard(int turnCard) + public void ShowTurnCards(int[] cards) { - Console.WriteLine("Turn card: {0}.", - Log.CardToString(turnCard)); + Console.WriteLine("\n--- Turn --- [{0}, {1}, {2}, {3}]", + Log.CardToString(cards[0]), + Log.CardToString(cards[1]), + Log.CardToString(cards[2]), + Log.CardToString(cards[3])); } - public void ShowRiverCard(int riverCard) + public void ShowRiverCards(int[] cards) { - Console.WriteLine("River card: {0}.", - Log.CardToString(riverCard)); + Console.WriteLine("\n--- River --- [{0}, {1}, {2}, {3}, {4}]", + Log.CardToString(cards[0]), + Log.CardToString(cards[1]), + Log.CardToString(cards[2]), + Log.CardToString(cards[3]), + Log.CardToString(cards[4])); + } + + public void PlayerWinsHideCards(string name, uint moneyWon) + { + Console.WriteLine("\n{0} wins ${1}.", name, moneyWon); } public void Error(string message) diff --git a/console/src/lib/Hand.cs b/console/src/lib/Hand.cs index 59cd5663..539315fd 100644 --- a/console/src/lib/Hand.cs +++ b/console/src/lib/Hand.cs @@ -52,6 +52,7 @@ namespace pokerth_lib m_state = State.Preflop; m_players = players; m_myPlayerId = myPlayerId; + m_smallBlind = smallBlind; } public State CurState @@ -85,12 +86,73 @@ namespace pokerth_lib } public uint MyPlayerId + { + get + { + // no need to lock a mutex, this is read only. + return m_myPlayerId; + } + } + + public uint SmallBlind + { + get + { + // no need to lock a mutex, this is read only. + return m_smallBlind; + } + } + + public uint HighestSet { get { lock (m_mutex) { - return m_myPlayerId; + return m_highestSet; + } + } + set + { + lock (m_mutex) + { + m_highestSet = value; + } + } + } + + public uint MinimumRaise + { + get + { + lock (m_mutex) + { + return m_minimumRaise; + } + } + set + { + lock (m_mutex) + { + m_minimumRaise = value; + } + } + } + + public int[] TableCards + { + get + { + lock (m_mutex) + { + return (int[])m_tableCards.Clone(); + } + } + set + { + lock (m_mutex) + { + m_tableCards = value; } } } @@ -98,6 +160,10 @@ namespace pokerth_lib private Object m_mutex; private State m_state; private Dictionary m_players; - uint m_myPlayerId; + private int[] m_tableCards; + private uint m_myPlayerId; + private uint m_smallBlind; + private uint m_highestSet; + private uint m_minimumRaise; } } diff --git a/console/src/lib/ICallback.cs b/console/src/lib/ICallback.cs index 3a0e59dd..3064e19e 100644 --- a/console/src/lib/ICallback.cs +++ b/console/src/lib/ICallback.cs @@ -28,13 +28,16 @@ namespace pokerth_lib void InitDone(); void JoinedGame(string name); void GameStarted(List players); - void HandStarted(Hand h); - void MyTurn(Hand.State state); + void HandStarted(int[] cards); + void SmallBlind(string name, uint blind); + void BigBlind(string name, uint blind); + void MyTurn(Hand.State state, uint highestSet, uint minimumRaise, uint money); void PlayersTurn(Hand.State state, string player); - void ActionDone(string name, Hand.Action action, uint totalBet, uint money, uint highestSet, uint minimumRaise); - void ShowFlopCards(int firstFlopCard, int secondFlopCard, int thirdFlopCard); - void ShowTurnCard(int turnCard); - void ShowRiverCard(int riverCard); + void ActionDone(string name, Hand.Action action, uint curBet); + void ShowFlopCards(int[] cards); + void ShowTurnCards(int[] cards); + void ShowRiverCards(int[] cards); + void PlayerWinsHideCards(string name, uint moneyWon); void Error(string message); } } diff --git a/console/src/lib/Log.cs b/console/src/lib/Log.cs index d7dbb38a..21b23bec 100644 --- a/console/src/lib/Log.cs +++ b/console/src/lib/Log.cs @@ -1,5 +1,7 @@ /*************************************************************************** * Copyright (C) 2008 by Lothar May * +* Inspired by log.cpp of the PokerTH client which is * + * Copyright (C) 2006 by FThauer FHammer * * * * This file is part of pokerth_console. * * pokerth_console is free software: you can redistribute it and/or * @@ -123,37 +125,38 @@ namespace pokerth_lib return stateString; } - public static string ActionToString(Hand.Action action) + public static string ActionToString(Hand.Action action, uint curSet) { string actionString; switch (action) { - case Hand.Action.None: - actionString = "None"; - break; case Hand.Action.Fold: - actionString = "Fold"; + actionString = "folds"; break; case Hand.Action.Check: - actionString = "Check"; + actionString = "checks"; break; case Hand.Action.Call: - actionString = "Call"; + actionString = "calls"; break; case Hand.Action.Bet: - actionString = "Bet"; + actionString = "bets"; break; case Hand.Action.Raise: - actionString = "Raise"; + actionString = "bets"; break; case Hand.Action.AllIn: - actionString = "All in"; + actionString = "is all in with"; break; default : - actionString = "Unknown action"; + actionString = "(invalid action)"; break; } + + if ((int)action >= 3) + actionString += " $" + Convert.ToString(curSet); + return actionString; } } diff --git a/console/src/lib/Player.cs b/console/src/lib/Player.cs index c052c9ea..3e0febff 100644 --- a/console/src/lib/Player.cs +++ b/console/src/lib/Player.cs @@ -104,6 +104,16 @@ namespace pokerth_lib } } + public void NewHand() + { + lock (m_mutex) + { + m_cards = null; + m_curAction = Hand.Action.None; + m_totalBet = 0; + } + } + private Object m_mutex; private int[] m_cards; private Hand.Action m_curAction; diff --git a/console/src/lib/net/INetPacketVisitor.cs b/console/src/lib/net/INetPacketVisitor.cs index ad4c5ad7..10a61c31 100644 --- a/console/src/lib/net/INetPacketVisitor.cs +++ b/console/src/lib/net/INetPacketVisitor.cs @@ -45,5 +45,6 @@ namespace pokerth_lib void VisitDealFlopCards(NetPacket p); void VisitDealTurnCard(NetPacket p); void VisitDealRiverCard(NetPacket p); + void VisitEndOfHandHideCards(NetPacket p); } } diff --git a/console/src/lib/net/NetPacket.cs b/console/src/lib/net/NetPacket.cs index 0a5fd580..ab3f0d4c 100644 --- a/console/src/lib/net/NetPacket.cs +++ b/console/src/lib/net/NetPacket.cs @@ -136,6 +136,7 @@ namespace pokerth_lib FlopThirdCard, TurnCard, RiverCard, + MoneyWon, } public enum ListPropType @@ -144,6 +145,11 @@ namespace pokerth_lib ManualBlindSlots, } + public enum RecordPropType + { + PlayerResult, + } + public static NetPacket Create(int type, int size, BinaryReader reader) { NetPacket tmpPacket = null; @@ -189,12 +195,76 @@ namespace pokerth_lib case NetTypeDealRiverCard : tmpPacket = new NetPacketDealRiverCard(size, reader); break; + case NetTypeEndOfHandHideCards : + tmpPacket = new NetPacketEndOfHandHideCards(size, reader); + break; default: break; } return tmpPacket; } + public NetPacket(int type) + { + m_type = type; + m_properties = new Dictionary(); + m_listProperties = new Dictionary>(); + m_recordProperties = new Dictionary>>(); + } + + public Dictionary Properties + { + set + { + m_properties = value; + } + get + { + return m_properties; + } + } + + public Dictionary> ListProperties + { + set + { + m_listProperties = value; + } + get + { + return m_listProperties; + } + } + + public Dictionary>> RecordProperties + { + set + { + m_recordProperties = value; + } + get + { + return m_recordProperties; + } + } + + public int Type + { + get + { + return m_type; + } + } + + public abstract void Accept(INetPacketVisitor visitor); + + public abstract byte[] ToByteArray(); + + static protected int AddPadding(int size) + { + return ((((size) + 3) / 4) * 4); + } + protected void ScanGameInfoBlock(BinaryReader r) { Properties.Add(PropType.MaxNumPlayers, @@ -258,56 +328,9 @@ namespace pokerth_lib } } - public NetPacket(int type) - { - m_type = type; - m_properties = new Dictionary(); - m_listProperties = new Dictionary>(); - } - - public Dictionary Properties - { - set - { - m_properties = value; - } - get - { - return m_properties; - } - } - - public Dictionary> ListProperties - { - set - { - m_listProperties = value; - } - get - { - return m_listProperties; - } - } - - public int Type - { - get - { - return m_type; - } - } - - public abstract void Accept(INetPacketVisitor visitor); - - public abstract byte[] ToByteArray(); - - static protected int AddPadding(int size) - { - return ((((size) + 3) / 4) * 4); - } - private int m_type; private Dictionary m_properties; private Dictionary> m_listProperties; + private Dictionary>> m_recordProperties; } } diff --git a/console/src/lib/net/NetPacketEndOfHandHideCards.cs b/console/src/lib/net/NetPacketEndOfHandHideCards.cs new file mode 100644 index 00000000..2afa0546 --- /dev/null +++ b/console/src/lib/net/NetPacketEndOfHandHideCards.cs @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Net; +using System.Net.Sockets; +using System.IO; + +namespace pokerth_lib +{ + class NetPacketEndOfHandHideCards : NetPacket + { + public NetPacketEndOfHandHideCards() + : base(NetPacket.NetTypeEndOfHandHideCards) + { + } + + public NetPacketEndOfHandHideCards(int size, BinaryReader r) + : base(NetPacket.NetTypeEndOfHandHideCards) + { + if (size != 16) + throw new NetPacketException("NetPacketEndOfHandHideCards invalid size."); + Properties.Add(PropType.PlayerId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + Properties.Add(PropType.MoneyWon, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + Properties.Add(PropType.PlayerMoney, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitEndOfHandHideCards(this); + } + + public override byte[] ToByteArray() + { + throw new NotImplementedException(); + } + } +} diff --git a/console/src/lib/net/NetPacketEndOfHandShowCards.cs b/console/src/lib/net/NetPacketEndOfHandShowCards.cs new file mode 100644 index 00000000..e9b69d65 --- /dev/null +++ b/console/src/lib/net/NetPacketEndOfHandShowCards.cs @@ -0,0 +1,69 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Net; +using System.Net.Sockets; +using System.IO; + + +namespace pokerth_lib +{ + class NetPacketEndOfHandShowCards : NetPacket + { + public NetPacketEndOfHandShowCards() + : base(NetPacket.NetTypeEndOfHandShowCards) + { + } + + public NetPacketEndOfHandShowCards(int size, BinaryReader r) + : base(NetPacket.NetTypeEndOfHandShowCards) + { + if (size < 40) + throw new NetPacketException("NetPacketEndOfHandShowCards invalid size."); + int numPlayerResults = IPAddress.NetworkToHostOrder((short)r.ReadUInt16()); + r.ReadUInt16(); //reserved + + List> tmpRecordList = new List>(); + for (int i = 0; i < numPlayerResults; i++) + { + Dictionary tmpList = new Dictionary(); + tmpList.Add(PropType.PlayerId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + tmpList.Add(PropType.FirstCard, + Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16()))); + tmpList.Add(PropType.SecondCard, + Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16()))); + } + RecordProperties.Add(RecordPropType.PlayerResult, tmpRecordList); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitEndOfHandHideCards(this); + } + + public override byte[] ToByteArray() + { + throw new NotImplementedException(); + } + } +} diff --git a/console/src/lib/net/NetPacketGameStart.cs b/console/src/lib/net/NetPacketGameStart.cs index c5ce014f..6b2c9b3c 100644 --- a/console/src/lib/net/NetPacketGameStart.cs +++ b/console/src/lib/net/NetPacketGameStart.cs @@ -43,7 +43,7 @@ namespace pokerth_lib Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); int curNumPlayers = IPAddress.NetworkToHostOrder((short)r.ReadUInt16()); Properties.Add(PropType.CurNumPlayers, Convert.ToString(curNumPlayers)); - r.ReadBytes(2); // reserved + r.ReadUInt16(); // reserved // Read player ids. List playerSlots = new List(); diff --git a/console/src/lib/net/NetParser.cs b/console/src/lib/net/NetParser.cs index 0f5cb1f0..fcf54af9 100644 --- a/console/src/lib/net/NetParser.cs +++ b/console/src/lib/net/NetParser.cs @@ -150,17 +150,23 @@ namespace pokerth_lib public void VisitHandStart(NetPacket p) { + // Reset players. + foreach (KeyValuePair player in m_players) + player.Value.NewHand(); + + // Assign own cards. int[] tmpCards = new int[2]; tmpCards[0] = Convert.ToInt32(p.Properties[NetPacket.PropType.FirstCard]); tmpCards[1] = Convert.ToInt32(p.Properties[NetPacket.PropType.SecondCard]); m_players[m_data.MyPlayerId].Cards = tmpCards; + m_data.CurHand = new Hand( m_players, m_data.MyPlayerId, Convert.ToUInt32(p.Properties[NetPacket.PropType.SmallBlind])); - m_callback.HandStarted(m_data.CurHand); + m_callback.HandStarted(tmpCards); } public void VisitPlayersTurn(NetPacket p) @@ -168,7 +174,8 @@ namespace pokerth_lib uint curPlayer = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); Hand.State state = (Hand.State)Convert.ToUInt16(p.Properties[NetPacket.PropType.GameState]); if (curPlayer == m_data.MyPlayerId) - m_callback.MyTurn(state); + m_callback.MyTurn(state, m_data.CurHand.HighestSet, + m_data.CurHand.MinimumRaise, m_players[curPlayer].Money); else m_callback.PlayersTurn(state, m_data.PlayerList.GetPlayerInfo(curPlayer).Name); } @@ -182,19 +189,32 @@ namespace pokerth_lib { uint playerId = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); Player curPlayer = m_data.CurHand.Players[playerId]; + string name = m_data.PlayerList.GetPlayerInfo(playerId).Name; curPlayer.CurAction = (Hand.Action)Convert.ToUInt16(p.Properties[NetPacket.PropType.PlayerAction]); curPlayer.Money = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerMoney]); - curPlayer.TotalBet = - Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerBetTotal]); - m_callback.ActionDone( - m_data.PlayerList.GetPlayerInfo(playerId).Name, - curPlayer.CurAction, - curPlayer.TotalBet, - curPlayer.Money, - Convert.ToUInt32(p.Properties[NetPacket.PropType.HighestSet]), - Convert.ToUInt32(p.Properties[NetPacket.PropType.MinimumRaise])); + uint curBet = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerBetTotal]) + - curPlayer.TotalBet; + curPlayer.TotalBet += curBet; + + m_data.CurHand.HighestSet = Convert.ToUInt32(p.Properties[NetPacket.PropType.HighestSet]); + m_data.CurHand.MinimumRaise = Convert.ToUInt32(p.Properties[NetPacket.PropType.MinimumRaise]); + + if (curPlayer.CurAction == Hand.Action.None) + { + if (curBet == m_data.CurHand.SmallBlind) + m_callback.SmallBlind(name, curBet); + else + m_callback.BigBlind(name, curBet); + } + else + { + m_callback.ActionDone( + name, + curPlayer.CurAction, + curBet); + } } public void VisitPlayersActionRejected(NetPacket p) @@ -204,22 +224,42 @@ namespace pokerth_lib public void VisitDealFlopCards(NetPacket p) { - m_callback.ShowFlopCards( - Convert.ToInt32(p.Properties[NetPacket.PropType.FlopFirstCard]), - Convert.ToInt32(p.Properties[NetPacket.PropType.FlopSecondCard]), - Convert.ToInt32(p.Properties[NetPacket.PropType.FlopThirdCard])); + int[] tmpCards = new int[3]; + tmpCards[0] = Convert.ToInt32(p.Properties[NetPacket.PropType.FlopFirstCard]); + tmpCards[1] = Convert.ToInt32(p.Properties[NetPacket.PropType.FlopSecondCard]); + tmpCards[2] = Convert.ToInt32(p.Properties[NetPacket.PropType.FlopThirdCard]); + m_data.CurHand.TableCards = tmpCards; + m_callback.ShowFlopCards(tmpCards); } public void VisitDealTurnCard(NetPacket p) { - m_callback.ShowTurnCard( - Convert.ToInt32(p.Properties[NetPacket.PropType.TurnCard])); + int[] tmpCards = new int[4]; + m_data.CurHand.TableCards.CopyTo(tmpCards, 0); + tmpCards[3] = Convert.ToInt32(p.Properties[NetPacket.PropType.TurnCard]); + m_data.CurHand.TableCards = tmpCards; + m_callback.ShowTurnCards(tmpCards); } public void VisitDealRiverCard(NetPacket p) { - m_callback.ShowRiverCard( - Convert.ToInt32(p.Properties[NetPacket.PropType.RiverCard])); + int[] tmpCards = new int[5]; + m_data.CurHand.TableCards.CopyTo(tmpCards, 0); + tmpCards[4] = Convert.ToInt32(p.Properties[NetPacket.PropType.RiverCard]); + m_data.CurHand.TableCards = tmpCards; + m_callback.ShowRiverCards(tmpCards); + } + + public void VisitEndOfHandHideCards(NetPacket p) + { + uint playerId = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); + string name = m_data.PlayerList.GetPlayerInfo(playerId).Name; + Player curPlayer = m_data.CurHand.Players[playerId]; + curPlayer.Money = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerMoney]); + + m_callback.PlayerWinsHideCards( + name, + Convert.ToUInt32(p.Properties[NetPacket.PropType.MoneyWon])); } private PokerTHData m_data;