TONGDB.CPP
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:7k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // TongDB.cpp: implementation of the CTongDB class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Global.h"
  6. #include "db.h"
  7. #include "DBTable.h"
  8. #include "TongDB.h"
  9. CTongDB g_cTongDB;
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. int get_TongName(DB *db, const DBT *pkey, const DBT *pdata, DBT *ikey) 
  14. {
  15. //给定一个完整的buffer,得到account作为索引
  16. memset( ikey, 0, sizeof( DBT ) );
  17. //TRoleData *pRoleData = (TRoleData *)pdata->data;
  18. //ikey->data = pRoleData->BaseInfo.caccname;
  19. //ikey->size = strlen( pRoleData->BaseInfo.caccname ) + 1;
  20. TMemberStruct* pMemberStruct = (TMemberStruct*)pdata->data;
  21. ikey->data = pMemberStruct->szTong;
  22. ikey->size = strlen( pMemberStruct->szTong ) + 1;
  23. return 0;
  24. }
  25. CTongDB::CTongDB()
  26. {//初始化变量
  27. m_TongListCount = 0;
  28. memset(m_TongList,0,sizeof(TTongList) * MAX_TONG_COUNT);
  29. }
  30. bool CTongDB::Open()
  31. {
  32. m_TongTable = new ZDBTable("TongDB","TongTable"); //打开帮会表
  33. if(!m_TongTable->open())return false;
  34. m_MemberTable = new ZDBTable("TongMemberDB","MemberTable"); //帮众表
  35. m_MemberTable->addIndex(get_TongName);
  36. if(!m_MemberTable->open())
  37. {
  38. m_TongTable->close();
  39. return false;
  40. }
  41. return true;
  42. }
  43. void CTongDB::Close()
  44. {
  45. m_TongTable->close();
  46. m_MemberTable->close();
  47. delete m_TongTable;
  48. delete m_MemberTable;
  49. }
  50. bool CTongDB::ChangeTong(CTongControl& aTongControl)
  51. {//创建和改变帮派
  52. bool aResult = true;
  53. int aKeySize = strlen(aTongControl.m_szName) + 1;
  54. int aSize = sizeof(TTongStruct);
  55. TTongStruct aTongStruct;
  56. memset(&aTongStruct, 0, aSize);
  57. aTongStruct.nCamp = aTongControl.m_nCamp;
  58. aTongStruct.dwMoney = aTongControl.m_dwMoney;
  59. aTongStruct.nCredit = aTongControl.m_nCredit;
  60. aTongStruct.nLevel = aTongControl.m_nLevel;
  61. strcpy(aTongStruct.szName, aTongControl.m_szName);
  62. strcpy(aTongStruct.szMasterTitle, aTongControl.m_szMasterTitle);
  63. strcpy(aTongStruct.szNormalTitle, aTongControl.m_szNormalTitle);
  64. int i;
  65. for(i=0;i<defTONG_MAX_DIRECTOR;++i)
  66. {
  67. strcpy(aTongStruct.szDirectorTitle[i], aTongControl.m_szDirectorTitle[i]);
  68. }
  69. for(i=0;i<defTONG_MAX_MANAGER;++i)
  70. {
  71. strcpy(aTongStruct.szManagerTitle[i], aTongControl.m_szManagerTitle[i]);
  72. }
  73. aResult = m_TongTable->add(aTongControl.m_szName, aKeySize,(char*)(&aTongStruct), aSize);
  74. return aResult;
  75. }
  76. bool CTongDB::DelTong(char* aTongName)
  77. {//删除帮派
  78. bool aResult = true;
  79. int aKeySize = strlen(aTongName) + 1;
  80. aResult = m_TongTable->remove(aTongName, aKeySize);
  81. if(!aResult) return aResult;
  82. while(!m_MemberTable->remove(aTongName, aKeySize, 0))
  83. {}
  84. return true;
  85. }
  86. bool CTongDB::SearchTong(char* aTongName, CTongControl& aTongControl)
  87. {//从数据库中取得帮派
  88. bool aResult = true;
  89. int aSize;
  90. int aKeySize = strlen(aTongName) + 1;
  91. char* aBuffer = m_TongTable->search(aTongName, aKeySize, aSize);
  92. if(!aBuffer) return false; //找不到帮派
  93. TTongStruct* pTongStruct = (TTongStruct*)aBuffer;
  94. aTongControl.m_nCamp = pTongStruct->nCamp;
  95. strcpy(aTongControl.m_szName, pTongStruct->szName);
  96. strcpy(aTongControl.m_szPassword, pTongStruct->szPassword);
  97. strcpy(aTongControl.m_szMasterTitle, pTongStruct->szMasterTitle);
  98. strcpy(aTongControl.m_szNormalTitle, pTongStruct->szNormalTitle);
  99. int i;
  100. for(i=0;i<defTONG_MAX_DIRECTOR;++i)
  101. {
  102. strcpy(aTongControl.m_szDirectorTitle[i], pTongStruct->szDirectorTitle[i]);
  103. }
  104. for(i=0;i<defTONG_MAX_MANAGER;++i)
  105. {
  106. strcpy(aTongControl.m_szManagerTitle[i], pTongStruct->szManagerTitle[i]);
  107. }
  108. int aDirectorNum = 0;
  109. int aManagerNum = 0;
  110. int aMemberNum = 0;
  111. aBuffer = m_MemberTable->search(aTongName, aKeySize, aSize, 0);
  112. while(aBuffer)
  113. {
  114. TMemberStruct* pMemberStruct = (TMemberStruct*)aBuffer;
  115. switch(pMemberStruct->MemberClass)
  116. {
  117. case enumTONG_FIGURE_MASTER:
  118. strcpy(aTongControl.m_szMasterName, pMemberStruct->szName);
  119. aTongControl.m_dwMasterID = g_String2Id(pMemberStruct->szName);
  120. break;
  121. case enumTONG_FIGURE_DIRECTOR:
  122. if (pMemberStruct->nTitleIndex < 0 || pMemberStruct->nTitleIndex >= defTONG_MAX_DIRECTOR)
  123. break;
  124. strcpy(aTongControl.m_szDirectorName[pMemberStruct->nTitleIndex], pMemberStruct->szName);
  125. aTongControl.m_dwDirectorID[pMemberStruct->nTitleIndex] = g_String2Id(pMemberStruct->szName);
  126. aDirectorNum++;
  127. break;
  128. case enumTONG_FIGURE_MANAGER:
  129. if (pMemberStruct->nTitleIndex < 0 || pMemberStruct->nTitleIndex >= defTONG_MAX_MANAGER)
  130. break;
  131. strcpy(aTongControl.m_szManagerName[pMemberStruct->nTitleIndex], pMemberStruct->szName);
  132. aTongControl.m_dwManagerID[pMemberStruct->nTitleIndex] = g_String2Id(pMemberStruct->szName);
  133. aManagerNum++;
  134. break;
  135. case enumTONG_FIGURE_MEMBER:
  136. aTongControl.AddMember(pMemberStruct->szName);
  137. aMemberNum++;
  138. break;
  139. }
  140. aBuffer = m_MemberTable->next(aSize);
  141. }
  142. aTongControl.m_nDirectorNum = aDirectorNum;
  143. aTongControl.m_nManagerNum = aManagerNum;
  144. aTongControl.m_nMemberNum = aMemberNum;
  145. delete [] aBuffer;
  146. return aResult;
  147. }
  148. int CTongDB::GetTongCount()
  149. {//取得门派数
  150. int aCount = 0;
  151. int aTongSize;
  152. int aTongKeySize;
  153. char aTongKeyBuffer[defTONG_STR_LENGTH];
  154. char aTongBuffer[sizeof(TTongStruct)];
  155. bool aResult = m_TongTable->GetRecordEx(aTongBuffer, aTongSize, aTongKeyBuffer, aTongKeySize,ZDBTable::cpFirst);
  156. while(aResult)
  157. {
  158. aCount++;
  159. aResult = m_TongTable->GetRecordEx(aTongBuffer, aTongSize, aTongKeyBuffer, aTongKeySize,ZDBTable::cpNext);
  160. }
  161. return aCount;
  162. }
  163. int CTongDB::GetTongList(TTongList* aTongList, int aMaxNum)
  164. {//取得帮会列表
  165. int aCount = 0;
  166. int aTongSize;
  167. int aTongKeySize;
  168. char aTongKeyBuffer[defTONG_STR_LENGTH];
  169. char aTongBuffer[sizeof(TTongStruct)];
  170. bool aResult = m_TongTable->GetRecordEx(aTongBuffer, aTongSize, aTongKeyBuffer, aTongKeySize,ZDBTable::cpFirst);
  171. while(aResult)
  172. {
  173. TTongStruct* pTong = (TTongStruct*)aTongBuffer;
  174. strcpy(aTongList[aCount].szName, pTong->szName);
  175. int aMemCount = 0;
  176. int aMemSize;
  177. char* aMemBuffer;
  178. aMemBuffer = m_MemberTable->search(aTongKeyBuffer, aTongKeySize, aMemSize, 0);
  179. while(aMemBuffer)
  180. {
  181. TMemberStruct* pMember = (TMemberStruct*)aMemBuffer;
  182. if(pMember->MemberClass == enumTONG_FIGURE_MEMBER)
  183. aMemCount++;
  184. delete [] aMemBuffer;
  185. aMemBuffer = m_MemberTable->next(aMemSize);
  186. }
  187. aTongList[aCount].MemberCount = aMemCount;
  188. aCount++;
  189. if( aCount>= aMaxNum ) break;
  190. aResult = m_TongTable->GetRecordEx(aTongBuffer, aTongSize, aTongKeyBuffer, aTongKeySize,ZDBTable::cpNext);
  191. }
  192. return aCount;
  193. }
  194. bool CTongDB::ChangeMember(TMemberStruct aMember)
  195. {//增加帮众
  196. return m_MemberTable->add(aMember.szName, strlen(aMember.szName) + 1, (char*)(&aMember), sizeof(TMemberStruct));
  197. }
  198. bool CTongDB::DelMember(char* aMemberName)
  199. {//删除帮众
  200. int aKeySize = strlen(aMemberName) + 1;
  201. return m_MemberTable->remove(aMemberName, aKeySize);
  202. }