GatewayRobot.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 11k
Development Platform:

C/C++

  1. // GatewayRobot.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "conio.h"
  5. #include "Win32Exception.h"
  6. #include "Player.h"
  7. #include <string>
  8. #include <list>
  9. using namespace OnlineGameLib::Win32;
  10. typedef std::list< IPlayer * > PLAYER_LIST;
  11. PLAYER_LIST g_thePlayers;
  12. //OnlineGameLib::Win32::CLibrary g_theRainbowLib( "rainbow.dll" );
  13. // 要跟随的角色的名字
  14. char g_szRoleName[32] = "";
  15. char *g_pRoleName = g_szRoleName;
  16. int g_AccoutIDMin = 500;
  17. int g_AccoutIDMax = 600;
  18. //char g_szServerIP[0x100] = "192.168.21.62";
  19. //char g_szServerIP[0x100] = "192.168.22.105";
  20. char g_szServerIP[0x100] = "192.168.26.1";
  21. int  g_nServerPort = 5622;
  22. int g_nLoginLogoutFlag = false;
  23. int g_nDeleteRoleFlag   = false;
  24. int g_nAddRoleFlag      = false;
  25. int g_nAddRoleGender    = -1;   // 性别
  26. int g_nAddRoleSeries    = -1;   // 五行
  27. int g_nAddRolePlaceID   = -1;    // 出生地
  28. // flying add these vars
  29. // 指定出生坐标
  30. POINT g_ptBirthPos;
  31. int g_nFlgSetPos = 0;
  32. int g_nFlgSetAttack = 0;
  33. int g_nFlgSetSilence = 0;
  34. int g_nFlgChatTimer = 0;
  35. unsigned char szGMCommand[128] = {0};
  36. char g_szGMCommandList[MAX_GM_COUNT][MAX_GM_SIZE];
  37. int ProcessArg(int argc, char *argv[])
  38. {
  39.     int nResult = false;
  40.     int nHaveArgcFlag = false;
  41.     while (--argc)
  42.     {
  43.         nHaveArgcFlag = true;
  44.         argv++;
  45.         if (stricmp(argv[0], "-AccMin") == 0)
  46.         {
  47.             if (argc < 2)
  48.                 goto Exit0;
  49.             g_AccoutIDMin = atoi(argv[1]);
  50.             argc--;
  51.             argv++;
  52.             continue;
  53.         }
  54.         if (stricmp(argv[0], "-AccMax") == 0)
  55.         {
  56.             if (argc < 2)
  57.                 goto Exit0;
  58.             g_AccoutIDMax = atoi(argv[1]);
  59.             argc--;
  60.             argv++;
  61.             continue;
  62.         }
  63.         if (stricmp(argv[0], "-ServerIP") == 0)
  64.         {
  65.             if (argc < 2)
  66.                 goto Exit0;
  67.             strcpy(g_szServerIP, argv[1]);
  68.             argc--;
  69.             argv++;
  70.             continue;
  71.         }
  72.         if (stricmp(argv[0], "-ServerPort") == 0)
  73.         {
  74.             if (argc < 2)
  75.                 goto Exit0;
  76.             g_nServerPort = atoi(argv[1]);
  77.             argc--;
  78.             argv++;
  79.             continue;
  80.         }
  81.         if (stricmp(argv[0], "-FollowRoleName") == 0)
  82.         {
  83.             if (argc < 2)
  84.                 goto Exit0;
  85.             strncpy(g_szRoleName, argv[1], 32);
  86.             g_szRoleName[31] = '';
  87.             argc--;
  88.             argv++;
  89.             continue;
  90.         }
  91.         if (stricmp(argv[0], "-LoginLogout") == 0)
  92.         {
  93.             g_nLoginLogoutFlag = true;
  94.             continue;
  95.         }
  96.         if (stricmp(argv[0], "-DeleteRole") == 0)
  97.         {
  98.             g_nDeleteRoleFlag = true;
  99.             continue;
  100.         }
  101.         if (stricmp(argv[0], "-AddRole") == 0)
  102.         {
  103.             g_nAddRoleFlag= true;
  104.             continue;
  105.         }
  106.         
  107.         if (stricmp(argv[0], "-AddRoleGender") == 0)
  108.         {
  109.             if (argc < 2)
  110.                 goto Exit0;
  111.             g_nAddRoleGender = atoi(argv[1]);
  112.             if (g_nAddRoleGender > 1)
  113.                 g_nAddRoleGender = -1;
  114.             argc--;
  115.             argv++;
  116.             continue;
  117.         }
  118.         if (stricmp(argv[0], "-AddRoleSeries") == 0)
  119.         {
  120.             if (argc < 2)
  121.                 goto Exit0;
  122.             g_nAddRoleSeries = atoi(argv[1]);
  123.             if (g_nAddRoleSeries >= 5)
  124.                 g_nAddRoleSeries = -1;
  125.             argc--;
  126.             argv++;
  127.             continue;
  128.         }
  129.         if (stricmp(argv[0], "-AddRolePlaceID") == 0)
  130.         {
  131.             if (argc < 2)
  132.                 goto Exit0;
  133.             g_nAddRolePlaceID = atoi(argv[1]);
  134.             argc--;
  135.             argv++;
  136.             continue;
  137.         }
  138. // flying add this branch. SetPos
  139. if (stricmp(argv[0], "-SetPos") == 0)
  140. {
  141. if (argc < 3)
  142. goto Exit0;
  143. g_ptBirthPos.x = atoi(argv[1]);
  144. g_ptBirthPos.y = atoi(argv[2]);
  145. g_nFlgSetPos = 1;
  146. argc -= 2;
  147. argv += 2;
  148. continue;
  149. }
  150. // flying add this branch, set auto attack
  151. if (stricmp(argv[0], "-SetAttack") == 0)
  152. {
  153. g_nFlgSetAttack = 1;
  154. continue;
  155. }
  156. // flying add this branch, Let all robots shut up
  157. if (stricmp(argv[0], "-Shutup") == 0)
  158. {
  159. g_nFlgSetSilence= 1;
  160. continue;
  161. }
  162. // flying add this branch, pass a gm command to the robot
  163. // this branch was removed, use "LoadGM" instead.
  164. // if (stricmp(argv[0], "-SetGM") == 0)
  165. // {
  166. // g_nFlgSetGM = 1;
  167. // strncpy((char *)szGMCommand, argv[1], 128);
  168. // argc--;
  169. // argv++;
  170. // continue;
  171. // }
  172. // load gm command list from a file
  173. if (stricmp(argv[0], "-LoadGM") == 0)
  174. {
  175. char szGMFile[MAX_PATH];
  176. FILE* fp = NULL;
  177. memset(szGMFile, 0, MAX_PATH);
  178. strncpy(szGMFile, argv[1], strlen(argv[1]));
  179. fp = fopen(szGMFile, "r");
  180. if (fp == NULL)
  181. {
  182. printf("GM Command list file %s is not exist!n", szGMFile);
  183. goto Exit0;
  184. }
  185. printf("Load GM command........n");
  186. int nSize = 0;
  187. for (int i = 0; i < MAX_GM_COUNT; i++)
  188. {
  189. fgets(g_szGMCommandList[i], MAX_GM_SIZE, fp);
  190. nSize = strlen(g_szGMCommandList[i]);
  191. if (nSize == 0)
  192. break;
  193. if (g_szGMCommandList[i][nSize-1] == 'r' 
  194. || g_szGMCommandList[i][nSize-1] == 'n')
  195. g_szGMCommandList[i][nSize-1] = 0;
  196. printf("+ %sn", g_szGMCommandList[i]);
  197. }
  198. fclose(fp);
  199. fp = NULL;
  200. argc--;
  201. argv++;
  202. continue;
  203. }
  204. // flying add this branch, set the timer of chatting. The 
  205. // currently timer is 0.8 s
  206. if (stricmp(argv[0], "-ChatTimer") == 0)
  207. {
  208. g_nFlgChatTimer= 1;
  209. continue;
  210. }
  211.         if (
  212.             (stricmp(argv[0], "-Help") == 0)  || 
  213.             (stricmp(argv[0], "-?") == 0)
  214.         )
  215.         {
  216.             goto Exit0;
  217.         }
  218.         goto Exit0;
  219.     }
  220.     if (!nHaveArgcFlag)
  221.         goto Exit0;     // goto Help
  222.     nResult = true;
  223. Exit0:
  224.     return nResult;
  225. }
  226. int Help()
  227. {
  228.     printf("GatewayRobot for Sword Online Game V1.0n");
  229.     printf("    Usage:n");
  230.     printf("    GatewayRobot -AccMin dddd -AccMax ddddn"); 
  231.     printf("                 -ServerIP ddd.ddd.ddd.ddd -ServerPort ddddn");
  232.     printf("                 -FollowRoleName Namen");
  233.     printf("                 -LoginLogoutn");
  234.     printf("                 -DeleteRolen");
  235.     printf("                 -AddRolen");
  236.     printf("                 -AddRoleGender     0(man)/1(woman)n");
  237.     printf("                 -AddRoleSeries     0..4n");
  238.     printf("                 -AddRolePlaceID    ddddn");
  239.     printf("n");
  240.     printf("    -AccMin:            Account Min Numbern");
  241.     printf("    -AccMax:            Account Max Numbern");
  242.     printf("    -ServerIP:          Gateway Server IPn");
  243.     printf("    -ServerPort:        Gateway Server Portn");
  244.     printf("    -LoginLogout:       Test Login / Logoutn");
  245.     printf("    -DeleteRole:        Delete All Role(maybe)n");
  246.     printf("    -AddRole:           Add one Rolen");
  247.     printf("    -FollowRoleName:    Robot follow Role Namen");
  248. // flying add this line, to put the robots at a specified point.
  249. printf("    -SetPos:            X, Yn");
  250. printf("    -SetAttackn");
  251. printf("    -SetGM "?gm xx ???"n");
  252. printf("    -LoadGM GM_COMMANDS_FILEn");
  253. printf("    -Shutupn");
  254. printf("    -ChatTimern");
  255.     printf("n");
  256.     printf("    dddd: digtal numbern");
  257.     printf("    ddd.ddd.ddd.ddd: IP address");
  258.     return true;
  259. }
  260. int main(int argc, char* argv[])
  261. {
  262.     int nResult = false;
  263.     int nRetCode = false;
  264.     int i = 0;
  265. printf("%sn", argv[0]);
  266.    srand( (unsigned)time( NULL ) );
  267. memset(g_szGMCommandList, 0, MAX_GM_COUNT * MAX_GM_SIZE);
  268.     nRetCode = ProcessArg(argc, argv);
  269.     if (!nRetCode)
  270.     {
  271.         Help();
  272.         goto Exit0;
  273.     }
  274.     //g_thePlayers.empty();
  275. for (i = g_AccoutIDMin; i <= g_AccoutIDMax; i++)
  276. {
  277. char szAccountName[0x100];
  278.         
  279.         sprintf(szAccountName, "Robot%04d", i);
  280.         printf("Create %s ... ", szAccountName);
  281. IPlayer *pPlayer = new CPlayer( 
  282.             g_szServerIP, g_nServerPort,
  283. szAccountName
  284.         );
  285. //Sleep(300);
  286.         if (!pPlayer)
  287.         {
  288.             printf("fail!n");
  289.             continue;
  290.         }
  291.         printf("ok!n");
  292. // flying add these, make the robot at the specified position
  293. if (g_nFlgSetPos)
  294. pPlayer->SetPos(g_ptBirthPos.x, g_ptBirthPos.y);
  295. // flying add these, make the robot auto attack to the NPCs
  296. if (g_nFlgSetAttack)
  297. pPlayer->SetAttack();
  298. pPlayer->SetSilence(g_nFlgSetSilence);
  299. pPlayer->SetChatTimer(g_nFlgChatTimer);
  300. pPlayer->ConnectToGateway();
  301.         g_thePlayers.push_back( pPlayer );
  302. }
  303.     printf("Press 'Q' to End Programn");
  304.     while (true)
  305.     {
  306.         int ch = 0;
  307.     PLAYER_LIST::iterator it;
  308.     for ( it = g_thePlayers.begin(); it != g_thePlayers.end(); it ++ )
  309.     {
  310.             while (true)
  311.             {
  312.                 if (!kbhit())
  313.                     break;
  314.                 ch = getch();
  315.                 ch = toupper(ch);
  316.             
  317.                 if ((ch == 'Q') || (ch == 27)) // ESC
  318.                     break;  
  319.             }
  320.             if ((ch == 'Q') || (ch == 27)) // ESC
  321.                 break;  
  322.             //Sleep(rand() % 40);
  323.     CPlayer *pPlayer = (CPlayer *)(*it);
  324.             if (pPlayer == NULL)
  325.             {
  326.                 Sleep(100);
  327.                 continue;
  328.             }
  329.             if ((pPlayer->GetStatus()) != CPlayer::enumExitGame)
  330.                 continue;
  331.     OnlineGameLib::Win32::_tstring sAccountName;
  332.             pPlayer->GetAccountName(sAccountName);
  333.             delete pPlayer;
  334.             pPlayer = NULL;
  335.             *it = NULL;
  336.             printf("Destory %s ... ok!n", sAccountName.c_str());
  337.             if (g_nAddRoleFlag || g_nDeleteRoleFlag)
  338.                 continue;
  339.         
  340.             printf("ReCreate %s ... ", sAccountName.c_str());
  341.     pPlayer = new CPlayer( 
  342.                 g_szServerIP, g_nServerPort,
  343.     sAccountName.c_str()
  344.             );
  345. Sleep(300);
  346.             if (!pPlayer)
  347.             {
  348.                 printf("fail!n");
  349.                 continue;
  350.             }
  351.             printf("ok!n");
  352.     pPlayer->ConnectToGateway();
  353.             *it = pPlayer;
  354.     }
  355.         if ((ch == 'Q') || (ch == 27)) // ESC
  356.             break;  
  357.         Sleep(rand() % 1000);
  358.     }
  359.     {
  360. PLAYER_LIST::iterator it;
  361. for ( it = g_thePlayers.begin(); it != g_thePlayers.end(); it ++ )
  362. {
  363. IPlayer *pPlayer = ( *it );
  364. if ( pPlayer )
  365. {
  366. delete pPlayer;
  367. }
  368. }
  369.     }
  370.     nResult = true;
  371. Exit0:
  372.     return nResult;
  373. }