Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Filterscript utilizada para a comunicação entre a aplicação nodejs e o servidor sa-mp.
- **/
- #include <a_samp>
- #include <a_http>
- #include <strlib>
- #include <sockets>
- #include <DiniToMysql>
- enum SalaInfo {
- SalaID[64],
- TargetName[MAX_PLAYER_NAME]
- };
- new
- port,
- Socket:sock,
- Socket:nodejsSock,
- nodejsId,
- ipLocal[64],
- tInds[20][128],
- pmsSalasInfo[MAX_PLAYERS][SalaInfo],
- tVals[20][128],
- str[7][128]
- ;
- public OnFilterScriptInit()
- {
- format(ipLocal, sizeof(ipLocal), "127.0.0.1");
- // Servidor UDP
- sock = socket_create(TCP);
- socket_set_max_connections(sock, 100);
- socket_bind(sock, ipLocal);
- socket_listen(sock, 7790);
- printf("Servidor TCP lendo no ip %s porta 7778", ipLocal);
- return 1;
- }
- public OnPlayerCommandText(playerid, cmdtext[])
- {
- new command[128];
- new tmp[128];
- new idx;
- command = strtok(cmdtext,idx);
- if(strcmp(command, "/res", true) == 0) {
- new text[128], msg[128];
- GetStringText(cmdtext,idx,text);
- tmp = strtok(cmdtext, idx);
- if(!strlen(tmp)) return SendClientMessage(playerid, 0xFFFF00,"Responde a mp para o último jogador que interagiu com você pelo UCP. Uso: /res [mensagem]");
- if(!strlen(pmsSalasInfo[playerid][SalaID])) {
- return SendClientMessage(playerid, 0xFF0000, "Ninguém interagiu com você!");
- }
- new playerName[MAX_PLAYER_NAME], sockCmd[512];
- GetPlayerName(playerid, playerName, sizeof(playerName));
- format(msg, sizeof(msg), "Enviado para %s: %s", pmsSalasInfo[playerid][TargetName], text);
- SendClientMessage(playerid, 0x00FFFF, msg);
- format(sockCmd, sizeof(sockCmd), "a=particularMessage&from=%s&message=%s&salaId=%s", playerName, text, pmsSalasInfo[playerid][SalaID]);
- cmd(sockCmd);
- }
- return 1;
- }
- forward teste();
- public teste() {
- new ip[34];
- get_remote_client_ip(nodejsSock, nodejsId, ip);
- printf("%s", ip);
- return true;
- }
- public OnFilterScriptExit()
- {
- //socket_destroy(sock);
- return 1;
- }
- public OnPlayerText(playerid, text[]) {
- new sockCmd[64], playerName[MAX_PLAYER_NAME];
- GetPlayerName(playerid, playerName, sizeof(playerName));
- format(sockCmd, sizeof(sockCmd), "a=msg&nick=%s&msg=%s", playerName, text);
- cmd(sockCmd);
- return 1;
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- public onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid) {
- printf("Nova conexão estabelecida através do IP %s", remote_client);
- nodejsSock = id;
- nodejsId = remote_clientid;
- new nome[MAX_PLAYER_NAME], sockCmd[512];
- for (new i = 0; i < GetMaxPlayers(); i++)
- {
- if(IsPlayerConnected(i))
- {
- GetPlayerName(i,nome,MAX_PLAYER_NAME);
- format(sockCmd, sizeof(sockCmd), "a=playerConnect&nick=%s", nome);
- cmd(sockCmd);
- }
- }
- //SetTimer("teste", 10000, true);
- return 1;
- }
- public onSocketReceiveData(Socket:id, remote_clientid, data[], data_len) {
- printf("Comando recebido do cliente %d: %s", remote_clientid, data);
- new buffer[128], sockCmd[512];
- tratar(data);
- if(strcmp(GET("a"), "chatMessage", false) == 0) {
- format(buffer, sizeof(buffer), "[WebChat] %s: %s", GET("username"), str_replace("+", " ", GET("message")));
- SendClientMessageToAll(0xFFFFFFFF, buffer);
- } else if(strcmp(GET("a"), "particularMessage", false) == 0) {
- new targetId = GetPlayerID(GET("to"));
- new msg[512];
- if(IsPlayerConnected(targetId)) {
- format(msg, sizeof(msg), "[MP] %s: %s", GET("from"), GET("message"));
- printf("%s", GET("salaId"));
- format(pmsSalasInfo[targetId][TargetName], 256, "%s", GET("from"));
- format(pmsSalasInfo[targetId][SalaID], 256, "%s", GET("salaId"));
- SendClientMessage(targetId, 0x00FFFF, msg);
- }
- } else if(strcmp(GET("a"), "login", false) == 0) {
- new arquivoRpg[128], arquivoMgs[128];
- format(arquivoRpg, sizeof(arquivoRpg), "contas_rpg/%s.txt", GET("nick"));
- format(arquivoMgs, sizeof(arquivoMgs), "contas_mgs/%s.txt", GET("nick"));
- if(dini_Exists(arquivoRpg)) {
- if(strcmp(dini_Get(arquivoRpg, "Senha"), GET("senha"), false) == 0) {
- format(sockCmd, sizeof(sockCmd), "a=login&auth=true&ip=%s", GET("ip"));
- cmd(sockCmd);
- return true;
- }
- }
- if(dini_Exists(arquivoMgs)) {
- if(strcmp(dini_Get(arquivoMgs, "Senha"), GET("senha"), false) == 0) {
- format(sockCmd, sizeof(sockCmd), "a=login&auth=true&ip=%s", GET("ip"));
- cmd(sockCmd);
- return true;
- }
- }
- format(sockCmd, sizeof(sockCmd), "a=login&auth=false&ip=%s", GET("ip"));
- cmd(sockCmd);
- } else if(strcmp(GET("a"), "getServerBasicStats", false) == 0) {
- new mapName[64], hostname[64], _cmd[256];
- new onlinePlayers = GetPlayersOnServer();
- new maxPlayers = GetMaxPlayers();
- GetServerVarAsString("mapname", mapName, sizeof(mapName));
- GetServerVarAsString("hostname", hostname, sizeof(hostname));
- format(_cmd, sizeof(_cmd), "onlinePlayers=%d&maxPlayers=%d&mapname=%s&hostname=%s&cmdId=%s", onlinePlayers, maxPlayers, mapName, hostname, GET("cmdId"));
- cmd(_cmd);
- }
- return 1;
- }
- public onUDPReceiveData(Socket:id, data[], data_len, remote_client_ip[], remote_client_port) {
- }
- GetPlayersOnServer() {
- new count;
- for(new x=0; x< MAX_PLAYERS; x++) { //x = MAX_PLAYERS
- if(IsPlayerConnected(x)) {
- count++;
- }
- }
- return count;
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- //public onSocketClose(Socket:id) {
- // print("teste");
- //}
- public OnPlayerDeath(playerid, killerid, reason)
- {
- printf("Morreu!");
- new cmdText[128], killerName[MAX_PLAYER_NAME], deadName[MAX_PLAYER_NAME];
- if(IsPlayerConnected(killerid)) {
- GetPlayerName(killerid, killerName, sizeof(killerName));
- GetPlayerName(playerid, deadName, sizeof(deadName));
- format(cmdText, sizeof(cmdText), "a=kill&killerName=%s&deadName=%s&reason=%d", killerName, deadName, reason);
- cmd(cmdText);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- public onSocketRemoteDisconnect(Socket:id, remote_clientid)
- {
- printf("%d", remote_clientid);
- return true;
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- public OnPlayerConnect(playerid)
- {
- new sockCmd[64], playerName[MAX_PLAYER_NAME];
- GetPlayerName(playerid, playerName, sizeof(playerName));
- format(sockCmd, sizeof(sockCmd), "a=playerConnect&nick=%s", playerName);
- cmd(sockCmd);
- return 1;
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- public OnPlayerDisconnect(playerid, reason)
- {
- new sockCmd[64], playerName[MAX_PLAYER_NAME];
- GetPlayerName(playerid, playerName, sizeof(playerName));
- format(sockCmd, sizeof(sockCmd), "a=playerDisconnect&nick=%s&reason=%d", playerName, reason);
- cmd(sockCmd);
- return 1;
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- forward cmd(_str[]);
- public cmd(_str[]) {
- new cd[600];
- if(strlen(_str) > 0) {
- format(cd, sizeof(cd), "%s&p=%d&t=server", str_replace(" ", "+", _str), port);
- socket_sendto_remote_client(nodejsSock, nodejsId, cd);
- printf(">> %s", cd);
- }
- }
- forward tratar(parametros[]);
- public tratar(parametros[]) {
- new vars[7][128], del[3][128], params[128];
- //A função explode dá crash no server se você passar uma string recebida por parâmetro diretamente para a função.
- format(params, sizeof(params), "%s", parametros);
- explode(vars, params, "&");
- for(new i = 0; i < sizeof(vars); ++i) {
- if(strfind(vars[i], "=", true) != -1) {
- explode(del, vars[i], "=");
- format(tInds[i], 128, "%s", del[0]);
- format(del[1], sizeof(del[]), "%s", str_replace("\r\n", "", del[1]));
- format(tVals[i], 128, "%s", del[1]);
- }
- }
- }
- stock GET(ind[]) {
- new result[128], i;
- i = 0;
- while(i < sizeof(tInds)) {
- if(strcmp(tInds[i], ind, false) == 0) {
- format(result, sizeof(result), "%s", tVals[i]);
- return result;
- }
- ++i;
- }
- return result;
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- stock GetPlayerID(const playername[])
- {
- new nome[MAX_PLAYER_NAME];
- new playerid = INVALID_PLAYER_ID;
- for (new i = 0; i < GetMaxPlayers(); i++)
- {
- if(IsPlayerConnected(i))
- {
- GetPlayerName(i,nome,MAX_PLAYER_NAME);
- if(strcmp(playername, nome, true) == 0)
- {
- playerid = i;
- break;
- }
- }
- }
- return playerid;
- }
- // ------------------------------------------------------------------------------------------------------------------------------------------------------------- //
- stock GetStringText(const string[],idx,text[128])//Abit modified strtok function to return string after 1st blank space
- {
- new length = strlen(string);
- while ((idx < length) && (string[idx] <= ' ')) { idx++; }
- new offset = idx; new result[128];
- while ((idx < length) && ((idx - offset) < (sizeof(result) - 1))) { result[idx - offset] = string[idx]; idx++; }
- result[idx - offset] = EOS;
- text = result;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement