Console client: Now functional, but several features are still missing.
This commit is contained in:
@@ -28,7 +28,8 @@ namespace pokerth_console
|
||||
{
|
||||
public void InitDone()
|
||||
{
|
||||
Console.WriteLine("Init successful.");
|
||||
Console.WriteLine("Connection established.");
|
||||
Console.WriteLine("Retrieving list of games...");
|
||||
}
|
||||
|
||||
public void JoinedGame(string name)
|
||||
@@ -69,6 +70,7 @@ namespace pokerth_console
|
||||
{
|
||||
Console.WriteLine("\n--> {0}: Your turn. Highest set: ${1}. Minimum raise: ${2}. Cash: ${3}.",
|
||||
Log.StateToString(state), highestSet, minimumRaise, money);
|
||||
Console.WriteLine("Enter: (f)old, (c)heck, ca(l)l, (b)et<num>, (r)aise<num>, (a)ll in");
|
||||
}
|
||||
|
||||
public void PlayersTurn(Hand.State state, string player)
|
||||
@@ -110,9 +112,22 @@ namespace pokerth_console
|
||||
Log.CardToString(cards[4]));
|
||||
}
|
||||
|
||||
public void AllInState()
|
||||
{
|
||||
Console.WriteLine(); // simply print a newline
|
||||
}
|
||||
|
||||
public void ShowCards(string name, int[] cards)
|
||||
{
|
||||
Console.WriteLine("{0} shows [{1}, {2}].",
|
||||
name,
|
||||
Log.CardToString(cards[0]),
|
||||
Log.CardToString(cards[1]));
|
||||
}
|
||||
|
||||
public void EndOfHandShowCards(string name, int[] cards, int cardsValue, bool allIn)
|
||||
{
|
||||
Console.WriteLine("{0} {1} [{2}, {3}] - {4}",
|
||||
Console.WriteLine("{0} {1} [{2}, {3}] - {4}.",
|
||||
name,
|
||||
allIn ? "has" : "shows",
|
||||
Log.CardToString(cards[0]),
|
||||
@@ -120,6 +135,11 @@ namespace pokerth_console
|
||||
Log.CardsValueToString(cardsValue));
|
||||
}
|
||||
|
||||
public void HandResult()
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public void PlayerWins(string name, uint moneyWon)
|
||||
{
|
||||
Console.WriteLine("{0} wins ${1}.", name, moneyWon);
|
||||
@@ -128,6 +148,8 @@ namespace pokerth_console
|
||||
public void Error(string message)
|
||||
{
|
||||
Console.WriteLine("Error: " + message);
|
||||
Console.WriteLine("Hint: Try using a different nickname.");
|
||||
System.Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+64
-9
@@ -29,21 +29,76 @@ namespace pokerth_console
|
||||
{
|
||||
static int Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("pokerth_console V0.1 - Copyright (C) 2008 by Lothar May");
|
||||
Console.WriteLine("See license.txt for license terms.");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Enter your nickname:");
|
||||
string name = Console.ReadLine();
|
||||
Console.WriteLine("Connecting to server...");
|
||||
Settings settings = new Settings();
|
||||
PokerTHData data = new PokerTHData("Testuser1");
|
||||
PokerTHData data = new PokerTHData(name);
|
||||
ConsoleCallback callback = new ConsoleCallback();
|
||||
Client client = new Client(settings, data, callback);
|
||||
client.Connect();
|
||||
client.Start();
|
||||
Thread.Sleep(2000);
|
||||
Console.WriteLine("Open Games:");
|
||||
Console.Write(data.GameList.ToString());
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Enter game id");
|
||||
string input = Console.ReadLine();
|
||||
uint gameId = Convert.ToUInt32(input);
|
||||
client.JoinGame(gameId);
|
||||
input = Console.ReadLine();
|
||||
string input;
|
||||
bool joined = false;
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
Console.WriteLine("Open Games:");
|
||||
Console.Write(data.GameList.GetOpenGamesString());
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Enter game id (or press enter to refresh)");
|
||||
input = Console.ReadLine();
|
||||
} while (input == "" || !Char.IsDigit(input[0]));
|
||||
Console.WriteLine("Joining game...");
|
||||
uint gameId = Convert.ToUInt32(input);
|
||||
client.JoinGame(gameId);
|
||||
if (client.WaitJoinGame(5000))
|
||||
joined = true;
|
||||
else
|
||||
Console.WriteLine("Could not join game.");
|
||||
} while (!joined);
|
||||
Console.WriteLine("Waiting for admin to start the game...");
|
||||
client.WaitStartGame();
|
||||
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
input = Console.ReadLine();
|
||||
} while (input == "");
|
||||
Hand.Action action = Hand.Action.None;
|
||||
uint bet = 0;
|
||||
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);
|
||||
} while (true);
|
||||
client.SetTerminateFlag();
|
||||
client.WaitTermination();
|
||||
return 0;
|
||||
|
||||
@@ -66,17 +66,20 @@ namespace pokerth_lib
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GetOpenGamesString()
|
||||
{
|
||||
string outString = "";
|
||||
lock (m_list)
|
||||
{
|
||||
foreach (KeyValuePair<uint, GameInfo> i in m_list)
|
||||
{
|
||||
outString += i.Key;
|
||||
outString += " ";
|
||||
outString += i.Value.Name;
|
||||
outString += '\n';
|
||||
if (i.Value.CurrentMode == GameInfo.Mode.Created)
|
||||
{
|
||||
outString += i.Key;
|
||||
outString += " ";
|
||||
outString += i.Value.Name;
|
||||
outString += '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
return outString;
|
||||
|
||||
@@ -37,7 +37,10 @@ namespace pokerth_lib
|
||||
void ShowFlopCards(int[] cards);
|
||||
void ShowTurnCards(int[] cards);
|
||||
void ShowRiverCards(int[] cards);
|
||||
void AllInState();
|
||||
void ShowCards(string name, int[] cards);
|
||||
void EndOfHandShowCards(string name, int[] cards, int cardsValue, bool allIn);
|
||||
void HandResult();
|
||||
void PlayerWins(string name, uint moneyWon);
|
||||
void Error(string message);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
|
||||
namespace pokerth_lib
|
||||
{
|
||||
@@ -33,6 +35,8 @@ namespace pokerth_lib
|
||||
m_myPlayerId = 0;
|
||||
m_myGameId = 0;
|
||||
m_myName = name;
|
||||
m_joinGameEvent = new AutoResetEvent(false);
|
||||
m_startGameEvent = new AutoResetEvent(false);
|
||||
}
|
||||
|
||||
public GameInfoList GameList
|
||||
@@ -113,6 +117,22 @@ namespace pokerth_lib
|
||||
}
|
||||
}
|
||||
|
||||
public EventWaitHandle JoinGameEvent
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_joinGameEvent;
|
||||
}
|
||||
}
|
||||
|
||||
public EventWaitHandle StartGameEvent
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_startGameEvent;
|
||||
}
|
||||
}
|
||||
|
||||
private GameInfoList m_gameInfoList;
|
||||
private PlayerInfoList m_playerInfoList;
|
||||
private Hand m_curHand;
|
||||
@@ -120,5 +140,7 @@ namespace pokerth_lib
|
||||
private uint m_myPlayerId;
|
||||
private uint m_myGameId;
|
||||
private string m_myName;
|
||||
private EventWaitHandle m_joinGameEvent;
|
||||
private EventWaitHandle m_startGameEvent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,6 +58,31 @@ namespace pokerth_lib
|
||||
SendJoinGame(gameId);
|
||||
}
|
||||
|
||||
public bool WaitJoinGame(int timeout)
|
||||
{
|
||||
return m_data.JoinGameEvent.WaitOne(timeout);
|
||||
}
|
||||
|
||||
public bool WaitStartGame()
|
||||
{
|
||||
return m_data.StartGameEvent.WaitOne();
|
||||
}
|
||||
|
||||
public void MyAction(Hand.Action action, uint bet)
|
||||
{
|
||||
NetPacket a = new NetPacketPlayersAction();
|
||||
a.Properties.Add(
|
||||
NetPacket.PropType.GameState,
|
||||
Convert.ToString((int)m_data.CurHand.CurState));
|
||||
a.Properties.Add(
|
||||
NetPacket.PropType.PlayerAction,
|
||||
Convert.ToString((int)action));
|
||||
a.Properties.Add(
|
||||
NetPacket.PropType.PlayerBet,
|
||||
Convert.ToString(bet));
|
||||
m_sender.Send(a);
|
||||
}
|
||||
|
||||
public void SetTerminateFlag()
|
||||
{
|
||||
m_sender.SetTerminateFlag();
|
||||
|
||||
@@ -29,6 +29,8 @@ namespace pokerth_lib
|
||||
void VisitInitAck(NetPacket p);
|
||||
void VisitGameListNew(NetPacket p);
|
||||
void VisitGameListUpdate(NetPacket p);
|
||||
void VisitGameListPlayerJoined(NetPacket p);
|
||||
void VisitGameListPlayerLeft(NetPacket p);
|
||||
void VisitRetrievePlayerInfo(NetPacket p);
|
||||
void VisitPlayerInfo(NetPacket p);
|
||||
void VisitCreateGame(NetPacket p);
|
||||
@@ -45,7 +47,9 @@ namespace pokerth_lib
|
||||
void VisitDealFlopCards(NetPacket p);
|
||||
void VisitDealTurnCard(NetPacket p);
|
||||
void VisitDealRiverCard(NetPacket p);
|
||||
void VisitAllInShowCards(NetPacket p);
|
||||
void VisitEndOfHandShowCards(NetPacket p);
|
||||
void VisitEndOfHandHideCards(NetPacket p);
|
||||
void VisitError(NetPacket p);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ namespace pokerth_lib
|
||||
|
||||
public enum PropType
|
||||
{
|
||||
ErrorReason,
|
||||
RequestedVersionMajor,
|
||||
RequestedVersionMinor,
|
||||
PlayerId,
|
||||
@@ -153,7 +154,8 @@ namespace pokerth_lib
|
||||
|
||||
public enum RecordPropType
|
||||
{
|
||||
PlayerResult,
|
||||
PlayerResults,
|
||||
PlayerCards,
|
||||
}
|
||||
|
||||
public static NetPacket Create(int type, int size, BinaryReader reader)
|
||||
@@ -171,6 +173,12 @@ namespace pokerth_lib
|
||||
case NetTypeGameListUpdate :
|
||||
tmpPacket = new NetPacketGameListUpdate(size, reader);
|
||||
break;
|
||||
case NetTypeGameListPlayerJoined :
|
||||
tmpPacket = new NetPacketGameListPlayerJoined(size, reader);
|
||||
break;
|
||||
case NetTypeGameListPlayerLeft :
|
||||
tmpPacket = new NetPacketGameListPlayerLeft(size, reader);
|
||||
break;
|
||||
case NetTypeJoinGameAck :
|
||||
tmpPacket = new NetPacketJoinGameAck(size, reader);
|
||||
break;
|
||||
@@ -201,12 +209,18 @@ namespace pokerth_lib
|
||||
case NetTypeDealRiverCard :
|
||||
tmpPacket = new NetPacketDealRiverCard(size, reader);
|
||||
break;
|
||||
case NetTypeAllInShowCards :
|
||||
tmpPacket = new NetPacketAllInShowCards(size, reader);
|
||||
break;
|
||||
case NetTypeEndOfHandShowCards :
|
||||
tmpPacket = new NetPacketEndOfHandShowCards(size, reader);
|
||||
break;
|
||||
case NetTypeEndOfHandHideCards :
|
||||
tmpPacket = new NetPacketEndOfHandHideCards(size, reader);
|
||||
break;
|
||||
case NetTypeError :
|
||||
tmpPacket = new NetPacketError(size, reader);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace pokerth_lib
|
||||
|
||||
tmpRecordList.Add(tmpList);
|
||||
}
|
||||
RecordProperties.Add(RecordPropType.PlayerResult, tmpRecordList);
|
||||
RecordProperties.Add(RecordPropType.PlayerResults, tmpRecordList);
|
||||
}
|
||||
|
||||
public override void Accept(INetPacketVisitor visitor)
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace pokerth_lib
|
||||
BinaryWriter w = new BinaryWriter(memStream);
|
||||
|
||||
w.Write(IPAddress.HostToNetworkOrder((short)Type));
|
||||
w.Write(IPAddress.HostToNetworkOrder((short)8));
|
||||
w.Write(IPAddress.HostToNetworkOrder((short)12));
|
||||
w.Write(IPAddress.HostToNetworkOrder((short)
|
||||
Convert.ToUInt16(Properties[PropType.GameState])));
|
||||
w.Write(IPAddress.HostToNetworkOrder((short)
|
||||
|
||||
@@ -71,6 +71,26 @@ namespace pokerth_lib
|
||||
m_data.GameList.GetGameInfo(id).CurrentMode = mode;
|
||||
}
|
||||
|
||||
public void VisitGameListPlayerJoined(NetPacket p)
|
||||
{
|
||||
GameInfo info = m_data.GameList.GetGameInfo(
|
||||
Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]));
|
||||
List<uint> playerSlots = info.PlayerSlots;
|
||||
playerSlots.Add(
|
||||
Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]));
|
||||
info.PlayerSlots = playerSlots;
|
||||
}
|
||||
|
||||
public void VisitGameListPlayerLeft(NetPacket p)
|
||||
{
|
||||
GameInfo info = m_data.GameList.GetGameInfo(
|
||||
Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]));
|
||||
List<uint> playerSlots = info.PlayerSlots;
|
||||
playerSlots.Remove(
|
||||
Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]));
|
||||
info.PlayerSlots = playerSlots;
|
||||
}
|
||||
|
||||
public void VisitRetrievePlayerInfo(NetPacket p)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
@@ -99,6 +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();
|
||||
}
|
||||
|
||||
public void VisitStartEvent(NetPacket p)
|
||||
@@ -146,6 +167,7 @@ namespace pokerth_lib
|
||||
}
|
||||
|
||||
m_callback.GameStarted(strPlayers);
|
||||
m_data.StartGameEvent.Set();
|
||||
}
|
||||
|
||||
public void VisitHandStart(NetPacket p)
|
||||
@@ -250,10 +272,28 @@ namespace pokerth_lib
|
||||
m_callback.ShowRiverCards(tmpCards);
|
||||
}
|
||||
|
||||
public void VisitAllInShowCards(NetPacket p)
|
||||
{
|
||||
List<Dictionary<NetPacket.PropType, string>> tmpList
|
||||
= p.RecordProperties[NetPacket.RecordPropType.PlayerCards];
|
||||
foreach (Dictionary<NetPacket.PropType, string> i in tmpList)
|
||||
{
|
||||
uint playerId = Convert.ToUInt32(i[NetPacket.PropType.PlayerId]);
|
||||
string name = m_data.PlayerList.GetPlayerInfo(playerId).Name;
|
||||
Player curPlayer = m_data.CurHand.Players[playerId];
|
||||
int[] tmpCards = new int[2];
|
||||
tmpCards[0] = Convert.ToInt32(i[NetPacket.PropType.FirstCard]);
|
||||
tmpCards[1] = Convert.ToInt32(i[NetPacket.PropType.SecondCard]);
|
||||
curPlayer.Cards = tmpCards;
|
||||
|
||||
m_callback.ShowCards(name, tmpCards);
|
||||
}
|
||||
}
|
||||
|
||||
public void VisitEndOfHandShowCards(NetPacket p)
|
||||
{
|
||||
List<Dictionary<NetPacket.PropType, string>> tmpList
|
||||
= p.RecordProperties[NetPacket.RecordPropType.PlayerResult];
|
||||
= p.RecordProperties[NetPacket.RecordPropType.PlayerResults];
|
||||
foreach (Dictionary<NetPacket.PropType, string> i in tmpList)
|
||||
{
|
||||
uint playerId = Convert.ToUInt32(i[NetPacket.PropType.PlayerId]);
|
||||
@@ -299,6 +339,11 @@ namespace pokerth_lib
|
||||
Convert.ToUInt32(p.Properties[NetPacket.PropType.MoneyWon]));
|
||||
}
|
||||
|
||||
public void VisitError(NetPacket p)
|
||||
{
|
||||
m_callback.Error(p.Properties[NetPacket.PropType.ErrorReason]);
|
||||
}
|
||||
|
||||
private PokerTHData m_data;
|
||||
private SenderThread m_sender;
|
||||
private ICallback m_callback;
|
||||
|
||||
Reference in New Issue
Block a user