[Tutorial] How to create bank system using jbank.inc
#1

How to create bank system using jbank.inc

Introduction

This is different from the other bank system tutorial.
This one teaches you how to create a bank system from my greatest include created collection.

jbank.inc

NOTE: Keep in mind that your script amx size will be high if you used this tutorial. It will increased to 410. If you don't use any function of jbank.inc and you only include it in the script then the amx size of your script will increase to 104 due to high string cell sizes. It will be fixed and lowered to 128 in v1.1.

Requirements

You need the following:

jBank.inc

Don't worry about YSI library and streamer. It is included in the file.

Started

First include this at the top of your script

pawn Код:
#include <jbank>
and add this

pawn Код:
new BLog[MAX_PLAYERS] = 0;
new accountstring[256];

native WP_Hash(buffer[], len, const str[]);
Now for some explanation.

pawn Код:
new BLog[MAX_PLAYERS] = 0;
We create a var. Later it will be used to check if player is logged to bank account or not. When the var is created it automatically set's to 0.

pawn Код:
new accountstring[256];
We create a var. Later it will be used to stored (used) in function BankAccount_Loc etc...

pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
    if(BankAccount_Loc(getName(playerid), accountstring, 256))
    /*
    Will check if player have bank account by checking his name if it exist
    in 'JBank/Accounts/'. accountstring is used to stored the bank account location
    from the function. 256 is the length of the string that should be stored.
    */

    {
        if(BLog[playerid] == 1)
        /*
        Checks if BLog var that we created is set to 1.
        */

        {
            SaveBankAccount(playerid, accountstring, 256, getName(playerid));
            /*
            Saves bank account. accountstring is used to stored the bank account
            location from the function. 256 is the length of the string that should
            be stored. */

        }
    }
    BLog[playerid] = 0;
    /*
    Sets the var BLog to 0
    */

    return 1;
}
Add this below your script or put it in any place but except inside the callback.

pawn Код:
CMD:registerbank(playerid, params[])
{
    if(BankAccount_Loc(getName(playerid), accountstring, 256)) return SendClientMessage(playerid, -1, "You already have a bank account!");
    /*
    Checks if player have account in 'JBank/Accounts'. If it is. SendClientMessage
    sented in color white.
    accountstring is used to stored the bankaccount location from the function.
    256 is the length of the string that should be stored.
    */

    if(sscanf(params, "s[32]", params)) return SendClientMessage(playerid, -1, "<!> /registerbank [password]");
    /*
    If you didn't type the password you get the usage message, the color is white.
    */

    if(strlen(params) < 3 || strlen(params) > 32) return SendClientMessage(playerid, -1, "Maxium Password Length is 30 - Minimum Password Length is 3");
    /*
    Checks if the password length is less than 3.
    Checks if the password length is more than 32.
    If it is SendClientMessage sented in color white.
    */

    new buf[129]; WP_Hash(buf, sizeof(buf), params);
    /*
    Creates a buf with cell size 129.
    Hashes the inputted password (params). By using buf.
    */

    CreateBankAccount(playerid, accountstring, 256, getName(playerid), buf, 1000);
    /*
    Creates bank account. accountstring is used to stored the bankaccount location
    from the function. 256 is the length of the string that should be stored.
    getName(playerid) is the player's name. buf is the hashed password. It is
    setted as password when the bank account is created. 1000 is the bank balance.
    It is setted when the bank account is created.
    */

    format(buf, 129, "Bank account created, You've free bank balance: $1000");
    /*
    format using buf with length 129.
    */

    SendClientMessage(playerid, -1, buf);
    /*
    Sends a client message in color white!
    */

    BLog[playerid] = 1;
    /*
    Sets var BLog to 1.
    */

    return 1;
}
pawn Код:
CMD:loginbank(playerid, params[])
{
    if(!BankAccount_Loc(getName(playerid), accountstring, 256)) return SendClientMessage(playerid, -1, "You do not have bank account!");
    /*
    Checks if player have account in 'JBank/Accounts'. If it doesn't have. SendClientMessage
    sented in color white.
    accountstring is used to stored the bankaccount location from the function.
    256 is the length of the string that should be stored.
    */

    else
    {
        if(BLog[playerid] == 1) return SendClientMessage(playerid, -1, "You're already logged in to your bank account!");
        /*
        Check if BLog is setted to 1.
        If it is 1. Sends a client message in color white!
        */

        if(sscanf(params, "s[32]", params)) return SendClientMessage(playerid, -1, "<!> /loginbank [password]");
        /*
        If player didn't put the password. Sends a client message in color
        white */

        new buf[129]; WP_Hash(buf, sizeof(buf), params); new buf2[129];
        /*
        Creates buf and buf2 and cell size 129.
        Hashes the inputted password using buf
        */

        LoadBankAccount(playerid, accountstring, 256, getName(playerid));
        /*
        Loads bank account. (INI_ParseFile) in order to make GetBPlayerPass function
        properly!
        */

        GetBPlayerPass(playerid, buf2, 129);
        /*
        Gets the bank account's password.
        buf2 is used to stored the password.
        256 is the length of the string that should be stored.
        */

        if(strcmp(buf2, buf, true) == 0)
        /*
        if buf (inputted password) matched buf2 (bank account password)
        */

        {
            LoadBankAccount(playerid, accountstring, 256, getName(playerid));
            /*
            Loads the bank account. (INI_ParseFile).
            */

            format(buf, 129, "Bank account logged in!: Current Balance: $%i", GetBPlayerBalance(playerid));
            /*
            format buf in length 129.
            $%i - integer. Used to display the balance
            */

            SendClientMessage(playerid, -1, buf);
            /*
            Sends a client message in color white
            */

            BLog[playerid] = 1;
            /*
            Sets the var BLog to 1
            */

        }
        else return SendClientMessage(playerid, -1, "Wrong Bank Password!");
        /*
        If the inputted password didn't matched the bank account password
        Sends a client message in color white
        */

    }
    return 1;
}
pawn Код:
CMD:resetbankbalance(playerid, params[])
{
    if(!BankAccount_Loc(getName(playerid), accountstring, 256)) return SendClientMessage(playerid, -1, "You do not have bank account!");
    /*
    Checks if player have account in 'JBank/Accounts'. If it doesn't have. SendClientMessage
    sented in color white.
    accountstring is used to stored the bankaccount location from the function.
    256 is the length of the string that should be stored.
    */

    if(BLog[playerid] == 0) return SendClientMessage(playerid, -1, "You must be logged in to your bank account to use this command!");
    /*
    Check if BLog is setted to 0.
    If it is 0. Sends a client message in color white!
    */

    ResetBPlayerBalance(playerid);
    /*
    Reset's player's bank account balance to 0
    */

    SendClientMessage(playerid, -1, "Bank Balance Resetted!");
    /*
    Sends a client message in color white
    */

    return 1;
}
pawn Код:
CMD:withdraw(playerid, params[])
{
    if(BankAccount_Loc(getName(playerid), accountstring, 256))
    /*
    Checks if player have account in 'JBank/Accounts'. If it have. then /withdraw
    function gets called!
    accountstring is used to stored the bankaccount location from the function.
    256 is the length of the string that should be stored.
    */

    {
        if(BLog[playerid] == 0) return SendClientMessage(playerid, -1, "You must be logged in to your bank account to use this command!");
        /*
        Check if BLog is setted to 0.
        If it is 0. Sends a client message in color white!
        */

        new value;
        /*
        Creates a var named value
        */

        if(sscanf(params, "i", value)) return SendClientMessage(playerid, -1, "<!> /withdraw [amount]");
        /*
        If player didn't put any amount of money. Sends a client message in color
        white */

        if(value > GetBPlayerBalance(playerid)) return SendClientMessage(playerid, -1, "You dont have that amount of money on your bank!");
        /*
        If the amount money inputted is higher than player's bank account balance.
        Sends a client message in color white!
        */

        LoadBankAccount(playerid, accountstring, 256, getName(playerid));
        /*
        Loads the bank account
        */

        SetBPlayerBalance(playerid, GetBPlayerBalance(playerid) - value);
        /*
        Subtracts the player's bank account balance to inputted amount money.
        Result = Withdrawed from bank account
        */

        GivePlayerMoney(playerid, value);
        /*
        Gives player a money. Money = Inputted amount money
        */

        new String[128];
        /*
        Creates a string with cell size 128 */

        format(String, sizeof String, "You withdrew $%i from your bank, and now got $%i on your bank left. ", value, GetBPlayerBalance(playerid));
        /*
        format string is length 128.
        $%i = integer. Used to display current balance
        */

        SendClientMessage(playerid, -1, String);
        /*
        Sends a client message in color white */

        SaveBankAccount(playerid, accountstring, 256, getName(playerid));
        /*
        Saves bank account.
        accountstring is used to stored the bankaccount location from the function.
        256 is the length of the string that should be stored.
        getName(playerid) is the player's name
        */

    }
    else return SendClientMessage(playerid, -1, "You do not have bank account!");
    /*
    if player doesn't have bank account, sends a client message in color white.
    */

    return 1;
}
pawn Код:
CMD:deposit(playerid, params[])
{
    if(BankAccount_Loc(getName(playerid), accountstring, 256))
    /*
    Checks if player have account in 'JBank/Accounts'. If it have. then /withdraw
    function gets called!
    accountstring is used to stored the bankaccount location from the function.
    256 is the length of the string that should be stored.
    */

    {
        if(BLog[playerid] == 0) return SendClientMessage(playerid, -1, "You must be logged in to your bank account to use this command!");
        /*
        Check if BLog is setted to 0.
        If it is 0. Sends a client message in color white!
        */

        new value;
        /*
        Creates a var named value
        */

        if(sscanf(params, "i", value)) return SendClientMessage(playerid, -1, "<!> /deposit [amount]");
        /*
        If player didn't put any amount of money. Sends a client message in color
        white */

        if(value > GetPlayerMoney(playerid)) return SendClientMessage(playerid, -1, "You dont have that amount of money on your pocket!");
        /*
        If the amount money inputted is higher than player's money.
        Sends a client message in color white!
        */

        LoadBankAccount(playerid, accountstring, 256, getName(playerid));
        /*
        Loads the bank account
        */

        SetBPlayerBalance(playerid, GetBPlayerBalance(playerid) + value);
        /*
        Adds the player's bank account balance to inputted amount money.
        Result = Deposit to bank account
        */

        GivePlayerMoney(playerid, -value);
        /*
        Removes Player's money = Inputted amount money
        */

        new String[128];
        /*
        Creates a string with cell size 128 */

        format(String, sizeof String, "You deposit $%i from your bank, and now got $%i on your bank. ", value, GetBPlayerBalance(playerid));
        /*
        format string is length 128.
        $%i = integer. Used to display current balance
        */

        SendClientMessage(playerid, -1, String);
        /* Sends a client message in color white */
        SaveBankAccount(playerid, accountstring, 256, getName(playerid));
        /*
        Saves bank account.
        accountstring is used to stored the bankaccount location from the function.
        256 is the length of the string that should be stored.
        getName(playerid) is the player's name
        */

    }
    else return SendClientMessage(playerid, -1, "You do not have bank account!");
    /*
    if player doesn't have bank account, sends a client message in color white.
    */

    return 1;
}
pawn Код:
CMD:balance(playerid, params[])
{
    if(!BankAccount_Loc(getName(playerid), accountstring, 256)) return SendClientMessage(playerid, -1, "You do not have bank account!");
    /*
    Checks if player have account in 'JBank/Accounts'. If it doesn't have. SendClientMessage
    sented in color white.
    accountstring is used to stored the bankaccount location from the function.
    256 is the length of the string that should be stored.
    */

    if(BLog[playerid] == 0) return SendClientMessage(playerid, -1, "You must be logged in to your bank account to use this command!");
    /*
    Check if BLog is setted to 0.
    If it is 0. Sends a client message in color white!
    */

    new str[100]; format(str, sizeof(str), "Bank Balance: $%i", GetBPlayerBalance(playerid));
    /*
    Creates a str with cell size 128
    format str is length 128.
    $%i = integer. Used to display current balance
    */

    SendClientMessage(playerid, -1, str);
    /*
    Sends a client message in color white
    */

    return 1;
}
Finished

You're done creating Bank System!
In just few lines!

Credits: Romel for the include, ****** for YSI, Incognito for streamer.
Reply
#2

I like this ! But the only one thing I disliked is using SaveBank and SaveAccount, it should just save it automatically on player disconnect. Also, if this was MySQL, webstats and all, imagine the freedom ! Nice work, anyways.
Reply
#3

thank you bro

sorry for high cell sizes. It will be fixed on v1.1
Reply
#4

Shouldn't this be in your own topic? This is for scripting tutorials, not 'how to use my include'.
Reply
#5

I didn't include any

'my'. in the name of the topic.
Yes i include. But i only included it in the introduction
Reply
#6

Good work add it in Jakadmin but F.Y.I i dont use you admin system but your admin system would be good with it
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)