Papadopolis

Untitled

Jan 11th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.83 KB | None | 0 0
  1. /**
  2.     Filterscript utilizada para a comunicação entre a aplicação nodejs e o servidor sa-mp.
  3. **/
  4.  
  5. #include <a_samp>
  6. #include <a_http>
  7. #include <strlib>
  8. #include <sockets>
  9. #include <DiniToMysql>
  10.  
  11. new
  12.     port,
  13.     Socket:sock,
  14.     Socket:nodejsSock,
  15.     nodejsId,
  16.     ipLocal[64],
  17.     tInds[20][128],
  18.     pmsSalasInfo[MAX_PLAYERS][256],
  19.     tVals[20][128],
  20.     str[7][128]
  21. ;
  22.  
  23. public OnFilterScriptInit()
  24. {
  25.  
  26.     format(ipLocal, sizeof(ipLocal), "127.0.0.1");
  27.  
  28.     // Servidor UDP
  29.     sock = socket_create(TCP);
  30.    
  31.     socket_set_max_connections(sock, 100);
  32.  
  33.     socket_bind(sock, ipLocal);
  34.     socket_listen(sock, 7778);
  35.  
  36.     printf("Servidor TCP lendo no ip %s porta 7778", ipLocal);
  37.  
  38.     return 1;
  39. }
  40.  
  41. public OnPlayerCommandText(playerid, cmdtext[])
  42. {
  43.     new command[128];
  44.     new tmp[128];
  45.     new idx;
  46.     command = strtok(cmdtext,idx);
  47.  
  48.     if(strcmp(command, "/res", true) == 0) {
  49.         tmp = strtok(cmdtext, idx);
  50.         if(!strlen(tmp)) return SendClientMessage(playerid, 0xFFFF00,"Responde a mp para o último jogador que interagiu com você pelo UCP. Uso: /res [mensagem]");
  51.         format(str[0],256,"%s",tmp);
  52.        
  53.         if(!strlen(pmsSalasInfo[playerid])) {
  54.             return SendClientMessage(playerid, 0xFF0000, "Ninguém interagiu com você!");
  55.         }
  56.        
  57.         new playerName[MAX_PLAYER_NAME], sockCmd[512];
  58.  
  59.         GetPlayerName(playerid, playerName, sizeof(playerName));
  60.        
  61.         format(sockCmd, sizeof(sockCmd), "a=particularMessage&from=%s&message=%s&salaId=%s", playerName, str[0], pmsSalasInfo[playerid]);
  62.         cmd(sockCmd);
  63.     }
  64.    
  65.     return 1;
  66. }
  67.  
  68. forward teste();
  69. public teste() {
  70.     new ip[34];
  71.     get_remote_client_ip(nodejsSock, nodejsId, ip);
  72.    
  73.     printf("%s", ip);
  74.    
  75.     return true;
  76. }
  77.  
  78. public OnFilterScriptExit()
  79. {
  80.     //socket_destroy(sock);
  81.    
  82.     return 1;
  83. }
  84.  
  85. public OnPlayerText(playerid, text[]) {
  86.     new sockCmd[64], playerName[MAX_PLAYER_NAME];
  87.    
  88.     GetPlayerName(playerid, playerName, sizeof(playerName));
  89.  
  90.     format(sockCmd, sizeof(sockCmd), "a=msg&nick=%s&msg=%s", playerName, text);
  91.     cmd(sockCmd);
  92.  
  93.     return 1;
  94. }
  95. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  96. public onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid) {
  97.     printf("Nova conexão estabelecida através do IP %s", remote_client);
  98.    
  99.     nodejsSock = id;
  100.     nodejsId = remote_clientid;
  101.    
  102.     //SetTimer("teste", 10000, true);
  103.  
  104.     return 1;
  105. }
  106.  
  107. public onSocketReceiveData(Socket:id, remote_clientid, data[], data_len) {
  108.     printf("Comando recebido do cliente %d: %s", remote_clientid, data);
  109.    
  110.     new buffer[128], sockCmd[512];
  111.  
  112.     tratar(data);
  113.     if(strcmp(GET("a"), "chatMessage", false) == 0) {
  114.         format(buffer, sizeof(buffer), "[WebChat] %s: %s", GET("username"), str_replace("+", " ", GET("message")));
  115.  
  116.         SendClientMessageToAll(0xFFFFFFFF, buffer);
  117.     } else if(strcmp(GET("a"), "particularMessage", false) == 0) {
  118.         new targetId = GetPlayerID(GET("to"));
  119.         new msg[512];
  120.        
  121.         if(IsPlayerConnected(targetId)) {
  122.             format(msg, sizeof(msg), "[MP] %s: %s", GET("from"), GET("message"));
  123.            
  124.             printf("%s", GET("salaId"));
  125.             format(pmsSalasInfo[targetId], 256, "%s", GET("salaId"));
  126.            
  127.             SendClientMessage(targetId, 0x00FFFF, msg);
  128.         }
  129.     } else if(strcmp(GET("a"), "login", false) == 0) {
  130.         new arquivoRpg[128], arquivoMgs[128];
  131.  
  132.         format(arquivoRpg, sizeof(arquivoRpg), "contas_rpg/%s.txt", GET("nick"));
  133.         format(arquivoMgs, sizeof(arquivoMgs), "contas_mgs/%s.txt", GET("nick"));
  134.  
  135.         if(dini_Exists(arquivoRpg)) {
  136.             if(strcmp(dini_Get(arquivoRpg, "Senha"), GET("senha"), false) == 0) {
  137.                 format(sockCmd, sizeof(sockCmd), "a=login&auth=true&ip=%s", GET("ip"));
  138.                 cmd(sockCmd);
  139.                 return true;
  140.             }
  141.         }
  142.  
  143.         if(dini_Exists(arquivoMgs)) {
  144.             if(strcmp(dini_Get(arquivoMgs, "Senha"), GET("senha"), false) == 0) {
  145.                 format(sockCmd, sizeof(sockCmd), "a=login&auth=true&ip=%s", GET("ip"));
  146.                 cmd(sockCmd);
  147.                 return true;
  148.             }
  149.         }
  150.  
  151.         format(sockCmd, sizeof(sockCmd), "a=login&auth=false&ip=%s", GET("ip"));
  152.         cmd(sockCmd);
  153.     }
  154.  
  155.     return 1;
  156. }
  157.  
  158. public onUDPReceiveData(Socket:id, data[], data_len, remote_client_ip[], remote_client_port) {
  159.  
  160. }
  161. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  162. //public onSocketClose(Socket:id) {
  163. //  print("teste");
  164. //}
  165. public OnPlayerDeath(playerid, killerid, reason)
  166. {
  167.     printf("Morreu!");
  168.     new cmdText[128], killerName[MAX_PLAYER_NAME], deadName[MAX_PLAYER_NAME];
  169.    
  170.     if(IsPlayerConnected(killerid)) {
  171.         GetPlayerName(killerid, killerName, sizeof(killerName));
  172.         GetPlayerName(playerid, deadName, sizeof(deadName));
  173.    
  174.         format(cmdText, sizeof(cmdText), "a=kill&killerName=%s&deadName=%s&reason=%d", killerName, deadName, reason);
  175.         cmd(cmdText);
  176.     }
  177. }
  178. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  179. public onSocketRemoteDisconnect(Socket:id, remote_clientid)
  180. {
  181.     printf("%d", remote_clientid);
  182.    
  183.     return true;
  184. }
  185. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  186. public OnPlayerConnect(playerid)
  187. {
  188.     new sockCmd[64], playerName[MAX_PLAYER_NAME];
  189.  
  190.     GetPlayerName(playerid, playerName, sizeof(playerName));
  191.  
  192.     format(sockCmd, sizeof(sockCmd), "a=playerConnect&nick=%s", playerName);
  193.     cmd(sockCmd);
  194.    
  195.     return 1;
  196. }
  197. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  198. public OnPlayerDisconnect(playerid, reason)
  199. {
  200.     new sockCmd[64], playerName[MAX_PLAYER_NAME];
  201.  
  202.     GetPlayerName(playerid, playerName, sizeof(playerName));
  203.  
  204.     format(sockCmd, sizeof(sockCmd), "a=playerDisconnect&nick=%s&reason=%d", playerName, reason);
  205.     cmd(sockCmd);
  206.    
  207.     return 1;
  208. }
  209. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  210. forward cmd(_str[]);
  211. public cmd(_str[]) {
  212.     new cd[600];
  213.  
  214.     if(strlen(_str) > 0) {
  215.         format(cd, sizeof(cd), "%s&p=%d&t=server", str_replace(" ", "+", _str), port);
  216.         socket_sendto_remote_client(nodejsSock, nodejsId, cd);
  217.         printf(">> %s", cd);
  218.     }
  219. }
  220.  
  221. forward tratar(parametros[]);
  222. public tratar(parametros[]) {
  223.     new vars[7][128], del[3][128], params[128];
  224.  
  225.     //A função explode dá crash no server se você passar uma string recebida por parâmetro diretamente para a função.
  226.     format(params, sizeof(params), "%s", parametros);
  227.  
  228.     explode(vars, params, "&");
  229.     for(new i = 0; i < sizeof(vars); ++i) {
  230.         if(strfind(vars[i], "=", true) != -1) {
  231.             explode(del, vars[i], "=");
  232.             format(tInds[i], 128, "%s", del[0]);
  233.  
  234.             format(del[1], sizeof(del[]), "%s", str_replace("\r\n", "", del[1]));
  235.             format(tVals[i], 128, "%s", del[1]);
  236.         }
  237.     }
  238. }
  239.  
  240. stock GET(ind[]) {
  241.     new result[128], i;
  242.  
  243.     i = 0;
  244.  
  245.     while(i < sizeof(tInds)) {
  246.         if(strcmp(tInds[i], ind, false) == 0) {
  247.             format(result, sizeof(result), "%s", tVals[i]);
  248.             return result;
  249.         }
  250.  
  251.         ++i;
  252.     }
  253.  
  254.     return result;
  255. }
  256. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  257. stock GetPlayerID(const playername[])
  258. {
  259.     new nome[MAX_PLAYER_NAME];
  260.     new playerid = INVALID_PLAYER_ID;
  261.     for (new i = 0; i < GetMaxPlayers(); i++)
  262.     {
  263.         if(IsPlayerConnected(i))
  264.         {
  265.             GetPlayerName(i,nome,MAX_PLAYER_NAME);
  266.             if(strcmp(playername, nome, true) == 0)
  267.             {
  268.                 playerid = i;
  269.                 break;
  270.             }
  271.         }
  272.     }
  273.     return playerid;
  274. }
Add Comment
Please, Sign In to add comment