14.08.2008, 05:44
How to create a simple
Registration System
using dini!
by bogeyman_EST
Getting startedFirst of all you will need to download dini, dutils and DUBD and also you have to place them in your include folder!
Now we can get started. I have to point out, that i will be using dcmd, to create the /register and /login commands! Ok, let's say we want to create a script, which saves the player's score, money, password and admin level to a .ini file. So in this tutorial, you will learn, how to set a value in a .ini file, read the value and use it to allow the player to log in. Also we will save the admin level for the file to a variable! So let's get started!
First we should want to create some variables to store the admin level, whether the player is logged in and we should want to define dcmd, too:
pawn Code:
#define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
new level[MAX_PLAYERS];
new logged[MAX_PLAYERS];
pawn Code:
dcmd(register, 8, cmdtext);
dcmd(login, 5, cmdtext);
Creating /register
Now let's start creating our /register command!
pawn Code:
dcmd_register(playerid, params[])
{
new file[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "\\Users\\%s.ini", pname);
if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "USAGE:/register [password]");
pawn Code:
dcmd_register(playerid, params[])
{
new file[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "\\Users\\%s.ini", pname);
if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "USAGE:/register [password]");
if(dini_Exists(file)) return SendClientMessage(playerid, COLOR_RED, "You are already registered!");
Now let's start writing into our file:
pawn Code:
dcmd_register(playerid, params[])
{
new file[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "\\Users\\%s.ini", pname);
if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "USAGE:/register [password]");
if(dini_Exists(file)) return SendClientMessage(playerid, COLOR_RED, "You are already registered!");
dini_Create(file);
dini_IntSet(file, "hashPW", udb_hash(params));
dini_Set(file, "password", params);
dini_IntSet(file, "level", 0);
dini_IntSet(file, "score", GetPlayerScore(playerid));
dini_IntSet(file, "money", GetPlayerMoney(playerid));
hashPW - Identifying later in our /login command
password - Just the password the player sent us
level - The player's admin level
score - The player's score
money - The player's money
Now let's add some messages and automatically login the player:
pawn Code:
dcmd_register(playerid, params[])
{
new file[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "\\Users\\%s.ini", pname);
if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "USAGE:/register [password]");
if(dini_Exists(file)) return SendClientMessage(playerid, COLOR_RED, "You are already registered!");
dini_Create(file);
dini_IntSet(file, "hashPW", udb_hash(params));
dini_Set(file, "password", params);
dini_IntSet(file, "level", 0);
dini_IntSet(file, "score", GetPlayerScore(playerid));
dini_IntSet(file, "money", GetPlayerMoney(playerid));
new string[128];
format(string, 128, "You succesfully registered the nickname %s with password %s", pname, params);
SendClientMessage(playerid, COLOR_YELLOW, string);
logged[playerid] = 1;
SendClientMessage(playerid, COLOR_YELLOW, "You have been automatically logged in!");
return 1;
}
This completes the /register command! Now we can start creating the /login command.
Creating /login
This is basically the same process as in /register: Create the variables, store the path, check if the file exists. But there is a difference, that we have to read the file instead of writing. I'll create the command and explain it after creating:
pawn Code:
dcmd_login(playerid, params[])
{
new file[128];
new string[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "\\Users\\%s.ini", pname);
if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /login [password]");
if(!dini_Exists(file)) return SendClientMessage(playerid, COLOR_RED, "You are not registered!");
if(logged[playerid]) return SendClientMessage(playerid, COLOR_RED, "You are already logged in!");
new tmp;
tmp = dini_Int(file, "hashPW");
if(udb_hash(params) != tmp)
{
format(string, 256, "You specified the wrong password for %s!", pname);
SendClientMessage(playerid, COLOR_RED, string);
}
else
{
logged[playerid] = 1;
level[playerid] = dini_Int(file, "level");
SetPlayerScore(playerid, dini_Int(file, "score"));
GivePlayerMoney(playerid, dini_Int(file, "money")-GetPlayerMoney(playerid));
SendClientMessage(playerid, COLOR_YELLOW, "You have succesfully logged in!");
printf("%s (%i) logged in with password %s", pname, playerid, params);
}
return 1;
}
Then it creates a variable "tmp" to store the hashed version of the password. Then it hashes the player's inputted password and checks if the two passwords match, if they do, logs the player in and sets the "level" variable to the level, their score to the score and their money to the money in the player's stats file!
Done!
See, wasn't that hard! =)
Download as a file:Please read this post COMPLETELY through before posting a problem. Everything you will need is here!