[Tutorial] How to make a bank system(saving with y_ini))
#1

Okay this is my third tutorial on how to make a easy and a simple bank system.

Here is the full script,I have explained the whole in the script.

PHP код:
#define filterscript//define of the filterscript.
//=========================Includes==============================//
#include <a_samp>//includes that include the files in the script.
#include <streamer>
#include <sscanf2>
#include <YSI\y_ini>
//=============================================================//
#define PATH "/BankAccounts/%s.ini"//This is the path that where you want to save the player's bank account data,in simple words,this will save player's data in a certain folder or some documents.
enum pInfo//this is the enum.
{
    
pBankAccount,//this variable will save the person's money in his account in the further codes,so in this code we are type or defining this.
    
pAccountdata//okay now this variable will save person's account data.
}
new 
PlayerInfo[MAX_PLAYERS][pInfo];//this is the define and the format in which we will insert our information.
forward LoadUser_data(playerid,name[],value[]);
stock GetName(playerid)//this is a stock,this will make our further codes more easy and not very long.
{
    new 
Name[MAX_PLAYER_NAME];//this variable will hold the player's name.
    
if(IsPlayerConnected(playerid))//this will check that if you are connected to the server or not.
    
{
        
GetPlayerName(playeridNamesizeof(Name));//this will get player's name and store it in the variable we defined as Name[MAX_PLAYER_NAME].
    
}
    return 
Name;//the return value.
}
public 
LoadUser_data(playerid,name[],value[])//this will load persons data.
{
    
INI_Int("BankAccount",PlayerInfo[playerid][pBankAccount]);//This will load player's money.
    
INI_Int("Datasaved",PlayerInfo[playerid][pAccountdata]);//this will load player's account data as a whole.
    
return 1;
}
stock UserPath(playerid)//now this is the path.This is pretty much like a format which will store player's information in an organized way.
{
    new 
string[128],playername[MAX_PLAYER_NAME];//this are the variables which will store differnt informations.
    
GetPlayerName(playerid,playername,sizeof(playername));//this will get player's name,and store it in the variable we defined as playername.
    
format(string,sizeof(string),PATH,playername);//now this will insert player's name in player's file,and in the path we defined earlier.
    
return string;
}
public 
OnPlayerDisconnect(playeridreason)
{
    new 
INI:File INI_Open(UserPath(playerid));//this will open player's file from the path we defined at the top of the script.
    
INI_SetTag(File,"data");//this will set it's tag as data.
    
INI_WriteInt(File,"BankAccount",PlayerInfo[playerid][pBankAccount]);//when a player will disconnect,it will store player's account money in his file.
    
INI_WriteInt(File,"Datasaved",PlayerInfo[playerid][pAccountdata]);//when a player will disconnect,it will store player's account data as a whole in his file.
    
INI_Close(File);//now after saving,this will save and close player's file.
    
return 1;
}
public 
OnPlayerConnect(playerid)
{
    if(
PlayerInfo[playerid][pAccountdata] == 0)//this will check that if player's account = 0 this will set his account balance to 0.
    
{
        new 
INI:File INI_Open(UserPath(playerid));//again this will open player's file from the player's file.
        
INI_SetTag(File,"data");//set's the tag to data.
        
INI_WriteInt(File,"BankAccount",0);//set's player bank balance to 0.
        
INI_WriteInt(File,"DataSaved",1);//Datasaved would be equal to 1.
        
INI_Close(File);//after saving it will close the file.
        
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra true, .extra playerid);//and then after closing it will again load player's file from the created file.
    
}
    else
    {
        
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra true, .extra playerid);//if player's account data is not equal to 0,this will load player's bank account data that was saved when he logged off last time.
    
}
    return 
1;
}
public 
OnPlayerCommandText(playeridcmdtext[])
{
    if (
strcmp("/bankhelp"cmdtexttrue10) == 0)//the help command.Was not needed but will make the script and the function look good.
    
{
        
SendClientMessage(playerid,-1,"|___________________BANK HELP___________________|");//this will send player message.
        
SendClientMessage(playerid,-1,"/witdraw /deposit /bankhelp /checkbalance");
        
SendClientMessage(playerid,-1,"|______________________________________________|");//again the same.
    
}
     else if (
strcmp("/deposit"cmdtexttrue10) == 0)//now the deposit command.
        
{
            new 
string[256];//the variable which will store the information of a string that we will format after wards.
            
new type;//this variable will store player's input.
            
{
                if(
sscanf(cmdtext"d"type)) return SendClientMessage(playerid, -1"[USAGE]: /deposit [amount]");//this will check that if he inserted the write format or not,if it is and incorrect format,it will break the codes there and end the process with an error message.
                
if(type == 0)//this will check that if the type = 0.
                
if(GetPlayerMoney(playerid)< type)//this will check that if he is inserting a higher amount of money then he has in hand.
                
{
                       
GivePlayerMoney(playerid,-type);//this minus his money from his hand.
                       
PlayerInfo[playerid][pBankAccount] += type;//this will store the minused money in player's file,and will add money to his account.
                      
format(stringsizeof(string), "INFO: You have successfully deposited $%d to your bank account."type);//this is the format of the following message of how much money you deposited into your account.
                      
SendClientMessage(playerid0xFFFFFFstring);//this will send the message which we formated above,the information was stored in the variable string.
                  
}
                  else 
SendClientMessage(playerid0xFFFFFF"SERVER: You do not have that much amount in your hand");//this will send an error message that if a player enter's an invalid amount.Or in simple words that if he enter's a higher amount then he has in hand.
            
}
        }
    else if (
strcmp("/withidraw"cmdtexttrue10) == 0)//now the withdraw command.
        
{
            new 
string[256];//this is the variable that will store the formated message,we will format it in the following codes.
            
new type;////this variable will store player's input.
            
{
                if(
sscanf(cmdtext"d"type)) return SendClientMessage(playerid, -1"[USAGE]: /withdraw [amount]");//this will check that if he inserted the write format or not,if it is and incorrect format,it will break the codes there and end the process with an error message.
                
if(type == 0)//this will check that if the type = 0.
                
if(PlayerInfo[playerid][pBankAccount] >= type)//this will check that if the inputed amount is higher then the amount in the bank account.
                
{
                       
PlayerInfo[playerid][pBankAccount] -= type;//this will minus the the amount or money he inserted, in the command.
                      
GivePlayerMoney(playeridtype);//this will give the player money he wanted to withdraw from the bank.
                      
format(stringsizeof(string), "INFO: You have successfully withdrawn $%d from your bank account."type);//this is the formated message.This will store the formatted message in the variable we defined as string.
                      
SendClientMessage(playerid0xFFFFFFstring);//will send the formated message we formatted above.
                  
}
                  else 
SendClientMessage(playerid0xFFFFFF"SERVER: You do not have that much amount in your account");//this will send an error message that if a player enter's an invalid amount.Or in simple words that if he enter's a higher amount then he has in bank account.
            
}
        }
    else if (
strcmp("/checkbalance"cmdtexttrue10) == 0)
    {
        new 
string[256];//the variable which will store the formatted message we will format in the next line.
        
format(stringsizeof(string), "You currently have $%d In your bank account."PlayerInfo[playerid][pBankAccount]);//this is the formated message which will tell the player that how much money he deposited in the bank account.
        
SendClientMessage(playerid,-1,string);//this will send the formatted above,and which was stored in the variable above.
    
}
    return 
0;

Usefull link are:

https://sampwiki.blast.hk/wiki/Format <<<< Info about formating messages.

https://sampforum.blast.hk/showthread.php?tid=175565<<<<<< info about y_ini (credits to ******)

http://forum.sa-mp.com/showthread.ph...hlight=sscanf2 <<<<<< info about sscanf

Hope this helped.
Reply
#2

Good job, would be useful. Rep+
Reply
#3

Great man too useful i learned somethings from it ! Keep it up
Reply
#4

Thanks.
Reply
#5

try to use [pawn ][ /pawn]instead, nice job....
Reply
#6

Thanks.
Reply
#7

Great, but can you change the commands to ZCMD?
Reply
#8

Quote:
Originally Posted by Guitar
Посмотреть сообщение
Great, but can you change the commands to ZCMD?
pawn Код:
#define filterscript//define of the filterscript.
//=========================Includes==============================//
#include <a_samp>//includes that include the files in the script.
#include <streamer>
#include <sscanf2>
#include <YSI\y_ini>
#include <zcmd>
//=============================================================//
#define PATH "/BankAccounts/%s.ini"//This is the path that where you want to save the player's bank account data,in simple words,this will save player's data in a certain folder or some documents.

enum pInfo//this is the enum.
{
    pBankAccount,//this variable will save the person's money in his account in the further codes,so in this code we are type or defining this.
    pAccountdata//okay now this variable will save person's account data.
}

new PlayerInfo[MAX_PLAYERS][pInfo];//this is the define and the format in which we will insert our information.

forward LoadUser_data(playerid,name[],value[]);

stock GetName(playerid)//this is a stock,this will make our further codes more easy and not very long.
{
    new Name[MAX_PLAYER_NAME];//this variable will hold the player's name.
    if(IsPlayerConnected(playerid))//this will check that if you are connected to the server or not.
    {
        GetPlayerName(playerid, Name, sizeof(Name));//this will get player's name and store it in the variable we defined as Name[MAX_PLAYER_NAME].
    }
    return Name;//the return value.
}

public LoadUser_data(playerid,name[],value[])//this will load persons data.
{
    INI_Int("BankAccount",PlayerInfo[playerid][pBankAccount]);//This will load player's money.
    INI_Int("Datasaved",PlayerInfo[playerid][pAccountdata]);//this will load player's account data as a whole.
    return 1;
}

stock UserPath(playerid)//now this is the path.This is pretty much like a format which will store player's information in an organized way.
{
    new string[128],playername[MAX_PLAYER_NAME];//this are the variables which will store differnt informations.
    GetPlayerName(playerid,playername,sizeof(playername));//this will get player's name,and store it in the variable we defined as playername.
    format(string,sizeof(string),PATH,playername);//now this will insert player's name in player's file,and in the path we defined earlier.
    return string;
}

public OnPlayerDisconnect(playerid, reason)
{
    new INI:File = INI_Open(UserPath(playerid));//this will open player's file from the path we defined at the top of the script.
    INI_SetTag(File,"data");//this will set it's tag as data.
    INI_WriteInt(File,"BankAccount",PlayerInfo[playerid][pBankAccount]);//when a player will disconnect,it will store player's account money in his file.
    INI_WriteInt(File,"Datasaved",PlayerInfo[playerid][pAccountdata]);//when a player will disconnect,it will store player's account data as a whole in his file.
    INI_Close(File);//now after saving,this will save and close player's file.
    return 1;
}

public OnPlayerConnect(playerid)
{
    if(PlayerInfo[playerid][pAccountdata] == 0)//this will check that if player's account = 0 this will set his account balance to 0.
    {
        new INI:File = INI_Open(UserPath(playerid));//again this will open player's file from the player's file.
        INI_SetTag(File,"data");//set's the tag to data.
        INI_WriteInt(File,"BankAccount",0);//set's player bank balance to 0.
        INI_WriteInt(File,"DataSaved",1);//Datasaved would be equal to 1.
        INI_Close(File);//after saving it will close the file.
        INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);//and then after closing it will again load player's file from the created file.
    }
    else
    {
        INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);//if player's account data is not equal to 0,this will load player's bank account data that was saved when he logged off last time.
    }
    return 1;
}
CMD:bankhelp(playerid,params[])//the help command.Was not needed but will make the script and the function look good.
{
    SendClientMessage(playerid,-1,"|___________________ATM HELP___________________|");//this will send player message.
    SendClientMessage(playerid,-1,"/witdraw /deposit /bankhelp /checkbalance");
    SendClientMessage(playerid,-1,"|______________________________________________|");//again the same.
}
CMD:deposit(playerid,params[])//now the deposit command.
{
    new string[256];//the variable which will store the information of a string that we will format after wards.
    new type;//this variable will store player's input.
        {
            if(sscanf(params, "d", type)) return SendClientMessage(playerid, -1, "[USAGE]: /deposit [amount]");//this will check that if he inserted the write format or not,if it is and incorrect format,it will break the codes there and end the process with an error message.
            if(type == 0)//this will check that if the type = 0.
            if(GetPlayerMoney(playerid)< type)//this will check that if he is inserting a higher amount of money then he has in hand.
            {
                GivePlayerMoney(playerid,-type);//this minus his money from his hand.
                PlayerInfo[playerid][pBankAccount] += type;//this will store the minused money in player's file,and will add money to his account.
                format(string, sizeof(string), "INFO: You have successfully deposited $%d to your bank account.", type);//this is the format of the following message of how much money you deposited into your account.
                SendClientMessage(playerid, 0xFFFFFF, string);//this will send the message which we formated above,the information was stored in the variable string.
            }
            else SendClientMessage(playerid, 0xFFFFFF, "SERVER: You do not have that much amount in your hand");//this will send an error message that if a player enter's an invalid amount.Or in simple words that if he enter's a higher amount then he has in hand.
        }
        return 1;
}
CMD:withdraw(playerid,params[])//now the withdraw command.
{
    new string[256];//this is the variable that will store the formated message,we will format it in the following codes.
    new type;////this variable will store player's input.
        {
            if(sscanf(params, "d", type)) return SendClientMessage(playerid, -1, "[USAGE]: /withdraw [amount]");//this will check that if he inserted the write format or not,if it is and incorrect format,it will break the codes there and end the process with an error message.
            if(type == 0)//this will check that if the type = 0.
            if(PlayerInfo[playerid][pBankAccount] >= type)//this will check that if the inputed amount is higher then the amount in the bank account.
            {
                PlayerInfo[playerid][pBankAccount] -= type;//this will minus the the amount or money he inserted, in the command.
                GivePlayerMoney(playerid, type);//this will give the player money he wanted to withdraw from the bank.
                format(string, sizeof(string), "INFO: You have successfully withdrawn $%d from your bank account.", type);//this is the formated message.This will store the formatted message in the variable we defined as string.
                SendClientMessage(playerid, 0xFFFFFF, string);//will send the formated message we formatted above.
            }
            else SendClientMessage(playerid, 0xFFFFFF, "SERVER: You do not have that much amount in your account");//this will send an error message that if a player enter's an invalid amount.Or in simple words that if he enter's a higher amount then he has in bank account.
        }
        return 1;
}
CMD:checkbalance(playerid,params[])
{
    new string[256];//the variable which will store the formatted message we will format in the next line.
    format(string, sizeof(string), "You currently have $%d In your bank account.", PlayerInfo[playerid][pBankAccount]);//this is the formated message which will tell the player that how much money he deposited in the bank account.
    SendClientMessage(playerid,-1,string);//this will send the formatted above,and which was stored in the variable above.
}
There you go.
Reply
#9

nice one
Reply
#10

Quote:
Originally Posted by Youice
Посмотреть сообщение
nice one
Thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)