Mentosan

Gamemode 0 - Login/Register system with MySQL

Oct 8th, 2018
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <a_samp>
  2. #include <zcmd>
  3. #include <sscanf2>
  4. #include <a_mysql>
  5. new SQL = -1, gQuery[256], gString[256];
  6.  
  7. #define function%0(%1)  forward %0(%1); public %0(%1)
  8. #define SCM             SendClientMessage
  9.  
  10. // Culori:
  11. #define COLOR_RED       0xB22222FF
  12. #define COLOR_ATTENTION 0xcdcdc1FF
  13.  
  14. enum {
  15.     // La Inregistrare
  16.     DIALOG_REGISTER,
  17.     DIALOG_EMAIL,
  18.     DIALOG_GENDER,
  19.     DIALOG_INFO,
  20.     // La Logare
  21.     DIALOG_LOGIN,
  22. }
  23.  
  24. enum pInfo {
  25.     pSQLID,
  26.     pName[MAX_PLAYER_NAME],
  27.     pPassword[32],
  28.     pEmail[32],
  29.     pGender,
  30.     pIP[16],
  31. }
  32. new PlayerInfo[MAX_PLAYERS][pInfo];
  33.  
  34. new
  35.     IncercariParola[MAX_PLAYERS];
  36.  
  37. main() {
  38.     print(" Gamemode-ul Mentosan in curs de procesare .. ");
  39. }
  40.  
  41.  
  42. public OnGameModeInit() {
  43.    
  44.     SQL = mysql_connect("localhost", "root", "mentos_db", "");
  45.     SetGameModeText("MENTOSAN V.0.01");
  46.     AddPlayerClass(0, 2007.1852, 1165.4506, 10.8136, 269.1425, 0, 0, 0, 0, 0, 0);
  47.     return 1;
  48. }
  49.  
  50. public OnGameModeExit() {
  51.  
  52.     return 1;
  53. }
  54.  
  55. public OnPlayerRequestClass(playerid, classid) {
  56.  
  57.     TogglePlayerSpectating(playerid, true);
  58.     return 1;
  59. }
  60.  
  61. public OnPlayerConnect(playerid) {
  62.  
  63.     IncercariParola[playerid] = 0;
  64.  
  65.     PlayAudioStreamForPlayer(playerid, "http://k003.kiwi6.com/hotlink/oo6ql0uclv/Mentoan-LoginRegister.mp3");
  66.     gQuery[0] = EOS;
  67.     mysql_format(SQL, gQuery, sizeof(gQuery), "SELECT * FROM `users` WHERE `Name`='%s' LIMIT 1", GetName(playerid));
  68.     mysql_tquery(SQL, gQuery, "verificareCont", "i", playerid);
  69.  
  70.     return 1;
  71. }
  72.  
  73. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  74.     switch(dialogid) {
  75.         case DIALOG_REGISTER: {
  76.             if(!response)
  77.                 return Kick(playerid);
  78.  
  79.             if(strlen(inputtext) < 6 || strlen(inputtext) > 32)
  80.                 ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Inregistrare Cont", " Introdu in spatiul de mai jos o parola pentru a te putea inregistra: (min 6caractere / max 32caractere)", "Done", "Cancel");
  81.             else {
  82.                 new playerIP[16];
  83.                 GetPlayerIp(playerid, playerIP, sizeof(playerIP));
  84.  
  85.                 gQuery[0] = EOS;
  86.                 mysql_format(SQL, gQuery, sizeof(gQuery), "INSERT INTO `users` (`Name`, `Password`, `IP`) VALUES ('%s', '%s', '%s')", GetName(playerid), inputtext, playerIP);
  87.                 mysql_tquery(SQL, gQuery, "AdaugareCont", "i", playerid);
  88.  
  89.                 format(PlayerInfo[playerid][pPassword], 32, inputtext);
  90.  
  91.                 ShowPlayerDialog(playerid, DIALOG_EMAIL, DIALOG_STYLE_INPUT, "Email", " Introdu E-Mail.ul contului tau: ", "Selecteaza", "Cancel");
  92.             }
  93.         }
  94.         case DIALOG_EMAIL: {
  95.             if(!response)
  96.                 return Kick(playerid);
  97.  
  98.             if(strlen(inputtext) < 4 || strlen(inputtext) > 32)
  99.                 ShowPlayerDialog(playerid, DIALOG_EMAIL, DIALOG_STYLE_INPUT, "Email", " Introdu E-Mail.ul contului tau: (min 4caractere / max 32caractere) ", "Selecteaza", "Cancel");
  100.  
  101.             gQuery[0] = EOS;
  102.             mysql_format(SQL, gQuery, sizeof(gQuery), "UPDATE `users` SET `Email`='%s' WHERE `ID`='%d'", inputtext, PlayerInfo[playerid][pSQLID]);
  103.             mysql_tquery(SQL, gQuery, "", "");
  104.  
  105.             format(PlayerInfo[playerid][pEmail], 32, inputtext);
  106.  
  107.             ShowPlayerDialog(playerid, DIALOG_GENDER, DIALOG_STYLE_MSGBOX, "Introduceti Sex-ul: ", " Introduceti Sex-Ul: ", "Masculin", "Feminin");
  108.         }
  109.         case DIALOG_GENDER: {
  110.             switch(response) {
  111.                 case 0: {
  112.                     PlayerInfo[playerid][pGender] = 0; // Masculin
  113.                 }
  114.                 case 1: {
  115.                     PlayerInfo[playerid][pGender] = 1; // Feminin
  116.                 }
  117.             }
  118.  
  119.             gQuery[0] = EOS;
  120.             mysql_format(SQL, gQuery, sizeof(gQuery), "UPDATE `users` SET `Gender`='%d' WHERE `ID`='%d'", PlayerInfo[playerid][pGender], PlayerInfo[playerid][pSQLID]);
  121.             mysql_tquery(SQL, gQuery, "", "");
  122.  
  123.             SpawnPlayer(playerid);
  124.             TogglePlayerSpectating(playerid, false);
  125.             StopAudioStreamForPlayer(playerid);
  126.             gString[0] = EOS;
  127.             //backup
  128.             format(gString, sizeof(gString), " Detalii despre contul %s[%d]: \n", GetName(playerid), playerid);
  129.             format(gString, sizeof(gString), "%s- Parola dvs contine %d caractere. \n", gString, strlen(PlayerInfo[playerid][pPassword]));
  130.             format(gString, sizeof(gString), "%s- Email-ul dvs este: %s\n", gString, PlayerInfo[playerid][pEmail]);
  131.             format(gString, sizeof(gString),  "%s- Sex-ul dvs este 1 - Masculin, 0 - Feminin (DVS: %d)", gString, PlayerInfo[playerid][pGender]);
  132.             ShowPlayerDialog(playerid, DIALOG_INFO, DIALOG_STYLE_MSGBOX, " Informatii Cont: ", gString, ".", ".");
  133.         }
  134.         case DIALOG_LOGIN: {
  135.             if(!response)
  136.                 Kick(playerid);
  137.  
  138.             mysql_format(SQL, gQuery, sizeof(gQuery), "SELECT * FROM `users` WHERE `Name`='%s' AND `Password`='%s' LIMIT 1", GetName(playerid), inputtext);
  139.             mysql_tquery(SQL, gQuery, "onLogin", "i", playerid);
  140.            
  141.             SpawnPlayer(playerid);
  142.             TogglePlayerSpectating(playerid, false);
  143.             StopAudioStreamForPlayer(playerid);
  144.         }
  145.     }
  146.  
  147.     return 1;
  148. }
  149.  
  150. function onLogin(playerid) {
  151.     switch(cache_num_rows()) {
  152.         case 0: {   //cand parola nu se potriveste cu cea din DATABASE
  153.             IncercariParola[playerid]++;
  154.  
  155.             gString[0] = EOS;
  156.             format(gString, sizeof(gString), "(!) Parola incorecta (%d/3 incercari ramase) ", IncercariParola[playerid]);
  157.             SCM(playerid, COLOR_RED, gString);
  158.  
  159.             if(IncercariParola[playerid] == 3)  Kick(playerid);
  160.             else ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logare Cont", " Introdu parola pentru a te putea loga: ", "Done", "Cancel");
  161.  
  162.         }
  163.         case 1: {
  164.             new rezultat[64];
  165.  
  166.             PlayerInfo[playerid][pSQLID] = cache_get_field_content_int(0, "ID");
  167.             PlayerInfo[playerid][pGender] = cache_get_field_content_int(0, "GENDER");
  168.  
  169.             cache_get_field_content(0, "Nume", rezultat);   format(PlayerInfo[playerid][pName], MAX_PLAYER_NAME, rezultat);
  170.             cache_get_field_content(0, "Parola", rezultat); format(PlayerInfo[playerid][pPassword], 32, rezultat);
  171.             cache_get_field_content(0, "Email", rezultat)format(PlayerInfo[playerid][pEmail], 32, rezultat);
  172.  
  173.             printf("(LOGARE):  %s (ID: %d) tocmai s-a logat! [Gender: %d, Email: %d, Password: %s", GetName(playerid), PlayerInfo[playerid][pSQLID], PlayerInfo[playerid][pGender], PlayerInfo[playerid][pEmail], PlayerInfo[playerid][pPassword]);
  174.         }
  175.     }
  176.  
  177.     return 1;
  178. }
  179.  
  180. function AdaugareCont(playerid) {
  181.     PlayerInfo[playerid][pSQLID] = cache_insert_id();
  182.     printf("(!) %s tocmai s-a inregistrat in baza de date cu SQL-ID #%d !", GetName(playerid), PlayerInfo[playerid][pSQLID]);
  183.  
  184.     return 1;
  185. }
  186.  
  187. function verificareCont(playerid) {
  188.     new rows, fields;
  189.     cache_get_data(rows, fields);
  190.  
  191.     if(rows)
  192.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logare:", " Introdu parola pentru a te putea conecta! ", "Conectare", "Iesire");
  193.     else {
  194.         ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Inregistrare Cont", " Introdu parola contului pentru a te putea inregistra! :", "Selecteaza", "Cancel");
  195.     }
  196.  
  197.     return 1;
  198. }
  199.  
  200. stock GetName(playerid) {
  201.     new playerName[MAX_PLAYER_NAME];
  202.     GetPlayerName(playerid, playerName, MAX_PLAYER_NAME);
  203.  
  204.     return playerName;
  205. }
RAW Paste Data