Console client: Adding proper termination after the game. Adding missing source files.

This commit is contained in:
lotodore
2008-12-26 18:27:48 +00:00
parent 8de66094f3
commit 6f9129818c
12 changed files with 456 additions and 54 deletions
+6 -1
View File
@@ -140,11 +140,16 @@ namespace pokerth_console
Console.WriteLine();
}
public void PlayerWins(string name, uint moneyWon)
public void PlayerWinsHand(string name, uint moneyWon)
{
Console.WriteLine("{0} wins ${1}.", name, moneyWon);
}
public void PlayerWinsGame(string name)
{
Console.WriteLine("\n{0} wins the game.", name);
}
public void Error(string message)
{
Console.WriteLine("Error: " + message);
+69 -50
View File
@@ -21,27 +21,15 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using pokerth_lib;
namespace pokerth_console
{
class Program
{
static int Main(string[] args)
static void ChooseOpenGame(Client client, PokerTHData data)
{
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(name);
ConsoleCallback callback = new ConsoleCallback();
Client client = new Client(settings, data, callback);
client.Connect();
client.Start();
Thread.Sleep(2000);
string input;
bool joined = false;
do
@@ -71,49 +59,80 @@ namespace pokerth_console
Console.WriteLine("Could not join game.");
} while (!joined);
Console.WriteLine("Waiting for admin to start the game...");
}
static void GameLoop(Client client, PokerTHData data)
{
string input;
do
{
do
while (Console.KeyAvailable)
Console.ReadKey();
input = Console.ReadLine();
if (input.Length > 0)
{
while (Console.KeyAvailable)
Console.ReadKey();
input = Console.ReadLine();
} while (input == "");
Hand.Action action = Hand.Action.None;
uint bet = 0;
try
{
switch (input[0])
Hand.Action action = Hand.Action.None;
uint bet = 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);
}
catch (FormatException)
{
}
} while (true);
} while (data.JoinedGame);
}
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(name);
ConsoleCallback callback = new ConsoleCallback();
Client client = new Client(settings, data, callback);
try
{
client.Connect();
}
catch (SocketException)
{
Console.WriteLine("Unable to connect to server.");
return 1;
}
client.Start();
Thread.Sleep(2000);
ChooseOpenGame(client, data);
GameLoop(client, data);
client.SetTerminateFlag();
client.WaitTermination();
return 0;
+2 -1
View File
@@ -41,7 +41,8 @@ namespace pokerth_lib
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 PlayerWinsHand(string name, uint moneyWon);
void PlayerWinsGame(string name);
void Error(string message);
}
}
+2
View File
@@ -36,6 +36,7 @@ namespace pokerth_lib
void VisitCreateGame(NetPacket p);
void VisitJoinGame(NetPacket p);
void VisitJoinGameAck(NetPacket p);
void VisitLeaveCurrentGame(NetPacket p);
void VisitStartEvent(NetPacket p);
void VisitStartEventAck(NetPacket p);
void VisitGameStart(NetPacket p);
@@ -50,6 +51,7 @@ namespace pokerth_lib
void VisitAllInShowCards(NetPacket p);
void VisitEndOfHandShowCards(NetPacket p);
void VisitEndOfHandHideCards(NetPacket p);
void VisitEndOfGame(NetPacket p);
void VisitError(NetPacket p);
}
}
+3
View File
@@ -218,6 +218,9 @@ namespace pokerth_lib
case NetTypeEndOfHandHideCards :
tmpPacket = new NetPacketEndOfHandHideCards(size, reader);
break;
case NetTypeEndOfGame :
tmpPacket = new NetPacketEndOfGame(size, reader);
break;
case NetTypeError :
tmpPacket = new NetPacketError(size, reader);
break;
@@ -0,0 +1,71 @@
/***************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
class NetPacketAllInShowCards : NetPacket
{
public NetPacketAllInShowCards()
: base(NetPacket.NetTypeAllInShowCards)
{
}
public NetPacketAllInShowCards(int size, BinaryReader r)
: base(NetPacket.NetTypeAllInShowCards)
{
if (size < 16)
throw new NetPacketException("NetPacketAllInShowCards invalid size.");
int numPlayerCards = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
r.ReadUInt16(); //reserved
List<Dictionary<PropType, string>> tmpRecordList = new List<Dictionary<PropType, string>>();
for (int i = 0; i < numPlayerCards; i++)
{
Dictionary<PropType, string> tmpList = new Dictionary<PropType, string>();
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())));
tmpRecordList.Add(tmpList);
}
RecordProperties.Add(RecordPropType.PlayerCards, tmpRecordList);
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitAllInShowCards(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+56
View File
@@ -0,0 +1,56 @@
/***************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
class NetPacketEndOfGame : NetPacket
{
public NetPacketEndOfGame()
: base(NetPacket.NetTypeEndOfGame)
{
}
public NetPacketEndOfGame(int size, BinaryReader r)
: base(NetPacket.NetTypeEndOfGame)
{
if (size != 8)
throw new NetPacketException("NetPacketEndOfGame invalid size.");
Properties.Add(PropType.PlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitEndOfGame(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+56
View File
@@ -0,0 +1,56 @@
/***************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
class NetPacketError : NetPacket
{
public NetPacketError()
: base(NetPacket.NetTypeError)
{
}
public NetPacketError(int size, BinaryReader r)
: base(NetPacket.NetTypeError)
{
if (size != 8)
throw new NetPacketException("NetPacketError invalid size.");
Properties.Add(PropType.ErrorReason,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitError(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,58 @@
/***************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
class NetPacketGameListPlayerJoined : NetPacket
{
public NetPacketGameListPlayerJoined()
: base(NetPacket.NetTypeGameListPlayerJoined)
{
}
public NetPacketGameListPlayerJoined(int size, BinaryReader r)
: base(NetPacket.NetTypeGameListPlayerJoined)
{
if (size != 12)
throw new NetPacketException("NetPacketGameListPlayerJoined invalid size.");
Properties.Add(PropType.GameId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.PlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitGameListPlayerJoined(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,58 @@
/***************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
class NetPacketGameListPlayerLeft : NetPacket
{
public NetPacketGameListPlayerLeft()
: base(NetPacket.NetTypeGameListPlayerLeft)
{
}
public NetPacketGameListPlayerLeft(int size, BinaryReader r)
: base(NetPacket.NetTypeGameListPlayerLeft)
{
if (size != 12)
throw new NetPacketException("NetPacketGameListPlayerLeft invalid size.");
Properties.Add(PropType.GameId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.PlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitGameListPlayerLeft(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,54 @@
/***************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
class NetPacketLeaveCurrentGame : NetPacket
{
public NetPacketLeaveCurrentGame()
: base(NetPacket.NetTypeLeaveCurrentGame)
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitLeaveCurrentGame(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
w.Write(IPAddress.HostToNetworkOrder((short)Type));
w.Write(IPAddress.HostToNetworkOrder((short)8));
w.Write(IPAddress.HostToNetworkOrder((int)0)); // reserved
return memStream.ToArray();
}
}
}
+21 -2
View File
@@ -122,6 +122,11 @@ namespace pokerth_lib
m_data.JoinedGame = true;
}
public void VisitLeaveCurrentGame(NetPacket p)
{
throw new NotImplementedException();
}
public void VisitStartEvent(NetPacket p)
{
// Request player names for player ids.
@@ -315,6 +320,7 @@ namespace pokerth_lib
m_callback.EndOfHandShowCards(name, tmpCards, curPlayer.CardsValue, false);
}
m_callback.HandResult();
foreach (Dictionary<NetPacket.PropType, string> i in tmpList)
{
uint moneyWon = Convert.ToUInt32(i[NetPacket.PropType.MoneyWon]);
@@ -322,7 +328,7 @@ namespace pokerth_lib
{
uint playerId = Convert.ToUInt32(i[NetPacket.PropType.PlayerId]);
string name = m_data.PlayerList.GetPlayerInfo(playerId).Name;
m_callback.PlayerWins(name, moneyWon);
m_callback.PlayerWinsHand(name, moneyWon);
}
}
}
@@ -334,11 +340,24 @@ namespace pokerth_lib
Player curPlayer = m_data.CurHand.Players[playerId];
curPlayer.Money = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerMoney]);
m_callback.PlayerWins(
m_callback.HandResult();
m_callback.PlayerWinsHand(
name,
Convert.ToUInt32(p.Properties[NetPacket.PropType.MoneyWon]));
}
public void VisitEndOfGame(NetPacket p)
{
uint playerId = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]);
string name = m_data.PlayerList.GetPlayerInfo(playerId).Name;
m_callback.PlayerWinsGame(name);
NetPacket leave = new NetPacketLeaveCurrentGame();
m_sender.Send(leave);
m_data.JoinedGame = false;
}
public void VisitError(NetPacket p)
{
m_callback.Error(p.Properties[NetPacket.PropType.ErrorReason]);