Advertisement
Lorenc

SQLite - Register system [ TUTORIAL FS ]

Aug 20th, 2011
3,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.71 KB | None | 0 0
  1. /*
  2.  *      SQLite register system
  3.  *
  4.  *          Used rBits.inc the best for creating efficient arrays.
  5.  *          codename: lorencRegister
  6.  *
  7.  *      Created by Lorenc (c)
  8. */
  9.  
  10. #include                        <a_samp>
  11. #include                        <rBits>
  12.  
  13. /* ** Configuration ** */
  14. #define FILE_VERSION            1.7
  15.  
  16. /* ** Colours ** */
  17. #define COL_GREEN               "{6EF83C}" // GREEN color in HEX ( USED )
  18. #define COL_RED                 "{F81414}" // RED color in HEX ( USED )
  19. #define COL_BLUE                "{00C0FF}" // BLUE color in HEX ( USED )
  20.  
  21. /* ** Player Data ** */
  22. new
  23.     Bit1:  g_PlayerLogged       <MAX_PLAYERS>, // Creates a 1 bit array
  24.     Bit16: g_AdminLevel         <MAX_PLAYERS>, // Creates a 16 bit array again.
  25.     DB: Database
  26. ;
  27.  
  28.  
  29. public OnFilterScriptInit()
  30. {
  31.     print("\nLorencRegister System ( VERSION "#FILE_VERSION" )\n");
  32.     Database = db_open("ServerDatabase.db"); // This will open the database under the name, 'ServerDatabase.db'
  33.     //If the table does not excists then we're going to write it inside.
  34.     db_query(Database, "CREATE TABLE IF NOT EXISTS `USERS` (`NAME`, `PASSWORD`, `IP`, `SCORE`, `CASH`, `ADMINLEVEL`)");
  35.     return 1;
  36. }
  37.  
  38. public OnFilterScriptExit()
  39. {
  40.     for(new i; i != MAX_PLAYERS; i++) OnPlayerDisconnect(i, 1);
  41.     db_close(Database);
  42.     return 1;
  43. }
  44.  
  45. public OnPlayerConnect(playerid)
  46. {
  47.     new
  48.         Query[ 150 ], // Create a Query
  49.         DBResult: Result, // Create a database Result
  50.         name[ MAX_PLAYER_NAME ] // create a name string
  51.     ;
  52.     GetPlayerName(playerid, name, sizeof(name)); // Gather the players name.
  53.    
  54.     Bit1_Set(g_PlayerLogged, playerid, false); // We're going to reset this variable to 0 = false.
  55.    
  56.     // Okay, we're now going to select from `USERS` where the name equals the players name.
  57.     format(Query, sizeof(Query), "SELECT `NAME` FROM `USERS` WHERE `NAME` = '%s'", DB_Escape(name));
  58.     // We're going to insert the query inside the db result. Query is to execute what ever said to the DB
  59.     Result = db_query(Database, Query);
  60.     // If the num rows are there, then that means his registered.
  61.     if(db_num_rows(Result))
  62.     {
  63.         // Send a welcome message
  64.         format(Query, sizeof(Query), "{FFFFFF}Welcome "COL_BLUE"%s(%d){FFFFFF} to the server, you're registered\n\nPlease log in by inputting your password.", name, playerid);
  65.         // Show a player the dialog. ( dialog login )
  66.         ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "{FFFFFF}Register System", Query, "Login", "Leave");
  67.     }
  68.     else // Else if he isn't, he isn't registered.
  69.     {
  70.         // Send a welcome message
  71.         format(Query, sizeof(Query), "{FFFFFF}Welcome "COL_BLUE"%s(%d){FFFFFF} to the server, you're "COL_RED"not{FFFFFF} registered\n\nPlease log in by inputting your password.", name, playerid);
  72.         // Show a player the dialog. ( dialog register )
  73.         ShowPlayerDialog(playerid, 0, DIALOG_STYLE_INPUT, "{FFFFFF}Register System", Query, "Register", "Leave");
  74.     }
  75.     db_free_result(Result);
  76.     return 1;
  77. }
  78.  
  79. public OnPlayerDisconnect(playerid, reason)
  80. {
  81.     new
  82.         Query[ 200 ], // We need to create such a query so we can format it.
  83.         name[ MAX_PLAYER_NAME ] // create a name string
  84.     ;
  85.     GetPlayerName(playerid, name, sizeof(name)); // Gather the name of the player.
  86.  
  87.     if(Bit1_Get(g_PlayerLogged, playerid) == 1)
  88.     {
  89.         // Formatting the query containing all the updating stuff, this will update the database with the latest information.
  90.         format(Query,sizeof(Query),"UPDATE `USERS` SET SCORE = '%d', CASH = '%d', ADMINLEVEL = '%d' WHERE `NAME` = '%s'",
  91.                                                                                         GetPlayerScore(playerid), // Gather the player's score
  92.                                                                                         GetPlayerMoney(playerid), // Gather the player's money
  93.                                                                                         Bit16_Get(g_AdminLevel, playerid), // Gather the Admin Level
  94.                                                                                         DB_Escape(name)); // Gather the name of the player then escape it.
  95.         // querying the formatted Query
  96.         db_query(Database, Query);
  97.         // We're going to reset this bit array to 0, = false.
  98.         Bit1_Set(g_PlayerLogged, playerid, false);
  99.     }
  100.     return 1;
  101. }
  102.  
  103. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  104. {
  105.     new
  106.         Query[ 256 ], // Creating a big array this time. No lies :X
  107.         DBResult: Result, // Create a database Result
  108.         name[ MAX_PLAYER_NAME ], // create a name string
  109.         ip[ 16 ] // Create a IP string
  110.     ;
  111.     GetPlayerName(playerid, name, sizeof(name)); // Gather the name of the player.
  112.     GetPlayerIp(playerid, ip, sizeof(ip));
  113.    
  114.     if(dialogid == 1) // The core of the login dialog
  115.     {
  116.         if(response) // Do what ever if the player selected 'Login"
  117.         {
  118.             // if  we find inside users the NAME equaled to the players name and the password equals username then
  119.             format(Query, sizeof(Query), "SELECT * FROM `USERS` WHERE `NAME` = '%s' AND `PASSWORD` = '%s'", DB_Escape(name), DB_Escape(inputtext));
  120.             // storing ^ that inside the databases result
  121.             Result = db_query(Database, Query);
  122.             // if the result is found that the user and password are exact, do below
  123.             if(db_num_rows(Result))
  124.             {
  125.                 new Field[ 20 ]; //Creating a field to retrieve the data
  126.                 // get the field "SCORE"'s value and insert it to Field itself.
  127.                 db_get_field_assoc(Result, "SCORE", Field, 30);
  128.                 // Set the Player score
  129.                 SetPlayerScore(playerid, strval(Field));
  130.                 // strval is to convert a string into a number
  131.                 // Now we keep doing the same thing with the rest.
  132.  
  133.                 // get the field "SCORE"'s value and insert it to Field itself.
  134.                 db_get_field_assoc(Result, "CASH", Field, 30);
  135.                 // Give the player the value of the field the amount of cash needed
  136.                 GivePlayerMoney(playerid, strval(Field));
  137.  
  138.                 // get the field "SCORE"'s value and insert it to Field itself.
  139.                 db_get_field_assoc(Result, "ADMINLEVEL", Field, 30);
  140.                 // Set the value of the bit "g_AdminLevel" to the fields value
  141.                 Bit16_Set(g_AdminLevel, playerid, strval(Field));
  142.                 // Log in the player
  143.                 Bit1_Set(g_PlayerLogged, playerid, true);
  144.                 // Send a client message about how the progress was between logging in
  145.                 SendClientMessage(playerid, -1, "You have "COL_GREEN"successfully{FFFFFF} logged in! ");
  146.             }
  147.             else // If the player's password is wrong:
  148.             {
  149.                 // Send a welcome message
  150.                 format(Query, sizeof(Query), "{FFFFFF}Welcome "COL_BLUE"%s(%d){FFFFFF} to the server, you're registered\n\nPlease log in by inputting your password.", name, playerid);
  151.                 // Show a player the dialog. ( dialog login )
  152.                 ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "{FFFFFF}Register System", Query, "Login", "Leave");
  153.                 // Show the player the wrong password message.
  154.                 SendClientMessage(playerid, -1, ""COL_RED"Wrong{FFFFFF} password, try again!");
  155.             }
  156.             db_free_result(Result);
  157.         }
  158.         else return Kick(playerid); // Kick the player if he selected 'Leave'
  159.     }
  160.     if(dialogid == 0) // The core of the register dialog
  161.     {
  162.         if(response) // Do what ever if the player selected 'Register"
  163.         {
  164.             //checking if the password is not is less/higher than 3 characters
  165.             if(strlen(inputtext) > 24 || strlen(inputtext) < 3)
  166.             {
  167.                 // Send a welcome message
  168.                 format(Query, sizeof(Query), "{FFFFFF}Welcome "COL_BLUE"%s(%d){FFFFFF} to the server, you're "COL_RED"not{FFFFFF} registered\n\nPlease log in by inputting your password.", name, playerid);
  169.                 // Reshow this dialog, so we can do this again.
  170.                 ShowPlayerDialog(playerid, 0, DIALOG_STYLE_INPUT, "{FFFFFF}Register System", Query, "Register", "Leave");
  171.                 // Send a message about the length of characters used for their password.
  172.                 SendClientMessage(playerid, -1, "Your password length must be from 3 - 24 characters!");
  173.             }
  174.             else
  175.             {
  176.                 // Inserting all these items into the database, confirming it's register was successful.
  177.                 format(Query, sizeof(Query), "INSERT INTO `USERS` (`NAME`, `PASSWORD`, `IP`, `SCORE`, `CASH`, `ADMINLEVEL`) VALUES('%s','%s','%s', '0', '500', '0')", DB_Escape(name), DB_Escape(inputtext), DB_Escape(ip));
  178.                 // Querying the formatted Query ^
  179.                 db_query(Database, Query);
  180.                 // Log in the player
  181.                 Bit1_Set(g_PlayerLogged, playerid, true);
  182.                 GivePlayerMoney(playerid, 500); // Give Player the money.
  183.                 // Reset score.
  184.                 SetPlayerScore(playerid, 0);
  185.                 // Show a message :)
  186.                 SendClientMessage(playerid, -1, "You have "COL_GREEN"successfully{FFFFFF} registered! You have been automatically logged in!");
  187.             }
  188.         }
  189.         else return Kick(playerid); // Kick the player if he selected 'Leave'
  190.     }
  191.     return 1;
  192. }
  193.  
  194. stock DB_Escape(text[])
  195. {
  196.     new
  197.         ret[80 * 2],
  198.         ch,
  199.         i,
  200.         j;
  201.     while ((ch = text[i++]) && j < sizeof (ret))
  202.     {
  203.         if (ch == '\'')
  204.         {
  205.             if (j < sizeof (ret) - 2)
  206.             {
  207.                 ret[j++] = '\'';
  208.                 ret[j++] = '\'';
  209.             }
  210.         }
  211.         else if (j < sizeof (ret))
  212.         {
  213.             ret[j++] = ch;
  214.         }
  215.         else
  216.         {
  217.             j++;
  218.         }
  219.     }
  220.     ret[sizeof (ret) - 1] = '\0';
  221.     return ret;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement