diff --git a/console/src/ConsoleCallback.cs b/console/src/ConsoleCallback.cs index 99885324..c7eb5283 100644 --- a/console/src/ConsoleCallback.cs +++ b/console/src/ConsoleCallback.cs @@ -56,6 +56,27 @@ namespace pokerth_console h.Players[h.MyPlayerId].Money); } + public void MyTurn(Hand.State state) + { + Console.WriteLine("--> {0}: Your turn.", + Log.StateToString(state)); + } + + public void PlayersTurn(Hand.State state, string player) + { + Console.WriteLine("{0}: {1}'s turn.", + Log.StateToString(state), + player); + } + + public void ActionDone(string name, Hand.Action action, uint totalBet, uint money, uint highestSet, uint minimumRaise) + { + Console.WriteLine("{0} acts: {1}. Total bet: {2}. Money left: {3}", + name, Log.ActionToString(action), totalBet, money); + Console.WriteLine("Highest set: {0}. Minimum raise: {1}", + highestSet, minimumRaise); + } + public void Error(string message) { Console.WriteLine("Error: " + message); diff --git a/console/src/lib/Hand.cs b/console/src/lib/Hand.cs index b3437920..59cd5663 100644 --- a/console/src/lib/Hand.cs +++ b/console/src/lib/Hand.cs @@ -35,6 +35,17 @@ namespace pokerth_lib River } + public enum Action + { + None = 0, + Fold, + Check, + Call, + Bet, + Raise, + AllIn + } + public Hand(Dictionary players, uint myPlayerId, uint smallBlind) { m_mutex = new Object(); diff --git a/console/src/lib/ICallback.cs b/console/src/lib/ICallback.cs index ef04ebc3..233eb985 100644 --- a/console/src/lib/ICallback.cs +++ b/console/src/lib/ICallback.cs @@ -10,6 +10,9 @@ namespace pokerth_lib void JoinedGame(string name); void GameStarted(List players); void HandStarted(Hand h); + void MyTurn(Hand.State state); + void PlayersTurn(Hand.State state, string player); + void ActionDone(string name, Hand.Action action, uint totalBet, uint money, uint highestSet, uint minimumRaise); void Error(string message); } } diff --git a/console/src/lib/Log.cs b/console/src/lib/Log.cs index a44f80d7..c49a63d9 100644 --- a/console/src/lib/Log.cs +++ b/console/src/lib/Log.cs @@ -77,5 +77,65 @@ namespace pokerth_lib } return cardString; } + + public static string StateToString(Hand.State state) + { + string stateString; + + switch (state) + { + case Hand.State.Preflop : + stateString = "Preflop"; + break; + case Hand.State.Flop : + stateString = "Flop"; + break; + case Hand.State.Turn : + stateString = "Turn"; + break; + case Hand.State.River : + stateString = "River"; + break; + default : + stateString = "Invalid state"; + break; + } + + return stateString; + } + + public static string ActionToString(Hand.Action action) + { + string actionString; + + switch (action) + { + case Hand.Action.None: + actionString = "None"; + break; + case Hand.Action.Fold: + actionString = "Fold"; + break; + case Hand.Action.Check: + actionString = "Check"; + break; + case Hand.Action.Call: + actionString = "Call"; + break; + case Hand.Action.Bet: + actionString = "Bet"; + break; + case Hand.Action.Raise: + actionString = "Raise"; + break; + case Hand.Action.AllIn: + actionString = "All in"; + break; + default : + actionString = "Unknown action"; + break; + } + return actionString; + } } } diff --git a/console/src/lib/Player.cs b/console/src/lib/Player.cs index d4322b51..83a6f9bd 100644 --- a/console/src/lib/Player.cs +++ b/console/src/lib/Player.cs @@ -9,6 +9,7 @@ namespace pokerth_lib public Player() { m_mutex = new Object(); + m_curAction = Hand.Action.None; } public int[] Cards @@ -30,6 +31,24 @@ namespace pokerth_lib } } + public Hand.Action CurAction + { + get + { + lock (m_mutex) + { + return m_curAction; + } + } + set + { + lock (m_mutex) + { + m_curAction = value; + } + } + } + public uint Money { get @@ -48,7 +67,7 @@ namespace pokerth_lib } } - private uint TotalBet + public uint TotalBet { get { @@ -68,6 +87,7 @@ namespace pokerth_lib private Object m_mutex; private int[] m_cards; + private Hand.Action m_curAction; private uint m_money; private uint m_totalBet; } diff --git a/console/src/lib/net/INetPacketVisitor.cs b/console/src/lib/net/INetPacketVisitor.cs index 8b8f0c64..0d25af1f 100644 --- a/console/src/lib/net/INetPacketVisitor.cs +++ b/console/src/lib/net/INetPacketVisitor.cs @@ -23,23 +23,24 @@ using System.Text; namespace pokerth_lib { - interface INetPacketVisitor + public interface INetPacketVisitor { - void VisitInit(NetPacketInit p); - void VisitInitAck(NetPacketInitAck p); - void VisitGameListNew(NetPacketGameListNew p); - void VisitGameListUpdate(NetPacketGameListUpdate p); - void VisitRetrievePlayerInfo(NetPacketRetrievePlayerInfo p); - void VisitPlayerInfo(NetPacketPlayerInfo p); - void VisitJoinGame(NetPacketJoinGame p); - void VisitJoinGameAck(NetPacketJoinGameAck p); - void VisitStartEvent(NetPacketStartEvent p); - void VisitStartEventAck(NetPacketStartEventAck p); - void VisitGameStart(NetPacketGameStart p); - void VisitHandStart(NetPacketHandStart p); - void VisitPlayersTurn(NetPacketPlayersTurn p); - void VisitPlayersAction(NetPacketPlayersAction p); - void VisitPlayersActionDone(NetPacketPlayersActionDone p); - void VisitPlayersActionRejected(NetPacketPlayersActionRejected p); + void VisitInit(NetPacket p); + void VisitInitAck(NetPacket p); + void VisitGameListNew(NetPacket p); + void VisitGameListUpdate(NetPacket p); + void VisitRetrievePlayerInfo(NetPacket p); + void VisitPlayerInfo(NetPacket p); + void VisitCreateGame(NetPacket p); + void VisitJoinGame(NetPacket p); + void VisitJoinGameAck(NetPacket p); + void VisitStartEvent(NetPacket p); + void VisitStartEventAck(NetPacket p); + void VisitGameStart(NetPacket p); + void VisitHandStart(NetPacket p); + void VisitPlayersTurn(NetPacket p); + void VisitPlayersAction(NetPacket p); + void VisitPlayersActionDone(NetPacket p); + void VisitPlayersActionRejected(NetPacket p); } } diff --git a/console/src/lib/net/NetPacket.cs b/console/src/lib/net/NetPacket.cs index aa5440cc..ee166355 100644 --- a/console/src/lib/net/NetPacket.cs +++ b/console/src/lib/net/NetPacket.cs @@ -26,7 +26,7 @@ using System.IO; namespace pokerth_lib { - abstract class NetPacket + public abstract class NetPacket { public const int NetTypeInit = 0x0001; public const int NetTypeInitAck = 0x0002; @@ -133,7 +133,7 @@ namespace pokerth_lib ActionRejectReason, } - public enum ListPropertyType + public enum ListPropType { PlayerSlots, ManualBlindSlots, @@ -172,13 +172,16 @@ namespace pokerth_lib case NetTypePlayersTurn : tmpPacket = new NetPacketPlayersTurn(size, reader); break; + case NetTypePlayersActionDone : + tmpPacket = new NetPacketPlayersActionDone(size, reader); + break; default: break; } return tmpPacket; } - public void ScanGameInfoBlock(BinaryReader r) + protected void ScanGameInfoBlock(BinaryReader r) { Properties.Add(PropType.MaxNumPlayers, Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16()))); @@ -206,14 +209,46 @@ namespace pokerth_lib List blindSlots = new List(); for (int i = 0; i < numManualBlinds; i++) blindSlots.Add(Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); - ListProperties.Add(ListPropertyType.ManualBlindSlots, blindSlots); + ListProperties.Add(ListPropType.ManualBlindSlots, blindSlots); + } + + protected void WriteGameInfoBlock(BinaryWriter w) + { + w.Write(IPAddress.HostToNetworkOrder((short) + Convert.ToUInt16(Properties[PropType.MaxNumPlayers]))); + w.Write(IPAddress.HostToNetworkOrder((short) + Convert.ToUInt16(Properties[PropType.RaiseIntervalMode]))); + w.Write(IPAddress.HostToNetworkOrder((short) + Convert.ToUInt16(Properties[PropType.RaiseSmallBlindInterval]))); + w.Write(IPAddress.HostToNetworkOrder((short) + Convert.ToUInt16(Properties[PropType.RaiseMode]))); + w.Write(IPAddress.HostToNetworkOrder((short) + Convert.ToUInt16(Properties[PropType.EndRaiseMode]))); + w.Write(IPAddress.HostToNetworkOrder((short) + ListProperties[ListPropType.ManualBlindSlots].Count)); + w.Write(IPAddress.HostToNetworkOrder((short) + Convert.ToUInt16(Properties[PropType.ProposedGuiSpeed]))); + w.Write(IPAddress.HostToNetworkOrder((short) + Convert.ToUInt16(Properties[PropType.PlayerActionTimeout]))); + w.Write(IPAddress.HostToNetworkOrder((int) + Convert.ToUInt32(Properties[PropType.FirstSmallBlind]))); + w.Write(IPAddress.HostToNetworkOrder((int) + Convert.ToUInt32(Properties[PropType.EndRaiseSmallBlindValue]))); + w.Write(IPAddress.HostToNetworkOrder((int) + Convert.ToUInt32(Properties[PropType.StartMoney]))); + + foreach (string s in ListProperties[ListPropType.ManualBlindSlots]) + { + w.Write(IPAddress.HostToNetworkOrder((int) + Convert.ToUInt32(s))); + } } public NetPacket(int type) { m_type = type; m_properties = new Dictionary(); - m_listProperties = new Dictionary>(); + m_listProperties = new Dictionary>(); } public Dictionary Properties @@ -228,7 +263,7 @@ namespace pokerth_lib } } - public Dictionary> ListProperties + public Dictionary> ListProperties { set { @@ -259,6 +294,6 @@ namespace pokerth_lib private int m_type; private Dictionary m_properties; - private Dictionary> m_listProperties; + private Dictionary> m_listProperties; } } diff --git a/console/src/lib/net/NetPacketException.cs b/console/src/lib/net/NetPacketException.cs index b21e2abd..96d906c6 100644 --- a/console/src/lib/net/NetPacketException.cs +++ b/console/src/lib/net/NetPacketException.cs @@ -23,7 +23,7 @@ using System.Text; namespace pokerth_lib { - class NetPacketException : Exception + public class NetPacketException : Exception { public NetPacketException(string message) : base(message) diff --git a/console/src/lib/net/NetPacketGameListNew.cs b/console/src/lib/net/NetPacketGameListNew.cs index 399ca3a1..7ab620d4 100644 --- a/console/src/lib/net/NetPacketGameListNew.cs +++ b/console/src/lib/net/NetPacketGameListNew.cs @@ -98,7 +98,7 @@ namespace pokerth_lib List playerSlots = new List(); for (int i = 0; i < curNumPlayers; i++) playerSlots.Add(Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); - ListProperties.Add(ListPropertyType.PlayerSlots, playerSlots); + ListProperties.Add(ListPropType.PlayerSlots, playerSlots); } public override void Accept(INetPacketVisitor visitor) diff --git a/console/src/lib/net/NetPacketGameStart.cs b/console/src/lib/net/NetPacketGameStart.cs index 5c297125..be3c3736 100644 --- a/console/src/lib/net/NetPacketGameStart.cs +++ b/console/src/lib/net/NetPacketGameStart.cs @@ -58,7 +58,7 @@ namespace pokerth_lib List playerSlots = new List(); for (int i = 0; i < curNumPlayers; i++) playerSlots.Add(Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); - ListProperties.Add(ListPropertyType.PlayerSlots, playerSlots); + ListProperties.Add(ListPropType.PlayerSlots, playerSlots); } public override void Accept(INetPacketVisitor visitor) diff --git a/console/src/lib/net/NetParser.cs b/console/src/lib/net/NetParser.cs index 78566b92..3ed5a7c8 100644 --- a/console/src/lib/net/NetParser.cs +++ b/console/src/lib/net/NetParser.cs @@ -32,31 +32,35 @@ namespace pokerth_lib m_callback = callback; } - public void VisitInit(NetPacketInit p) + public void VisitInit(NetPacket p) { throw new NotImplementedException(); } - public void VisitInitAck(NetPacketInitAck p) + public void VisitInitAck(NetPacket p) { m_data.MyPlayerId = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); + // Add self to list. + m_data.PlayerList.AddPlayerInfo(new PlayerInfo( + m_data.MyPlayerId, + m_data.MyName)); m_callback.InitDone(); } - public void VisitGameListNew(NetPacketGameListNew p) + public void VisitGameListNew(NetPacket p) { // Add game to list. m_data.GameList.AddGameInfo(new GameInfo( Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]), p.Properties[NetPacket.PropType.GameName], (GameInfo.Mode)Convert.ToInt32(p.Properties[NetPacket.PropType.GameMode]), - p.ListProperties[NetPacket.ListPropertyType.PlayerSlots]. + p.ListProperties[NetPacket.ListPropType.PlayerSlots]. ConvertAll(Convert.ToUInt32), Convert.ToUInt32(p.Properties[NetPacket.PropType.StartMoney]))); } - public void VisitGameListUpdate(NetPacketGameListUpdate p) + public void VisitGameListUpdate(NetPacket p) { GameInfo.Mode mode = (GameInfo.Mode)Convert.ToInt32(p.Properties[NetPacket.PropType.GameMode]); @@ -67,12 +71,12 @@ namespace pokerth_lib m_data.GameList.GetGameInfo(id).CurrentMode = mode; } - public void VisitRetrievePlayerInfo(NetPacketRetrievePlayerInfo p) + public void VisitRetrievePlayerInfo(NetPacket p) { throw new NotImplementedException(); } - public void VisitPlayerInfo(NetPacketPlayerInfo p) + public void VisitPlayerInfo(NetPacket p) { // Add player to list. m_data.PlayerList.AddPlayerInfo(new PlayerInfo( @@ -80,19 +84,24 @@ namespace pokerth_lib p.Properties[NetPacket.PropType.PlayerName])); } - public void VisitJoinGame(NetPacketJoinGame p) + public void VisitCreateGame(NetPacket p) { throw new NotImplementedException(); } - public void VisitJoinGameAck(NetPacketJoinGameAck p) + public void VisitJoinGame(NetPacket p) + { + throw new NotImplementedException(); + } + + public void VisitJoinGameAck(NetPacket p) { m_data.MyGameId = Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]); m_callback.JoinedGame(m_data.GameList.GetGameInfo(m_data.MyGameId).Name); } - public void VisitStartEvent(NetPacketStartEvent p) + public void VisitStartEvent(NetPacket p) { // Request player names for player ids. List playerSlots = m_data.GameList.GetGameInfo(m_data.MyGameId).PlayerSlots; @@ -110,17 +119,17 @@ namespace pokerth_lib m_sender.Send(ack); } - public void VisitStartEventAck(NetPacketStartEventAck p) + public void VisitStartEventAck(NetPacket p) { throw new NotImplementedException(); } - public void VisitGameStart(NetPacketGameStart p) + public void VisitGameStart(NetPacket p) { // Generate player list, for gui and as hand data. List strPlayers = new List(); - List slots = p.ListProperties[NetPacket.ListPropertyType.PlayerSlots]. + List slots = p.ListProperties[NetPacket.ListPropType.PlayerSlots]. ConvertAll(Convert.ToUInt32); m_players = new Dictionary(); @@ -128,8 +137,6 @@ namespace pokerth_lib { if (m_data.PlayerList.HasPlayer(i)) strPlayers.Add(m_data.PlayerList.GetPlayerInfo(i).Name); - else if (i == m_data.MyPlayerId) - strPlayers.Add(m_data.MyName); else strPlayers.Add(Convert.ToString(i)); // Set player data. @@ -141,7 +148,7 @@ namespace pokerth_lib m_callback.GameStarted(strPlayers); } - public void VisitHandStart(NetPacketHandStart p) + public void VisitHandStart(NetPacket p) { int[] tmpCards = new int[2]; tmpCards[0] = @@ -156,22 +163,41 @@ namespace pokerth_lib m_callback.HandStarted(m_data.CurHand); } - public void VisitPlayersTurn(NetPacketPlayersTurn p) + public void VisitPlayersTurn(NetPacket p) { - // TODO + uint curPlayer = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); + Hand.State state = (Hand.State)Convert.ToUInt16(p.Properties[NetPacket.PropType.GameState]); + if (curPlayer == m_data.MyPlayerId) + m_callback.MyTurn(state); + else + m_callback.PlayersTurn(state, m_data.PlayerList.GetPlayerInfo(curPlayer).Name); } - public void VisitPlayersAction(NetPacketPlayersAction p) + public void VisitPlayersAction(NetPacket p) { throw new NotImplementedException(); } - public void VisitPlayersActionDone(NetPacketPlayersActionDone p) + public void VisitPlayersActionDone(NetPacket p) { - // TODO + uint playerId = Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]); + Player curPlayer = m_data.CurHand.Players[playerId]; + curPlayer.CurAction = + (Hand.Action)Convert.ToUInt16(p.Properties[NetPacket.PropType.PlayerAction]); + curPlayer.Money = + Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerMoney]); + curPlayer.TotalBet = + Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerBetTotal]); + m_callback.ActionDone( + m_data.PlayerList.GetPlayerInfo(playerId).Name, + curPlayer.CurAction, + curPlayer.TotalBet, + curPlayer.Money, + Convert.ToUInt32(p.Properties[NetPacket.PropType.HighestSet]), + Convert.ToUInt32(p.Properties[NetPacket.PropType.MinimumRaise])); } - public void VisitPlayersActionRejected(NetPacketPlayersActionRejected p) + public void VisitPlayersActionRejected(NetPacket p) { // TODO }