//红包结构体
struct tagRedPacket {
tagRedPacket() { ReSet(); }
void ReSet() { ZeroMemory(this, sizeof(tagRedPacket)); }
LONGLONG lTime; //发红包的时间
WORD wId; //红包id
DWORD dwUserId; //发红包的用户id
DWORD dwCellScore; //红包金额
BYTE cbRay; //雷
DWORD dwRayCompensation; //中雷赔偿金额
BYTE cbRayCount; //中雷次数
BYTE cbTimeOut; //过期标识
BYTE cbCount; //红包数量
CMD_S_PlayerInfo PlayerInfo; //发送玩家信息
tagOpenRedPacket wRedPacketUser[GAME_PLAYER]; //开红包数组
};
std::map<WORD, tagRedPacket*> m_RedPacketMgr;
tagRedPacket * RedPacketMgr::NewRedPacket()
{
m_nMaxId++;
for (std::map<WORD,tagRedPacket*>::iterator it = m_RedPacketMgr.begin(); it != m_RedPacketMgr.end();++it)
{
if (NULL == it->second) {
m_RedPacketMgr.erase(it);
break;
}
if(it->second->cbTimeOut){
it->second->ReSet();
it->second->wId = m_nMaxId;
return it->second;
}
}
tagRedPacket * pRedPacket = new tagRedPacket();
pRedPacket->wId = m_nMaxId;
return pRedPacket;
}
void RedPacketMgr::AddRedPacket(tagRedPacket *RedPacket)
{
if(NULL == RedPacket)
return;
if (m_RedPacketMgr.find(RedPacket->wId) != m_RedPacketMgr.end())
return;
m_RedPacketMgr[RedPacket->wId] = RedPacket;
}
这样的代码 m_RedPacketMgr会出现包id重复
改下面这样
tagRedPacket * RedPacketMgr::NewRedPacket()
{
m_nMaxId++;
for (std::map<WORD,tagRedPacket*>::iterator it = m_RedPacketMgr.begin(); it != m_RedPacketMgr.end();++it)
{
tagRedPacket* ptagRedPacket = it->second;
if (NULL == ptagRedPacket) {
m_RedPacketMgr.erase(it);
break;
}
//过期的红包
if (ptagRedPacket->cbTimeOut) {
m_RedPacketMgr.erase(it);
delete ptagRedPacket;
ptagRedPacket = NULL;
break;
}
}
tagRedPacket * pRedPacket = new tagRedPacket();
pRedPacket->wId = m_nMaxId;
return pRedPacket;
}