[Tutorial] How to use BUD - Blazing User DB
#1

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 Код:
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 Код:
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 Код:
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 Код:
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 Код:
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 Код:
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 Код:
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 Код:
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 Код:
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 Код:
BUD::MultiSet( uid, type definitions, ( entry, value )... )
Its quite specific, actually.
To be used its for example:
pawn Код:
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 Код:
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 Код:
GivePlayerMoney(playerid, BUD::GetIntEntry(userid, "Money");
The function is called:
pawn Код:
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 Код:
BUD::CheckAuth( name[], password[] );
So it would be something like:
pawn Код:
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.
Reply
#2

This is pretty easy as now understanding the basics of the include.
Reply
#3

Quote:
Originally Posted by Kitten
Посмотреть сообщение
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.
Reply
#4

Quote:
Originally Posted by FireCat
Посмотреть сообщение
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
#5

Quote:
Originally Posted by Lexi'
Посмотреть сообщение
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!
Reply
#6

Great, I don't know what that's for, but I will soon Thank you and welcome back sexy!
Reply
#7

Quote:
Originally Posted by Guitar
Посмотреть сообщение
Great, I don't know what that's for, but I will soon Thank you and welcome back sexy!
Thanks! :P
Reply
#8

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

LOL i bumped it.
Reply
#9

That what i wanted! Thanks FireCat.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)