SHARE
TWEET

Untitled

Papadopolis Jan 11th, 2017 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. enum SalaInfo {
  12.     SalaID[64],
  13.     TargetName[MAX_PLAYER_NAME]
  14. };
  15.  
  16. new
  17.     port,
  18.     Socket:sock,
  19.     Socket:nodejsSock,
  20.     nodejsId,
  21.     ipLocal[64],
  22.     tInds[20][128],
  23.     pmsSalasInfo[MAX_PLAYERS][SalaInfo],
  24.     tVals[20][128],
  25.     str[7][128]
  26. ;
  27.  
  28. public OnFilterScriptInit()
  29. {
  30.  
  31.     format(ipLocal, sizeof(ipLocal), "127.0.0.1");
  32.  
  33.     // Servidor UDP
  34.     sock = socket_create(TCP);
  35.    
  36.     socket_set_max_connections(sock, 100);
  37.  
  38.     socket_bind(sock, ipLocal);
  39.     socket_listen(sock, 7790);
  40.  
  41.     printf("Servidor TCP lendo no ip %s porta 7778", ipLocal);
  42.  
  43.     return 1;
  44. }
  45.  
  46. public OnPlayerCommandText(playerid, cmdtext[])
  47. {
  48.     new command[128];
  49.     new tmp[128];
  50.     new idx;
  51.     command = strtok(cmdtext,idx);
  52.  
  53.     if(strcmp(command, "/res", true) == 0) {
  54.         new text[128], msg[128];
  55.    
  56.         GetStringText(cmdtext,idx,text);
  57.  
  58.         tmp = strtok(cmdtext, idx);
  59.         if(!strlen(tmp)) return SendClientMessage(playerid, 0xFFFF00,"Responde a mp para o último jogador que interagiu com você pelo UCP. Uso: /res [mensagem]");
  60.        
  61.         if(!strlen(pmsSalasInfo[playerid][SalaID])) {
  62.             return SendClientMessage(playerid, 0xFF0000, "Ninguém interagiu com você!");
  63.         }
  64.        
  65.         new playerName[MAX_PLAYER_NAME], sockCmd[512];
  66.  
  67.         GetPlayerName(playerid, playerName, sizeof(playerName));
  68.        
  69.         format(msg, sizeof(msg), "Enviado para %s: %s", pmsSalasInfo[playerid][TargetName], text);
  70.        
  71.         SendClientMessage(playerid, 0x00FFFF, msg);
  72.        
  73.         format(sockCmd, sizeof(sockCmd), "a=particularMessage&from=%s&message=%s&salaId=%s", playerName, text, pmsSalasInfo[playerid][SalaID]);
  74.         cmd(sockCmd);
  75.     }
  76.    
  77.     return 1;
  78. }
  79.  
  80. forward teste();
  81. public teste() {
  82.     new ip[34];
  83.     get_remote_client_ip(nodejsSock, nodejsId, ip);
  84.    
  85.     printf("%s", ip);
  86.    
  87.     return true;
  88. }
  89.  
  90. public OnFilterScriptExit()
  91. {
  92.     //socket_destroy(sock);
  93.    
  94.     return 1;
  95. }
  96.  
  97. public OnPlayerText(playerid, text[]) {
  98.     new sockCmd[64], playerName[MAX_PLAYER_NAME];
  99.    
  100.     GetPlayerName(playerid, playerName, sizeof(playerName));
  101.  
  102.     format(sockCmd, sizeof(sockCmd), "a=msg&nick=%s&msg=%s", playerName, text);
  103.     cmd(sockCmd);
  104.  
  105.     return 1;
  106. }
  107. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  108. public onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid) {
  109.     printf("Nova conexão estabelecida através do IP %s", remote_client);
  110.    
  111.     nodejsSock = id;
  112.     nodejsId = remote_clientid;
  113.    
  114.     new nome[MAX_PLAYER_NAME], sockCmd[512];
  115.    
  116.     for (new i = 0; i < GetMaxPlayers(); i++)
  117.     {
  118.         if(IsPlayerConnected(i))
  119.         {
  120.             GetPlayerName(i,nome,MAX_PLAYER_NAME);
  121.            
  122.             format(sockCmd, sizeof(sockCmd), "a=playerConnect&nick=%s", nome);
  123.             cmd(sockCmd);
  124.         }
  125.     }
  126.    
  127.     //SetTimer("teste", 10000, true);
  128.  
  129.     return 1;
  130. }
  131.  
  132. public onSocketReceiveData(Socket:id, remote_clientid, data[], data_len) {
  133.     printf("Comando recebido do cliente %d: %s", remote_clientid, data);
  134.    
  135.     new buffer[128], sockCmd[512];
  136.  
  137.     tratar(data);
  138.     if(strcmp(GET("a"), "chatMessage", false) == 0) {
  139.         format(buffer, sizeof(buffer), "[WebChat] %s: %s", GET("username"), str_replace("+", " ", GET("message")));
  140.  
  141.         SendClientMessageToAll(0xFFFFFFFF, buffer);
  142.     } else if(strcmp(GET("a"), "particularMessage", false) == 0) {
  143.         new targetId = GetPlayerID(GET("to"));
  144.         new msg[512];
  145.        
  146.         if(IsPlayerConnected(targetId)) {
  147.             format(msg, sizeof(msg), "[MP] %s: %s", GET("from"), GET("message"));
  148.            
  149.             printf("%s", GET("salaId"));
  150.            
  151.             format(pmsSalasInfo[targetId][TargetName], 256, "%s", GET("from"));
  152.             format(pmsSalasInfo[targetId][SalaID], 256, "%s", GET("salaId"));
  153.            
  154.             SendClientMessage(targetId, 0x00FFFF, msg);
  155.         }
  156.     } else if(strcmp(GET("a"), "login", false) == 0) {
  157.         new arquivoRpg[128], arquivoMgs[128];
  158.  
  159.         format(arquivoRpg, sizeof(arquivoRpg), "contas_rpg/%s.txt", GET("nick"));
  160.         format(arquivoMgs, sizeof(arquivoMgs), "contas_mgs/%s.txt", GET("nick"));
  161.  
  162.         if(dini_Exists(arquivoRpg)) {
  163.             if(strcmp(dini_Get(arquivoRpg, "Senha"), GET("senha"), false) == 0) {
  164.                 format(sockCmd, sizeof(sockCmd), "a=login&auth=true&ip=%s", GET("ip"));
  165.                 cmd(sockCmd);
  166.                 return true;
  167.             }
  168.         }
  169.  
  170.         if(dini_Exists(arquivoMgs)) {
  171.             if(strcmp(dini_Get(arquivoMgs, "Senha"), GET("senha"), false) == 0) {
  172.                 format(sockCmd, sizeof(sockCmd), "a=login&auth=true&ip=%s", GET("ip"));
  173.                 cmd(sockCmd);
  174.                 return true;
  175.             }
  176.         }
  177.  
  178.         format(sockCmd, sizeof(sockCmd), "a=login&auth=false&ip=%s", GET("ip"));
  179.         cmd(sockCmd);
  180.     } else if(strcmp(GET("a"), "getServerBasicStats", false) == 0) {
  181.         new mapName[64], hostname[64], _cmd[256];
  182.    
  183.         new onlinePlayers = GetPlayersOnServer();
  184.         new maxPlayers = GetMaxPlayers();
  185.  
  186.         GetServerVarAsString("mapname", mapName, sizeof(mapName));
  187.         GetServerVarAsString("hostname", hostname, sizeof(hostname));
  188.        
  189.         format(_cmd, sizeof(_cmd), "onlinePlayers=%d&maxPlayers=%d&mapname=%s&hostname=%s&cmdId=%s", onlinePlayers, maxPlayers, mapName, hostname, GET("cmdId"));
  190.         cmd(_cmd);
  191.     }
  192.  
  193.     return 1;
  194. }
  195.  
  196. public onUDPReceiveData(Socket:id, data[], data_len, remote_client_ip[], remote_client_port) {
  197.  
  198. }
  199.  
  200. GetPlayersOnServer() {
  201.     new count;
  202.     for(new x=0; x< MAX_PLAYERS; x++) { //x = MAX_PLAYERS
  203.         if(IsPlayerConnected(x)) {
  204.             count++;
  205.         }
  206.     }
  207.     return count;
  208. }
  209. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  210. //public onSocketClose(Socket:id) {
  211. //  print("teste");
  212. //}
  213. public OnPlayerDeath(playerid, killerid, reason)
  214. {
  215.     printf("Morreu!");
  216.     new cmdText[128], killerName[MAX_PLAYER_NAME], deadName[MAX_PLAYER_NAME];
  217.    
  218.     if(IsPlayerConnected(killerid)) {
  219.         GetPlayerName(killerid, killerName, sizeof(killerName));
  220.         GetPlayerName(playerid, deadName, sizeof(deadName));
  221.    
  222.         format(cmdText, sizeof(cmdText), "a=kill&killerName=%s&deadName=%s&reason=%d", killerName, deadName, reason);
  223.         cmd(cmdText);
  224.     }
  225. }
  226. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  227. public onSocketRemoteDisconnect(Socket:id, remote_clientid)
  228. {
  229.     printf("%d", remote_clientid);
  230.    
  231.     return true;
  232. }
  233. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  234. public OnPlayerConnect(playerid)
  235. {
  236.     new sockCmd[64], playerName[MAX_PLAYER_NAME];
  237.  
  238.     GetPlayerName(playerid, playerName, sizeof(playerName));
  239.  
  240.     format(sockCmd, sizeof(sockCmd), "a=playerConnect&nick=%s", playerName);
  241.     cmd(sockCmd);
  242.    
  243.     return 1;
  244. }
  245. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  246. public OnPlayerDisconnect(playerid, reason)
  247. {
  248.     new sockCmd[64], playerName[MAX_PLAYER_NAME];
  249.  
  250.     GetPlayerName(playerid, playerName, sizeof(playerName));
  251.  
  252.     format(sockCmd, sizeof(sockCmd), "a=playerDisconnect&nick=%s&reason=%d", playerName, reason);
  253.     cmd(sockCmd);
  254.    
  255.     return 1;
  256. }
  257. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  258. forward cmd(_str[]);
  259. public cmd(_str[]) {
  260.     new cd[600];
  261.  
  262.     if(strlen(_str) > 0) {
  263.         format(cd, sizeof(cd), "%s&p=%d&t=server", str_replace(" ", "+", _str), port);
  264.         socket_sendto_remote_client(nodejsSock, nodejsId, cd);
  265.         printf(">> %s", cd);
  266.     }
  267. }
  268.  
  269. forward tratar(parametros[]);
  270. public tratar(parametros[]) {
  271.     new vars[7][128], del[3][128], params[128];
  272.  
  273.     //A função explode dá crash no server se você passar uma string recebida por parâmetro diretamente para a função.
  274.     format(params, sizeof(params), "%s", parametros);
  275.  
  276.     explode(vars, params, "&");
  277.     for(new i = 0; i < sizeof(vars); ++i) {
  278.         if(strfind(vars[i], "=", true) != -1) {
  279.             explode(del, vars[i], "=");
  280.             format(tInds[i], 128, "%s", del[0]);
  281.  
  282.             format(del[1], sizeof(del[]), "%s", str_replace("\r\n", "", del[1]));
  283.             format(tVals[i], 128, "%s", del[1]);
  284.         }
  285.     }
  286. }
  287.  
  288. stock GET(ind[]) {
  289.     new result[128], i;
  290.  
  291.     i = 0;
  292.  
  293.     while(i < sizeof(tInds)) {
  294.         if(strcmp(tInds[i], ind, false) == 0) {
  295.             format(result, sizeof(result), "%s", tVals[i]);
  296.             return result;
  297.         }
  298.  
  299.         ++i;
  300.     }
  301.  
  302.     return result;
  303. }
  304. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  305. stock GetPlayerID(const playername[])
  306. {
  307.     new nome[MAX_PLAYER_NAME];
  308.     new playerid = INVALID_PLAYER_ID;
  309.     for (new i = 0; i < GetMaxPlayers(); i++)
  310.     {
  311.         if(IsPlayerConnected(i))
  312.         {
  313.             GetPlayerName(i,nome,MAX_PLAYER_NAME);
  314.             if(strcmp(playername, nome, true) == 0)
  315.             {
  316.                 playerid = i;
  317.                 break;
  318.             }
  319.         }
  320.     }
  321.     return playerid;
  322. }
  323. // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  324. stock GetStringText(const string[],idx,text[128])//Abit modified strtok function to return string after 1st blank space
  325. {
  326.     new length = strlen(string);
  327.     while ((idx < length) && (string[idx] <= ' ')) { idx++; }
  328.     new offset = idx; new result[128];
  329.     while ((idx < length) && ((idx - offset) < (sizeof(result) - 1))) { result[idx - offset] = string[idx]; idx++; }
  330.     result[idx - offset] = EOS;
  331.     text = result;
  332.     return result;
  333. }
RAW Paste Data
Top