diff --git a/console/src/ConsoleCallback.cs b/console/src/ConsoleCallback.cs index 48d6405e..ba6b8392 100644 --- a/console/src/ConsoleCallback.cs +++ b/console/src/ConsoleCallback.cs @@ -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); diff --git a/console/src/Program.cs b/console/src/Program.cs index 30668ccd..7a321d46 100644 --- a/console/src/Program.cs +++ b/console/src/Program.cs @@ -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; diff --git a/console/src/lib/ICallback.cs b/console/src/lib/ICallback.cs index 04cda2e5..87748f8c 100644 --- a/console/src/lib/ICallback.cs +++ b/console/src/lib/ICallback.cs @@ -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); } } diff --git a/console/src/lib/net/INetPacketVisitor.cs b/console/src/lib/net/INetPacketVisitor.cs index 39facad7..3bcbd1b9 100644 --- a/console/src/lib/net/INetPacketVisitor.cs +++ b/console/src/lib/net/INetPacketVisitor.cs @@ -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); } } diff --git a/console/src/lib/net/NetPacket.cs b/console/src/lib/net/NetPacket.cs index afeee147..64d73524 100644 --- a/console/src/lib/net/NetPacket.cs +++ b/console/src/lib/net/NetPacket.cs @@ -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; diff --git a/console/src/lib/net/NetPacketAllInShowCards.cs b/console/src/lib/net/NetPacketAllInShowCards.cs new file mode 100644 index 00000000..6807b0e9 --- /dev/null +++ b/console/src/lib/net/NetPacketAllInShowCards.cs @@ -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 . * + ***************************************************************************/ + +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> tmpRecordList = new List>(); + for (int i = 0; i < numPlayerCards; i++) + { + Dictionary tmpList = new Dictionary(); + 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(); + } + } +} diff --git a/console/src/lib/net/NetPacketEndOfGame.cs b/console/src/lib/net/NetPacketEndOfGame.cs new file mode 100644 index 00000000..fe24c76f --- /dev/null +++ b/console/src/lib/net/NetPacketEndOfGame.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketError.cs b/console/src/lib/net/NetPacketError.cs new file mode 100644 index 00000000..4a5f5111 --- /dev/null +++ b/console/src/lib/net/NetPacketError.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketGameListPlayerJoined.cs b/console/src/lib/net/NetPacketGameListPlayerJoined.cs new file mode 100644 index 00000000..9f2332fd --- /dev/null +++ b/console/src/lib/net/NetPacketGameListPlayerJoined.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketGameListPlayerLeft.cs b/console/src/lib/net/NetPacketGameListPlayerLeft.cs new file mode 100644 index 00000000..e368c433 --- /dev/null +++ b/console/src/lib/net/NetPacketGameListPlayerLeft.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketLeaveCurrentGame.cs b/console/src/lib/net/NetPacketLeaveCurrentGame.cs new file mode 100644 index 00000000..088b05a9 --- /dev/null +++ b/console/src/lib/net/NetPacketLeaveCurrentGame.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetParser.cs b/console/src/lib/net/NetParser.cs index 9132fc62..be273621 100644 --- a/console/src/lib/net/NetParser.cs +++ b/console/src/lib/net/NetParser.cs @@ -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 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]);