[Tutorial] Advanced registration system
#1

Advanced registration system
intro:
Hello, i've seen many tut on this forum on how to make a registration/login system etc. but.. their is one big problem with those i've seen.. i a player isn't logged in, you can change their values. in my system it is actualy possible to save and load player variabels, even if the player isn't online

This isn't for the noobcake's, but.. if you want a good server.. try learn this as quick as possible..
i used Dini in this tutorial, but on my own server i use Hash.. dini is a bit easier for some ppl, so thats why i choose it for this tut.


Disclaimer:
i only show you how to make it, alway's make a back-up of your server in case something terrible goes on(BOOWM!)


the method:
ok, lets walk thrue the "system" a bit.
we make a few files, like "PLN.ini"(player name) "PLX.ini" "PLY.ini" "PLZ.ini" (player x y z(respawn)) and "PLP.ini"(player pass) these are our files for now. instead of making 1 file ea player, we use 10+/100+ files for a player.. (you think im stupid dont ya?).
but now the fun part, we won't store some thing like this in a file:
Код:
Name=Me-Borno
X=0.00
Y=0.00
Z=0.00
but we'll add something like this to te files (for example "PLN.ini", witch could be "PLayer Names")
Код:
1=Me-Borno
2=Other
3=Random
in this way we can use a for-statement to check all names. got it? so if some one registers, you only need to find an empty slot/ID to store his name in. and if the player logs-in, you can find his id by his name.


The scriptage:
pfew, that was only the beginning xD

so first thing to do is: loading dini.. (if you don't know how to do that: 1. Leave this tut 2. view this)
Код:
#include <dini>
now make a few new variables:
Код:
new IDCache[MAX_PLAYERS]; //this will store the ID of the player.. it is the ID we manualy make, so not the "playerid" one
new LoggedIn[MAX_PLAYERS]; //checks if the player is logged in
new NickCache[MAX_PLAYERS][64]; //this will store the nickname of a player
also add this:
Код:
public OnPlayerConnect(playerid)
{
     new name[64]; GetPlayerName(playerid, name);
     NickCache[playerid] = name; //stored the player name into NickCache[playerid]
}
public OnPlayerDisconnect(playerid, reason)
{
     NickCache[playerid] = ""; //makes the NickCache[playerid] empty
     LoggedIn[playerid] = 0; //dont forget this, else if someone joins on the playerid of the previous player, he whould be logged in xD
}
and add these functions:
Код:
stock GetUserID(name[]) //this is the function name, duh..
{
	new result; //this will eventualy store the ID of the player.
	for(new a = 1; a <= 10000; a++) //loops true all players, in/decrease if you have less slots
	{
	    if(strlen(dini_Get("PLN.ini", a)) != 0) //this wil check if the name corresponding to the ID != ""
	    {
			if(strfind(dini_Get("PLN.ini", a), name, true) == 0) //this wil check if the User name == name[]
			{
				result = a; //so the result will be a(the id)
				a = 10001; //and this will quit the for, so we wont continue
			}
		}
		else a = 10001; //if the slot doesn't contain anny info, it'll quit the for-statement
	}
	if(result != 0) return result; //so this returns the ID if found
	else return -1; // and if no result found, it'll return -1
}
with this function we can get the ID of a player, using its name. this is usefull wenn the player tries to login, or if you want to use a part of name you could get the other's ID.

that was the most important function! if you understand that one, the next part whould be as hard xD

Registration
ok, this will be the registration of a person. if i were you, change the messages a bit to your own taste

Код:
stock Register(playerid, pass[])
{
      if(LoggedIn[playerid] == 1) return SendClientMessage(playerid, 0x000000, "You are already loggedin"); //if the player is loggedin, it will send this message
      if(GetUserID(NickCache[playerid]) != -1) return SendClientMessage(playerid, 0x000000, "You are already registered"); //if a player is already registered, itll send this message
      if(!strlen(pass)) return SendClientMessage(playerid, 0x000000, "Usage: /register <pass>"); //if the user didn't gave a password, itll send this message
      new result = -1;
      for(new a=1; a<10000; a++) //loop thrue all players
      {
            if(!strlen(dini_Get("PLN.ini", a))) //checks if the ID is empty
            {
                  result = a; //will be the ID
                  a = 10001; //will end the loop
            }
      }
      if(result != -1)
      {
            dini_Set("PLP.ini", result, pass); //sets the password in the ID table
            dini_Set("PLN.ini", result, NickCache[playerid]); //saves the name in the ID table
            SendClientMessage(playerid, 0x000000, "thanks for registering!");
            IDCache[playerid] = result; //stores the ID
            LoggedIn[playerid] = 1; //the player is now loggedin
      }
      return 1;
}
we're nearly at the end, it's not that long

Logging in:
almost 1 more function.

I need to add some comments to this function.. but i guess its kinda obvious
Код:
stock Login(playerid, pass[])
{
      if(LoggedIn[playerid] == 1) return SendClientMessage(playerid, 0x000000, "You are already loggedin");
      if(GetUserID(NickCache[playerid]) == -1) return SendClientMessage(playerid, 0x000000, "Your account is not registered");
      if(!strlen(pass)) return SendClientMessage(playerid, 0x000000, "Usage: /login <pass>");
      new uid = GetUserID(NickCache[playerid]);
      if(strcmp(dini_Get("PLP.ini", uid), pass, true , strlen(dini_Get("PLP.ini", uid))) return SendClientMessage(playerid, 0x000000, "Incorrect password");
      IDCache[playerid] = result;
      LoggedIn[playerid] = 1;
      return 1;
}
Credits:
i whould like to thank:
- My self, i wrote this tutorial + functions etc.
- Ozzie(rumble server, samp.stirredfork.net), for the massive system(he thought me how to make it )
- *do you want to be placed in this list, help me inprove this tut


End:
yeah, all you have to do now to login and register is making the command(/login <pass> & /register <pass)
ill add some more functions, like how to read the player X Y Z position etc... but for now, this whould be it
hope you guy's like it.. if you've got some problems ask them, and ill try to answer.
Reply


Messages In This Thread
Advanced registration system - by me-borno - 20.01.2011, 21:43
Respuesta: Advanced registration system - by leaNN! - 20.01.2011, 22:10
Re: Advanced registration system - by Marty_Alex - 20.01.2011, 22:15
Re: Advanced registration system - by me-borno - 20.01.2011, 22:19
Re: Advanced registration system - by MEC - 22.01.2011, 20:38
Re: Advanced registration system - by me-borno - 22.01.2011, 21:42

Forum Jump:


Users browsing this thread: 3 Guest(s)