Added additional packet for showing cards when allInCondition is true.

This commit is contained in:
lotodore
2007-06-05 23:04:51 +00:00
parent 5562feb5ce
commit ce6f489fba
4 changed files with 201 additions and 7 deletions
+139 -2
View File
@@ -40,8 +40,9 @@ using namespace std;
#define NET_TYPE_DEAL_FLOP_CARDS 0x000B
#define NET_TYPE_DEAL_TURN_CARD 0x000C
#define NET_TYPE_DEAL_RIVER_CARD 0x000D
#define NET_TYPE_END_OF_HAND_SHOW_CARDS 0x000E
#define NET_TYPE_END_OF_HAND_HIDE_CARDS 0x000F
#define NET_TYPE_ALL_IN_SHOW_CARDS 0x000E
#define NET_TYPE_END_OF_HAND_SHOW_CARDS 0x000F
#define NET_TYPE_END_OF_HAND_HIDE_CARDS 0x0010
#define NET_TYPE_SEND_CHAT_TEXT 0x0200
#define NET_TYPE_CHAT_TEXT 0x0201
@@ -196,6 +197,21 @@ struct GCC_PACKED NetPacketDealRiverCardData
u_int16_t reserved;
};
struct GCC_PACKED NetPacketAllInShowCardsData
{
NetPacketHeader head;
u_int16_t numberOfPlayerCards;
u_int16_t reserved;
};
struct GCC_PACKED PlayerCardsData
{
u_int16_t playerId;
u_int16_t card1;
u_int16_t card2;
u_int16_t reserved;
};
struct GCC_PACKED NetPacketEndOfHandShowCardsData
{
NetPacketHeader head;
@@ -314,6 +330,9 @@ NetPacket::Create(char *data, unsigned &dataSize)
case NET_TYPE_DEAL_RIVER_CARD:
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketDealRiverCard);
break;
case NET_TYPE_ALL_IN_SHOW_CARDS:
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketAllInShowCards);
break;
case NET_TYPE_END_OF_HAND_SHOW_CARDS:
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketEndOfHandShowCards);
break;
@@ -488,6 +507,12 @@ NetPacket::ToNetPacketDealRiverCard() const
return NULL;
}
const NetPacketAllInShowCards *
NetPacket::ToNetPacketAllInShowCards() const
{
return NULL;
}
const NetPacketEndOfHandShowCards *
NetPacket::ToNetPacketEndOfHandShowCards() const
{
@@ -1544,6 +1569,118 @@ NetPacketDealRiverCard::Check(const NetPacketHeader* data) const
//-----------------------------------------------------------------------------
NetPacketAllInShowCards::NetPacketAllInShowCards()
: NetPacket(NET_TYPE_ALL_IN_SHOW_CARDS, sizeof(NetPacketAllInShowCardsData))
{
}
NetPacketAllInShowCards::~NetPacketAllInShowCards()
{
}
boost::shared_ptr<NetPacket>
NetPacketAllInShowCards::Clone() const
{
boost::shared_ptr<NetPacket> newPacket(new NetPacketAllInShowCards);
try
{
newPacket->SetRawData(GetRawData());
} catch (const NetException &)
{
// Need to return the new packet anyway.
}
return newPacket;
}
void
NetPacketAllInShowCards::SetData(const NetPacketAllInShowCards::Data &inData)
{
u_int16_t numPlayerCards = (u_int16_t)inData.playerCards.size();
if (!numPlayerCards || numPlayerCards > MAX_NUM_PLAYER_CARDS)
throw NetException(ERR_NET_INVALID_PLAYER_CARDS, 0);
// Resize the packet so that the data fits in.
Resize((u_int16_t)
(sizeof(NetPacketAllInShowCardsData) + numPlayerCards * sizeof(PlayerCardsData)));
NetPacketAllInShowCardsData *tmpData = (NetPacketAllInShowCardsData *)GetRawData();
assert(tmpData);
tmpData->numberOfPlayerCards = htons(numPlayerCards);
PlayerCardsList::const_iterator i = inData.playerCards.begin();
PlayerCardsList::const_iterator end = inData.playerCards.end();
// Copy the player cards data to continous memory
PlayerCardsData *curPlayerCardsData =
(PlayerCardsData *)((char *)tmpData + sizeof(NetPacketAllInShowCardsData));
while (i != end)
{
curPlayerCardsData->playerId = htons((*i).playerId);
curPlayerCardsData->card1 = htons((*i).cards[0]);
curPlayerCardsData->card2 = htons((*i).cards[1]);
++curPlayerCardsData;
++i;
}
}
void
NetPacketAllInShowCards::GetData(NetPacketAllInShowCards::Data &outData) const
{
NetPacketAllInShowCardsData *tmpData = (NetPacketAllInShowCardsData *)GetRawData();
assert(tmpData);
outData.playerCards.clear();
u_int16_t numPlayerCards = ntohs(tmpData->numberOfPlayerCards);
PlayerCardsData *curPlayerCardsData =
(PlayerCardsData *)((char *)tmpData + sizeof(NetPacketAllInShowCardsData));
// Store all available player cards.
for (int i = 0; i < numPlayerCards; i++)
{
PlayerCards tmpPlayerCards;
tmpPlayerCards.playerId = ntohs(curPlayerCardsData->playerId);
tmpPlayerCards.cards[0] = ntohs(curPlayerCardsData->card1);
tmpPlayerCards.cards[1] = ntohs(curPlayerCardsData->card2);
outData.playerCards.push_back(tmpPlayerCards);
++curPlayerCardsData;
}
}
const NetPacketAllInShowCards *
NetPacketAllInShowCards::ToNetPacketAllInShowCards() const
{
return this;
}
void
NetPacketAllInShowCards::Check(const NetPacketHeader* data) const
{
assert(data);
u_int16_t dataLen = ntohs(data->length);
if (dataLen < sizeof(NetPacketAllInShowCardsData))
{
throw NetException(ERR_SOCK_INVALID_PACKET, 0);
}
NetPacketAllInShowCardsData *tmpData = (NetPacketAllInShowCardsData *)data;
int numPlayerCards = ntohs(tmpData->numberOfPlayerCards);
if (dataLen !=
sizeof(NetPacketAllInShowCardsData)
+ numPlayerCards * sizeof(PlayerCardsData))
{
throw NetException(ERR_SOCK_INVALID_PACKET, 0);
}
// TODO: semantic checks within PlayerCardsData
}
//-----------------------------------------------------------------------------
NetPacketEndOfHandShowCards::NetPacketEndOfHandShowCards()
: NetPacket(NET_TYPE_END_OF_HAND_SHOW_CARDS, sizeof(NetPacketEndOfHandShowCardsData))
{
+34
View File
@@ -35,6 +35,7 @@
#define MAX_PASSWORD_SIZE 64
#define MAX_CHAT_TEXT_SIZE 128
#define MAX_NUM_PLAYER_RESULTS MAX_NUMBER_OF_PLAYERS
#define MAX_NUM_PLAYER_CARDS MAX_NUMBER_OF_PLAYERS
struct NetPacketHeader;
@@ -52,6 +53,7 @@ class NetPacketPlayersActionRejected;
class NetPacketDealFlopCards;
class NetPacketDealTurnCard;
class NetPacketDealRiverCard;
class NetPacketAllInShowCards;
class NetPacketEndOfHandShowCards;
class NetPacketEndOfHandHideCards;
class NetPacketSendChatText;
@@ -88,6 +90,7 @@ public:
virtual const NetPacketDealFlopCards *ToNetPacketDealFlopCards() const;
virtual const NetPacketDealTurnCard *ToNetPacketDealTurnCard() const;
virtual const NetPacketDealRiverCard *ToNetPacketDealRiverCard() const;
virtual const NetPacketAllInShowCards *ToNetPacketAllInShowCards() const;
virtual const NetPacketEndOfHandShowCards *ToNetPacketEndOfHandShowCards() const;
virtual const NetPacketEndOfHandHideCards *ToNetPacketEndOfHandHideCards() const;
virtual const NetPacketSendChatText *ToNetPacketSendChatText() const;
@@ -428,6 +431,37 @@ protected:
virtual void Check(const NetPacketHeader* data) const;
};
class NetPacketAllInShowCards : public NetPacket
{
public:
struct PlayerCards
{
u_int16_t playerId;
u_int16_t cards[2];
};
typedef std::list<PlayerCards> PlayerCardsList;
struct Data
{
PlayerCardsList playerCards;
};
NetPacketAllInShowCards();
virtual ~NetPacketAllInShowCards();
virtual boost::shared_ptr<NetPacket> Clone() const;
void SetData(const Data &inData);
void GetData(Data &outData) const;
virtual const NetPacketAllInShowCards *ToNetPacketAllInShowCards() const;
protected:
virtual void Check(const NetPacketHeader* data) const;
};
class NetPacketEndOfHandShowCards : public NetPacket
{
public:
+4 -3
View File
@@ -48,9 +48,10 @@
#define ERR_NET_INVALID_PASSWORD_STR 105
#define ERR_NET_PLAYER_NAME_IN_USE 106
#define ERR_NET_INVALID_PLAYER_NAME 107
#define ERR_NET_INVALID_PLAYER_RESULTS 108
#define ERR_NET_INVALID_CHAT_TEXT 109
#define ERR_NET_UNKNOWN_PLAYER_ID 110
#define ERR_NET_INVALID_PLAYER_CARDS 108
#define ERR_NET_INVALID_PLAYER_RESULTS 109
#define ERR_NET_INVALID_CHAT_TEXT 110
#define ERR_NET_UNKNOWN_PLAYER_ID 111
// This is an internal message which is not reported.
#define MSG_SOCK_INTERNAL_PENDING 0