诸暨麻将添加redis
您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 
 
 
 

346 行
9.4 KiB

  1. #include "StdAfx.h"
  2. #include "DistributeManager.h"
  3. //////////////////////////////////////////////////////////////////////////////////
  4. //静态成员
  5. tagDistributeNode * CDistributeNodePool::m_pHeadOfFreeList = NULL;
  6. //////////////////////////////////////////////////////////////////////////////////
  7. //常量定义
  8. const int CDistributeNodePool::BLOCK_SIZE = 40;
  9. //////////////////////////////////////////////////////////////////////////////////
  10. //构造函数
  11. CDistributeNodePool::CDistributeNodePool()
  12. {
  13. }
  14. //析构函数
  15. CDistributeNodePool::~CDistributeNodePool()
  16. {
  17. //释放列表
  18. if (m_pHeadOfFreeList != NULL)
  19. {
  20. tagDistributeNode * pDistributeNode = m_pHeadOfFreeList;
  21. while (pDistributeNode != NULL)
  22. {
  23. //安全释放
  24. m_pHeadOfFreeList = pDistributeNode->pNextDistributeNode;
  25. SafeDelete(pDistributeNode);
  26. pDistributeNode = m_pHeadOfFreeList;
  27. }
  28. }
  29. }
  30. //分配结点
  31. tagDistributeNode * CDistributeNodePool::AllocNode()
  32. {
  33. //获取头结点
  34. tagDistributeNode *pDistributeNode = m_pHeadOfFreeList;
  35. if (pDistributeNode != NULL)
  36. {
  37. m_pHeadOfFreeList = pDistributeNode->pNextDistributeNode;
  38. }
  39. else
  40. {
  41. //分配内存
  42. for (int nIndex = 0; nIndex < BLOCK_SIZE; nIndex++)
  43. {
  44. tagDistributeNode * pNewBlock = new tagDistributeNode;
  45. pNewBlock->pNextDistributeNode = m_pHeadOfFreeList;
  46. m_pHeadOfFreeList = pNewBlock;
  47. }
  48. //设置结点
  49. pDistributeNode = m_pHeadOfFreeList;
  50. m_pHeadOfFreeList = pDistributeNode->pNextDistributeNode;
  51. }
  52. //初始节点
  53. ZeroMemory(pDistributeNode, sizeof(tagDistributeNode));
  54. return pDistributeNode;
  55. }
  56. //释放结点
  57. VOID CDistributeNodePool::FreeNode(void * pNode)
  58. {
  59. //归还结点
  60. tagDistributeNode * pDeadNode = static_cast<tagDistributeNode*>(pNode);
  61. pDeadNode->pNextDistributeNode = m_pHeadOfFreeList;
  62. m_pHeadOfFreeList = pDeadNode;
  63. return;
  64. }
  65. //////////////////////////////////////////////////////////////////////////////////
  66. //构造函数
  67. CDistributeManager::CDistributeManager()
  68. {
  69. //设置变量
  70. m_pHeadNode = NULL;
  71. m_wNodeCount = 0;
  72. m_wRealCount = 0;
  73. m_wAndroidCount = 0;
  74. m_cbDistributeRule = 0;
  75. }
  76. //析构函数
  77. CDistributeManager::~CDistributeManager()
  78. {
  79. //移除节点
  80. RemoveAll();
  81. }
  82. //插入结点
  83. BOOL CDistributeManager::InsertDistributeNode(const tagDistributeInfo & DistributeInfo)
  84. {
  85. //查找用户
  86. if (SearchNode(DistributeInfo.pIServerUserItem) != NULL) return false;
  87. //头部判断
  88. if (m_pHeadNode == NULL)
  89. {
  90. //分配结点
  91. m_pHeadNode = m_DistributeNodePool.AllocNode();
  92. if (m_pHeadNode == NULL) return false;
  93. //设置变量
  94. CopyMemory(&m_pHeadNode->DistributeInfo, &DistributeInfo, sizeof(DistributeInfo));
  95. m_pHeadNode->DistributeInfo.pPertainNode = m_pHeadNode;
  96. }
  97. else
  98. {
  99. //分配结点
  100. tagDistributeNode * pDistributeNode = m_DistributeNodePool.AllocNode();
  101. if (pDistributeNode == NULL) return false;
  102. //设置结点
  103. CopyMemory(&pDistributeNode->DistributeInfo, &DistributeInfo, sizeof(DistributeInfo));
  104. pDistributeNode->DistributeInfo.pPertainNode = pDistributeNode;
  105. //插入结点
  106. if (m_pHeadNode->pNextDistributeNode != NULL)
  107. {
  108. m_pHeadNode->pNextDistributeNode->pPrepDistributeNode = pDistributeNode;
  109. pDistributeNode->pNextDistributeNode = m_pHeadNode->pNextDistributeNode;
  110. }
  111. //链接结点
  112. pDistributeNode->pPrepDistributeNode = m_pHeadNode;
  113. m_pHeadNode->pNextDistributeNode = pDistributeNode;
  114. }
  115. //更新数目
  116. if (DistributeInfo.pIServerUserItem->IsAndroidUser())
  117. ++m_wAndroidCount;
  118. else
  119. ++m_wRealCount;
  120. ++m_wNodeCount;
  121. return true;
  122. }
  123. //移除结点
  124. VOID CDistributeManager::RemoveDistributeNode(const IServerUserItem * pIServerUserItem)
  125. {
  126. //查找结点
  127. tagDistributeNode *pDistributeNode = SearchNode(pIServerUserItem);
  128. if (pDistributeNode != NULL) RemoveDistributeNode(pDistributeNode);
  129. return;
  130. }
  131. //移除结点
  132. VOID CDistributeManager::RemoveDistributeNode(tagDistributeNode * pDistributeNode)
  133. {
  134. //参数校验
  135. if (pDistributeNode == NULL) return;
  136. //变量定义
  137. tagDistributeNode *pPrepNode = pDistributeNode->pPrepDistributeNode;
  138. tagDistributeNode *pNextNode = pDistributeNode->pNextDistributeNode;
  139. //删除结点
  140. if (pPrepNode != NULL)
  141. {
  142. if (pNextNode != NULL)
  143. {
  144. pPrepNode->pNextDistributeNode = pNextNode;
  145. pNextNode->pPrepDistributeNode = pPrepNode;
  146. }
  147. else
  148. {
  149. //尾部结点
  150. if (pPrepNode->pNextDistributeNode == pDistributeNode)
  151. {
  152. pPrepNode->pNextDistributeNode = NULL;
  153. }
  154. }
  155. }
  156. else
  157. {
  158. if (pNextNode != NULL)
  159. {
  160. pNextNode->pPrepDistributeNode = NULL;
  161. //重置表头
  162. m_pHeadNode = pNextNode;
  163. }
  164. else
  165. {
  166. m_pHeadNode = NULL;
  167. }
  168. }
  169. //更新数目
  170. if (pDistributeNode->DistributeInfo.pIServerUserItem->IsAndroidUser())
  171. --m_wAndroidCount;
  172. else
  173. --m_wRealCount;
  174. --m_wNodeCount;
  175. //安全释放
  176. m_DistributeNodePool.FreeNode(pDistributeNode);
  177. }
  178. //移除结点
  179. VOID CDistributeManager::RemoveAll()
  180. {
  181. //释放内存
  182. while (m_pHeadNode != NULL)
  183. {
  184. RemoveDistributeNode(m_pHeadNode);
  185. }
  186. //重置变量
  187. m_pHeadNode = NULL;
  188. m_wNodeCount = 0;
  189. m_wAndroidCount = 0;
  190. m_wRealCount = 0;
  191. return;
  192. }
  193. //执行分组
  194. WORD CDistributeManager::PerformDistribute(CDistributeInfoArray & DistributeInfoArray, WORD wNeedCount)
  195. {
  196. //定义变量
  197. tagDistributeNode * pMoveNode = m_pHeadNode;
  198. if (pMoveNode != NULL)
  199. {
  200. DistributeInfoArray.Add(pMoveNode->DistributeInfo);
  201. pMoveNode = pMoveNode->pNextDistributeNode;
  202. }
  203. //获取用户
  204. while (pMoveNode != NULL)
  205. {
  206. //定义变量
  207. BOOL bFirstSuccess = TRUE;
  208. //等级过滤
  209. if (DistributeInfoArray.GetCount() > 0 && DistributeInfoArray[0].wDistribute != pMoveNode->DistributeInfo.wDistribute)
  210. bFirstSuccess = FALSE;
  211. //机器过滤
  212. if (bFirstSuccess == TRUE && DistributeInfoArray.GetCount() == wNeedCount - 1 &&
  213. FilterRuleIsAllAndroid(DistributeInfoArray, pMoveNode->DistributeInfo.pIServerUserItem))
  214. bFirstSuccess = FALSE;
  215. //同桌过滤
  216. if (bFirstSuccess == TRUE && (m_cbDistributeRule&DISTRIBUTE_LAST_TABLE) == 0 &&
  217. FilterRuleExitsSameTable(DistributeInfoArray, pMoveNode->DistributeInfo.wLastTableID) == TRUE)
  218. bFirstSuccess = FALSE;
  219. //同IP过滤
  220. if (bFirstSuccess == TRUE && (m_cbDistributeRule&DISTRIBUTE_SAME_ADDRESS) == 0 &&
  221. FilterRuleExitsIPAddr(DistributeInfoArray, pMoveNode->DistributeInfo.pIServerUserItem->GetClientAddr()) == TRUE)
  222. bFirstSuccess = FALSE;
  223. //获取成功
  224. if (bFirstSuccess == TRUE) DistributeInfoArray.Add(pMoveNode->DistributeInfo);
  225. //向前推进
  226. pMoveNode = pMoveNode->pNextDistributeNode;
  227. //成功判断
  228. if (DistributeInfoArray.GetCount() == wNeedCount) break;
  229. }
  230. return (WORD)DistributeInfoArray.GetCount();
  231. }
  232. //查找结点
  233. tagDistributeNode * CDistributeManager::SearchNode(const IServerUserItem * const pIServerUserItem)
  234. {
  235. if (m_pHeadNode == NULL) return NULL;
  236. //设置变量
  237. tagDistributeNode * pMoveNode = m_pHeadNode;
  238. //查找结点
  239. while (pMoveNode != NULL)
  240. {
  241. //接口判断
  242. if (pMoveNode->DistributeInfo.pIServerUserItem == pIServerUserItem)
  243. return pMoveNode;
  244. //向前推进
  245. pMoveNode = pMoveNode->pNextDistributeNode;
  246. }
  247. return NULL;
  248. }
  249. //IP同址
  250. BOOL CDistributeManager::FilterRuleExitsIPAddr(const CDistributeInfoArray & DistributeInfoArray, DWORD dwClientAddr)
  251. {
  252. //查找同IP
  253. for (INT_PTR nIndex = 0; nIndex < DistributeInfoArray.GetCount(); nIndex++)
  254. {
  255. if (DistributeInfoArray[nIndex].pIServerUserItem->GetClientAddr() == dwClientAddr)
  256. return TRUE;
  257. }
  258. return FALSE;
  259. }
  260. //上局同桌
  261. BOOL CDistributeManager::FilterRuleExitsSameTable(const CDistributeInfoArray & DistributeInfoArray, WORD wLastTableID)
  262. {
  263. //查找同IP
  264. for (INT_PTR nIndex = 0; nIndex < DistributeInfoArray.GetCount(); nIndex++)
  265. {
  266. if (DistributeInfoArray[nIndex].wLastTableID == wLastTableID &&
  267. wLastTableID != INVALID_TABLE)
  268. return TRUE;
  269. }
  270. return FALSE;
  271. }
  272. //机器过滤
  273. BOOL CDistributeManager::FilterRuleIsAllAndroid(const CDistributeInfoArray & DistributeInfoArray, IServerUserItem * const pIServerUserItem)
  274. {
  275. //参数校验
  276. if (pIServerUserItem == NULL || DistributeInfoArray.GetCount() == 0) return FALSE;
  277. //变量定义
  278. WORD wAndroidCount = 0;
  279. //统计机器
  280. for (INT_PTR nIndex = 0; nIndex < DistributeInfoArray.GetCount(); nIndex++)
  281. {
  282. if (DistributeInfoArray[nIndex].pIServerUserItem->IsAndroidUser() == true)
  283. ++wAndroidCount;
  284. }
  285. return (wAndroidCount == DistributeInfoArray.GetCount()) && pIServerUserItem->IsAndroidUser();
  286. }
  287. //////////////////////////////////////////////////////////////////////////////////