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

模拟服务器

开发平台:

C/C++

  1. /*****************************************************************************************
  2. // 存档角色选择
  3. // Copyright : Kingsoft 2002
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2002-9-12
  6. ------------------------------------------------------------------------------------------
  7. *****************************************************************************************/
  8. #include <Winsock2.h>
  9. #include <time.h>
  10. #include <crtdbg.h>
  11. #include "KEngine.h"
  12. #include "SelSavedCharacter.h"
  13. #include "../../Core/Src/CoreShell.h"
  14. #include "../../Core/Src/KNpcRes.h"
  15. #pragma comment (lib, "Ws2_32.lib")
  16. extern iCoreShell* g_pCoreShell;
  17. static unsigned gs_holdrand = time(NULL);
  18. static inline unsigned _Rand()
  19. {
  20.     gs_holdrand = gs_holdrand * 244213L + 1541021L;
  21.      
  22.     return gs_holdrand;
  23. }
  24. void RandMemSet(int nSize, unsigned char *pbyBuffer)
  25. {
  26.     _ASSERT(nSize);
  27.     _ASSERT(pbyBuffer);
  28.     while (nSize--)
  29.     {
  30.         *pbyBuffer++ = (unsigned char)_Rand();
  31.     }
  32. }
  33. //--------------------------------------------------------------------------
  34. // 功能:构造函数
  35. //--------------------------------------------------------------------------
  36. KSelSavedCharacter::KSelSavedCharacter()
  37. {
  38. m_AccountName[0] = 0;
  39. m_szProcessingRoleName[0] = 0;
  40. m_Status = SSC_S_IDLE;
  41. m_nNumCharacter = 0;
  42. m_nRequestTime = 0;
  43. m_nLastOperResult = SSC_R_NOTHING;
  44. }
  45. KSelSavedCharacter::~KSelSavedCharacter()
  46. {
  47. FreeData();
  48. }
  49. //--------------------------------------------------------------------------
  50. // 功能:开始载入角色数据
  51. //--------------------------------------------------------------------------
  52. int KSelSavedCharacter::LoadData()
  53. {
  54. if (m_Status != SSC_S_IDLE)
  55. return false;
  56. g_NetConnectAgent.UpdateClientRequestTime(false);
  57. m_Status = SSC_S_LOADING_DATA;
  58. return true;
  59. }
  60. //--------------------------------------------------------------------------
  61. // 功能:返回值为梅举SSC_STATUS的取值之一,返回值含义请看相关的值声明
  62. //--------------------------------------------------------------------------
  63. int KSelSavedCharacter::GetStatus()
  64. {
  65. return ((int)m_Status);
  66. }
  67. //--------------------------------------------------------------------------
  68. // 功能:设置最后一次操作的返回结果
  69. //--------------------------------------------------------------------------
  70. void KSelSavedCharacter::SetLastActionResult(int nResult)
  71. {
  72. m_nLastOperResult = nResult;
  73. }
  74. //--------------------------------------------------------------------------
  75. // 功能:得到最后一次操作的返回结果
  76. //--------------------------------------------------------------------------
  77. int KSelSavedCharacter::GetLastActionResult()
  78. {
  79. int nRet = m_nLastOperResult;
  80. m_nLastOperResult = SSC_R_NOTHING;
  81. return nRet;
  82. }
  83. //--------------------------------------------------------------------------
  84. // 功能:获取角色的数目
  85. //--------------------------------------------------------------------------
  86. int KSelSavedCharacter::GetCharacterNum()
  87. {
  88. return m_nNumCharacter;
  89. }
  90. //--------------------------------------------------------------------------
  91. // 功能:获取某个角色的信息
  92. //--------------------------------------------------------------------------
  93. int KSelSavedCharacter::GetCharacterInfo(int nIndex, KNewCharacterInfo* pInfo)
  94. {
  95. if (nIndex >= 0 && nIndex < m_nNumCharacter)
  96. {
  97. if (pInfo)
  98. {
  99. strcpy(pInfo->Name, m_BaseInfo[nIndex].szName);
  100. pInfo->Gender = m_BaseInfo[nIndex].Sex;
  101. pInfo->Attribute = m_BaseInfo[nIndex].Series;
  102. pInfo->nLevel = m_BaseInfo[nIndex].Level;
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. //--------------------------------------------------------------------------
  109. // 功能:请求新建一个角色
  110. //--------------------------------------------------------------------------
  111. int KSelSavedCharacter::NewCharacter(KNewCharacterInfo* pData)
  112. {
  113. if (!pData)
  114. return false;
  115. if (pData->Gender < 0 || pData->Gender >= ROLE_NO || pData->Attribute < 0 || pData->Attribute >= series_num)
  116. return false;
  117. int nNameLen = strlen(pData->Name);
  118. if (nNameLen < 1)
  119. return false;
  120. char Data[sizeof(TProcessData) + sizeof(NEW_PLAYER_COMMAND)];
  121. TProcessData* pNetCommand = (TProcessData*)&Data;
  122. NEW_PLAYER_COMMAND* pInfo = (NEW_PLAYER_COMMAND*)pNetCommand->pDataBuffer;
  123. pInfo->m_btRoleNo = pData->Gender;
  124. pInfo->m_btSeries = pData->Attribute;
  125. pInfo->m_NativePlaceId = pData->NativePlaceId;
  126. memcpy(pInfo->m_szName, pData->Name, nNameLen);
  127. pInfo->m_szName[nNameLen] = '';
  128. strcpy(m_szProcessingRoleName, pData->Name);
  129. pNetCommand->nProtoId = c2s_newplayer;
  130. pNetCommand->nDataLen = sizeof(NEW_PLAYER_COMMAND) - sizeof(pInfo->m_szName) + nNameLen + 1/* sizeof( '' ) */;
  131. pNetCommand->ulIdentity = 0;
  132. g_NetConnectAgent.SendMsg(&Data, sizeof(TProcessData) - sizeof(pNetCommand->pDataBuffer) + pNetCommand->nDataLen);
  133. g_NetConnectAgent.UpdateClientRequestTime(false);
  134. m_Status = SSC_S_CREATING_CHARACTER;
  135. m_nLastOperResult = SSC_R_NOTHING;
  136. return true;
  137. }
  138. //--------------------------------------------------------------------------
  139. // 功能:请求删除一个角色
  140. //--------------------------------------------------------------------------
  141. int KSelSavedCharacter::DeleteCharacter(int nIndex, const char* pszPassword)
  142. {
  143. if (m_Status != SSC_S_STANDBY || nIndex < 0 || nIndex >= m_nNumCharacter || pszPassword == NULL)
  144. return false;
  145. tagDBDelPlayer NetCommand;
  146. RandMemSet(sizeof(tagDBDelPlayer), (BYTE*)&NetCommand); // random memory for make a cipher
  147. NetCommand.cProtocol = c2s_roleserver_deleteplayer;
  148. strcpy(NetCommand.szAccountName, m_AccountName);
  149. strcpy(NetCommand.szPassword, pszPassword);
  150. strncpy(NetCommand.szRoleName, m_BaseInfo[nIndex].szName, sizeof(NetCommand.szRoleName));
  151. g_NetConnectAgent.SendMsg(&NetCommand, sizeof(tagDBDelPlayer));
  152. memset(&NetCommand.szPassword, 0, sizeof(NetCommand.szPassword));
  153. g_NetConnectAgent.UpdateClientRequestTime(false);
  154. strcpy(m_szProcessingRoleName, m_BaseInfo[nIndex].szName);
  155. m_Status = SSC_S_DELETING_CHARACTER;
  156. m_nLastOperResult = SSC_R_NOTHING;
  157. return true;
  158. }
  159. //--------------------------------------------------------------------------
  160. // 功能:选择某个角色
  161. //--------------------------------------------------------------------------
  162. int KSelSavedCharacter::SelCharacter(int nIndex)
  163. {
  164. if (m_Status != SSC_S_STANDBY || nIndex < 0 || nIndex >= m_nNumCharacter)
  165. return false;
  166. tagDBSelPlayer NetCommand;
  167. NetCommand.cProtocol = c2s_dbplayerselect;
  168. strcpy(NetCommand.szRoleName, m_BaseInfo[nIndex].szName);
  169. g_NetConnectAgent.SendMsg(&NetCommand, sizeof(tagDBSelPlayer));
  170. g_NetConnectAgent.UpdateClientRequestTime(false);
  171. g_DebugLog("Send Select Message to Server");
  172. strcpy(m_szProcessingRoleName, m_BaseInfo[nIndex].szName);
  173. m_Status = SSC_S_LOADING_CHARACTER;
  174. m_nLastOperResult = SSC_R_NOTHING;
  175. return true;
  176. }
  177. //--------------------------------------------------------------------------
  178. // 功能:释放角色数据
  179. //--------------------------------------------------------------------------
  180. void KSelSavedCharacter::FreeData()
  181. {
  182. m_Status = SSC_S_IDLE;
  183. m_nNumCharacter = 0;
  184. m_nRequestTime = 0;
  185. m_nLastOperResult = SSC_R_NOTHING;
  186. }
  187. void KSelSavedCharacter::SetCharacterBaseInfo(int nNum, const RoleBaseInfo *pInfo)
  188. {
  189. if (nNum > MAX_PLAYER_PER_ACCOUNT)
  190. nNum = MAX_PLAYER_PER_ACCOUNT;
  191. m_nNumCharacter = 0;
  192. for (int i = 0; i < nNum; i++)
  193. {
  194. if (pInfo[i].szName[0])
  195. m_nNumCharacter++;
  196. else
  197. break;
  198. }
  199. if (m_nNumCharacter > 0)
  200. memcpy(m_BaseInfo, pInfo, sizeof(RoleBaseInfo) * m_nNumCharacter);
  201. m_Status = SSC_S_STANDBY;
  202. m_nLastOperResult = SSC_R_UPDATE;
  203. }
  204. // -------------------------------------------------------------------------
  205. // 函数 : KUiSelPlayer::AcceptNetMsg
  206. // 功能 : 处理网络消息
  207. // -------------------------------------------------------------------------
  208. void KSelSavedCharacter::AcceptNetMsg(void* pMsgData)
  209. {
  210. if (m_Status != SSC_S_STANDBY && m_Status != SSC_S_IDLE)
  211. {
  212. PROTOCOL_MSG_TYPE eProtoId = *(PROTOCOL_MSG_TYPE*)pMsgData;
  213. switch(eProtoId)
  214. {
  215. case s2c_roleserver_getrolelist_result:
  216. if (m_Status == SSC_S_LOADING_DATA)
  217. {
  218. TProcessData* pProtocol = (TProcessData*)pMsgData;
  219. SetCharacterBaseInfo(pProtocol->pDataBuffer[0], (RoleBaseInfo*)&pProtocol->pDataBuffer[1]);
  220. g_NetConnectAgent.UpdateClientRequestTime(true);
  221. }
  222. break;
  223. case s2c_notifyplayerlogin:
  224. if (m_Status == SSC_S_LOADING_CHARACTER)
  225. {
  226. tagNotifyPlayerLogin* pNPL = (tagNotifyPlayerLogin*)pMsgData;
  227. if (strcmp((const char*)pNPL->szRoleName, m_szProcessingRoleName) == 0 && pNPL->bPermit)
  228. {
  229. // 开始与GameSvr进行连接
  230. if (g_NetConnectAgent.ConnectToGameSvr((const unsigned char*)&pNPL->nIPAddr, pNPL->nPort, &pNPL->guid))
  231. {
  232. g_NetConnectAgent.UpdateClientRequestTime(false);
  233. m_nLastOperResult = SSC_R_IN_PROGRESS;
  234. }
  235. else
  236. {
  237. m_nLastOperResult = SSC_R_FAILED;
  238. m_Status = SSC_S_STANDBY;
  239. }
  240. // 断开与网关的连接
  241. g_NetConnectAgent.DisconnectClient();
  242. g_NetConnectAgent.UpdateClientRequestTime(true);
  243. }
  244. else
  245. {
  246. m_nLastOperResult = SSC_R_SVR_DOWN;
  247. m_Status = SSC_S_STANDBY;
  248. }
  249. }
  250. break;
  251. case s2c_rolenewdelresponse:
  252. tagNewDelRoleResponse* pResponse = (tagNewDelRoleResponse*)pMsgData;
  253. // if (strcmp(pResponse->szRoleName, m_szProcessingRoleName) == 0) /* 刘鹏调试版 */
  254. {
  255. if (m_Status == SSC_S_CREATING_CHARACTER)
  256. {
  257. if (pResponse->bSucceeded)
  258. {
  259. g_NetConnectAgent.UpdateClientRequestTime(false);
  260. m_nLastOperResult = SSC_R_CREATE_ROLE_SUCCEED;
  261. m_Status = SSC_S_LOADING_CHARACTER;
  262. }
  263. else
  264. {
  265. g_NetConnectAgent.UpdateClientRequestTime(true);
  266. m_nLastOperResult = SSC_R_INVALID_ROLENAME;
  267. m_Status = SSC_S_STANDBY;
  268. }
  269. }
  270. else if (m_Status == SSC_S_DELETING_CHARACTER)
  271. {
  272. g_NetConnectAgent.UpdateClientRequestTime(true);
  273. if (pResponse->bSucceeded)
  274. {
  275. m_nLastOperResult = SSC_R_UPDATE;
  276. for (int i = 0; i < m_nNumCharacter; i++)
  277. {
  278. if (strcmp(m_BaseInfo[i].szName, m_szProcessingRoleName) == 0)
  279. {
  280. m_nNumCharacter--;
  281. for (; i < m_nNumCharacter; i++)
  282. m_BaseInfo[i] = m_BaseInfo[i + 1];
  283. break;
  284. }
  285. }
  286. }
  287. else
  288. {
  289. m_nLastOperResult = SSC_R_FAILED;
  290. }
  291. m_Status = SSC_S_STANDBY;
  292. }
  293. }
  294. break;
  295. }
  296. }
  297. }
  298. void KSelSavedCharacter::SetAccountName(const char* pAccount)
  299. {
  300. if (pAccount)
  301. strcpy(m_AccountName, pAccount);
  302. }