[h5棋牌项目]-13-红包扫雷随机红包金额算法
.h部分代码
//红包结构体
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[MAX_STATISTICAL_RED_PACKET];//开红包数组
};
class RedPacketMgr
{
tagRedPacket * GetRedPacket(WORD wId);
void RandRedPacket(int count);
float GetRandRedPacket(int index);
float m_RedPacketUser[MAX_STATISTICAL_RED_PACKET]; //红包比例
std::map<WORD, tagRedPacket*> m_RedPacketMgr;
}
.cpp部分代码
tagRedPacket * RedPacketMgr::GetRedPacket(WORD wId)
{
if(m_RedPacketMgr.find(wId) == m_RedPacketMgr.end())
return NULL;
if(m_RedPacketMgr[wId]->cbTimeOut){
return NULL;
}
return m_RedPacketMgr[wId];
}
void RedPacketMgr::RandRedPacket(int count)
{
if (count < 1 ||MAX_STATISTICAL_RED_PACKET < count)
return;
float nAll = 0.0f;
int max = 50;
for (int i = 0; i < count; i++) {
m_RedPacketUser[i] = rand() % max + 10;
nAll += m_RedPacketUser[i];
}
for (int i = 0; i < count; i++) {
m_RedPacketUser[i] = m_RedPacketUser[i] / nAll;
}
}
float RedPacketMgr::GetRandRedPacket(int index)
{
if (index < 0 || MAX_STATISTICAL_RED_PACKET <= index)
return 0.1;
return m_RedPacketUser[index];
}
应用
RedPacketMgr m_RedPacketMgr; //红包管理
tagRedPacket* pRedPacket = m_RedPacketMgr.GetRedPacket(wId);
wRedPacket = pRedPacket->dwCellScore * m_RedPacketMgr.GetRandRedPacket(bCount);