Advertisement
iSphex

[MySQL] spRegister V0.1

Apr 13th, 2012
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.55 KB | None | 0 0
  1. /*
  2.  .--.       .-.              
  3. : .--'      : :              
  4. `. `. .---. : `-.  .--. .-.,-.
  5.  _`, :: .; `: .. :' '_.'`.  .'
  6. `.__.': ._.':_;:_;`.__.':_,._;
  7.       : :                    
  8.       :_;                    
  9.      
  10.     spRegister by Sphex
  11.     Creation Date: 12/04/2012 20:48
  12.     Version: 0.1
  13. */
  14.  
  15. // =-=-= [ Includes ] =-=-=
  16. #include <a_samp>
  17. #include <a_mysql>
  18.  
  19. // =-=-= [ Defines ] =-=-=
  20. // General
  21. #define VERSION "0.1"
  22. #define GN GetName(playerid)
  23.  
  24. // SQL
  25. #define sql_host "localhost" // change to your host
  26. #define sql_user "root" // change to your username
  27. #define sql_pass "" // change to your password
  28. #define sql_db "spregister" // change to your db name
  29.  
  30. // Dialogs
  31. #define DIALOG_MSG 900 // change if the id already exists
  32. #define DIALOG_REGISTER 901 // change if the id already exists
  33. #define DIALOG_LOGIN 902 // change if the id already exists
  34.  
  35. // Dialog Colors
  36. #define DIALOG_GOLD "{B8860A}"
  37. #define DIALOG_GREEN "{33AA33}"
  38. #define DIALOG_YELLOW "{FFFF00}"
  39. #define DIALOG_RED "{AA3333}"
  40. #define DIALOG_LIGHTBLUE "{33CCFF}"
  41.  
  42. // =-=-= [ Vars ] =-=-=
  43. new str[128], query[300];
  44.  
  45. // =-=-= [ Publics ] =-=-=
  46. public OnFilterScriptInit() {
  47.     print("\n\t____________________________________");
  48.     printf("\n\t__________ spRegister V%s _________", VERSION);
  49.     print("\n\t____________________________________\n");
  50.    
  51.     mysql_debug(1);
  52.     mysql_connect(sql_host, sql_user, sql_db, sql_pass);
  53.     mysql_query("CREATE TABLE IF NOT EXISTS users(id INT NULL AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(250), PRIMARY KEY(id))");
  54.     return 1;
  55. }
  56.  
  57. public OnPlayerConnect(playerid) {
  58.     format(str, sizeof(str), "[spRegister V%s] This server uses a registration system built by Sphex.", VERSION);
  59.     SendClientMessage(playerid, 0xFFFF00AA, str);
  60.    
  61.     format(query, sizeof(query), "SELECT id FROM users WHERE username='%s'", GN);
  62.     mysql_query(query);
  63.     mysql_store_result();
  64.     if (mysql_num_rows() == 0) { // not registered
  65.         format(str, sizeof(str), ""DIALOG_YELLOW"Welcome %s!"DIALOG_LIGHTBLUE"\nPlease type in a password to register:", GN);
  66.         ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""DIALOG_GOLD"Registration", str, "Register", "Cancel");
  67.     } else { // registered
  68.         format(str, sizeof(str), ""DIALOG_YELLOW"Welcome %s!"DIALOG_LIGHTBLUE"\nPlease type in a password to login:", GN);
  69.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""DIALOG_GOLD"Login", str, "Login", "Cancel");
  70.     }
  71.     mysql_free_result();
  72.     return 1;
  73. }
  74.  
  75. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  76.     if (dialogid == DIALOG_REGISTER) {
  77.         if (!response) return Kick(playerid);
  78.         if (!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""DIALOG_GOLD"Registration", ""DIALOG_RED"Please type in a password!\n"DIALOG_LIGHTBLUE"Please type in a password to register:", "Register", "Cancel");
  79.         format(query, sizeof(query), "INSERT INTO users VALUES(NULL, '%s', '%d')", GN, udb_hash(inputtext));
  80.         mysql_query(query);
  81.         mysql_free_result();
  82.         return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""DIALOG_GOLD"Login", ""DIALOG_GREEN"You have been successfully registered!\n"DIALOG_LIGHTBLUE"Please type in a password to login:", "Login", "Cancel");
  83.     }
  84.    
  85.     if (dialogid == DIALOG_LOGIN) {
  86.         if (!response) return Kick(playerid);
  87.         if (!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""DIALOG_GOLD"Login", ""DIALOG_RED"Please type in a password!\n"DIALOG_LIGHTBLUE"Please type in a password to login:", "Login", "Cancel");
  88.         format(query, sizeof(query), "SELECT id FROM users WHERE password='%d' AND username='%s'", udb_hash(inputtext), GN);
  89.         mysql_query(query);
  90.         mysql_store_result();
  91.         if (mysql_num_rows() == 0) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""DIALOG_GOLD"Login", ""DIALOG_RED"The password you've entered is invalid!\n"DIALOG_LIGHTBLUE"Please type in a password to login:", "Login", "Cancel");
  92.         mysql_free_result();
  93.         return ShowPlayerDialog(playerid, DIALOG_MSG, DIALOG_STYLE_MSGBOX, ""DIALOG_GOLD"Login", ""DIALOG_GREEN"You have been successfully logged in!", "Okay", "");
  94.     }
  95.     return 0;
  96. }
  97.  
  98. // =-=-= [ Stocks ] =-=-=
  99. stock GetName(playerid) {
  100.     new pname[MAX_PLAYER_NAME];
  101.     GetPlayerName(playerid, pname, sizeof(pname));
  102.     return pname;
  103. }
  104.  
  105. stock udb_hash(buf[]) { //Credits to Dracoblue
  106.     new length=strlen(buf);
  107.     new s1 = 1;
  108.     new s2 = 0;
  109.     new n;
  110.     for (n=0; n<length; n++)
  111.     {
  112.        s1 = (s1 + buf[n]) % 65521;
  113.        s2 = (s2 + s1)     % 65521;
  114.     }
  115.     return (s2 << 16) + s1;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement