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

模拟服务器

开发平台:

C/C++

  1. //******************************************************************************
  2. /*! file       Player.cpp
  3. *   brief      简要描述
  4. *
  5. * 详细描述
  6. *   author 作者
  7. *   version 版本号
  8. *   date 2003-6-4 11:49:24
  9. *   sa 参考内容
  10. *   todo 将要做的工作
  11. *******************************************************************************/
  12. #pragma once
  13. //#include <Windows.h>
  14. #include "stdafx.h"
  15. #include <objbase.h>
  16. #include <initguid.h>
  17. #include "Win32Exception.h"
  18. //#include "KWin32.h"
  19. // that fucking macro defined here
  20. #include "Player.h"
  21. //#include "....Sword3PaySysS3AccServerAccountLoginDef.h"
  22. #include "....s3clientloginLoginDef.h"
  23. //#include "....CoresrcMsgGenreDef.h"
  24. #include "KProtocolDef.h"
  25. #include "KRelayProtocol.h"
  26. #include "KProtocol.h"
  27. #include "md5.h"
  28. using namespace OnlineGameLib::Win32;
  29. extern int g_nLoginLogoutFlag;
  30. extern int g_nDeleteRoleFlag;
  31. extern int g_nAddRoleFlag;
  32. extern int g_nAddRoleGender;
  33. extern int g_nAddRoleSeries;
  34. extern int g_nAddRolePlaceID;
  35. extern char g_szGMCommandList[MAX_GM_COUNT][MAX_GM_SIZE];
  36. //#define _DETAIL_PROTOCOL_DEBUG_
  37. //#define _GENERAL_DEBUG_
  38. CCriticalSection g_csPlayerList;
  39. //extern int g_nProtocolSize[MAX_PROTOCOL_NUM];
  40. //#define g_nGameProtocolSize g_nProtocolSize
  41. int g_nGameProtocolSize[MAX_PROTOCOL_NUM] = 
  42. {
  43. -1, // s2c_login,
  44. -1, // s2c_logout,
  45. sizeof(BYTE), // s2c_syncend,
  46. sizeof(CURPLAYER_SYNC), // s2c_synccurplayer,
  47. -1, // s2c_synccurplayerskill
  48. sizeof(CURPLAYER_NORMAL_SYNC),// s2c_synccurplayernormal
  49. -1, // s2c_newplayer,
  50. -1, // s2c_removeplayer,
  51. sizeof(WORLD_SYNC), // s2c_syncworld,
  52. sizeof(PLAYER_SYNC), // s2c_syncplayer,
  53. sizeof(PLAYER_NORMAL_SYNC), // s2c_syncplayermin,
  54. -1, //sizeof(NPC_SYNC), // s2c_syncnpc,
  55. sizeof(NPC_NORMAL_SYNC), // s2c_syncnpcmin,
  56. sizeof(NPC_PLAYER_TYPE_NORMAL_SYNC), // s2c_syncnpcminplayer,
  57. -1,//sizeof(OBJ_ADD_SYNC), // s2c_objadd,
  58. sizeof(OBJ_SYNC_STATE), // s2c_syncobjstate,
  59. sizeof(OBJ_SYNC_DIR), // s2c_syncobjdir,
  60. sizeof(OBJ_SYNC_REMOVE), // s2c_objremove,
  61. sizeof(OBJ_SYNC_TRAP_ACT), // s2c_objTrapAct,
  62. sizeof(NPC_REMOVE_SYNC), // s2c_npcremove,
  63. sizeof(NPC_WALK_SYNC), // s2c_npcwalk,
  64. sizeof(NPC_RUN_SYNC), // s2c_npcrun,
  65. -1, // s2c_npcattack,
  66. -1, // s2c_npcmagic,
  67. sizeof(NPC_JUMP_SYNC), // s2c_npcjump,
  68. -1, // s2c_npctalk,
  69. sizeof(NPC_HURT_SYNC), // s2c_npchurt,
  70. sizeof(NPC_DEATH_SYNC), // s2c_npcdeath,
  71. sizeof(NPC_CHGCURCAMP_SYNC),// s2c_npcchgcurcamp,
  72. sizeof(NPC_CHGCAMP_SYNC), // s2c_npcchgcamp,
  73. sizeof(NPC_SKILL_SYNC), // s2c_skillcast,
  74. -1, // s2c_playertalk,
  75. sizeof(PLAYER_EXP_SYNC), // s2c_playerexp,
  76. sizeof(PLAYER_SEND_TEAM_INFO), // s2c_teaminfo,
  77. sizeof(PLAYER_SEND_SELF_TEAM_INFO), // s2c_teamselfinfo,
  78. sizeof(PLAYER_APPLY_TEAM_INFO_FALSE), // s2c_teamapplyinfofalse,
  79. sizeof(PLAYER_SEND_CREATE_TEAM_SUCCESS),// s2c_teamcreatesuccess,
  80. sizeof(PLAYER_SEND_CREATE_TEAM_FALSE), // s2c_teamcreatefalse,
  81. sizeof(PLAYER_TEAM_OPEN_CLOSE), // s2c_teamopenclose,
  82. sizeof(PLAYER_APPLY_ADD_TEAM), // s2c_teamgetapply,
  83. sizeof(PLAYER_TEAM_ADD_MEMBER), // s2c_teamaddmember,
  84. sizeof(PLAYER_LEAVE_TEAM), // s2c_teamleave,
  85. sizeof(PLAYER_TEAM_CHANGE_CAPTAIN), // s2c_teamchangecaptain,
  86. sizeof(PLAYER_FACTION_DATA), // s2c_playerfactiondata,
  87. sizeof(PLAYER_LEAVE_FACTION), // s2c_playerleavefaction,
  88. sizeof(PLAYER_FACTION_SKILL_LEVEL), // s2c_playerfactionskilllevel,
  89. -1,//sizeof(PLAYER_SEND_CHAT_SYNC), // s2c_playersendchat,
  90. sizeof(PLAYER_LEAD_EXP_SYNC), // s2c_playersyncleadexp
  91. sizeof(PLAYER_LEVEL_UP_SYNC), // s2c_playerlevelup
  92. sizeof(PLAYER_TEAMMATE_LEVEL_SYNC), // s2c_teammatelevel
  93. sizeof(PLAYER_ATTRIBUTE_SYNC), // s2c_playersyncattribute
  94. sizeof(PLAYER_SKILL_LEVEL_SYNC), // s2c_playerskilllevel
  95. sizeof(ITEM_SYNC), // s2c_syncitem
  96. sizeof(ITEM_REMOVE_SYNC), // s2c_removeitem
  97. sizeof(PLAYER_MONEY_SYNC), // s2c_syncmoney
  98. sizeof(PLAYER_MOVE_ITEM_SYNC), // s2c_playermoveitem
  99. -1, // s2c_playershowui
  100. sizeof(CHAT_APPLY_ADD_FRIEND_SYNC), // s2c_chatapplyaddfriend
  101. sizeof(CHAT_ADD_FRIEND_SYNC), // s2c_chataddfriend
  102. -1,//sizeof(CHAT_REFUSE_FRIEND_SYNC), // s2c_chatrefusefriend
  103. sizeof(CHAT_ADD_FRIEND_FAIL_SYNC), // s2c_chataddfriendfail
  104. sizeof(CHAT_LOGIN_FRIEND_NONAME_SYNC), // s2c_chatloginfriendnoname
  105. -1,//sizeof(CHAT_LOGIN_FRIEND_NAME_SYNC), // s2c_chatloginfriendname
  106. sizeof(CHAT_ONE_FRIEND_DATA_SYNC), // s2c_chatonefrienddata
  107. sizeof(CHAT_FRIEND_ONLINE_SYNC), // s2c_chatfriendinline
  108. sizeof(CHAT_DELETE_FRIEND_SYNC), // s2c_chatdeletefriend
  109. sizeof(CHAT_FRIEND_OFFLINE_SYNC), // s2c_chatfriendoffline
  110. sizeof(ROLE_LIST_SYNC), // s2c_syncrolelist
  111. sizeof(TRADE_CHANGE_STATE_SYNC), // s2c_tradechangestate
  112. -1, // NPC_SET_MENU_STATE_SYNC    s2c_npcsetmenustate
  113. sizeof(TRADE_MONEY_SYNC), // s2c_trademoneysync
  114. sizeof(TRADE_DECISION_SYNC), // s2c_tradedecision
  115. -1, // sizeof(CHAT_SCREENSINGLE_ERROR_SYNC)s2c_chatscreensingleerror
  116. sizeof(NPC_SYNC_STATEINFO), // s2c_syncnpcstate,
  117. -1, // sizeof(TEAM_INVITE_ADD_SYNC)    s2c_teaminviteadd
  118. sizeof(TRADE_STATE_SYNC), // s2c_tradepressoksync
  119. sizeof(PING_COMMAND), // s2c_ping
  120. sizeof(NPC_SIT_SYNC), // s2c_npcsit
  121. sizeof(SALE_BOX_SYNC), // s2c_opensalebox
  122. sizeof(NPC_SKILL_SYNC), // s2cDirectlyCastSkill
  123. -1, // s2c_msgshow
  124. -1, // s2c_syncstateeffect
  125. sizeof(BYTE), // s2c_openstorebox
  126. sizeof(NPC_REVIVE_SYNC), // s2c_playerrevive
  127. sizeof(NPC_REQUEST_FAIL), // s2c_requestnpcfail
  128. sizeof(TRADE_APPLY_START_SYNC), // s2c_tradeapplystart
  129. sizeof(tagNewDelRoleResponse), // s2c_rolenewdelresponse
  130. sizeof(ITEM_AUTO_MOVE_SYNC), // s2c_ItemAutoMove
  131. sizeof(BYTE), // s2c_itemexchangefinish
  132. sizeof(SYNC_WEATHER), // s2c_changeweather
  133. sizeof(PK_NORMAL_FLAG_SYNC), // s2c_pksyncnormalflag
  134. -1,//sizeof(PK_ENMITY_STATE_SYNC), // s2c_pksyncenmitystate
  135. -1,//sizeof(PK_EXERCISE_STATE_SYNC), // s2c_pksyncexercisestate
  136. sizeof(PK_VALUE_SYNC), // s2c_pksyncpkvalue
  137. sizeof(NPC_SLEEP_SYNC), // s2c_npcsleepmode
  138. sizeof(VIEW_EQUIP_SYNC), // s2c_viewequip
  139. sizeof(LADDER_DATA), // s2c_ladderresult
  140. -1, // s2c_ladderlist
  141. sizeof(TONG_CREATE_SYNC), // s2c_tongcreate
  142. sizeof(PING_COMMAND), // s2c_replyclientping
  143. sizeof(NPC_GOLD_CHANGE_SYNC), // s2c_npcgoldchange
  144. sizeof(ITEM_DURABILITY_CHANGE), // s2c_itemdurabilitychange
  145. };
  146. using OnlineGameLib::Win32::CPackager;
  147. using OnlineGameLib::Win32::CLibrary;
  148. using OnlineGameLib::Win32::_tstring;
  149. #if defined _USE_RAINBOW
  150. CLibrary CPlayer::s_theRainbowLib( "rainbow.dll" );
  151. #else
  152. //#include "IClient.h"
  153. #endif
  154. // 跟踪角色的名字
  155. extern char *g_pRoleName;
  156. // 跟踪的角色的ID
  157. DWORD CPlayer::m_dwTargetID = ( DWORD )( -1 );
  158. // The timer of chat.. 
  159. // the value is 0.8 * 50
  160. const int g_nChatTimer = 40;
  161. const char *g_szText[] = {
  162. "%s, who are you??",
  163. "Shut up your fucking mouth, %s!",
  164. "%s, I love you..",
  165. "Oh, %s, what a fucking day today, isn't it??",
  166. "you looks like a white, fat, stupid pig, %s, I swear.",
  167. "Michael Jackson is the king of POP",
  168. "Michael Jordan is the king of Ball",
  169. "who knows what dangdangdangdangdangdang, dangdangdangdangdangdang is?",
  170. "dangdangdangdangdangdang, dangdangdangdangdangdang is ...。。。oooOOO Only you, can get the bible with me.",
  171. "Only you, can kill the ghost.",
  172. "Only you, can protect me, prevent the fishes eatting me....",
  173. "%s, Do you have any brothers? Please speak, I just wanna get a friend before death..",
  174. "We are both Athena's saint. We should fight for the love and justice of the world.",
  175. "%s, What fucking you are doing??",
  176. "Oh, shit!! %s, I meet Michael Jordan today, ohhhhh!",
  177. "Note: Don't trust benchmarks more than you trust politicians. Trust my benchmarks even less.",
  178. "%s, I think it's time for sleeping.",
  179. "Linus Benedict Torvalds: Read The Fucking Source Code!" 
  180. };
  181. const int g_TextNumber = 17;
  182. struct tagRGB
  183. {
  184. char r;
  185. char g;
  186. char b;
  187. };
  188. #pragma warning( disable : 4305 4309 )
  189. const tagRGB g_rgbText[] = {
  190. { 140, 255, 206 },
  191. { 255, 227, 33 },
  192. { 255, 130, 41 },
  193. { 255, 0, 0 },
  194. { 255, 113, 206 },
  195. { 0, 134, 255 },
  196. { 255, 251, 181 },
  197. { 255, 255, 255 },
  198. { 0, 211, 33 }
  199. };
  200. const int g_rgbNumber = 9;
  201. CPlayer::CPlayer( const char *pSrvIP, unsigned short nPort, const char *pAccName )
  202. : m_hQuit( NULL, true, false, pAccName )
  203. , m_pClientToGateWay( NULL )
  204. , m_pClientToGameSvr(NULL)
  205. , m_sGatewayIP( pSrvIP )
  206. , m_nGatewayPort( nPort )
  207. , m_sAccName( pAccName )
  208. , m_nStatus( enumStartup ) 
  209. , m_dwID( ( DWORD )( -1 ) )
  210. {
  211.     m_pbyRoleMsg = NULL;
  212. m_ptCurPos.x = 0;
  213. m_ptCurPos.y = 0;
  214. m_ptTagPos.x = 0;
  215. m_ptTagPos.y = 0;
  216.     m_RandValue = rand();
  217.     m_uGameServerLoopCount = 0;
  218.     m_nShutDownGatewayFlag = false;
  219. // The initialization of all flags goes here.
  220. // flying comment
  221. m_nMoveRange = 800;
  222. m_nFlgSetPos = 0;
  223. m_nFlgSetAttack = 0;
  224. m_nFlgSilence = 0;
  225. m_nFlgChatTimer = 0;
  226. m_nFlgGMDone = false;
  227. m_nChatTimer = 0;
  228. m_dwNpcID = 0;
  229. m_nFlgAI = 0;
  230. m_nDoing = 0;
  231. m_nTeamLeader = 0;
  232. memset(&m_ptTarget, 0, sizeof(POINT));
  233. memset(&m_ptBirth, 0, sizeof(POINT));
  234. memset(&m_ptOffset, 0, sizeof(POINT));
  235. memset(&m_player, 0, sizeof(PLAYER_INFO));
  236. memset(szGMCommand, 0, 128);
  237. // 机器人运动偏移量
  238. m_ptOffset.x = 500;
  239. m_ptOffset.y = 300;
  240. m_nGMChannelID = 0;
  241. m_nCurChannel = 0;
  242. // ext size
  243. g_nGameProtocolSize[s2c_extend - s2c_clientbegin - 1] = -1;
  244. g_nGameProtocolSize[s2c_extendchat - s2c_clientbegin - 1] = -1;
  245. g_nGameProtocolSize[s2c_extendfriend - s2c_clientbegin - 1] = -1;
  246. g_nGameProtocolSize[s2c_extendtong - s2c_clientbegin - 1] = -1;
  247. }
  248. CPlayer::~CPlayer()
  249. {
  250. Free();
  251. }
  252. //******************************************************************************
  253. /*! fn     void CPlayer::Free()
  254. *   brief  释放和Server的连接
  255. *******************************************************************************/
  256. void CPlayer::Free()
  257. {
  258. // 关闭到网关的连接
  259. ShutdownGateway();
  260. // 关闭到游戏服务器的连接
  261. ShutdownGameSvr();
  262. // 退出工作线程
  263. m_hQuit.Set();
  264.     if (m_pbyRoleMsg)
  265.     {
  266.         delete []m_pbyRoleMsg;
  267.         m_pbyRoleMsg = NULL;
  268.     }
  269.     try
  270.     {
  271.     if ( !Wait( 500000 ) )
  272.     {
  273.     Terminate();
  274.     }
  275.     }
  276.     catch (...) 
  277.     {
  278.     }
  279. }
  280. //******************************************************************************
  281. /*! fn     bool CPlayer::ConnectToGateway()
  282. *   brief  连接到网关
  283. *******************************************************************************/
  284. bool CPlayer::ConnectToGateway()
  285. {
  286. #if defined _USE_RAINBOW
  287. pfnCreateClientInterface pClientFactroyFun = ( pfnCreateClientInterface )( s_theRainbowLib.GetProcAddress( _T( "CreateInterface" ) ) );
  288. IClientFactory *pClientFactory = NULL;
  289. if ( pClientFactroyFun && SUCCEEDED( pClientFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  290. {
  291. pClientFactory->SetEnvironment( 1024 * 256 );
  292. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &m_pClientToGateWay ) );
  293. SAFE_RELEASE( pClientFactory );
  294. }
  295. else
  296. return false;
  297. #else
  298. // zcj's client goes here.
  299. #endif
  300. if ( m_pClientToGateWay )
  301. {
  302. int nRetCode = 0;
  303. nRetCode = m_pClientToGateWay->Startup();
  304. #if defined _USE_RAINBOW
  305. m_pClientToGateWay->RegisterMsgFilter( ( void * )this, ClientEventNotify );
  306. #else
  307. //typedef void (*CALLBACK_CLIENT_EVENT )(void *lpParam, const unsigned long &ulnEventType);
  308. m_pClientToGateWay->RegisterMsgFilter( ( void * )this, ClientEventNotify );
  309. #endif
  310. if ( FAILED( m_pClientToGateWay->ConnectTo( m_sGatewayIP.c_str(), m_nGatewayPort ) ) )
  311. {
  312. m_pClientToGateWay->Cleanup();
  313. SAFE_RELEASE( m_pClientToGateWay );
  314. return false;
  315. }
  316. }
  317. else
  318. return false;
  319. // 启动线程
  320. Start();
  321. SetStatus(enumStartup);
  322. return true;
  323. }
  324. //******************************************************************************
  325. /*! fn     int CPlayer::Run()
  326. *   brief  线程的运行函数
  327. *******************************************************************************/
  328. static long gs_lRandSeed = time(NULL);
  329. int CPlayer::Run()
  330. {
  331. // CCriticalSection::Owner locker( g_csPlayerList );
  332.     srand(InterlockedIncrement(&gs_lRandSeed));
  333. while ( !m_hQuit.Wait( 0 ) )
  334. {
  335. m_nChatTimer++;
  336. ProcessGateway();
  337. ProcessGameSvr();
  338. // Keep the loop per-second to exactly 20 times.
  339. ::Sleep( 50 );
  340. // hardware overflow? what a fucking thing!!
  341. if (m_nChatTimer > g_nChatTimer * 1000)
  342. m_nChatTimer = 0;
  343. }
  344. return 0;
  345. }
  346. //******************************************************************************
  347. /*! fn     void CPlayer::ProcessGateway()
  348. *   brief  处理和网关的通讯
  349. *******************************************************************************/
  350. void CPlayer::ProcessGateway()
  351. {
  352. if (NULL == m_pClientToGateWay)
  353. {
  354. return;
  355. }
  356. ProcessGatewayMsg();
  357. ProcessGatewayLoop();
  358. }
  359. //******************************************************************************
  360. /*! fn     void ProcessGatewayMsg()
  361. *   brief  处理网关发来的消息
  362. *******************************************************************************/
  363. void CPlayer::ProcessGatewayMsg()
  364. {
  365.     if (GetStatus() == enumExitGame)
  366.         return;
  367. size_t datalength = 0;
  368.     bool        bRetCode    = false;
  369. const char *pData = ( const char * )( m_pClientToGateWay->GetPackFromServer( datalength ) );
  370. const char *pMsg = pData;
  371. int nMsgLen = 0;
  372.     // for debug
  373. //    if (datalength > 0) {
  374. //        for (int i = 0; i < datalength; i++)
  375. //            printf("%d_", pData[i]);
  376. //        printf("n");
  377. //    }
  378. // printf("The length of fucking data: %dn", datalength);
  379. while ( pMsg < ( pData + datalength ) )
  380.     {
  381.         if (GetStatus() == enumExitGame)
  382.             return;
  383. BYTE cProtocol = CPackager::Peek( pMsg );
  384. switch ( cProtocol )
  385. {
  386. case s2c_login:
  387.  // 需要错误处理
  388. bRetCode = OnAccountLoginNotify(pMsg);
  389.             if (!bRetCode)
  390.             {
  391.                 Sleep(rand() % 100);
  392.                 SetStatus(enumExitGame);
  393.             }
  394. nMsgLen = *( unsigned short * )( pMsg + sizeof( BYTE ) );
  395. pMsg += sizeof( BYTE ) + nMsgLen;
  396. break;
  397. case s2c_roleserver_getrolelist_result:
  398.         {
  399.             printf("%s: Getrolelist Result ok!n", m_sAccName.c_str());
  400.             
  401.             // Save RoleMsg for next process
  402.             if (m_pbyRoleMsg)
  403.             {
  404.                 delete []m_pbyRoleMsg;
  405.                 m_pbyRoleMsg = NULL;
  406.             }
  407.         const S3DBI_RoleBaseInfo *pRoleList = NULL;
  408.         TProcessData *pPA = ( TProcessData * )pMsg;
  409.          nMsgLen = sizeof(TProcessData) + pPA->nDataLen - 1;
  410.             m_pbyRoleMsg = new unsigned char [0x1000];
  411.             if (!m_pbyRoleMsg)
  412.             {
  413.                 Sleep(rand() % 100);
  414.                 SetStatus(enumExitGame);
  415.                 break;
  416.             }
  417.             memcpy(m_pbyRoleMsg, pMsg, nMsgLen);
  418. nMsgLen = OnGetRoleList((char *)m_pbyRoleMsg);
  419. pMsg += nMsgLen;
  420.             //Sleep(rand() % 10);
  421.             //SetStatus(enumExitGame);
  422. break;
  423.         }
  424.         //case s2c_logout:
  425.         //    OnAccountLogoutNotify(pMsg);
  426.         //    break;
  427. case s2c_rolenewdelresponse:
  428.         {
  429. tagNewDelRoleResponse* pResponse = (tagNewDelRoleResponse*)pMsg;
  430.             printf("%s: Rolenewdelresponse Result %d!n", m_sAccName.c_str(), pResponse->bSucceeded);
  431.             //if (!pResponse->bSucceeded)
  432.             //{
  433.             //  
  434.             //}
  435.             pMsg += sizeof(tagNewDelRoleResponse);
  436.             if (g_nAddRoleFlag || g_nDeleteRoleFlag)
  437.             {
  438.             const S3DBI_RoleBaseInfo *pRoleList = NULL;
  439.             TProcessData *pPA = ( TProcessData * )m_pbyRoleMsg;
  440.     
  441.              int nRoleCount = *( char * )( pPA->pDataBuffer );
  442.                
  443.                 if (g_nDeleteRoleFlag)
  444.                 {
  445.                     if (nRoleCount > 0)
  446.                     {
  447.                         OnGetRoleList((char *)m_pbyRoleMsg);
  448.                         break;
  449.                     }
  450.                 }
  451.                 else if (g_nAddRoleFlag)
  452.                 {
  453.                     if (nRoleCount < 3)
  454.                     {
  455.                         OnGetRoleList((char *)m_pbyRoleMsg);
  456.                         break;
  457.                     }
  458.                 }
  459.             }
  460.             Sleep(rand() % 100);
  461.             SetStatus(enumExitGame);
  462.             break;
  463.         }
  464. case s2c_notifyplayerlogin:
  465.             
  466.             // 暂时不测试登入服务器的动作
  467.             //Sleep(rand() % 100);
  468.             //SetStatus(enumExitGame);
  469.             printf("%s: Notifyplayerlogin...n", m_sAccName.c_str());
  470.             
  471.             bRetCode = OnPlayerLoginNotify(pMsg);
  472.             if (!bRetCode)
  473.             {
  474.                 Sleep(rand() % 100);
  475.                 SetStatus(enumExitGame);
  476.             }
  477.             ShutdownGateway();
  478.             if (GetStatus() == enumExitGame)
  479.                 return;
  480. pMsg += sizeof(tagNotifyPlayerLogin);
  481. break;
  482. default:
  483.             Sleep(rand() % 100);
  484.             SetStatus(enumExitGame);
  485. return;
  486. }
  487. }
  488. }
  489. //******************************************************************************
  490. /*! fn     void CPlayer::ProcessGatewayLoop()
  491. *   brief  向网关发送的命令
  492. *******************************************************************************/
  493. void CPlayer::ProcessGatewayLoop()
  494. {
  495. if ( m_nStatus == enumStartup )
  496. {
  497. LoginGateway();
  498. SetStatus(enumConnectToGateway);
  499. }
  500. }
  501. //******************************************************************************
  502. /*! fn     void CPlayer::ProcessGameSvr()
  503. *   brief  处理和GameSvr的通讯
  504. *******************************************************************************/
  505. void CPlayer::ProcessGameSvr()
  506. {
  507. if (NULL == m_pClientToGameSvr)
  508. {
  509. return;
  510. }
  511. // Bug is in "ProcessGameSvrMsg"
  512. ProcessGameSvrMsg();
  513. ProcessGameSvrLoop();
  514. }
  515. //******************************************************************************
  516. /*! fn     void CPlayer::ProcessGameSvrMsg()
  517. *   brief  处理和游戏Server发来的消息
  518. *******************************************************************************/
  519. void CPlayer::ProcessGameSvrMsg()
  520. {
  521. char *pData = NULL;
  522. char *pMsg = NULL;
  523. char* pGetData = NULL;
  524. unsigned int nGetLength;
  525. pData = (char *)( m_pClientToGameSvr->GetPackFromServer( nGetLength ) );
  526. pMsg = pData;
  527. while (nGetLength)
  528. {
  529. while ( pMsg < ( pData + nGetLength ) )
  530. {
  531. #ifdef _GENERAL_DEBUG_
  532. printf("%d ==== %dn", pMsg, pData + nGetLength);
  533. #endif
  534. BYTE cProtocol = CPackager::Peek( pMsg );
  535. /*
  536. if ( !pData )
  537. return 0; 
  538. return *( ( const BYTE * )pData + index ); 
  539. */
  540. switch ( cProtocol )
  541. {
  542. case s2c_syncend:
  543. SyncEnd();
  544.             
  545. printf("%s: Enter Game Server SyncEnd!n", m_sAccName.c_str());
  546. if (g_nLoginLogoutFlag)
  547. {
  548. Sleep(rand() % 600);
  549. ShutdownGameSvr();
  550. SetStatus(enumExitGame);
  551. }
  552. break;
  553. case s2c_syncworld:
  554. {
  555. WORLD_SYNC *pWorldSync = (WORLD_SYNC *)pMsg;
  556. //pWorldSync->SubWorld
  557. #ifdef _DETAIL_PROTOCOL_DEBUG_
  558. printf("Protocol: SyncWorldn");
  559. #endif
  560. }
  561. break;
  562. case s2c_syncplayer:
  563. #ifdef _DETAIL_PROTOCOL_DEBUG_
  564. printf("Protocol: SyncPlayern");
  565. #endif
  566. break;
  567. case s2c_syncnpc:
  568. {
  569. NPC_SYNC *pPS = ( NPC_SYNC * )pMsg;
  570. #ifdef _DETAIL_PROTOCOL_DEBUG_
  571. printf("Protocol: SyncNpcn");
  572. #endif
  573. printf("NPC ID: %dn", pPS->ID);
  574. if ( pPS && ( '' != g_pRoleName[0] ) )
  575. {
  576. if ( 0 == memcmp( ( const char * )pPS->m_szName, g_pRoleName, sizeof( g_pRoleName ) ) )
  577. {
  578. m_dwTargetID = pPS->ID;
  579. }
  580. else
  581. {
  582. if (m_nFlgSetAttack)
  583. {
  584. ProcessNpcSync((NPC_NORMAL_SYNC*)pPS);
  585. }
  586. }
  587. }
  588. }
  589. break;
  590. case s2c_syncnpcmin:
  591. // {
  592. // NPC_NORMAL_SYNC *pNS = (NPC_NORMAL_SYNC *)pMsg;
  593. //#ifdef _DETAIL_PROTOCOL_DEBUG_
  594. // printf("Protocol: SyncNpcMin [%d]n");
  595. //#endif
  596. // printf("NPC Found: ID [%d]tCamp [%d]tDoing [%d]n",
  597. // pNS->ID, pNS->Camp, pNS->Doing);
  598. // if ( ( DWORD )( -1 ) != m_dwID )
  599. // {
  600. // if ( pNS && pNS->ID == m_dwID )
  601. // {
  602. // m_ptCurPos.x = pNS->MapX;
  603. // m_ptCurPos.y = pNS->MapY;
  604. // }
  605. // else if ( pNS && pNS->ID == m_dwTargetID )
  606. // {
  607. // m_ptTagPos.x = pNS->MapX;
  608. // m_ptTagPos.y = pNS->MapY;
  609. // }
  610. // else
  611. // {
  612. // // flying add this
  613. // // Auto attack against an NPC
  614. // if (m_nFlgSetAttack)
  615. // {
  616. // printf("XXXXXXXX A NPC will be attacked!!!!n");
  617. // ProcessNpcSync(pNS);
  618. // }
  619. // }
  620. // if ( ( DWORD )( -1 ) == m_dwTargetID && '' != g_pRoleName[0] )
  621. // {
  622. // if (
  623. // (rand() % 200) < (rand() % 100)
  624. // )
  625. // {
  626. // NPC_REQUEST_COMMAND nrc;
  627. //
  628. // nrc.ProtocolType = c2s_requestnpc;
  629. // nrc.ID = pNS->ID;
  630. //
  631. // m_pClientToGameSvr->SendPackToServer( reinterpret_cast< char * >( &nrc ), 
  632. // sizeof( NPC_REQUEST_COMMAND ) );
  633. // }
  634. // }
  635. // }
  636. // }
  637. break;
  638. case s2c_synccurplayer:
  639. {
  640. CURPLAYER_SYNC *pCS = (CURPLAYER_SYNC *)pMsg;
  641. #ifdef _DETAIL_PROTOCOL_DEBUG_
  642. printf("Protocol: SyncPlayern");
  643. #endif
  644. if ( pCS )
  645. {
  646. m_dwID = pCS->m_dwID;
  647. }
  648. }
  649. break;
  650. case s2c_syncnpcminplayer:
  651. {
  652. #ifdef _DETAIL_PROTOCOL_DEBUG_
  653. printf("Protocol: SyncNpcMinPlayern");
  654. #endif
  655. if ( ( DWORD )( -1 ) != m_dwID )
  656. {
  657. NPC_PLAYER_TYPE_NORMAL_SYNC *pNPTNS = (NPC_PLAYER_TYPE_NORMAL_SYNC *)pMsg;
  658. if ( pNPTNS && pNPTNS->m_dwNpcID == m_dwID )
  659. {
  660. m_ptCurPos.x = pNPTNS->m_dwMapX;
  661. m_ptCurPos.y = pNPTNS->m_dwMapY;
  662. }
  663. else if ( pNPTNS && pNPTNS->m_dwNpcID == m_dwTargetID )
  664. {
  665. m_ptTagPos.x = pNPTNS->m_dwMapX;
  666. m_ptTagPos.y = pNPTNS->m_dwMapY;
  667. }
  668. }
  669. }
  670. break;
  671. case s2c_skillcast:
  672. {
  673. NPC_SKILL_SYNC *pNpcSkillSync = (NPC_SKILL_SYNC *)pMsg;
  674. #ifdef _DETAIL_PROTOCOL_DEBUG_
  675. printf("Protocol: SkillCastn");
  676. #endif
  677. if (pNpcSkillSync->ID == m_dwTargetID)  // 如果主角攻击,大家一起配合
  678. {
  679. NPC_SKILL_COMMAND NpcSkillCmd;
  680. NpcSkillCmd.ProtocolType = c2s_npcskill;
  681. NpcSkillCmd.nSkillID = pNpcSkillSync->nSkillID;
  682. NpcSkillCmd.nMpsX = pNpcSkillSync->nMpsX;
  683. NpcSkillCmd.nMpsY = pNpcSkillSync->nMpsY;
  684. ASSERT(m_pClientToGameSvr);
  685. m_pClientToGameSvr->SendPackToServer( reinterpret_cast< char * >( &NpcSkillCmd ), 
  686. sizeof( NPC_SKILL_COMMAND ) );
  687. }
  688. }
  689. // flying add this branch
  690. // the robot has killed his "enemy NPC", oh yeah!!!! -_-|
  691. case s2c_npcdeath:
  692. {
  693. NPC_DEATH_SYNC* death = (NPC_DEATH_SYNC*) pMsg;
  694. #ifdef _DETAIL_PROTOCOL_DEBUG_
  695. printf("Protocol: NpcDeathn");
  696. #endif
  697. if (death->ID == m_dwID)
  698. {
  699. // do revive
  700. //printf("ACTION: Die!! Revive at the local place!n");
  701. NPC_REVIVE_COMMAND ReviveCmd;
  702. ReviveCmd.ProtocolType = c2s_playerrevive;
  703. //ReviveCmd.ReviveType = LOCAL_REVIVE_TYPE;
  704. ASSERT(m_pClientToGameSvr);
  705. m_pClientToGameSvr->SendPackToServer((BYTE *)&ReviveCmd, sizeof(NPC_REVIVE_COMMAND));
  706. }
  707. else if (death->ID == m_dwNpcID)
  708. {
  709. //printf("ACTION: %d Die!!!n", m_dwNpcID);
  710. m_nDoing = do_none;
  711. m_dwNpcID = 0;
  712. }
  713. }
  714. break;
  715. // flying add this branch
  716. // 组队相关
  717. // 入队邀请
  718. case s2c_teaminviteadd:
  719. {
  720. #ifdef _DETAIL_PROTOCOL_DEBUG_
  721. printf("Protocol: TeamInviteAddn");
  722. #endif
  723. TEAM_INVITE_ADD_SYNC* pSync = (TEAM_INVITE_ADD_SYNC *) pMsg;
  724. TEAM_REPLY_INVITE_COMMAND sReply;
  725. sReply.ProtocolType = c2s_teamreplyinvite;
  726. sReply.m_nIndex = pSync->m_nIdx;
  727. sReply.m_btResult = true;
  728. ASSERT(m_pClientToGameSvr);
  729. m_pClientToGameSvr->SendPackToServer(&sReply, sizeof(TEAM_REPLY_INVITE_COMMAND));
  730. }
  731. break;
  732. // 更换队长了
  733. case s2c_teamchangecaptain:
  734. {
  735. #ifdef _DETAIL_PROTOCOL_DEBUG_
  736. printf("Protocol: TeamChangeCaptainn");
  737. #endif
  738. PLAYER_TEAM_CHANGE_CAPTAIN* pData = (PLAYER_TEAM_CHANGE_CAPTAIN*)pMsg;
  739. m_nTeamLeader = pData->m_dwCaptainID;
  740. // 反馈拒绝信息
  741. }
  742. break;
  743. // 离队邀请(叫你走人)
  744. case s2c_teamleave:
  745. {
  746. #ifdef _DETAIL_PROTOCOL_DEBUG_
  747. printf("Protocol: TeamLeaven");
  748. #endif
  749. PLAYER_LEAVE_TEAM* pData = (PLAYER_LEAVE_TEAM*)pMsg;
  750. if (pData->m_dwNpcID == m_dwID)
  751. m_nTeamLeader = 0;
  752. }
  753. break;
  754. // 跨服务器
  755. case s2c_notifyplayerexchange:
  756. {
  757. #ifdef _DETAIL_PROTOCOL_DEBUG_
  758. printf("Protocol: NotifyPlayerExchangen");
  759. #endif
  760. ProcessSwitchGameServer((void *)pMsg);
  761. }
  762. break;
  763. // linux服务器开始测试了
  764. case s2c_ping:
  765. {
  766. PING_COMMAND* pData = (PING_COMMAND*)pMsg;
  767. if (pData == NULL)
  768. printf("Ping command data error!n");
  769. else
  770. {
  771. SendPing(pData->m_dwTime);
  772. }
  773. }
  774. break;
  775. // 服务端同步装备
  776. case s2c_syncitem:
  777. break;
  778. // 获取玩家聊天信息
  779. case s2c_playersendchat:
  780. {
  781. PLAYER_SEND_CHAT_SYNC* pData = (PLAYER_SEND_CHAT_SYNC *)pMsg;
  782. #ifdef _DETAIL_PROTOCOL_DEBUG_
  783. printf("Protocol: SyncSendChatn");
  784. #endif
  785. }
  786. break;
  787. // 扩展协议
  788. case s2c_extend:
  789. #ifdef _DETAIL_PROTOCOL_DEBUG_
  790. printf("Extend Protocol: [%d]n", cProtocol);
  791. #endif
  792. ProcessExtProtocol((char *)pMsg);
  793. break;
  794. default:
  795. #ifdef _DETAIL_PROTOCOL_DEBUG_
  796. printf("Ignored Protocol: [%d]n", cProtocol);
  797. #endif
  798. break;
  799. }
  800. /*
  801.  * Get next protocol
  802.  */
  803. int nProtocolSize = g_nGameProtocolSize[ cProtocol - s2c_clientbegin - 1 ];
  804. if ( nProtocolSize >= 0 )
  805. {
  806. pMsg += nProtocolSize;
  807. }
  808. else
  809. {
  810. unsigned short usMsgSize = *( unsigned short * )( pMsg + sizeof( BYTE ) );
  811. pMsg += sizeof( BYTE ) + usMsgSize;
  812. }
  813. } // while ( pMsg < ( pData + nGetLength ) )
  814. pData = (char *)( m_pClientToGameSvr->GetPackFromServer( nGetLength ) );
  815. #ifdef _GENERAL_DEBUG_
  816. printf("%d bytes m_pClientToGameSvr->GetPackFromServern", nGetLength);
  817. #endif
  818. pMsg = pData;
  819. } // while (nGetLength)
  820. }
  821. //******************************************************************************
  822. /*! fn     int CPlayer::ProcessExtProtocol(void* pMsg)
  823. *   brief  处理扩展协议
  824. *******************************************************************************/
  825. // flying add this
  826. int CPlayer::ProcessExtProtocol(char* pMsg)
  827. {
  828. int nResult = false;
  829. int nProtoType = 0;
  830. // int wLength = 0;
  831. char* pData = NULL;
  832. pData = pMsg + sizeof(tagExtendProtoHeader);
  833. EXTEND_HEADER* pHeader = (EXTEND_HEADER*)pData;
  834. ASSERT(pHeader->ProtocolFamily == pf_playercommunity);
  835. if (pMsg == NULL)
  836. goto Exit0;
  837. switch (pHeader->ProtocolID)
  838. {
  839. // query channel id result:
  840. case playercomm_s2c_notifychannelid:
  841. {
  842. // get the result first
  843. PLAYERCOMM_NOTIFYCHANNELID* pResult = (PLAYERCOMM_NOTIFYCHANNELID *)pData;
  844. m_nCurChannel = pResult->channelid;
  845. // subcribe the chat channel
  846. char szBuffer[sizeof(tagExtendProtoHeader) + sizeof(PLAYERCOMM_SUBSCRIBE)];
  847. tagExtendProtoHeader Header;
  848. PLAYERCOMM_SUBSCRIBE setChannel;
  849. Header.ProtocolType = c2s_extend;
  850. Header.wLength = sizeof(tagExtendProtoHeader) + sizeof(CHAT_SET_CHANNEL_COMMAND) - 1;
  851. setChannel.ProtocolFamily = pf_playercommunity;
  852. setChannel.ProtocolID = playercomm_c2s_subscribe;
  853. setChannel.channelid = m_nCurChannel;
  854. setChannel.subscribe = true;
  855. memcpy(szBuffer, &Header, sizeof(tagExtendProtoHeader));
  856. memcpy(szBuffer + sizeof(tagExtendProtoHeader), &setChannel, sizeof(PLAYERCOMM_SUBSCRIBE));
  857. m_pClientToGameSvr->SendPackToServer( szBuffer , 
  858. sizeof(tagExtendProtoHeader) + sizeof( PLAYERCOMM_SUBSCRIBE ) );
  859. }
  860. break;
  861. default:
  862. break;
  863. }
  864. nResult = true;
  865. Exit0:
  866. return nResult;
  867. }
  868. //******************************************************************************
  869. /*! fn     void CPlayer::ProcessSwitchGameServer()
  870. *   brief  切换游戏服务器
  871. *******************************************************************************/
  872. // flying add this
  873. int CPlayer::ProcessSwitchGameServer(void* lpBuffer)
  874. {
  875. int nResult = false;
  876. tagNotifyPlayerExchange* pInfo = (tagNotifyPlayerExchange*)lpBuffer;
  877. //_ASSERT(pInfo && pInfo->cProtocol == s2c_notifyplayerexchange);
  878. DWORD dwIp = pInfo->nIPAddr;
  879. int nPort = pInfo->nPort;
  880. GUID guid;
  881. guid = pInfo->guid;
  882. printf("Switch game server: %d : %dn", dwIp, nPort);
  883. if (dwIp && nPort)
  884. {
  885. // the first step is to close the connection from gameserver
  886. m_pClientToGameSvr->Shutdown();
  887. // 开始与GameSvr进行连接
  888. if (ConnectToGameSvr((char *)&dwIp, nPort, guid))
  889. goto Exit1;
  890. else
  891. goto Exit0;
  892. }
  893. else
  894. {
  895. goto Exit0;
  896. }
  897. Exit1:
  898. nResult = true;
  899. Exit0:
  900. return nResult;
  901. }
  902. //******************************************************************************
  903. /*! fn     void CPlayer::ProcessGameSvrLoop()
  904. *   brief  处理向游戏世界发送消息
  905. *******************************************************************************/
  906. void CPlayer::ProcessGameSvrLoop()
  907. {
  908. int nRandom = 0;
  909. //#define SEED 65530
  910. #define SEED 1024
  911. if( NULL == m_pClientToGameSvr)
  912. return;
  913. nRandom = rand();
  914. //if ( rand() % 65530 < 5 )
  915. // flying use this instead.
  916. if (m_nFlgChatTimer)
  917. {
  918. // TODO: Timer-chat goes here.
  919. if (m_nChatTimer % 40 == 0)
  920. {
  921. char szSpeakText[128];
  922. int nIndex = rand() % g_TextNumber;
  923. printf("Chat timern");
  924. sprintf(szSpeakText, (const char *)g_szText[nIndex], g_pRoleName);
  925. SpeakWord(szSpeakText, strlen(szSpeakText));
  926. }
  927. }
  928. if (nRandom % SEED < 4)
  929. {
  930. if (m_nFlgSilence == 0 && m_nFlgChatTimer == 0)
  931. {
  932. char szSpeakText[128];
  933. int nIndex = rand() % g_TextNumber;
  934. sprintf(szSpeakText, (const char *)g_szText[nIndex], g_pRoleName);
  935. SpeakWord(szSpeakText, strlen(szSpeakText));
  936. //printf("Speakingn");
  937. }
  938. }
  939. // cast...
  940. else if ( nRandom % SEED < 256)
  941. {
  942. CastSkill();
  943. }
  944. // flying add this branch, to walk randomly
  945. else if ( nRandom % SEED < 512)
  946. {
  947. NPC_WALK_COMMAND cmd;
  948. //cmd.CmdKind = do_walk;
  949. //printf("ACTION: Robot walk, haha!n");
  950. cmd.ProtocolType = c2s_npcwalk;
  951. if (rand() % 2)
  952. cmd.nMpsX = m_ptCurPos.x + rand() % m_ptOffset.x;
  953. else
  954. cmd.nMpsX = m_ptCurPos.x - rand() % m_ptOffset.x;
  955. if (rand() % 2)
  956. cmd.nMpsY = m_ptCurPos.y + rand() % m_ptOffset.y;
  957. else
  958. cmd.nMpsY = m_ptCurPos.y - rand() % m_ptOffset.y;
  959. //printf("Walk...n");
  960. m_pClientToGameSvr->SendPackToServer( reinterpret_cast< char * >( &cmd ), 
  961. sizeof( NPC_WALK_COMMAND ) );
  962. //printf("Walk donen");
  963. }
  964. else
  965. {
  966.         m_uGameServerLoopCount++;
  967.         if ((m_uGameServerLoopCount % 5) == 0)
  968.         {
  969.      WalkTo();
  970.         }
  971. }
  972. // 定时ping一把
  973. return;
  974. }
  975. //******************************************************************************
  976. /*! fn     void CPlayer::SendPing()
  977. *   brief  发送ping命令而已,所以不对外提供。
  978. *******************************************************************************/
  979. // flying add this
  980. void CPlayer::SendPing(DWORD dTime)
  981. {
  982. PING_CLIENTREPLY_COMMAND PingCmd;
  983. PingCmd.ProtocolType = c2s_ping;
  984. PingCmd.m_dwReplyServerTime = dTime;
  985. PingCmd.m_dwClientTime = GetTickCount();
  986. if (m_pClientToGameSvr)
  987. {
  988. m_pClientToGameSvr->SendPackToServer((BYTE*)&PingCmd, sizeof(PING_CLIENTREPLY_COMMAND));
  989. printf("Reply ping command: [%d]....n", dTime);
  990. }
  991. }
  992. //******************************************************************************
  993. /*! fn     void CPlayer::ProcessNpcSync(NPC_SYNC* pPS)
  994. *   brief  处理NPC同步
  995. *******************************************************************************/
  996. // flying add this
  997. int CPlayer::ProcessNpcSync(NPC_NORMAL_SYNC* pPS)
  998. {
  999. int nResult = false;
  1000. // 自己
  1001. if (pPS->ID == m_dwID)
  1002. {
  1003. m_player.nCamp = pPS->Camp;
  1004. goto Exit1;
  1005. }
  1006. // 普通战斗NPC
  1007. //if (pPS->m_btKind == kind_normal)
  1008. if (pPS->Camp == 0)
  1009. {
  1010. if (m_nDoing == do_skill || m_nDoing == do_attack)
  1011. goto Exit0;
  1012. m_nDoing = do_skill;
  1013. m_dwNpcID = pPS->ID;
  1014.         NPC_SKILL_COMMAND NpcSkillCmd;
  1015.         NpcSkillCmd.ProtocolType = c2s_npcskill;
  1016.         NpcSkillCmd.nSkillID = pPS->ID;
  1017.         NpcSkillCmd.nMpsX = pPS->MapX;
  1018.         NpcSkillCmd.nMpsY = pPS->MapY;
  1019.         m_pClientToGameSvr->SendPackToServer( reinterpret_cast< char * >( &NpcSkillCmd ), 
  1020. sizeof( NPC_SKILL_COMMAND ) );
  1021. goto Exit1;
  1022. }
  1023. goto Exit0;
  1024. Exit1:
  1025. nResult = true;
  1026. Exit0:
  1027. return nResult;
  1028. }
  1029. //******************************************************************************
  1030. /*! fn     void CPlayer::ProcessPlayerSync(NPC_PLAYER_TYPE_NORMAL_SYNC* pPS)
  1031. *   brief  处理NPC同步
  1032. *******************************************************************************/
  1033. // flying add this
  1034. int CPlayer::ProcessPlayerSync(NPC_PLAYER_TYPE_NORMAL_SYNC* pPS)
  1035. {
  1036. int nResult = false;
  1037. m_nDoing = pPS->m_byDoing;
  1038. nResult = true;
  1039. //Exit0:
  1040. return nResult;
  1041. }
  1042. //******************************************************************************
  1043. /*! fn     int CPlayer::SetSilence(int nSilence)
  1044. *   brief  设定机器人沉默
  1045. *******************************************************************************/
  1046. // add by flying
  1047. int CPlayer::SetChatTimer(int nTimer)
  1048. {
  1049. m_nFlgChatTimer = nTimer;
  1050. return true;
  1051. }
  1052. //******************************************************************************
  1053. /*! fn     int CPlayer::SetSilence(int nSilence)
  1054. *   brief  设定机器人沉默
  1055. *******************************************************************************/
  1056. // add by flying
  1057. int CPlayer::SetSilence(int nSilence)
  1058. {
  1059. m_nFlgSilence = nSilence;
  1060. return true;
  1061. }
  1062. //******************************************************************************
  1063. /*! fn     int CPlayer::SetAttack()
  1064. *   brief  设定机器人自动攻击NPC
  1065. *******************************************************************************/
  1066. // add by flying
  1067. int CPlayer::SetAttack()
  1068. {
  1069. m_nFlgSetAttack = 1;
  1070. return true;
  1071. }
  1072. //******************************************************************************
  1073. /*! fn     void CPlayer::SetPos()
  1074. *   brief  通过GM指令移动机器人
  1075. *******************************************************************************/
  1076. // add by flying
  1077. int CPlayer::SetPos(int nX, int nY)
  1078. {
  1079. int nResult = false;
  1080. m_nFlgSetPos = 1;
  1081. m_ptCurPos.x = nX;
  1082. m_ptCurPos.y = nY;
  1083. nResult = true;
  1084. //Exit0:
  1085. return nResult;
  1086. }
  1087. //******************************************************************************
  1088. /*! fn     void CPlayer::DoSetPos()
  1089. *   brief  通过GM指令移动机器人
  1090. *******************************************************************************/
  1091. // add by flying
  1092. int CPlayer::DoSetPos()
  1093. {
  1094. int nResult = false;
  1095. char szGmCommand[128];
  1096. // 判断XY的合法性,待定
  1097. memset(szGmCommand, 0, 128);
  1098. // 这里需要具体的Gm指令 ?gm ds SetPos(x, y)
  1099. sprintf(szGmCommand, "?gm ds SetPos(%d, %d)", m_ptCurPos.x, m_ptCurPos.y);
  1100. //printf("CM: %sn", szGmCommand);
  1101. SpeakWord(szGmCommand, strlen(szGmCommand));
  1102. m_nFlgSetPos = 0;
  1103. nResult = true;
  1104. return nResult;
  1105. }
  1106. //******************************************************************************
  1107. /*! fn     void CPlayer::LoginGateway()
  1108. *   brief  向网关发出登陆请求
  1109. *******************************************************************************/
  1110. void CPlayer::LoginGateway()
  1111. {
  1112. _tstring buffer;
  1113. KLoginAccountInfo lai;
  1114. char szTempPasswd[64];
  1115. ZeroMemory( &lai, sizeof( KLoginAccountInfo ) );
  1116. memset(szTempPasswd, 0, 64);
  1117. // 填充帐号
  1118. size_t used = sizeof( lai.Account );
  1119. used = ( used > m_sAccName.length() ) ? m_sAccName.length() : used;
  1120. memcpy( lai.Account, m_sAccName.c_str(), used );
  1121. // 填充密码
  1122. used = sizeof( lai.Password );
  1123. used = ( used > m_sAccName.length() ) ? m_sAccName.length() : used;
  1124. //memcpy( (void *)&(lai.Password), m_sAccName.c_str(), used );
  1125. memcpy(szTempPasswd, m_sAccName.c_str(), used);
  1126. KSG_StringToMD5String(lai.Password.szPassword, szTempPasswd);
  1127.     #ifdef USE_KPROTOCOL_VERSION
  1128.     // Add By Freeway Chen in 2003.7.1
  1129.     lai.ProtocolVersion = KPROTOCOL_VERSION;
  1130.     #endif
  1131. // 其他信息
  1132. lai.Param = LOGIN_A_LOGIN | LOGIN_R_REQUEST;
  1133. lai.Size = sizeof( lai );
  1134. buffer.resize( lai.Size + 1/* protocol id */ );
  1135. BYTE bProtocol = c2s_login;
  1136. memcpy( const_cast< char * >( buffer.c_str() ), &bProtocol, sizeof( BYTE ) );
  1137. memcpy( const_cast< char * >( buffer.c_str() + 1 ), &lai, sizeof( lai ) );
  1138. // 向网关发送请求
  1139. m_pClientToGateWay->SendPackToServer( buffer.c_str(), buffer.size() );
  1140. return;
  1141. }
  1142. // 
  1143. //******************************************************************************
  1144. /*! fn     bool CPlayer::OnAccountLoginNotify(const char* pMsg)
  1145. *   brief  处理网关发回的帐号登陆请求的结果
  1146. *******************************************************************************/
  1147. bool CPlayer::OnAccountLoginNotify(const char* pMsg)
  1148. {
  1149. KLoginAccountInfo *pLAI = ( KLoginAccountInfo * )( ( const BYTE * )pMsg + 1 );
  1150. bool bRet = false;
  1151. // if ( ( pLAI->Param | LOGIN_A_LOGIN ) &&
  1152. //  0 == m_sAccName.compare( pLAI->Account ) &&
  1153. //  0 == m_sAccName.compare( pLAI->Password ) )
  1154. if ( ( pLAI->Param | LOGIN_A_LOGIN ) &&
  1155.  0 == m_sAccName.compare( pLAI->Account ))
  1156. {
  1157. if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_ACCOUNT_OR_PASSWORD_ERROR )
  1158. {
  1159.             printf("%s: An incorrect account or password!n", m_sAccName.c_str());
  1160. }
  1161. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_FAILED )
  1162. {
  1163.             printf("%s: Login failed!n", m_sAccName.c_str());
  1164. }
  1165. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_ACCOUNT_EXIST )
  1166. {
  1167.             printf("%s: Login failed! your accout was existed!n", m_sAccName.c_str());
  1168. }
  1169. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_TIMEOUT )
  1170. {
  1171.             printf("%s: Login failed! no money!n", m_sAccName.c_str());
  1172. }
  1173. // // comment by flying
  1174. // else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_INVALID_PROTOCOLVERSION )
  1175. // {
  1176. //            printf("%s: Login failed! Invalid Protocol Version!n", m_sAccName.c_str());
  1177. // }
  1178. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_SUCCESS )
  1179. {
  1180.             printf("%s: Login ok!n", m_sAccName.c_str());
  1181. bRet = true;
  1182. }
  1183. }
  1184. return bRet;
  1185. }
  1186. // 
  1187. //******************************************************************************
  1188. /*! fn     bool CPlayer::OnAccountLogoutNotify(const char* pMsg)
  1189. *   brief  处理网关发回的帐号登出请求的结果
  1190. *******************************************************************************/
  1191. //bool CPlayer::OnAccountLogoutNotify(const char* pMsg)
  1192. //{
  1193. //
  1194. //    return true;
  1195. //}
  1196. //******************************************************************************
  1197. /*! fn     int CPlayer::OnGetRoleListRole(const char* pMsg)
  1198. *   brief  响应网关发来的消息,选择角色列表
  1199. *******************************************************************************/
  1200. int CPlayer::OnGetRoleList(const char* pMsg)
  1201. {
  1202. int nRoleCount = 0;
  1203. int nSelRoleIndex = 0;
  1204. S3DBI_RoleBaseInfo *pRoleList = NULL;
  1205. TProcessData *pPA = ( TProcessData * )pMsg;
  1206.     int nMsgLen = sizeof(TProcessData) + pPA->nDataLen - 1;
  1207. nRoleCount = *( char * )( pPA->pDataBuffer );
  1208. pRoleList = (S3DBI_RoleBaseInfo * )( ( const char * )( pPA->pDataBuffer ) + 1/* sizeof( char ) */ );
  1209. const size_t datalength = sizeof( S3DBI_RoleBaseInfo ) * nRoleCount;
  1210.     int nAction = 0;
  1211.     if (g_nDeleteRoleFlag)
  1212.     {
  1213.         nAction = 2;
  1214.     }
  1215.     else if (g_nAddRoleFlag)
  1216.     {
  1217.         nAction = 1;
  1218.     }
  1219.     else
  1220.     {
  1221.         if (nRoleCount > 0)
  1222.             nAction = 0;
  1223.         else
  1224.             nAction = 1;
  1225.     }
  1226.     switch (nAction) 
  1227.     {
  1228.     case 0:     // 选择角色
  1229.     {
  1230.         if (nRoleCount > 0)
  1231.         {
  1232.             nSelRoleIndex = rand() % nRoleCount;
  1233.         }
  1234.         
  1235.         if (nSelRoleIndex < nRoleCount)
  1236.         {
  1237.             // Add by Freeway Chen for Old Version BiShop
  1238.             if (nSelRoleIndex >= 3)
  1239.                 nSelRoleIndex = 2;
  1240.             printf("%s : Select Character %d/%d(%s)!n", m_sAccName.c_str(), nSelRoleIndex, nRoleCount, pRoleList[nSelRoleIndex].szName);
  1241.         }
  1242.         else
  1243.         {
  1244.             printf("%s : Select Empty Character!n", m_sAccName.c_str());
  1245.         }
  1246.     tagDBSelPlayer tDBSP;
  1247.     tDBSP.cProtocol = c2s_dbplayerselect;
  1248.     size_t nRoleNameLen = strlen( pRoleList[nSelRoleIndex].szName );
  1249.     memcpy( tDBSP.szRoleName, pRoleList[nSelRoleIndex].szName, nRoleNameLen );
  1250.     tDBSP.szRoleName[nRoleNameLen] = '';
  1251.     m_pClientToGateWay->SendPackToServer( &tDBSP, sizeof( tagDBSelPlayer ) );
  1252.         break;
  1253.     } // case 0:     // 选择角色
  1254.     case 1:     // 增加角色
  1255.     {
  1256.         if (nRoleCount >= 3)
  1257.             break;
  1258.         // 创建角色
  1259.         const int cnAddressIDCount = 5;
  1260.         static int s_AddressIDTable[cnAddressIDCount] = 
  1261.         {
  1262.             1,      // 凤翔
  1263.             20,     // 江津村
  1264.             45,     // 天忍教
  1265.             53,     // 巴陵县
  1266.             81      // 武当
  1267.         };
  1268.         int nNameLen = 0;
  1269.     char Data[sizeof(TProcessData) + sizeof(NEW_PLAYER_COMMAND)];
  1270.     TProcessData* pNetCommand = (TProcessData*)&Data;
  1271.     NEW_PLAYER_COMMAND* pInfo = (NEW_PLAYER_COMMAND*)pNetCommand->pDataBuffer;
  1272.     if (g_nAddRoleGender == -1)
  1273.             pInfo->m_btRoleNo = rand() % 2;     // 男女
  1274.         else 
  1275.             pInfo->m_btRoleNo = g_nAddRoleGender;     // 男女
  1276.         
  1277.         if (g_nAddRoleSeries == -1)    
  1278.         pInfo->m_btSeries = rand() % 5;     // 五行
  1279.         else
  1280.         pInfo->m_btSeries = g_nAddRoleSeries;     // 五行
  1281.         if (g_nAddRolePlaceID == -1)
  1282.         pInfo->m_NativePlaceId = s_AddressIDTable[rand() % cnAddressIDCount];
  1283.         else
  1284.             pInfo->m_NativePlaceId = g_nAddRolePlaceID;
  1285.         int nNameRand = rand() % 100;
  1286.     sprintf(pInfo->m_szName, "%s_%d", m_sAccName.c_str(), nNameRand);
  1287.         nNameLen = strlen(pInfo->m_szName);
  1288.     
  1289.     pNetCommand->nProtoId = c2s_newplayer;
  1290.     pNetCommand->nDataLen = sizeof(NEW_PLAYER_COMMAND) - sizeof(pInfo->m_szName) + nNameLen + 1/* sizeof( '' ) */;
  1291.     pNetCommand->ulIdentity = 0;
  1292.         printf("%s : Add Character %d(%s)!n", m_sAccName.c_str(), nRoleCount, pInfo->m_szName);
  1293.     m_pClientToGateWay->SendPackToServer(&Data, sizeof(TProcessData) - sizeof(pNetCommand->pDataBuffer) + pNetCommand->nDataLen);
  1294.         if (nRoleCount < 3)
  1295.         {
  1296.             memcpy((void *)pRoleList[nRoleCount].szName, pInfo->m_szName, nNameLen + 1);
  1297.             (*(char *)(pPA->pDataBuffer))++;
  1298.             nRoleCount++;
  1299.         }
  1300.         break;    
  1301.     }   // case 1:     // 增加角色
  1302.     case 2:     // 删除角色
  1303.     {
  1304.         if (nRoleCount > 0)
  1305.         {
  1306.             nSelRoleIndex = rand() % nRoleCount;
  1307.         }
  1308.         
  1309.         if (nSelRoleIndex < nRoleCount)
  1310.         {
  1311.             printf("%s : Del Character %d/%d(%s)!n", m_sAccName.c_str(), nSelRoleIndex, nRoleCount, pRoleList[nSelRoleIndex].szName);
  1312.         tagDBDelPlayer tDBDP;
  1313.             tDBDP.cProtocol = c2s_roleserver_deleteplayer;
  1314.             strcpy(tDBDP.szAccountName, m_sAccName.c_str());
  1315.             //strcpy(tDBDP.szPassword,    m_sAccName.c_str());
  1316.             strcpy(tDBDP.szRoleName,    pRoleList[nSelRoleIndex].szName);
  1317.         m_pClientToGateWay->SendPackToServer(&tDBDP, sizeof(tDBDP));
  1318.             while (nSelRoleIndex < (nRoleCount - 1))
  1319.             {
  1320.                 pRoleList[nSelRoleIndex] = pRoleList[nSelRoleIndex + 1]; 
  1321.                 
  1322.                 nSelRoleIndex++;
  1323.             }
  1324.             (*(char *)(pPA->pDataBuffer))--;
  1325.         }
  1326.         else
  1327.         {
  1328.             printf("%s : Del Empry Character!n", m_sAccName.c_str());
  1329.             
  1330.             Sleep(rand() % 10);
  1331.             SetStatus(enumExitGame);
  1332.         }
  1333.         break;    
  1334.     }   // case 2:     // 删除角色
  1335.     default :
  1336.         Sleep(rand() % 10);
  1337.         SetStatus(enumExitGame);
  1338.     
  1339.     }   // switch
  1340. return nMsgLen;
  1341. }
  1342. //******************************************************************************
  1343. /*! fn     bool CPlayer::OnPlayerLoginNotify(const char* pMsg)
  1344. *   brief  处理网关发来的消息,响应角色进入游戏的通告
  1345. *******************************************************************************/
  1346. bool CPlayer::OnPlayerLoginNotify(const char* pMsg)
  1347. {
  1348. tagNotifyPlayerLogin *pNPL = ( tagNotifyPlayerLogin * )pMsg;
  1349.     if (!pNPL->bPermit)
  1350.         return false;
  1351. const char *pIPAddr = OnlineGameLib::Win32::net_ntoa( pNPL->nIPAddr );
  1352. return ConnectToGameSvr( pIPAddr, pNPL->nPort, pNPL->guid );
  1353. }
  1354. //******************************************************************************
  1355. /*! fn     bool CPlayer::ConnectToGameSvr( const char *pIPAddr, unsigned short nPort, GUID &guid )
  1356. *   brief  处理网关发来的消息,客户连接到网关
  1357. *******************************************************************************/
  1358. bool CPlayer::ConnectToGameSvr( const char *pIPAddr, unsigned short nPort, GUID &guid )
  1359. {
  1360.     if (nPort == 0)
  1361.         return false;
  1362. #if defined _USE_RAINBOW
  1363. pfnCreateClientInterface pFactroyFun = ( pfnCreateClientInterface )( s_theRainbowLib.GetProcAddress( "CreateInterface" ) );
  1364. IClientFactory *pClientFactory = NULL;
  1365. if ( pFactroyFun && SUCCEEDED( pFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  1366. {
  1367. pClientFactory->SetEnvironment( 1024 * 256 );
  1368. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &m_pClientToGameSvr ) );
  1369. pClientFactory->Release();
  1370. }
  1371. if ( !m_pClientToGameSvr )
  1372. {
  1373. //::MessageBox(NULL, "Initialization failed! Don't find a correct rainbow.dll", "Error", MB_OK);
  1374.         printf("Initialization failed! Don't find a correct rainbow.dlln");
  1375. return false;
  1376. }
  1377. #else
  1378. //m_pClientToGameSvr = 
  1379. #endif
  1380. /*
  1381.  * Create a connection
  1382.  */
  1383. m_pClientToGameSvr->Startup();
  1384. //pGameSvrClient->RegisterMsgFilter( reinterpret_cast< void * >( pClient ), NULL );
  1385. #if defined _USE_RAINBOW
  1386.     HRESULT h = m_pClientToGameSvr->RegisterMsgFilter( ( void * )this, GameServerToClientEventNotify );
  1387. #else
  1388. m_pClientToGameSvr->RegisterMsgFilter( ( void * )this, GameServerToClientEventNotify );
  1389. #endif
  1390. if ( FAILED( m_pClientToGameSvr->ConnectTo( pIPAddr, nPort ) ) )
  1391. {
  1392. //::MessageBox(NULL, "Connection failed! Don't connect to game server.", "Error", MB_OK);
  1393.         printf("Connection failed! Don't connect to game server.n");
  1394. return false;
  1395. }
  1396. tagLogicLogin ll;
  1397. ll.cProtocol = c2s_logiclogin;
  1398. memcpy( &ll.guid, &guid, sizeof( GUID ) );
  1399. m_pClientToGameSvr->SendPackToServer( &ll, sizeof( tagLogicLogin ) );
  1400. ShutdownGateway();
  1401. return true;
  1402. }
  1403. //******************************************************************************
  1404. /*! fn     void CPlayer::SyncEnd()
  1405. *   brief  响应游戏服务器发来的消息,初始同步结束
  1406. *           登入或者切换服务器的时候产生这个消息。
  1407. * SyncWorld, SyncNpc, SyncPlayer, SyncCurPlayer, SyncCurPlayerSkill
  1408. * SyncEnd, SyncNpcMinPlayer
  1409. *******************************************************************************/
  1410. void CPlayer::SyncEnd()
  1411. {
  1412. SetStatus( enumPlayGame );
  1413. BYTE cSyscEndFlag = c2s_syncend;
  1414. m_pClientToGameSvr->SendPackToServer( reinterpret_cast< char * >( &cSyscEndFlag ), sizeof( BYTE ) );
  1415. // flying add the following code
  1416. // Initialize the "birth position"
  1417. if (m_nFlgSetPos)
  1418. DoSetPos();
  1419. // Query channel
  1420. QueryDefaultChannel("\S", 3);
  1421. // Execute GM Commands
  1422. SendGMCommand();
  1423. }
  1424. //******************************************************************************
  1425. /*! fn     void CPlayer::QueryDefaultChannel()
  1426. *   brief  查询默认频道
  1427. *******************************************************************************/
  1428. void CPlayer::QueryDefaultChannel(char* szName, int nSize)
  1429. {
  1430. if (szName == NULL || strlen(szName)==0)
  1431. return;
  1432. char szBuffer[sizeof(tagExtendProtoHeader) + sizeof(PLAYERCOMM_QUERYCHANNELID)];
  1433. tagExtendProtoHeader Header;
  1434. PLAYERCOMM_QUERYCHANNELID setChannel;
  1435. Header.ProtocolType = c2s_extend;
  1436. Header.wLength = sizeof(tagExtendProtoHeader) + sizeof(PLAYERCOMM_QUERYCHANNELID) - 1;
  1437. setChannel.ProtocolFamily = pf_playercommunity;
  1438. setChannel.ProtocolID = playercomm_c2s_querychannelid;
  1439. // Public Channel
  1440. memcpy(setChannel.channel, szName, nSize);
  1441. memcpy(szBuffer, &Header, sizeof(tagExtendProtoHeader));
  1442. memcpy(szBuffer + sizeof(tagExtendProtoHeader), &setChannel, sizeof(PLAYERCOMM_QUERYCHANNELID));
  1443. m_pClientToGameSvr->SendPackToServer( szBuffer , 
  1444. sizeof(tagExtendProtoHeader) + sizeof( PLAYERCOMM_QUERYCHANNELID ) );
  1445. }
  1446. static inline unsigned _RandValue(unsigned uHoldRand)
  1447. {
  1448.     uHoldRand = uHoldRand * 214013L + 2531011L;
  1449.      
  1450.     return uHoldRand;
  1451. }
  1452. //******************************************************************************
  1453. /*! fn     void CPlayer::WalkTo()
  1454. *   brief  机器人行走
  1455. *******************************************************************************/
  1456. void CPlayer::WalkTo()
  1457. {
  1458. if ( ( DWORD )( -1 ) != m_dwTargetID && m_ptTagPos.x && m_ptTagPos.y )
  1459. {
  1460. if ( abs( m_ptCurPos.x - m_ptTagPos.x ) > 16 ||
  1461. abs( m_ptCurPos.y - m_ptTagPos.y ) >  16 )
  1462. {
  1463.             NPC_RUN_COMMAND     nrc;
  1464.             nrc.ProtocolType = c2s_npcrun;
  1465.             nrc.nMpsX = m_ptTagPos.x + ((_RandValue(m_RandValue)             % 9) - 5) * 32;
  1466. nrc.nMpsY = m_ptTagPos.y + ((_RandValue(_RandValue(m_RandValue)) % 9) - 5) * 32;
  1467. m_pClientToGameSvr->SendPackToServer( reinterpret_cast< BYTE * >( &nrc ), sizeof( nrc ) );
  1468. }
  1469. }
  1470. }
  1471. //******************************************************************************
  1472. /*! fn     void CPlayer::CastSkill()
  1473. *   brief  发送技能
  1474. *******************************************************************************/
  1475. void CPlayer::CastSkill()
  1476. {
  1477. char* szGMAddMana = "?gm ds RestoreMana()";
  1478. if (!m_nFlgGMDone)
  1479. return;
  1480. // skill casting implementation goes here
  1481. NPC_SKILL_COMMAND NpcSkillCmd;
  1482. NpcSkillCmd.ProtocolType = c2s_npcskill;
  1483. NpcSkillCmd.nSkillID = 1;
  1484. NpcSkillCmd.nMpsX = m_ptTagPos.x + ((_RandValue(m_RandValue) % 9) - 5) * 32;
  1485. NpcSkillCmd.nMpsY = m_ptTagPos.y + ((_RandValue(_RandValue(m_RandValue)) % 9) - 5) * 32;;
  1486. ASSERT(m_pClientToGameSvr);
  1487. m_pClientToGameSvr->SendPackToServer( reinterpret_cast< char * >( &NpcSkillCmd ), 
  1488. sizeof( NPC_SKILL_COMMAND ) );
  1489. SpeakWord(szGMAddMana, strlen(szGMAddMana));
  1490. }
  1491. //******************************************************************************
  1492. /*! fn     void CPlayer::SendGMCommand()
  1493. *   brief  发送GM指令
  1494. *******************************************************************************/
  1495. void CPlayer::SendGMCommand()
  1496. {
  1497. if (m_nFlgGMDone)
  1498. return;
  1499. printf("Try to send GM command_S_.........n");
  1500. for (int i = 0; i < MAX_GM_COUNT; i++)
  1501. {
  1502. if (g_szGMCommandList[i][0] == 0)
  1503. break;
  1504. SpeakWord((char *)g_szGMCommandList[i], strlen((char *)szGMCommand));
  1505. printf("--> %sn", g_szGMCommandList[i]);
  1506. Sleep(20);
  1507. }
  1508. printf("GM Completed...............n");
  1509. m_nFlgGMDone = true;
  1510. return;
  1511. }
  1512. //******************************************************************************
  1513. /*! fn     void CPlayer::SpeakWord( const char *pText, size_t size  )
  1514. *   brief  随机聊天
  1515. *******************************************************************************/
  1516. void CPlayer::SpeakWord( const char *pText, size_t size /*0*/ )
  1517. {
  1518. if ( NULL == pText || 0 == size )
  1519. {
  1520. return;
  1521. }
  1522. size_t dataLength = size;
  1523. if ( 0 == dataLength )
  1524. {
  1525. dataLength = strlen(pText);
  1526. }
  1527. // New protocol
  1528. int nPackSize = sizeof(tagExtendProtoHeader) + sizeof(CHAT_CHANNELCHAT_CMD) + dataLength;
  1529. char *ProtocolBuffer = (char*)malloc(nPackSize);
  1530. int nProtocolOffset = 0;
  1531. tagExtendProtoHeader* pExHeader = (tagExtendProtoHeader*)ProtocolBuffer;
  1532. pExHeader->ProtocolType = c2s_extendchat;
  1533. pExHeader->wLength = nPackSize - 1;
  1534. nProtocolOffset += sizeof(tagExtendProtoHeader);
  1535. CHAT_CHANNELCHAT_CMD* pCccCmd = (CHAT_CHANNELCHAT_CMD*)(ProtocolBuffer + nProtocolOffset);
  1536. pCccCmd->ProtocolType = chat_channelchat;
  1537. pCccCmd->wSize = dataLength - 1;
  1538. pCccCmd->packageID = -1;
  1539. pCccCmd->filter = 1;
  1540. if (strncmp(pText, "?gm ds", 6) == 0)
  1541. pCccCmd->channelid = m_nGMChannelID;
  1542. else
  1543. pCccCmd->channelid = m_nCurChannel;
  1544. pCccCmd->cost = 0;
  1545. pCccCmd->sentlen = dataLength;
  1546. nProtocolOffset += sizeof(CHAT_CHANNELCHAT_CMD);
  1547. memcpy(pCccCmd + 1, pText, dataLength);
  1548. nProtocolOffset += dataLength;
  1549. if (m_pClientToGameSvr)
  1550. m_pClientToGameSvr->SendPackToServer(ProtocolBuffer, nProtocolOffset);
  1551. free(ProtocolBuffer);
  1552. }
  1553. //******************************************************************************
  1554. /*! fn     void ShutdownGateway()
  1555. *   brief  切断与网关的连接
  1556. *******************************************************************************/
  1557. void CPlayer::ShutdownGateway()
  1558. {
  1559.     m_nShutDownGatewayFlag = true;
  1560. // 关闭到网关的连接
  1561. if ( m_pClientToGateWay )
  1562. {
  1563. m_pClientToGateWay->Cleanup();
  1564. SAFE_RELEASE( m_pClientToGateWay );
  1565. }
  1566.     //SetStatus(enumExitGame);
  1567. }
  1568. //******************************************************************************
  1569. /*! fn     void ShutdownGateway()
  1570. *   brief  切断与网关的连接
  1571. *******************************************************************************/
  1572. void CPlayer::ShutdownGameSvr()
  1573. {
  1574. // 关闭到网关的连接
  1575. if ( m_pClientToGameSvr )
  1576. {
  1577. m_pClientToGameSvr->Cleanup();
  1578. SAFE_RELEASE( m_pClientToGameSvr );
  1579. }
  1580. }
  1581. #if defined _USE_RAINBOW
  1582. void __stdcall CPlayer::ClientEventNotify(
  1583. LPVOID lpParam,
  1584. const unsigned long &ulnEventType )
  1585. #else
  1586. void CPlayer::ClientEventNotify(
  1587. LPVOID lpParam,
  1588. const unsigned long &ulnEventType )
  1589. #endif
  1590. {
  1591. CPlayer *pPlayer = ( CPlayer * )lpParam;
  1592. ASSERT( pPlayer );
  1593. try
  1594. {
  1595. pPlayer->_ClientEventNotify( ulnEventType );
  1596. }
  1597. catch(...)
  1598. {
  1599. //::MessageBeep( -1 );
  1600. }
  1601. }
  1602. void CPlayer::_ClientEventNotify( const unsigned long &ulnEventType )
  1603. {
  1604. switch( ulnEventType )
  1605. {
  1606. case enumServerConnectCreate:
  1607. //::MessageBeep( -1 );
  1608. break;
  1609. case enumServerConnectClose:
  1610.         if (!m_nShutDownGatewayFlag)
  1611.         {
  1612.             // 如果不是主动断开网络的连接,说明是被网关踢掉
  1613.             SetStatus(enumExitGame);
  1614.         }
  1615. //::MessageBeep( -1 );
  1616. break;
  1617. }
  1618. }
  1619. #if defined _USE_RAINBOW
  1620. void __stdcall CPlayer::GameServerToClientEventNotify(
  1621. LPVOID lpParam,
  1622. const unsigned long &ulnEventType )
  1623. #else
  1624. void CPlayer::GameServerToClientEventNotify(
  1625. LPVOID lpParam,
  1626. const unsigned long &ulnEventType )
  1627. #endif
  1628. {
  1629. CPlayer *pPlayer = ( CPlayer * )lpParam;
  1630. ASSERT( pPlayer );
  1631. try
  1632. {
  1633. pPlayer->_GameServerToClientEventNotify( ulnEventType );
  1634. }
  1635. catch(...)
  1636. {
  1637. //::MessageBeep( -1 );
  1638. }
  1639. }
  1640. void CPlayer::_GameServerToClientEventNotify( const unsigned long &ulnEventType )
  1641. {
  1642. switch( ulnEventType )
  1643. {
  1644. case enumServerConnectCreate:
  1645. //::MessageBeep( -1 );
  1646. break;
  1647. case enumServerConnectClose:
  1648.         SetStatus(enumExitGame);
  1649. //::MessageBeep( -1 );
  1650. break;
  1651. }
  1652. }
  1653. int CPlayer::KSG_StringToMD5String(char szDestMD5String[64], const char cszSrcString[])
  1654. {
  1655.     int nResult = false;
  1656.     md5_state_t md5_state;
  1657.     unsigned char MD5Value[16];
  1658.     int nSrcStringLen = 0;
  1659.     if (!szDestMD5String)
  1660.         goto Exit0;
  1661.     szDestMD5String[0] = '';
  1662.     if  (!cszSrcString)
  1663.        goto Exit0;
  1664.     nSrcStringLen = strlen(cszSrcString);
  1665.     if (!nSrcStringLen)
  1666.         goto Exit0;
  1667.     md5_init(&md5_state);
  1668.     md5_append(&md5_state, (unsigned char *)cszSrcString, nSrcStringLen);
  1669.     md5_finish(&md5_state, MD5Value);
  1670.     sprintf(
  1671.         szDestMD5String,
  1672.         "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  1673.         MD5Value[ 0], MD5Value[ 1], MD5Value[ 2], MD5Value[ 3],
  1674.         MD5Value[ 4], MD5Value[ 5], MD5Value[ 6], MD5Value[ 7],
  1675.         MD5Value[ 8], MD5Value[ 9], MD5Value[10], MD5Value[11],
  1676.         MD5Value[12], MD5Value[13], MD5Value[14], MD5Value[15]
  1677.     );
  1678.     nResult = true;
  1679. Exit0:
  1680.     return nResult;
  1681. }