Advertisement
pmiranda

(MySQL) Base

May 20th, 2013
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 14.55 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <zcmd>
  4. #include <sscanf2>
  5. #include <CTime>
  6. //#include <streamer>
  7.  
  8. native gpci(playerid, serial[], len = sizeof serial);
  9.  
  10. // Constantes
  11. new const
  12.     SCRIPT_VERSION[] = "0.1",
  13.     SERVER_NAME[] = "(MySQL) Get started!",
  14.  
  15.     DB_HOST[] = "localhost",
  16.     DB_USER[] = "root",
  17.     DB_NAME[] = "rpg",
  18.     DB_PASS[] = "";
  19.  
  20.  
  21. new gstring[1024];
  22. new g_dbConnection = 0;
  23.  
  24.  
  25. #if defined MAX_PLAYERS
  26.     #undef MAX_PLAYERS
  27. #endif
  28.  
  29. enum {
  30.     MAX_PLAYERS         = 100,
  31.  
  32.     MIN_PASS_LENGTH     = 05,
  33.     MAX_PASS_LENGTH     = 32,
  34.     MAX_LOGIN_ATT       = 05,
  35. }
  36.  
  37.  
  38. enum {
  39.     // Register/login
  40.     DIALOG_REGISTER,
  41.     DIALOG_LOGIN
  42. }
  43.  
  44.  
  45.  
  46. // Correções
  47. #define Kick(%0) SetTimerEx("kick_Fix", 100, 0, "i", %0)
  48.  
  49.  
  50.  
  51. // Players
  52. enum E_PLAYER_INFO {
  53.     // Identificação
  54.     playerID,
  55.     playerName[25],
  56.     playerSerial[129],
  57.  
  58.     // Registro e login
  59.     playerPass[129],
  60.     playerLoginAtt,
  61.     playerLogged,
  62.  
  63.     // Importante
  64.     playerAdmin,
  65.  
  66.     // Personagem
  67.     playerMoney,
  68.     playerSkin
  69. }
  70. new gPlayerInfo[MAX_PLAYERS][E_PLAYER_INFO];
  71.  
  72.  
  73. main();
  74.  
  75. public OnGameModeInit() {
  76.  
  77.     g_dbConnection = mysql_connect(DB_HOST, DB_USER, DB_NAME, DB_PASS);
  78.     if(mysql_ping(g_dbConnection) == -1) {
  79.         print("\n\n!! Ocorreu um erro durante a tentativa de conexão com o banco de dados.");
  80.         print("!! O servidor não foi inicializado com êxito.\n\n");
  81.  
  82.         return SendRconCommand("exit");
  83.     }
  84.  
  85.     UsePlayerPedAnims();
  86.     ShowPlayerMarkers(0);
  87.     EnableStuntBonusForAll(0);
  88.     DisableInteriorEnterExits();
  89.  
  90.     SendRconCommand("mapname Los Santos");
  91.     SendRconCommand((format(gstring, sizeof gstring, "gamemodetext SA:MP v%s", SCRIPT_VERSION), gstring));
  92.     SendRconCommand((format(gstring, sizeof gstring, "hostname [BR] %s v%s", SERVER_NAME, SCRIPT_VERSION), gstring));
  93.     return true;
  94. }
  95.  
  96.  
  97. public OnGameModeExit() {
  98.  
  99.     mysql_close(g_dbConnection);
  100.     return true;
  101. }
  102.  
  103.  
  104. public OnPlayerConnect(playerid) {
  105.  
  106.     GetPlayerName(playerid, gPlayerInfo[playerid][playerName], 25);
  107.     gpci(playerid, gPlayerInfo[playerid][playerSerial]);
  108.  
  109.     SetPlayerColor(playerid, 0x696969ff);
  110.     TogglePlayerSpectating(playerid, true);
  111.  
  112.     Player_Init(playerid);
  113.     return true;
  114. }
  115.  
  116.  
  117. public OnPlayerDisconnect(playerid, reason) {
  118.  
  119.     if(gPlayerInfo[playerid][playerLogged]) {
  120.         gPlayerInfo[playerid][playerLogged] = 0;
  121.  
  122.         gPlayerInfo[playerid][playerMoney] = GetPlayerMoney(playerid);
  123.         gPlayerInfo[playerid][playerSkin] = GetPlayerSkin(playerid);
  124.  
  125.         mysql_format(g_dbConnection, gstring, sizeof gstring, "UPDATE `player_info` SET `money` = %d, `skin` = %d WHERE `id` = %d",
  126.             gPlayerInfo[playerid][playerMoney],
  127.             gPlayerInfo[playerid][playerSkin],
  128.             gPlayerInfo[playerid][playerID]);
  129.         mysql_function_query(g_dbConnection, gstring, false, #, #);
  130.     }
  131.     return true;
  132. }
  133.  
  134.  
  135. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  136.  
  137.     switch(dialogid) {
  138.         // Registro/login
  139.         case DIALOG_REGISTER: {
  140.             if(!response) {
  141.                 SendClientMessage(playerid, -1, "    Você optou por não se registrar no servidor.");
  142.                 SendClientMessage(playerid, -1, "    Logo, foi retirado do mesmo.");
  143.  
  144.                 Kick(playerid);
  145.             }
  146.             else {
  147.                 if(!((MIN_PASS_LENGTH - 1) < strlen(inputtext) < (MAX_PASS_LENGTH - 1))) {
  148.                     format(gstring, sizeof gstring, "{ffffff}Caro(a) {f6f600}%s{ffffff}, ocorreu um erro durante seu registro.\n\nSua senha deve conter entre {f6f600}%02d {ffffff}e {f6f600}%02d {ffffff}caracteres.\nPor favor, tente novamente.",
  149.                         gPlayerInfo[playerid][playerName], MIN_PASS_LENGTH, MAX_PASS_LENGTH);
  150.                     ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{ffffff}Registro", gstring, "Registrar", "Sair");
  151.                 }
  152.                 else {
  153.                     format(gPlayerInfo[playerid][playerPass], (MAX_PASS_LENGTH + 1), inputtext);
  154.  
  155.                     SendClientMessage(playerid, -1, "    Você se registrou com sucesso no servidor!");
  156.                     SendClientMessage(playerid, -1, "    Aguarde enquanto o redirecionamos para a cidade...");
  157.  
  158.                     mysql_format(g_dbConnection, gstring, sizeof gstring, "INSERT INTO `player_info` (`name`, `password`) VALUES ('%s', PASSWORD('%e'))",
  159.                         gPlayerInfo[playerid][playerName], gPlayerInfo[playerid][playerPass]);
  160.                     mysql_function_query(g_dbConnection, gstring, false, #, #);
  161.  
  162.                     Player_Login(playerid, gPlayerInfo[playerid][playerPass]);
  163.                 }
  164.             }
  165.         }
  166.         case DIALOG_LOGIN: {
  167.             if(!response) {
  168.                 SendClientMessage(playerid, -1, "    Você optou por não fazer login no servidor.");
  169.                 SendClientMessage(playerid, -1, "    Logo, foi retirado do mesmo.");
  170.  
  171.                 Kick(playerid);
  172.             }
  173.             else {
  174.                 Player_Login(playerid, inputtext);
  175.             }
  176.         }
  177.  
  178.         //
  179.     }
  180.     return true;
  181. }
  182.  
  183.  
  184.  
  185. // Players
  186. forward Player_Init(playerid);
  187. forward Player_Login(playerid, password[]);
  188.  
  189. forward r@Player_Init(playerid);
  190. forward r@Player_Login(playerid);
  191.  
  192.  
  193. public Player_Init(playerid) {
  194.  
  195.     mysql_format(g_dbConnection, gstring, sizeof gstring, "SELECT * FROM `player_info` WHERE `name` = '%s'",
  196.         gPlayerInfo[playerid][playerName]);
  197.  
  198.     mysql_function_query(g_dbConnection, gstring, true, "r@Player_Init", "i", playerid);
  199.     return true;
  200. }
  201.  
  202. public Player_Login(playerid, password[]) {
  203.  
  204.     mysql_format(g_dbConnection, gstring, sizeof gstring, "SELECT * FROM `player_info` WHERE `name` = '%s' AND `password` = PASSWORD('%e')",
  205.         gPlayerInfo[playerid][playerName], password);
  206.  
  207.     mysql_function_query(g_dbConnection, gstring, true, "r@Player_Login", "i", playerid);
  208.     return true;
  209. }
  210.  
  211.  
  212. public r@Player_Init(playerid) {
  213.  
  214.     new rows, fields;
  215.     cache_get_data(rows, fields, g_dbConnection);
  216.  
  217.     if(rows) {
  218.         // Registrado
  219.         format(gstring, sizeof gstring, "{ffffff}Olá, {f6f600}%s{ffffff}.\nSeja bem-vindo ao {f6f600}%s{ffffff}!\n\nDigite sua senha abaixo para fazer login.",
  220.             gPlayerInfo[playerid][playerName], SERVER_NAME);
  221.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", gstring, "Entrar", "Sair");
  222.     }
  223.     else {
  224.         // Não registrado
  225.         format(gstring, sizeof gstring, "{ffffff}Olá, {f6f600}%s{ffffff}.\nSeja bem-vindo ao {f6f600}%s{ffffff}!\nParece que você é novo por aqui, não?\n\nPara se registrar, basta digitar uma senha no campo abaixo.",
  226.             gPlayerInfo[playerid][playerName], SERVER_NAME);
  227.         ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{ffffff}Registro", gstring, "Registrar", "Sair");
  228.     }
  229.     return true;
  230. }
  231.  
  232. public r@Player_Login(playerid) {
  233.  
  234.     new rows, fields;
  235.     cache_get_data(rows, fields, g_dbConnection);
  236.  
  237.     if(rows) {
  238.         gPlayerInfo[playerid][playerLogged] = 1;
  239.  
  240.         gPlayerInfo[playerid][playerID] = cache_get_field_content_int(0, "id", g_dbConnection);
  241.  
  242.         cache_get_field_content(0, "password", gPlayerInfo[playerid][playerPass], g_dbConnection);
  243.  
  244.         gPlayerInfo[playerid][playerAdmin] = cache_get_field_content_int(0, "admin", g_dbConnection);
  245.  
  246.         GivePlayerMoney(playerid, (gPlayerInfo[playerid][playerMoney] = cache_get_field_content_int(0, "money", g_dbConnection)));
  247.         SetPlayerSkin(playerid, (gPlayerInfo[playerid][playerSkin] = cache_get_field_content_int(0, "skin", g_dbConnection)));
  248.  
  249.         SetSpawnInfo(playerid, 0, gPlayerInfo[playerid][playerSkin], 2247.6187, -1262.2136, 23.9550, 266.1028, 0, 0, 0, 0, 0, 0);
  250.  
  251.         SendClientMessage(playerid, -1, "Aguarde enquanto verificamos se há algum registro ativo de ban em sua conta.");
  252.         Ban_Check(playerid);
  253.     }
  254.     else {
  255.         gPlayerInfo[playerid][playerLoginAtt]++;
  256.         if(gPlayerInfo[playerid][playerLoginAtt] == MAX_LOGIN_ATT) {
  257.             SendClientMessage(playerid, -1, "    Você excedeu o limite de tentativas de login.");
  258.             SendClientMessage(playerid, -1, "    Por isso foi despejado do servidor.");
  259.  
  260.             Kick(playerid);
  261.         }
  262.         else {
  263.             format(gstring, sizeof gstring, "{ffffff}Senha incorreta! (%02d/%02d)\n\nCaro(a) {f6f600}%s{ffffff}, ocorreu um erro durante o processo de login.\nVocê digitou uma senha não corresponde à registrada no banco de dados.\nPor favor, tente novamente.",
  264.                 gPlayerInfo[playerid][playerLoginAtt], MAX_LOGIN_ATT, gPlayerInfo[playerid][playerName]);
  265.             ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", gstring, "Entrar", "Sair");
  266.         }
  267.     }
  268.     return true;
  269. }
  270.  
  271.  
  272.  
  273. // Ban
  274. forward Ban_Apply(playerid, admin[], reason[], time);
  275. forward Ban_Check(playerid);
  276.  
  277. forward r@Ban_Check(playerid);
  278.  
  279.  
  280. public Ban_Apply(playerid, admin[], reason[], time) {
  281.  
  282.     if(time != -1)
  283.         time *= 86400;
  284.  
  285.     mysql_format(g_dbConnection, gstring, sizeof gstring, "INSERT INTO `ban_info` (`pid`, `admin`, `reason`, `date`, `expire`) VALUES (%d, '%s', '%s', UNIX_TIMESTAMP(), %d)",
  286.         gPlayerInfo[playerid][playerID], admin, reason,
  287.         time != -1 ? gettime() + time : time);
  288.     mysql_function_query(g_dbConnection, gstring, false, #, #);
  289.  
  290.     if(time == -1)
  291.         format(gstring, sizeof gstring, "AdmCmd: %s foi banido por tempo indeterminado pelo admin %s. (%s)", gPlayerInfo[playerid][playerName], admin, reason);
  292.     else
  293.         format(gstring, sizeof gstring, "AdmCmd: %s foi banido por %02d dias pelo admin %s. (%s)", gPlayerInfo[playerid][playerName], (time / 86400), admin, reason);
  294.     SendClientMessageToAll(0xff0000ff, gstring);
  295.  
  296.     Kick(playerid);
  297.     return true;
  298. }
  299.  
  300. public Ban_Check(playerid) {
  301.  
  302.     mysql_format(g_dbConnection, gstring, sizeof gstring, "SELECT * FROM `ban_info` WHERE `pid` = %d AND (`expire` = -1 OR `expire` > UNIX_TIMESTAMP())",
  303.         gPlayerInfo[playerid][playerID]);
  304.     mysql_function_query(g_dbConnection, gstring, true, "r@Ban_Check", "i", playerid);
  305.     return true;
  306. }
  307.  
  308.  
  309. public r@Ban_Check(playerid) {
  310.  
  311.     new rows, fields;
  312.     cache_get_data(rows, fields, g_dbConnection);
  313.  
  314.     if(rows) {
  315.         // Registros encontrados
  316.         SendClientMessage(playerid, -1, "Foram encontrados registros ativos de ban em sua conta.");
  317.         SendClientMessage(playerid, -1, "Verifique as ocorrências e busque uma revisão caso necessário.");
  318.  
  319.         new admin[25], reason[64],
  320.             Time:date, Time:expire,
  321.             tm<tmTime>;
  322.  
  323.         cache_get_field_content(0, "admin", admin, g_dbConnection);
  324.         cache_get_field_content(0, "reason", reason, g_dbConnection);
  325.  
  326.         date = Time:cache_get_field_content_int(0, "date", g_dbConnection);
  327.         localtime(Time:date, tmTime);
  328.         strftime(gstring, sizeof gstring, "%d/%m/%Y às %X", tmTime);
  329.  
  330.         format(gstring, sizeof gstring, "    %s, aplicado pelo administrador %s.", gstring, admin);
  331.         SendClientMessage(playerid, -1, gstring);
  332.  
  333.         format(gstring, sizeof gstring, "    Motivo: %s", reason);
  334.         SendClientMessage(playerid, -1, gstring);
  335.  
  336.         expire = Time:cache_get_field_content_int(0, "expire", g_dbConnection);
  337.         if(_:expire != -1) {
  338.             localtime(Time:expire, tmTime);
  339.             strftime(gstring, sizeof gstring, "%d/%m/%Y às %X", tmTime);
  340.  
  341.             format(gstring, sizeof gstring, "    Expira em %s", gstring);
  342.             SendClientMessage(playerid, -1, gstring);
  343.         }
  344.         else {
  345.             SendClientMessage(playerid, -1, "    Não há um prazo determinado para expirar.");
  346.         }
  347.        
  348.         Kick(playerid);
  349.     }
  350.     else {
  351.         // Nenhum registro encontrado. Liberado!
  352.         SendClientMessage(playerid, -1, "    Nenhum registro encontrado, tenha um bom jogo!");
  353.  
  354.         TogglePlayerSpectating(playerid, false);
  355.         SpawnPlayer(playerid);
  356.     }
  357.     return true;
  358. }
  359.  
  360.  
  361.  
  362. // AdmCmd: Comandos de exemplo
  363. CMD:admin(playerid, params[]) {
  364.  
  365.     if(!gPlayerInfo[playerid][playerAdmin])
  366.         return SendClientMessage(playerid, 0xff0000ff, "(!) Ação exclusiva de administradores.");
  367.  
  368.     new targetid;
  369.     if(sscanf(params, "u", targetid))
  370.         return SendClientMessage(playerid, 0x32cd32ff, "CMD: /admin <id/nome>");
  371.  
  372.     if(!gPlayerInfo[targetid][playerLogged])
  373.         return SendClientMessage(playerid, 0xff0000ff, "(!) O jogador não efetuou login.");
  374.  
  375.     format(gstring, sizeof gstring, "  Você promoveu %s a administrador.", gPlayerInfo[targetid][playerName]);
  376.     SendClientMessage(playerid, 0x32cd32ff, gstring);
  377.     format(gstring, sizeof gstring, "  %s promoveu você a administrador.", gPlayerInfo[playerid][playerName]);
  378.     SendClientMessage(playerid, 0x32cd32ff, gstring);
  379.  
  380.     gPlayerInfo[targetid][playerAdmin] = 1;
  381.  
  382.     mysql_format(g_dbConnection, gstring, sizeof gstring, "UPDATE `player_info` SET `admin` = 1 WHERE `id` = %d", gPlayerInfo[targetid][playerID]);
  383.     mysql_function_query(g_dbConnection, gstring, false, #, #);
  384.     return true;
  385. }
  386.  
  387.  
  388. CMD:ban(playerid, params[]) {
  389.  
  390.     if(!gPlayerInfo[playerid][playerAdmin])
  391.         return SendClientMessage(playerid, 0xff0000ff, "(!) Ação exclusiva de administradores.");
  392.  
  393.     new targetid, reason[64], days = -1;
  394.     if(sscanf(params, "us[64]I(-1)", targetid, reason, days)) {
  395.         SendClientMessage(playerid, 0x32cd32ff, "CMD: /ban <id/nome> <motivo> <dias>");
  396.         SendClientMessage(playerid, 0x32cd32ff, "     Para banir por tempo indeterminado, não digite os dias ou coloque \"-1\".");
  397.         return true;
  398.     }
  399.  
  400.     if(!gPlayerInfo[targetid][playerLogged])
  401.         return SendClientMessage(playerid, 0xff0000ff, "(!) O jogador não efetuou login.");
  402.  
  403.     Ban_Apply(targetid, gPlayerInfo[playerid][playerName], reason, days);
  404.     return true;
  405. }
  406.  
  407.  
  408. CMD:grana(playerid, params[]) {
  409.  
  410.     if(!gPlayerInfo[playerid][playerAdmin])
  411.         return SendClientMessage(playerid, 0xff0000ff, "(!) Ação exclusiva de administradores.");
  412.  
  413.     new targetid, amount;
  414.     if(sscanf(params, "ui", targetid, amount))
  415.         return SendClientMessage(playerid, 0x32cd32ff, "CMD: /grana <id/nome> <valor>");
  416.  
  417.     if(!gPlayerInfo[targetid][playerLogged])
  418.         return SendClientMessage(playerid, 0xff0000ff, "(!) O jogador não efetuou login.");
  419.  
  420.     GivePlayerMoney(targetid, amount);
  421.     return true;
  422. }
  423.  
  424.  
  425. CMD:skin(playerid, params[]) {
  426.  
  427.     if(!gPlayerInfo[playerid][playerAdmin])
  428.         return SendClientMessage(playerid, 0xff0000ff, "(!) Ação exclusiva de administradores.");
  429.  
  430.     new targetid, skinid;
  431.     if(sscanf(params, "ui", targetid, skinid))
  432.         return SendClientMessage(playerid, 0x32cd32ff, "CMD: /skin <id/nome> <skinid>");
  433.  
  434.     if(!gPlayerInfo[targetid][playerLogged])
  435.         return SendClientMessage(playerid, 0xff0000ff, "(!) O jogador não efetuou login.");
  436.  
  437.     SetPlayerSkin(targetid, skinid);
  438.     return true;
  439. }
  440.  
  441.  
  442. CMD:v(playerid, params[]) {
  443.  
  444.     if(!gPlayerInfo[playerid][playerAdmin])
  445.         return SendClientMessage(playerid, 0xff0000ff, "(!) Ação exclusiva de administradores.");
  446.  
  447.     new modelid, color1, color2;
  448.     if(sscanf(params, "iii", modelid, color1, color2))
  449.         return SendClientMessage(playerid, 0x32cd32ff, "CMD: /v <modelo> <cor 1> <cor 2>");
  450.  
  451.     if(modelid < 400 || modelid > 611)
  452.         return true;
  453.     if(color1 < 0 || color2 < 0 || color1 > 255 || color2 > 255)
  454.         return true;
  455.  
  456.     new Float:p[4];
  457.     GetPlayerPos(playerid, p[0], p[1], p[2]);
  458.     GetPlayerFacingAngle(playerid, p[3]);
  459.  
  460.     PutPlayerInVehicle(playerid, CreateVehicle(modelid, p[0], p[1], p[2], p[3], color1, color2, -1), 0);
  461.     return true;
  462. }
  463.  
  464.  
  465.  
  466.  
  467.  
  468. // Correção bug kick (0.3x)
  469. forward kick_Fix(playerid);
  470. public kick_Fix(playerid) {
  471.    
  472.     #undef Kick
  473.  
  474.     Kick(playerid);
  475.     #define Kick(%0) SetTimerEx("kick_Fix", 100, 0, "i", %0)
  476.     return true;
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement