SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 13/09/2012, 07:12 PM   #1
FireCat
High-roller
 
FireCat's Avatar
 
Join Date: Jul 2010
Posts: 2,322
Reputation: 609
Default How to use BUD - Blazing User DB

Introduction
Well, for the 23453245234th time that I've been unbanned, I would like to start off (again) by making a tutorial!
Its going to be about BUD - Blazing User Database.

BUD uses SQLite, I don't know whether if Slice is going to make a MySQL or not.
I find this include very efficient, fast and easy!

Needed
For this tutorial, you will need:

sscanf 2.6 - ******
BUD - Blazing User DB - Slice

Scripting
So under OnGameModeInit or OnFilterScriptInit you want to add this:
pawn Code:
BUD::Setting(opt.Database, "DATABASENAME.db");

What does that mean?
That means we are setting the database variable to the database selected.
So like that we don't need to keep declaring the name of the database, and aswell it will be easier for the
database management!
If the database is not created, it will create it automatically.

Another thing you want to add (not necessary) is (still under the OnGameModeInit or filterscript)
pawn Code:
BUD::Setting(opt.KeepAliveTime, 3000);
That means, each time the BUD system calls a query, it will keep the database opened for 3 seconds / 3000 miliseconds.
Thats a good thing, because like that, the server has a better performance, and makes things run faster!

Now, before we do anything else, we want to initialize the BUD system.
So after all the stuff we putted under OnGameModeInit/OnFilterScriptInit, you have to put this:
pawn Code:
BUD::Initialize( );
That means the system is ready to be used, and it will set all the values to the default ones, that is, if we haven't changed them to be the contrary.
For example, we can use whirlpool to automatically hash things like passwords.
And it will check if its hashing stuff correctly, if its not, it will send a message on the console, informing you that whirpool is not running correctly.

After you initialized the system, you want to check if the columns are created/correct in the database that we declared before.
So for us to do that, theres a function called "BUD::VerifyColumn(columnname[], BUD::TYPE);"
Lets quickly strip that down.
pawn Code:
BUD::VerifyColumn(
columname[] <- The column that we want to check in the database.
BUD::TYPE <- Declare if its an integer, float, array,
"BUD::TYPE_NUMBER,
BUD::TYPE_FLOAT,
BUD::TYPE_STRING"

If I wanted to check if theres a column created in the database called "Money" I would do:
pawn Code:
BUD::VerifyColumn("Money", BUD::TYPE_NUMBER);
If the column is not created, it will attempt to create it.
If it fails to create the column, it will appear in the console saying that BUD
Was not able to create the column.

But, imagine I want to set automatically the column to a default value other than 0
Its easy!
Lets use for example 100.
Each time a new row is created, it will set the "Money" column to 100.
pawn Code:
BUD::VerifyColumn("Money", BUD::TYPE_NUMBER, 100);


Understood all of that? Great!
Lets check if the player is register.
So, the function is:
BUD::IsNameRegistered( name[] );

An example:
pawn Code:
public OnPlayerConnect(playerid)
{
new pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof pName);
if(BUD::IsNameRegistered(pName))
SendClientMessage(playerid, -1, "Your username is registered!");
else
SendClientMessage(playerid, -4, "Your username is not registered!");
return 1;
}
So, we're using the callback OnPlayerConnect, the most used callback to check if the player is registered or not (IMO).
We have to create a variable to store the players name, in this case its called "pName";
After creating the variable, we're getting the player's name, and storing it in the pName variable.
Now, to check the if the player is registered, we're using the function I specified.
"BUD::IsNameRegistered(pName)"
If the player is registered, it will send him a message saying he's registered.
Else it will send a message saying it's not registered.


To manage accounts, its used by accountid.
What does that mean?
We need to use a function called:
pawn Code:
BUD::GetNameUID( name[] );
"GetNameUserID"

So thats a good function to set variables in the database.
We could use it, under OnPlayerDisconnect.
Check if the player is registered, if he is, set some variables.
It would look something like this:
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
new pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof pName);
if(BUD::IsNameRegistered(pName))
{
new userid = BUD::GetNameUID( pName);
BUD::MultiSet(userid, "ii",
"Money", GetPlayerMoney(playerid),
"Score", GetPlayerScore(playerid));
}
return 1;
}

Damn easy, right?
So, we're creating a variable called pName, so we can store the players name
For future usage.
We're getting the players name, and storing it in the variable pName.
Afterwards, we're checking if the player is registered.

"new userid = BUD::GetNameUID( pName);"

We're getting the ID of the player (not playerid, the id of registration).
Then we're setting, in the database, the variables declared.
In this case its "Money and score".

So as you can see, we used "MultiSet".
Thats a function, that we use to set multiple stuff at the same time, pretty cool?

The function is:
pawn Code:
BUD::MultiSet( uid, type definitions, ( entry, value )... )
Its quite specific, actually.
To be used its for example:
pawn Code:
BUD::MultiSet(userid, "ifffs[10]",
"Money", GetPlayerMoney(playerid),
"X", x,
"Y", y,
"Z", z,
"Text", "I'm rich!");

And of course, there's a function called "MultiGet", to get various variables at the same time!
pawn Code:
BUD::MultiGet( uid, type definitions, ( entry, &variable )... )
Quite specific.
Here's how you use it:
[pawn]
new iScore, iMoney;
BUD::MultiGet(userid, "ii",
"Money", iMoney,
"Score", iScore);

GivePlayerMoney(playerid, iMoney);
SetPlayerScore(playerid, iScore);
[pawn]




Ok, ok, we can MultiSet/Get, but can we set/get one variable alone?
Of course we can!
If I wanted, for example, to get just the money column, I would do something like:
pawn Code:
GivePlayerMoney(playerid, BUD::GetIntEntry(userid, "Money");

The function is called:
pawn Code:
BUD::GetIntEntry(uid, entry[]);

BUD::GetIntEntry(
uid <- Userid
entry <- the column name


Oh I forgot!
There's a function to check if the password is correct.
I mean for example the player does "/login firecat"
The syntax being "/login <password>"

The function is called:
pawn Code:
BUD::CheckAuth( name[], password[] );

So it would be something like:
pawn Code:
CMD:login(playerid, params[])
{
new password[23];
if(sscanf(params,"s[23]",password)) return SendClientMessage(playerid, -1, "USAGE: /login (password)");
new userid, pName[MAX_PLAYER_NAME];

GetPlayerName(playerid, pName, sizeof pName);
userid = BUD::GetNameUID(pName);

if(BUD::CheckAuth(userid, password))
{
SendClientMessage(playerid, -1, "You have been logged in!");
//"BUD::MultiGet..."
}
else
{
SendClientMessage(playerid, -4, "Wrong password!");
}
return 1;
}
So, we're creating the variable "password", so we can assign it with sscanf...
Then we're creating the variable userid, so we can store the player's account id
And the variable pName, so we can store the player name.
Afterwards, we're getting the players name and storing it in the pName variable.

So after that, we're going to use the function "BUD::CheckAuth(userid, password)"
To check if the password is the same as the one in the database.
If it is, it sends a message saying he's logged in
Otherwise it will send a message saying "Wrong password!"

The End
If you actually read it all, then thank you, and I hope you understood all of it!
If you have any questions, feel free to ask!
I hope you liked it.

Last edited by FireCat; 11/11/2012 at 05:45 PM.
FireCat is offline   Reply With Quote
Old 13/09/2012, 07:22 PM   #2
Kitten
High-roller
 
Join Date: Jul 2010
Location: United States
Posts: 2,168
Reputation: 339
Default Re: How to use BUD - Blazing User DB

This is pretty easy as now understanding the basics of the include.
Kitten is offline   Reply With Quote
Old 13/09/2012, 07:23 PM   #3
FireCat
High-roller
 
FireCat's Avatar
 
Join Date: Jul 2010
Posts: 2,322
Reputation: 609
Default Re: How to use BUD - Blazing User DB

Quote:
Originally Posted by Kitten View Post
This is pretty easy as now understanding the basics of the include.
I wanted to make this, because I haven't found any tutorial about it.
FireCat is offline   Reply With Quote
Old 13/09/2012, 07:41 PM   #4
Glint
Guest
 
Posts: n/a
Default Re: How to use BUD - Blazing User DB

Quote:
Originally Posted by FireCat View Post
I wanted to make this, because I haven't found any tutorial about it.
Actually there is one, but it explains how to make a register and login system using BUD, however he didn't explain what each thing does like you did so i say really good job.
  Reply With Quote
Old 13/09/2012, 07:53 PM   #5
FireCat
High-roller
 
FireCat's Avatar
 
Join Date: Jul 2010
Posts: 2,322
Reputation: 609
Default Re: How to use BUD - Blazing User DB

Quote:
Originally Posted by Lexi' View Post
Actually there is one, but it explains how to make a register and login system using BUD, however he didn't explain what each thing does like you did so i say really good job.
Thanks!
FireCat is offline   Reply With Quote
Old 13/09/2012, 08:47 PM   #6
Guitar
Huge Clucker
 
Guitar's Avatar
 
Join Date: Feb 2012
Location: ►░▒▓♫♪▓▒░◄
Posts: 357
Reputation: 3
Default Re: How to use BUD - Blazing User DB

Great, I don't know what that's for, but I will soon Thank you and welcome back sexy!
__________________
3habGamingY_INI Registration system
Click meClick me
Code:
funland.dyndns.org:7777 - Connect and test my work :) [Tutorial] Making a registration system - Using "Y_INI + Whirlpool + Dialogs" 3habGaming is looking for members/helpers/staff - www.3habgaming.icyboards.net :oops:. I am interested in learning the basics to a Roleplay GM.
Jansish is great :) + I hate Dialogs :(
Guitar is offline   Reply With Quote
Old 13/09/2012, 09:59 PM   #7
FireCat
High-roller
 
FireCat's Avatar
 
Join Date: Jul 2010
Posts: 2,322
Reputation: 609
Default Re: How to use BUD - Blazing User DB

Quote:
Originally Posted by Guitar View Post
Great, I don't know what that's for, but I will soon Thank you and welcome back sexy!
Thanks! :P
FireCat is offline   Reply With Quote
Old 20/12/2013, 07:14 AM   #8
ToiletDuck
Huge Clucker
 
ToiletDuck's Avatar
 
Join Date: Apr 2012
Location: Philippines, Lucena City
Posts: 398
Reputation: 43
Default Re: How to use BUD - Blazing User DB

well thanks for this i might start using BUD of Slice. anyways good job.

LOL i bumped it.
ToiletDuck is offline   Reply With Quote
Old 20/12/2013, 07:51 AM   #9
Tuntun
High-roller
 
Tuntun's Avatar
 
Join Date: May 2012
Posts: 1,642
Reputation: 85
Default Re: How to use BUD - Blazing User DB

That what i wanted! Thanks FireCat.
__________________
Are you an aviation fan? to get more aviation photo like:
https://www.********.com/profile.php?id=657212634354407
Tuntun is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] [BETA] BUD - Blazing User DB Slice Includes 235 09/12/2019 02:34 PM
[Tutorial] Blazing User Database, login and register system with dialogs. new121 Tutorials 3 18/03/2012 04:35 PM
Bad user name Programie Server Support 9 06/04/2011 03:36 PM
User Notorious29 Help Archive 7 02/03/2011 07:51 PM
user mattyross55 Client Support 1 19/09/2009 09:52 PM


All times are GMT. The time now is 04:05 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.