[Tutorial] [TUT]Creating a basic Account system_
#1

Howdy, im here to show you how to make a simple account system to use in your gamemode!

this will save all player data on disconnect. So dont worrie!

First let's start with our Variables.


pawn Код:
#include <dini>
#include <dudb>
Put these at the top of your script below your current includes. (this system use's dini)

then add these.

pawn Код:
new logged[MAX_PLAYERS];
enum pInfo
{
    AdminLevel,
    cash,
    score,
}
new PlayerInfo[MAX_PLAYERS][pInfo];
that enum you see above is what holds the stat variables, such as the admin level, cash, and score!

now, we get on to the next step.
Detecting if the player has an account when they connect.

add this to your OnPlayerConnect

pawn Код:
new name[MAX_PLAYER_NAME];
    new file[128];
    GetPlayerName(playerid, name, sizeof(name));
    format(file,sizeof(file),"/Users/%s.ini",name);
    if(!fexist(file))
    {
    SendClientMessage(playerid, COLOR_GREEN, "OMGWTFBBQ You arent registered! type /register to save your stats!");
    logged[playerid] = 0;
    }
    if(fexist(file))
    {
    SendClientMessage(playerid, COLOR_GREEN, "hot damn sexy! You are registered! type /login [pass]!!");
    }
There we go! now it detects if the player has or doesnt have a registered account.

Now we add the saving lines to OnPlayerDisconnect
add the folowing.

pawn Код:
new file[128];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(file,sizeof(file),"/Users/%s.ini",name);
    if(dini_Exists(file))
    {
  dini_IntSet(file, "score", PlayerInfo[playerid][score]);
  dini_IntSet(file, "money", PlayerInfo[playerid][cash]);
  dini_IntSet(file, "AdminLevel",PlayerInfo[playerid][AdminLevel]);
    }
    logged[playerid] = 0;
there we go! now it saves their stats when the disconenct/crash.

Now we add the /register command. This is in DCMD so make sure you have the proper defines for it.

first add

pawn Код:
dcmd(register, 8, cmdtext);
    dcmd(login, 5, cmdtext);
add these to your OnPlayerCommandText

Now we add the commands.

pawn Код:
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, "[SYSTEM]: /register [password]");
  if(dini_Exists(file)) return SendClientMessage(playerid, COLOR_RED, "[SYSTEM]: You are already registered!");
  dini_Create(file);
  dini_IntSet(file, "hashPW", udb_hash(params));
  dini_Set(file, "password", params);
  dini_IntSet(file, "AdminLevel",PlayerInfo[playerid][AdminLevel] = 0);
  dini_IntSet(file, "score", PlayerInfo[playerid][score] = 0);
  dini_IntSet(file, "money", PlayerInfo[playerid][cash] = 500);
  new string[128];
  format(string, 128, "[SYSTEM]: You succesfully registered the nickname %s with password %s", pname, params);
  SendClientMessage(playerid, COLOR_YELLOW, string);
  logged[playerid] = 1;
  SendClientMessage(playerid, COLOR_YELLOW, "[SYSTEM]: You have been automatically logged in!");
  return 1;}
Theres the register command, it will create their file in the "users" folder in your scriptfiles.
and will also automaticly log them in when they register, (also shows their password along with the hashed password)

Next the /login command. Witch will pull their data from their file.

pawn Код:
dcmd_login(playerid, params[])
{
  new file[128];
  new string[MAX_STRING], 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, "[SYSTEM]: /login [password]");
  if(!dini_Exists(file)) return SendClientMessage(playerid, COLOR_RED, "[SYSTEM]: You are not registered!");
  if(logged[playerid]) return SendClientMessage(playerid, COLOR_RED, "[SYSTEM]: 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;
    PlayerInfo[playerid][AdminLevel] = dini_Int(file, "AdminLevel");
    SetPlayerScore(playerid, PlayerInfo[playerid][score]);
    new lebel = PlayerInfo[playerid][AdminLevel];
    GivePlayerMoney(playerid, dini_Int(file, "money")-GetPlayerMoney(playerid));
    format(string, 256, "[SYSTEM]: Successfully logged in! (level: %d)!", lebel);
        SendClientMessage(playerid,COLOR_RED, string);
    printf("%s (%i) logged in with password %s", pname, playerid, params);
  }
  return 1;}
Great! We're done! this should now have /register, /login, and save stats on logout!


good luck! with further projects, and thanks for using my tutorial, any bugs or errors, please post them! ill be happy to help!


EDIT: To add more items to save, simply add them to the enum we added at the top. then add dini_IntSet(file, "filename", PlayerInfo[playerid][enumfilename] = 0); to the /register command then add PlayerInfo[playerid][enumfilename] = dini_Int(file, "filename"); to the login!
Reply
#2

Thank you for taking the time and posting this TUT. It is very useful for scripters just starting out. Well done.
Reply
#3

Good work, tho you should explain the parts a bit more, this is kinda just a copy & paste list, you didn't tell how to add more thing to the saves.

But Great work, it will help those who need a account system (:
Reply
#4

Thanks, ill edit.
Reply
#5

Hi,

I wanted to know how I would add the admin level to a command, ive tried allsorts.

This is basicly, what I want
Код:
dcmd_kick(playerid, params[])
{
new iD, Message[128];
		new eAccount[MAX_PLAYER_NAME];
		if (sscanf(params, "u", iD)) SendClientMessage(playerid, COLOR_WHITE, "~ Usage: /kick [playerid]");
		else if (iD == INVALID_PLAYER_ID) SendClientMessage(playerid, COLOR_RED, "~ Inactive Player");
		else
		{
			Kick(iD);
			GetPlayerName(playerid, eAccount, MAX_PLAYER_NAME);
			format(Message, sizeof(Message), "~ %s has been kicked from the server.", eAccount);
			SendClientMessageToAll(COLOR_RED, Message);
		}
	}
	return 1;
}
Reply
#6

I just wanted to make it into a admin command basicly. Ive tried everything I could think of.
Reply
#7

Quote:
Originally Posted by eCho'
I just wanted to make it into a admin command basicly. Ive tried everything I could think of.
Firstly: Don't double post.

Secondly: Try this:

Код:
dcmd_kick(playerid, params[])
{
             new iD, Message[128];
		new eAccount[MAX_PLAYER_NAME];

       if(PlayerInfo[playerid][AdminLevel] <1) return SendClientMessage(playerid,0xffffffaa,"You are not an Admin"); //this is where you check if a player is not an admin
		if (sscanf(params, "u", iD)) SendClientMessage(playerid, COLOR_WHITE, "~ Usage: /kick [playerid]");
		else if (iD == INVALID_PLAYER_ID) SendClientMessage(playerid, COLOR_RED, "~ Inactive Player");
		else
		{
			Kick(iD);
			GetPlayerName(playerid, eAccount, MAX_PLAYER_NAME);
			format(Message, sizeof(Message), "~ %s has been kicked from the server.", eAccount);
			SendClientMessageToAll(COLOR_RED, Message);
		}
	}
	return 1;
}
Reply
#8

Nice and simple Just what a new scripter
will be looking for
Reply
#9

Ya, very useful. But one thing is missing. If you do a login and register command you must include a stats command, too.
Maybe youw ant to add it? Would be very useful for scripting beginners.
Reply
#10

a stats command? this saves instantly.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)