[Tutorial] Simple SQL Commands/Features
#1

Simple SQLite Commands/Features
By Voxel

Info:
Hello fella's,

So in my previous topic i posted a SQL base starting script and you can view it here:
https://sampforum.blast.hk/showthread.php?tid=476204

I decided to make a tutorial to explain how to make simple SQL commands such as /givemoney or /setscore, /setkills etc. This can be very helpfull to administrate your server.
Lets get started shall we?

Tutorial:

First things first, i reccomend you use my filterscript to follow this tutorial, or if you can convert it to your variables then thats fine aswell.

1. A cash test command
First we will make a command that tests if your money saving is working properly.
(note that i am using ZCMD)
pawn Код:
CMD:cashtest(playerid, params[]) //the command
{
    User[playerid][USER_MONEY] += 1000000;
    //User can also be pInfo and USER_MONEY can be pMoney (depends on what you set)
    GivePlayerMoney(playerid, 1000000);
    //gives the player the in-game money wich is then stored in the database (at USER_MONEY)
    return 1;
}
2. /setkills command
Second we will make a command that will change the kills value in your database.
pawn Код:
CMD:setkills(playerid, params[])
{
    if(User[playerid][USER_ADMIN] >= 5)
   //we check if the user is admin level 5 or higher
    {
        new targetid ,amount;
        if(sscanf(params, "ud", targetid, amount)) return  SendClientMessage(playerid, COL_WHITE, "/setkills [playerid] [kills]");
        if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid, COL_WHITE, "Player is not online");

        User[playerid][USER_KILLS] = amount;
        //gets the variable amount and sets it to the entered value, so /setkills [id] [value = amount]
    }
    else
    {
        SendClientMessage(playerid, COL_RED, "You are not a administrator!");
    }
    return 1;
}
3. /setdeaths command
This is basicly the same as /setkills only we change the database info from USER_KILLS to USER_DEATHS and we change the command params to /setdeaths [playerid] [deaths]
pawn Код:
CMD:setdeaths(playerid, params[])
{
    if(User[playerid][USER_ADMIN] >= 5)
   //we check if the user is admin level 5 or higher
    {
        new targetid ,amount;
        if(sscanf(params, "ud", targetid, amount)) return  SendClientMessage(playerid, COL_WHITE, "/setdeaths [playerid] [deaths]");
        if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid, COL_WHITE, "Player is not online");

        User[playerid][USER_DEATHS] = amount;
        //gets the variable amount and sets it to the entered value, so /setdeaths [id] [value = amount]
    }
    else
    {
        SendClientMessage(playerid, COL_RED, "You are not a administrator!");
    }
    return 1;
}
4. /setscore command
As fourth we make a /setscore command to set the players score to the desired value, again you use the same functions as you do with kills and deaths
pawn Код:
CMD:setscore(playerid, params[])
{
    if(User[playerid][USER_ADMIN] >= 5)
   //we check if the user is admin level 5 or higher
    {
        new targetid ,amount;
        if(sscanf(params, "ud", targetid, amount)) return  SendClientMessage(playerid, COL_WHITE, "/setscore [playerid] [score]");
        if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid, COL_WHITE, "Player is not online");

        User[playerid][USER_SCORE] = amount;
        //gets the variable amount and sets it to the entered value, so /setscore [id] [value = amount]
    }
    else
    {
        SendClientMessage(playerid, COL_RED, "You are not a administrator!");
    }
    return 1;
}
5. /givemoney command
Now we will make a /givemoney command to give the player money on top of his already set value with a limit so it cannot be abused
pawn Код:
CMD:givemoney(playerid, params[])
{
    if(User[playerid][USER_ADMIN] >= 5)
    //we check if the user is admin level 5 or higher
    {
        new targetid, amount;
        if(sscanf(params, "ud", targetid, amount)) return  SendClientMessage(playerid, COL_WHITE, "/givemoney [playerid] [money]");
        if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid, COL_WHITE, "Player is not online");
        if(amount < 0 || amount > 30000) return SendClientMessage(playerid, COL_WHITE, "You can only give a maximum amount of $30 000");
        //here we check if the user types a amount between 0 and 30000, if he exceeds this value he will recieve a message that he can only give a maximum of 300000

        User[playerid][USER_MONEY] += amount;
        //here we get the value of the givin amount again but this time we put: plus and equal
        GivePlayerMoney(playerid, amount);
        //and finally we give the player the amount (value) that we put in our params of the command!
    }
    else
    {
        SendClientMessage(playerid, COL_RED, "You are not a administrator!");
    }
    return 1;
}
6. /setadmin command
And finally we will need to set a players admin rank!
pawn Код:
CMD:setadmin(playerid,params[])
{
    if(User[playerid][USER_ADMIN] >= 6 || IsPlayerAdmin(playerid))
    //we check if he has the maximum admin rank or if he is a rcon admin
    {
        new targetid, level;
        if(sscanf(params, "ud", targetid, level)) return  SendClientMessage(playerid, COL_WHITE,"/setadmin [playerid] [level]");
        if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid, COL_WHITE, "Player is not online");
        if(level < 0 || level > 6) return SendClientMessage(playerid, COL_WHITE, "You can only set admin levels from 1 to 6!");
        //we check if he enters a value between 0 and 6 otherwise he will get a error saying only admin levels between 1 and 6.

        User[targetid][USER_ADMIN] = level;
        //we set the variable level to the value we gave in the params and it saves it in the database
    }
    else
    {
        SendClientMessage(playerid, COL_RED, "You are not a administrator!");
    }
    return 1;
}
I hope you guys understand this and good luck with your SQLite database!
Reply


Messages In This Thread
Simple SQLite Commands/Features - by Voxel - 17.11.2013, 10:46
Re: Simple SQL Commands/Features - by TahaAsif12 - 07.12.2013, 10:02
Re: Simple SQL Commands/Features - by Voxel - 07.12.2013, 11:49
Re: Simple SQL Commands/Features - by Tomix - 07.12.2013, 11:52
Re: Simple SQL Commands/Features - by iJumbo - 07.12.2013, 13:45
AW: Simple SQL Commands/Features - by BigETI - 07.12.2013, 15:42
Re: Simple SQL Commands/Features - by Voxel - 07.12.2013, 16:44
Re: Simple SQL Commands/Features - by nickdodd25 - 07.12.2013, 16:51
Re: Simple SQL Commands/Features - by Voxel - 07.12.2013, 16:57
Re: Simple SQL Commands/Features - by nickdodd25 - 07.12.2013, 17:06

Forum Jump:


Users browsing this thread: 1 Guest(s)