[Tutorial] Starting off with your admin system
#1

Introduction

Hello guys, well I'm going to try to explain to you how you can start off a basic admin system...
Then in future you can release it :P (be sure to give me some credits ^^)

What will you need

For this tutorial you will need:
Starting off

Ok, first of all, you need the stated includes, after you downloaded them, and places them in the pawno/includes folder, do the following:
pawn Code:
#include <a_samp>
#include <sscanf2>
#include <SII>
#include <zcmd>
Basically, what we're doing here, is including all the "script" that each include has, so we can use them, instead of copying and pasting all the includes in your script.

-Ok, now time for the enums.
Well an enum, basically is where you can store all the vars, but you can use a "name" to state them, instead of remembering each name of it. Didn't understand? Then read the following:
pawn Code:
enum PlayerInfo
{
    Logged,//To check if the player is logged in
    Level,//To check the player's admin level
}
new PInfo[MAX_PLAYERS][PlayerInfo];
So from now on, you won't do
pawn Code:
if(Logged[playerid] == 1)
You will do:
pawn Code:
if(PInfo[playerid][Logged] == 1)
Creating the script

Ok, now you want to check if the player is registered when he connects.
So you'll have to do this:
pawn Code:
public OnPlayerConnect(playerid)
{
    new file[64],PlayerName[25];//Creating a variable where we can store the file path, and the variable to store the player's name.
    GetPlayerName(playerid,PlayerName,sizeof PlayerName);//Storing the players name in the PlayerName variable.
    format(file,sizeof file,"Admin/%s.ini",PlayerName);//Storing the file path with the players name.
    if(!fexist(file))//Checking if the file exists
    {//Here goes the stuff you want to do if the user is not registered.
        SendClientMessage(playerid,-4,"You are not registered! Please use /register <password>");
    }
    else
    {//Here goes the stuff you want to do if the user is registered.
        SendClientMessage(playerid,-4,"You are registered! Use /login <password>");
    }
    return 1;
}
Great! We have now completed the step, to check if the player is registered or not when he connects!
Ok now lets create a few commands
pawn Code:
CMD:register(playerid,params[])
{
    if(PInfo[playerid][Logged] == 1) return SendClientMessage(playerid,-4,"You are already logged in!");//Checking if the player is logged in, if he is, it won't allow him to re-register
    new password[23];//Creating a variable to store the password
    if(sscanf(params,"s[23]",password)) return SendClientMessage(playerid,-1,"USAGE: /register <password>");//Here we're checking if the player inputs any password, if not, it will return to him a message saying the proper usage.
    new file[64],PlayerName[24];//Creating a variable to store the file path, and a variable to store the players name.
    GetPlayerName(playerid,PlayerName,sizeof PlayerName);
    format(file,sizeof file,"Admin/%s.ini",PlayerName);
    if(fexist(file)) return SendClientMessage(playerid,-4,"Somehow you're already registered!");//Checking if the player is already registered, again....
    INI_Open(file);//Opening the file with SII include (with this function, if the file is not created, it will automatically create the file.)
    INI_WriteString("Password",password);//Writing in the file the password the player has inputted.
    INI_WriteInt("Level",PInfo[playerid][Level]);//Writing in the file, the variable of the admin level.
    INI_Save();//After we write something to the file, we already have to use this to save the information in the player's file.
    INI_Close();//"Closing the file", that means that we're not using it anymore :P
    SendClientMessage(playerid,-1,"You have successfully registered!");
    PInfo[playerid][Logged] = 1;//Setting the logged in variable to 1
    return 1;
}
Great!! We have created a /register command.
Now lets create a login command.

pawn Code:
CMD:login(playerid,params[])
{
    if(PInfo[playerid][Logged] == 1) return SendClientMessage(playerid,-4,"You are already logged in!");//Checking if the player is logged in, if he is, it won't allow him to login
    new password[23],password2[23];//Creating a variable to store the password, and another one to store the password from the user's file.
    if(sscanf(params,"s[23]",password)) return SendClientMessage(playerid,-1,"USAGE: /login <password>");//Here we're checking if the player inputs any password, if not, it will return to him a message saying the proper usage.
    new file[64],PlayerName[24];//Creating a variable to store the file path, and a variable to store the players name.
    GetPlayerName(playerid,PlayerName,sizeof PlayerName);
format(file,sizeof file,"Admin/%s.ini",PlayerName);
    if(!fexist(file)) return SendClientMessage(playerid,-4,"Please use /register");//Checking if the player is not registered, again....
    INI_Open(file);//Opening the file with SII include
    INI_ReadString(password2,"Password");
    if(strcmp(password,password2) != 0) return SendClientMessage(playerid,-4,"Wrong password!"),INI_Close();//Checking if he inputted the correct password, if not, retrieve him a message and closing the file;
    PInfo[playerid][Level] = INI_ReadInt("Level");//Setting the admin level variable, to the one thats in his file.
    INI_Close();//"Closing the file", that means that we're not using it anymore :P
    SendClientMessage(playerid,-1,"You have been successfully logged in!");
    PInfo[playerid][Logged] = 1;//Setting the logged in variable to 1
    return 1;
}
Now we have created the login command! Yay!
Lets create a kick command...
pawn Code:
CMD:kick(playerid,params[])
{
    if(PInfo[playerid][Level] < 3) return SendClientMessage(playerid,-4,"You need to be level 3 to kick players");//Checking if the player has admin level 3, if not it sends him a message.
    new id;//Creating a variable to store the selected id;
    if(sscanf(params,"u",id)) return SendClientMessage(playerid,-1,"USAGE: /kick <id>");//Checking if the player has selected an id, other wise it sends him a message. We used the "u" specifier, because he can put a name or an id.
    if(!IsPlayerConnected(id)) return SendClientMessage(playerid,-4,"That player is not connected!");//Checking if the selected user is connected or not.
    Kick(id);
    SendClientMessage(playerid,-1,"You have kicked the selected user!");
    return 1;
}
Nice, now we've created the kick command!
Lets make a /setlevel command.
pawn Code:
CMD:setlevel(playerid,params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-4,"Only rcon admins can set admin levels!");//Checking if the player is rcon admin to set an admin level
    new id, level;//Creating the id variable to store the selected id and a level variable for the chosen admin level.
    if(sscanf(params,"ui",id,level)) return SendClientMessage(playerid,-1,"USAGE: /setlevel <id> <level>");//Check if the player inputted a username or id and a admin level.
    if(!IsPlayerConnected(id)) return SendClientMessage(playerid,-4,"That player is not connected!");//Checking if the selected user is connected or not.
    new file[64],PlayerName[24];//Creating a variable to store the file path, and a variable to store the players name.
    GetPlayerName(id,PlayerName,sizeof PlayerName);//Retrieving the selected id's name,
    format(file,sizeof file,"Admin/%s.ini",PlayerName);
    if(!fexist(file)) return SendClientMessage(playerid,-4,"That player is not registered");//Checking if the player is not registered
    INI_Open(file);//Opening the file with SII include
    INI_WriteInt("Level",level);//Writing the line "Level" the selected admin level.
    INI_Save();//Saving the file
    INI_Close();//Closing the file
    PInfo[id][Level] = level;
    SendClientMessage(playerid,-1,"You have changed the selected user's admin level");
    SendClientMessage(id,-1,"Your admin level has been changed");
    return 1;
}
Awesome!
We're nearly done, now add this under OnPlayerDisconnect

pawn Code:
public OnPlayerDisconnect(playerid,reason)
{
    PInfo[playerid][Logged] = 0;//Setting the logged in variable to 0.
    return 1;
}
Le end

Well I hope you liked my tutorial, and understood it well!
If you have any errors, post them here, because this code was not tested.
Thanks
Reply


Messages In This Thread
Starting off with your admin system - by FireCat - 02.07.2012, 13:11
Re: Starting off with your admin system - by Spookie98 - 02.07.2012, 13:17
Re: Starting off with your admin system - by FireCat - 02.07.2012, 13:18
Re: Starting off with your admin system - by Spookie98 - 02.07.2012, 13:20
Re: Starting off with your admin system - by StrangeLove - 02.07.2012, 13:21
Re: Starting off with your admin system - by FireCat - 02.07.2012, 13:23
Re: Starting off with your admin system - by Vince - 02.07.2012, 14:34
Re: Starting off with your admin system - by FireCat - 02.07.2012, 15:43
Re: Starting off with your admin system - by Hiddos - 02.07.2012, 18:57
Re: Starting off with your admin system - by FireCat - 02.07.2012, 20:01

Forum Jump:


Users browsing this thread: 1 Guest(s)