From 36ef30bedc10653a2f83193694c1bb4a5702dbb0 Mon Sep 17 00:00:00 2001 From: lotodore Date: Fri, 26 Dec 2008 13:54:36 +0000 Subject: [PATCH] Console client: Several fixes (mainly mono specific). --- console/src/Program.cs | 65 ++++++++++++++++++++------------ console/src/lib/PokerTHData.cs | 24 ++++++------ console/src/lib/net/Client.cs | 15 +++----- console/src/lib/net/NetParser.cs | 4 +- 4 files changed, 59 insertions(+), 49 deletions(-) diff --git a/console/src/Program.cs b/console/src/Program.cs index 3cd7c982..30668ccd 100644 --- a/console/src/Program.cs +++ b/console/src/Program.cs @@ -57,47 +57,62 @@ namespace pokerth_console Console.WriteLine("Joining game..."); uint gameId = Convert.ToUInt32(input); client.JoinGame(gameId); - if (client.WaitJoinGame(5000)) + // Hack this, because Mono does not support WaitOne(...). + DateTime cur = DateTime.Now; + while (!client.HasJoinedGame()) + { + Thread.Sleep(15); + if (DateTime.Now.Subtract(cur).Seconds > 5) + break; + } + if (client.HasJoinedGame()) joined = true; else Console.WriteLine("Could not join game."); } while (!joined); Console.WriteLine("Waiting for admin to start the game..."); - client.WaitStartGame(); do { do { + while (Console.KeyAvailable) + Console.ReadKey(); input = Console.ReadLine(); } while (input == ""); Hand.Action action = Hand.Action.None; uint bet = 0; - switch (input[0]) + try + { + switch (input[0]) + { + case 'f': + action = Hand.Action.Fold; + break; + case 'c': + action = Hand.Action.Check; + break; + case 'l': + action = Hand.Action.Call; + break; + case 'b': + action = Hand.Action.Bet; + bet = Convert.ToUInt32(input.Substring(1)); + break; + case 'r': + action = Hand.Action.Raise; + bet = Convert.ToUInt32(input.Substring(1)); + break; + case 'a': + action = Hand.Action.AllIn; + break; + } + if (action != Hand.Action.None) + client.MyAction(action, bet); + } + catch (FormatException) { - case 'f': - action = Hand.Action.Fold; - break; - case 'c': - action = Hand.Action.Check; - break; - case 'l': - action = Hand.Action.Call; - break; - case 'b': - action = Hand.Action.Bet; - bet = Convert.ToUInt32(input.Substring(1)); - break; - case 'r': - action = Hand.Action.Raise; - bet = Convert.ToUInt32(input.Substring(1)); - break; - case 'a': - action = Hand.Action.AllIn; - break; } - if (action != Hand.Action.None) - client.MyAction(action, bet); } while (true); client.SetTerminateFlag(); client.WaitTermination(); diff --git a/console/src/lib/PokerTHData.cs b/console/src/lib/PokerTHData.cs index 6f19c33f..52013d65 100644 --- a/console/src/lib/PokerTHData.cs +++ b/console/src/lib/PokerTHData.cs @@ -35,8 +35,7 @@ namespace pokerth_lib m_myPlayerId = 0; m_myGameId = 0; m_myName = name; - m_joinGameEvent = new AutoResetEvent(false); - m_startGameEvent = new AutoResetEvent(false); + m_joinedGame = false; } public GameInfoList GameList @@ -117,19 +116,21 @@ namespace pokerth_lib } } - public EventWaitHandle JoinGameEvent + public bool JoinedGame { get { - return m_joinGameEvent; + lock (m_mutex) + { + return m_joinedGame; + } } - } - - public EventWaitHandle StartGameEvent - { - get + set { - return m_startGameEvent; + lock (m_mutex) + { + m_joinedGame = value; + } } } @@ -140,7 +141,6 @@ namespace pokerth_lib private uint m_myPlayerId; private uint m_myGameId; private string m_myName; - private EventWaitHandle m_joinGameEvent; - private EventWaitHandle m_startGameEvent; + private bool m_joinedGame; } } diff --git a/console/src/lib/net/Client.cs b/console/src/lib/net/Client.cs index b621963f..5c2e57a7 100644 --- a/console/src/lib/net/Client.cs +++ b/console/src/lib/net/Client.cs @@ -39,11 +39,11 @@ namespace pokerth_lib { // Connect to the server. // Try IPv6 first. - IPAddress[] addresses = new IPAddress[2]; + /*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); + m_tcpClient.Connect(addresses, m_settings.ServerSettings.Port);*/ + m_tcpClient.Connect("localhost", 7234); } public void Start() @@ -58,14 +58,9 @@ namespace pokerth_lib SendJoinGame(gameId); } - public bool WaitJoinGame(int timeout) + public bool HasJoinedGame() { - return m_data.JoinGameEvent.WaitOne(timeout); - } - - public bool WaitStartGame() - { - return m_data.StartGameEvent.WaitOne(); + return m_data.JoinedGame; } public void MyAction(Hand.Action action, uint bet) diff --git a/console/src/lib/net/NetParser.cs b/console/src/lib/net/NetParser.cs index 5f6aeef4..9132fc62 100644 --- a/console/src/lib/net/NetParser.cs +++ b/console/src/lib/net/NetParser.cs @@ -119,7 +119,7 @@ namespace pokerth_lib m_data.MyGameId = Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]); m_callback.JoinedGame(m_data.GameList.GetGameInfo(m_data.MyGameId).Name); - m_data.JoinGameEvent.Set(); + m_data.JoinedGame = true; } public void VisitStartEvent(NetPacket p) @@ -167,7 +167,6 @@ namespace pokerth_lib } m_callback.GameStarted(strPlayers); - m_data.StartGameEvent.Set(); } public void VisitHandStart(NetPacket p) @@ -195,6 +194,7 @@ namespace pokerth_lib { uint curPlayer = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); Hand.State state = (Hand.State)Convert.ToUInt16(p.Properties[NetPacket.PropType.GameState]); + m_data.CurHand.CurState = state; if (curPlayer == m_data.MyPlayerId) m_callback.MyTurn(state, m_data.CurHand.HighestSet, m_data.CurHand.MinimumRaise, m_players[curPlayer].Money);