From e4ead9e706e626e0ab7925752f633db4c5f14dbb Mon Sep 17 00:00:00 2001 From: lotodore Date: Sat, 27 Dec 2008 16:24:49 +0000 Subject: [PATCH] Console client: Several fixes and improvements (thanks to heyn). --- console/src/ConsoleCallback.cs | 26 +++++++++++++++---- console/src/Program.cs | 4 +++ console/src/lib/GameInfo.cs | 2 +- console/src/lib/Hand.cs | 20 ++++++++++++-- console/src/lib/ICallback.cs | 3 ++- console/src/lib/IdObject.cs | 4 +-- console/src/lib/Log.cs | 2 +- console/src/lib/PokerTHData.cs | 2 +- console/src/lib/ServerSettings.cs | 13 ++++++++++ console/src/lib/net/Client.cs | 19 +++++++++----- .../lib/net/NetPacketPlayersActionRejected.cs | 2 +- console/src/lib/net/NetParser.cs | 15 ++++++++++- 12 files changed, 91 insertions(+), 21 deletions(-) diff --git a/console/src/ConsoleCallback.cs b/console/src/ConsoleCallback.cs index 13eb96db..7b776e9a 100644 --- a/console/src/ConsoleCallback.cs +++ b/console/src/ConsoleCallback.cs @@ -80,9 +80,20 @@ namespace pokerth_console player); } - public void ActionDone(string name, Hand.Action action, uint curBet) + public void ActionDone(string name, Hand.Action action, uint curBet, uint money, List nonFoldPlayers) { - Console.WriteLine("{0} {1}.", name, Log.ActionToString(action, curBet)); + Console.WriteLine("{0} {1}. Cash: {2}.", name, Log.ActionToString(action, curBet), money); + if (action == Hand.Action.Fold) + { + string outPlayers = ""; + foreach (string s in nonFoldPlayers) + { + if (outPlayers.Length != 0) + outPlayers += ", "; + outPlayers += s; + } + Console.WriteLine("Remaining players: {0}", outPlayers); + } } public void ActionRejected() @@ -90,9 +101,14 @@ namespace pokerth_console Console.WriteLine("Your action was rejected by the server."); } + public void ShowPot(uint pot) + { + Console.WriteLine("\nPot is now {0}.", pot); + } + public void ShowFlopCards(int[] cards) { - Console.WriteLine("\n--- Flop --- [{0}, {1}, {2}]", + Console.WriteLine("--- Flop --- [{0}, {1}, {2}]", Log.CardToString(cards[0]), Log.CardToString(cards[1]), Log.CardToString(cards[2])); @@ -100,7 +116,7 @@ namespace pokerth_console public void ShowTurnCards(int[] cards) { - Console.WriteLine("\n--- Turn --- [{0}, {1}, {2}, {3}]", + Console.WriteLine("--- Turn --- [{0}, {1}, {2}, {3}]", Log.CardToString(cards[0]), Log.CardToString(cards[1]), Log.CardToString(cards[2]), @@ -109,7 +125,7 @@ namespace pokerth_console public void ShowRiverCards(int[] cards) { - Console.WriteLine("\n--- River --- [{0}, {1}, {2}, {3}, {4}]", + Console.WriteLine("--- River --- [{0}, {1}, {2}, {3}, {4}]", Log.CardToString(cards[0]), Log.CardToString(cards[1]), Log.CardToString(cards[2]), diff --git a/console/src/Program.cs b/console/src/Program.cs index 8d907cd7..6d34e671 100644 --- a/console/src/Program.cs +++ b/console/src/Program.cs @@ -113,10 +113,14 @@ namespace pokerth_console Console.WriteLine("pokerth_console V0.1 - Copyright (C) 2008 by Lothar May"); Console.WriteLine("See COPYING.txt for license terms."); Console.WriteLine(); + Console.WriteLine("Enter your nickname:"); string name = Console.ReadLine(); Console.WriteLine("Connecting to server..."); Settings settings = new Settings(); + // Parse command line options. + if (args.Length > 0) + settings.ServerSettings.Server = args[0]; PokerTHData data = new PokerTHData(name); ConsoleCallback callback = new ConsoleCallback(); Client client = new Client(settings, data, callback); diff --git a/console/src/lib/GameInfo.cs b/console/src/lib/GameInfo.cs index 6d6192ac..8ddc1156 100644 --- a/console/src/lib/GameInfo.cs +++ b/console/src/lib/GameInfo.cs @@ -92,6 +92,6 @@ namespace pokerth_lib private Object m_mutex; private Mode m_mode; private List m_playerSlots; - private uint m_startMoney; + readonly private uint m_startMoney; } } diff --git a/console/src/lib/Hand.cs b/console/src/lib/Hand.cs index c5baaf38..f5e0090d 100644 --- a/console/src/lib/Hand.cs +++ b/console/src/lib/Hand.cs @@ -53,6 +53,7 @@ namespace pokerth_lib m_players = players; m_myPlayerId = myPlayerId; m_smallBlind = smallBlind; + m_pot = 0; } public State CurState @@ -73,7 +74,10 @@ namespace pokerth_lib m_state = value; // Reset bets. foreach (KeyValuePair player in m_players) + { + m_pot += player.Value.TotalBet; player.Value.TotalBet = 0; + } } } } @@ -163,13 +167,25 @@ namespace pokerth_lib } } + public uint Pot + { + get + { + lock (m_mutex) + { + return m_pot; + } + } + } + private Object m_mutex; private State m_state; private Dictionary m_players; private int[] m_tableCards; - private uint m_myPlayerId; - private uint m_smallBlind; + readonly private uint m_myPlayerId; + readonly private uint m_smallBlind; private uint m_highestSet; private uint m_minimumRaise; + private uint m_pot; } } diff --git a/console/src/lib/ICallback.cs b/console/src/lib/ICallback.cs index 416cf444..c3371d84 100644 --- a/console/src/lib/ICallback.cs +++ b/console/src/lib/ICallback.cs @@ -33,8 +33,9 @@ namespace pokerth_lib 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 curBet); + void ActionDone(string name, Hand.Action action, uint curBet, uint money, List nonFoldPlayers); void ActionRejected(); + void ShowPot(uint pot); void ShowFlopCards(int[] cards); void ShowTurnCards(int[] cards); void ShowRiverCards(int[] cards); diff --git a/console/src/lib/IdObject.cs b/console/src/lib/IdObject.cs index 857cf059..83b98f18 100644 --- a/console/src/lib/IdObject.cs +++ b/console/src/lib/IdObject.cs @@ -49,7 +49,7 @@ namespace pokerth_lib } } - private uint m_id; - private string m_name; + readonly private uint m_id; + readonly private string m_name; } } diff --git a/console/src/lib/Log.cs b/console/src/lib/Log.cs index 592931bc..ea51e08c 100644 --- a/console/src/lib/Log.cs +++ b/console/src/lib/Log.cs @@ -286,7 +286,7 @@ namespace pokerth_lib cardString = "Two Pairs, " + CardCodeToString(secondPart, true) + " and " - + CardCodeToString(secondPart, true); + + CardCodeToString(thirdPart, true); break; // One Pair case 1 : diff --git a/console/src/lib/PokerTHData.cs b/console/src/lib/PokerTHData.cs index 52013d65..50b7542f 100644 --- a/console/src/lib/PokerTHData.cs +++ b/console/src/lib/PokerTHData.cs @@ -140,7 +140,7 @@ namespace pokerth_lib private Object m_mutex; private uint m_myPlayerId; private uint m_myGameId; - private string m_myName; + readonly private string m_myName; private bool m_joinedGame; } } diff --git a/console/src/lib/ServerSettings.cs b/console/src/lib/ServerSettings.cs index fff0eb4b..3d3353ef 100644 --- a/console/src/lib/ServerSettings.cs +++ b/console/src/lib/ServerSettings.cs @@ -65,8 +65,21 @@ namespace pokerth_lib } } + public string Server + { + get + { + return m_server; + } + set + { + m_server = value; + } + } + private string m_ipv4Address = ""; private string m_ipv6Address = ""; + private string m_server = ""; private int m_port = 0; } } diff --git a/console/src/lib/net/Client.cs b/console/src/lib/net/Client.cs index e867c785..de2aa3b7 100644 --- a/console/src/lib/net/Client.cs +++ b/console/src/lib/net/Client.cs @@ -38,12 +38,19 @@ namespace pokerth_lib public void Connect() { // Connect to the server. - // Try IPv6 first. - IPAddress[] addresses = new IPAddress[2]; - addresses[0] = IPAddress.Parse(m_settings.ServerSettings.IPv6Address); - addresses[1] = IPAddress.Parse(m_settings.ServerSettings.IPv4Address); - m_tcpClient.Connect(addresses, m_settings.ServerSettings.Port); - //m_tcpClient.Connect("localhost", 7234); + // If the user specified a server, try this. + if (m_settings.ServerSettings.Server.Length > 0) + m_tcpClient.Connect( + m_settings.ServerSettings.Server, m_settings.ServerSettings.Port); + else + { + // Try IPv6 first. + IPAddress[] addresses = new IPAddress[2]; + addresses[0] = IPAddress.Parse(m_settings.ServerSettings.IPv6Address); + addresses[1] = IPAddress.Parse(m_settings.ServerSettings.IPv4Address); + m_tcpClient.Connect(addresses, m_settings.ServerSettings.Port); + //m_tcpClient.Connect("localhost", 7234); + } } public void Start() diff --git a/console/src/lib/net/NetPacketPlayersActionRejected.cs b/console/src/lib/net/NetPacketPlayersActionRejected.cs index c5b84c7c..3184d5cc 100644 --- a/console/src/lib/net/NetPacketPlayersActionRejected.cs +++ b/console/src/lib/net/NetPacketPlayersActionRejected.cs @@ -37,7 +37,7 @@ namespace pokerth_lib public NetPacketPlayersActionRejected(int size, BinaryReader r) : base(NetPacket.NetTypePlayersActionRejected) { - if (size != 28) + if (size != 16) throw new NetPacketException("NetPacketPlayersActionRejected invalid size."); Properties.Add(PropType.GameState, Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16()))); diff --git a/console/src/lib/net/NetParser.cs b/console/src/lib/net/NetParser.cs index 9f9292a2..a11aee32 100644 --- a/console/src/lib/net/NetParser.cs +++ b/console/src/lib/net/NetParser.cs @@ -236,10 +236,20 @@ namespace pokerth_lib } else { + // Collect non-fold players. + List nonFoldPlayers = new List(); + foreach (KeyValuePair player in m_players) + { + if (player.Value.CurAction != Hand.Action.Fold) + nonFoldPlayers.Add(m_data.PlayerList.GetPlayerInfo(player.Key).Name); + } + // Inform about action. m_callback.ActionDone( name, curPlayer.CurAction, - curBet); + curBet, + curPlayer.Money, + nonFoldPlayers); } } @@ -256,6 +266,7 @@ namespace pokerth_lib 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.ShowPot(m_data.CurHand.Pot); m_callback.ShowFlopCards(tmpCards); } @@ -266,6 +277,7 @@ namespace pokerth_lib m_data.CurHand.TableCards.CopyTo(tmpCards, 0); tmpCards[3] = Convert.ToInt32(p.Properties[NetPacket.PropType.TurnCard]); m_data.CurHand.TableCards = tmpCards; + m_callback.ShowPot(m_data.CurHand.Pot); m_callback.ShowTurnCards(tmpCards); } @@ -276,6 +288,7 @@ namespace pokerth_lib m_data.CurHand.TableCards.CopyTo(tmpCards, 0); tmpCards[4] = Convert.ToInt32(p.Properties[NetPacket.PropType.RiverCard]); m_data.CurHand.TableCards = tmpCards; + m_callback.ShowPot(m_data.CurHand.Pot); m_callback.ShowRiverCards(tmpCards); }