SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 14/08/2008, 06:44 AM   #1
bogeymanEST
Huge Clucker
 
Join Date: Sep 2007
Posts: 229
Reputation: 2
Default [HowTo] Create a simple registration system using dini!

How to create a simple
Registration System
using dini!
by bogeyman_EST

Getting started

First 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];

Ok, now we can start creating our commands. First, put
pawn Code:
dcmd(register, 8, cmdtext);
dcmd(login, 5, cmdtext);
into your OnPlayerCommandText callback!

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]");

This script created a variable to store the file path and the player's name, it wrote the file path to the variable "file" and checked if there is anything in the params. Now we can start checking, if the player's stats file exists:

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 our script checks if a file stored in the variable "file" exists, if it does, returns an error message.
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));

Ok, now our little script created the file in the "file" variable, then set some things in the file:
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;
}

Now it sent a message displaying the password and logged the player in!

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;
}
Ok, this script, like in the /register command, creates the "file" variable to store the file path, a "string" for the messages, and "pname" to store the player's name. Then some basic if() statements to check if the player is registered, logged in or there are no params.

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!
bogeymanEST is offline   Reply With Quote
Old 14/08/2008, 06:49 AM   #2
MaTrIx4057
Gangsta
 
MaTrIx4057's Avatar
 
Join Date: Apr 2007
Posts: 805
Reputation: 23
Default Re: [HowTo] Create a simple registration system using dini!

Very nice tutorial
MaTrIx4057 is offline   Reply With Quote
Old 14/08/2008, 07:29 AM   #3
Kapil
Gangsta
 
Join Date: Jun 2007
Posts: 759
Reputation: 22
Default Re: [HowTo] Create a simple registration system using dini!

edit:

nice
Kapil is offline   Reply With Quote
Old 14/08/2008, 08:36 AM   #4
bogeymanEST
Huge Clucker
 
Join Date: Sep 2007
Posts: 229
Reputation: 2
Default Re: [HowTo] Create a simple registration system using dini!

It's good that, there is a better way of doing this, but this a tutorial for noobs, who don't know anything about creating registration systems etc. But thanks for pointing that out anyway =)
bogeymanEST is offline   Reply With Quote
Old 25/08/2008, 11:06 AM   #5
Maniac_X
Little Clucker
 
Join Date: Mar 2008
Posts: 35
Reputation: 0
Default Re: [HowTo] Create a simple registration system using dini!

Hi i'm not sure why it's doing this but i'm getting the following error :

blahblahblah.pwn(177) : error 017: undefined symbol "string"
blahblahblah.pwn(17 : error 017: undefined symbol "string"
blahblahblah.pwn(179) : error 017: undefined symbol "string"
blahblahblah.pwn(193) : error 037: invalid string (possibly non-terminated string)
blahblahblah.pwn(193) : error 029: invalid expression, assumed zero
blahblahblah.pwn(193) : error 029: invalid expression, assumed zero
blahblahblah.pwn(193) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase


7 Errors.


relating to this code :

Code:
LINE
(177)  dini_IntSet(string, "level", 0);
(178)  dini_IntSet(string, "score", GetPlayerScore(playerid));
(179)  dini_IntSet(string, "money", GetPlayerMoney(playerid));
Maniac_X is offline   Reply With Quote
Old 25/08/2008, 12:04 PM   #6
mamorunl
Godfather
 
mamorunl's Avatar
 
Join Date: Feb 2007
Location: Groningen/Netherlands
Posts: 5,839
Reputation: 2246
Default Re: [HowTo] Create a simple registration system using dini!

pawn Code:
new string[218]; // you can change 218 to a number you prefer regarding to the size of the string

I would suggest using DUDB for user accounts
mamorunl is offline   Reply With Quote
Old 25/08/2008, 12:13 PM   #7
Antironix
High-roller
 
Join Date: May 2007
Posts: 1,429
Reputation: 20
Default Re: [HowTo] Create a simple registration system using dini!

Quote:
Originally Posted by [NT
SpeedDevil ]
pawn Code:
new string[218]; // you can change 218 to a number you prefer regarding to the size of the string

I would suggest using DUDB for user accounts
Explain me why please.
Antironix is offline   Reply With Quote
Old 25/08/2008, 12:20 PM   #8
bogeymanEST
Huge Clucker
 
Join Date: Sep 2007
Posts: 229
Reputation: 2
Default Re: [HowTo] Create a simple registration system using dini!

Quote:
Originally Posted by Maniac_X
Hi i'm not sure why it's doing this but i'm getting the following error :

blahblahblah.pwn(177) : error 017: undefined symbol "string"
blahblahblah.pwn(17 : error 017: undefined symbol "string"
blahblahblah.pwn(179) : error 017: undefined symbol "string"
blahblahblah.pwn(193) : error 037: invalid string (possibly non-terminated string)
blahblahblah.pwn(193) : error 029: invalid expression, assumed zero
blahblahblah.pwn(193) : error 029: invalid expression, assumed zero
blahblahblah.pwn(193) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase


7 Errors.


relating to this code :

Code:
LINE
(177)  dini_IntSet(string, "level", 0);
(178)  dini_IntSet(string, "score", GetPlayerScore(playerid));
(179)  dini_IntSet(string, "money", GetPlayerMoney(playerid));
yeah, had a small typo.. fixed now
bogeymanEST is offline   Reply With Quote
Old 25/08/2008, 03:31 PM   #9
mamorunl
Godfather
 
mamorunl's Avatar
 
Join Date: Feb 2007
Location: Groningen/Netherlands
Posts: 5,839
Reputation: 2246
Default Re: [HowTo] Create a simple registration system using dini!

Quote:
Originally Posted by Wadabak
Quote:
Originally Posted by [NT
SpeedDevil ]
pawn Code:
new string[218]; // you can change 218 to a number you prefer regarding to the size of the string

I would suggest using DUDB for user accounts
Explain me why please.
explain what?
mamorunl is offline   Reply With Quote
Old 25/08/2008, 03:37 PM   #10
Antironix
High-roller
 
Join Date: May 2007
Posts: 1,429
Reputation: 20
Default Re: [HowTo] Create a simple registration system using dini!

Why you are suggesting DUDB for user accounts instead of Dini.
Antironix 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
[HowTo]Create Pickups Mikep Help Archive 6 07/07/2010 04:26 AM
[HowTo]Create Properties [GM]The_Don Help Archive 67 19/06/2010 01:35 PM
[FilterScript] [FS] MafiaGuy's Registration System (DINI/INI - Dialogs) MafiaGuy™ Filterscripts 9 25/04/2010 02:40 AM
[HOWTO]Create a Simple command with 3dtext campkz Help Archive 2 11/04/2010 08:51 AM


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


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