Console client: Several fixes (mainly mono specific).

This commit is contained in:
lotodore
2008-12-26 13:54:36 +00:00
parent 4f48fdbe2a
commit 36ef30bedc
4 changed files with 59 additions and 49 deletions
+40 -25
View File
@@ -57,47 +57,62 @@ namespace pokerth_console
Console.WriteLine("Joining game..."); Console.WriteLine("Joining game...");
uint gameId = Convert.ToUInt32(input); uint gameId = Convert.ToUInt32(input);
client.JoinGame(gameId); 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; joined = true;
else else
Console.WriteLine("Could not join game."); Console.WriteLine("Could not join game.");
} while (!joined); } while (!joined);
Console.WriteLine("Waiting for admin to start the game..."); Console.WriteLine("Waiting for admin to start the game...");
client.WaitStartGame();
do do
{ {
do do
{ {
while (Console.KeyAvailable)
Console.ReadKey();
input = Console.ReadLine(); input = Console.ReadLine();
} while (input == ""); } while (input == "");
Hand.Action action = Hand.Action.None; Hand.Action action = Hand.Action.None;
uint bet = 0; 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); } while (true);
client.SetTerminateFlag(); client.SetTerminateFlag();
client.WaitTermination(); client.WaitTermination();
+12 -12
View File
@@ -35,8 +35,7 @@ namespace pokerth_lib
m_myPlayerId = 0; m_myPlayerId = 0;
m_myGameId = 0; m_myGameId = 0;
m_myName = name; m_myName = name;
m_joinGameEvent = new AutoResetEvent(false); m_joinedGame = false;
m_startGameEvent = new AutoResetEvent(false);
} }
public GameInfoList GameList public GameInfoList GameList
@@ -117,19 +116,21 @@ namespace pokerth_lib
} }
} }
public EventWaitHandle JoinGameEvent public bool JoinedGame
{ {
get get
{ {
return m_joinGameEvent; lock (m_mutex)
{
return m_joinedGame;
}
} }
} set
public EventWaitHandle StartGameEvent
{
get
{ {
return m_startGameEvent; lock (m_mutex)
{
m_joinedGame = value;
}
} }
} }
@@ -140,7 +141,6 @@ namespace pokerth_lib
private uint m_myPlayerId; private uint m_myPlayerId;
private uint m_myGameId; private uint m_myGameId;
private string m_myName; private string m_myName;
private EventWaitHandle m_joinGameEvent; private bool m_joinedGame;
private EventWaitHandle m_startGameEvent;
} }
} }
+5 -10
View File
@@ -39,11 +39,11 @@ namespace pokerth_lib
{ {
// Connect to the server. // Connect to the server.
// Try IPv6 first. // Try IPv6 first.
IPAddress[] addresses = new IPAddress[2]; /*IPAddress[] addresses = new IPAddress[2];
addresses[0] = IPAddress.Parse(m_settings.ServerSettings.IPv6Address); addresses[0] = IPAddress.Parse(m_settings.ServerSettings.IPv6Address);
addresses[1] = IPAddress.Parse(m_settings.ServerSettings.IPv4Address); addresses[1] = IPAddress.Parse(m_settings.ServerSettings.IPv4Address);
m_tcpClient.Connect(addresses, m_settings.ServerSettings.Port); m_tcpClient.Connect(addresses, m_settings.ServerSettings.Port);*/
//m_tcpClient.Connect("localhost", 7234); m_tcpClient.Connect("localhost", 7234);
} }
public void Start() public void Start()
@@ -58,14 +58,9 @@ namespace pokerth_lib
SendJoinGame(gameId); SendJoinGame(gameId);
} }
public bool WaitJoinGame(int timeout) public bool HasJoinedGame()
{ {
return m_data.JoinGameEvent.WaitOne(timeout); return m_data.JoinedGame;
}
public bool WaitStartGame()
{
return m_data.StartGameEvent.WaitOne();
} }
public void MyAction(Hand.Action action, uint bet) public void MyAction(Hand.Action action, uint bet)
+2 -2
View File
@@ -119,7 +119,7 @@ namespace pokerth_lib
m_data.MyGameId = m_data.MyGameId =
Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]); Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]);
m_callback.JoinedGame(m_data.GameList.GetGameInfo(m_data.MyGameId).Name); m_callback.JoinedGame(m_data.GameList.GetGameInfo(m_data.MyGameId).Name);
m_data.JoinGameEvent.Set(); m_data.JoinedGame = true;
} }
public void VisitStartEvent(NetPacket p) public void VisitStartEvent(NetPacket p)
@@ -167,7 +167,6 @@ namespace pokerth_lib
} }
m_callback.GameStarted(strPlayers); m_callback.GameStarted(strPlayers);
m_data.StartGameEvent.Set();
} }
public void VisitHandStart(NetPacket p) public void VisitHandStart(NetPacket p)
@@ -195,6 +194,7 @@ namespace pokerth_lib
{ {
uint curPlayer = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); uint curPlayer = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]);
Hand.State state = (Hand.State)Convert.ToUInt16(p.Properties[NetPacket.PropType.GameState]); Hand.State state = (Hand.State)Convert.ToUInt16(p.Properties[NetPacket.PropType.GameState]);
m_data.CurHand.CurState = state;
if (curPlayer == m_data.MyPlayerId) if (curPlayer == m_data.MyPlayerId)
m_callback.MyTurn(state, m_data.CurHand.HighestSet, m_callback.MyTurn(state, m_data.CurHand.HighestSet,
m_data.CurHand.MinimumRaise, m_players[curPlayer].Money); m_data.CurHand.MinimumRaise, m_players[curPlayer].Money);