In search of advice
#1

Hello, SA-MP Community,

I'm glad to see that the game is still alive after so many years.
Recently I've started playing again and I'm going to create a server myself with an self-written gamemode. Never did PWN scripting before so I am still learning the basics.

I've created a little bank command and I was wondering if you professionals got any advice on my code. Perhaps some bad habits I used or just details. The current code works perfect, it's just that I was wondering if I started off on the right foot.

Thanks in advance, here's my code:

PHP код:

CMD
:bank(playeridparams[])
{
    
// Build a main state to check if a player is inside a bank
    
new type[20];
    
    if (
sscanf(params"s[20]"type))
    {
        return 
SendSyntaxMessage(playerid"/bank [balance/withdraw/deposit/savings/loan]");
    }
    
    
// Balance
    
if (!strcmp(type"balance"true7))
    {
        
SendClientMessageEx(playeridCOLOR_INFO"Your current balance is: %d."FormatNumber(Player[playerid][pBank]));
           return 
1;
    }
    
    
// Deposit
    
if (!strcmp(type"deposit"true7))
    {
        new 
amount[20];
        
        if (
sscanf(params"s[20]d[20]"typeamount))
        {
            
SendSyntaxMessage(playerid"/bank deposit [amount]");
            return 
1;
        }
        else
        {
            if(
amount[0] > Player[playerid][pCash])
            {
                
SendErrorMessage(playerid"You don't have enough money on your hand to do that.");
                return 
1;
            }
            
            if(
amount[0] <= 0)
            {
                
SendErrorMessage(playerid"You can't deposit zero or less cash.");
                return 
1;
            }
            
            else {
                new 
nBanknCash;
                
                
nBank Player[playerid][pBank] + amount[0];
                
nCash Player[playerid][pCash] - amount[0];
                
                
Player[playerid][pBank] = nBank;
                
Player[playerid][pCash] = nCash;
                
                
GivePlayerMoney(playerid, - amount[0]);
                
                
SendClientMessageEx(playeridCOLOR_SUCCESS"You've deposited %d onto your bank account."FormatNumber(amount[0]));
                
SendClientMessageEx(playeridCOLOR_INFO"Your new balance is %d."FormatNumber(Player[playerid][pBank]));
                return 
1;
            }
        }
    }
    
    
// Withdraw
    
if (!strcmp(type"withdraw"true8))
    {
        new 
amount[20];
        if (
sscanf(params"s[20]d[20]"typeamount))
        {
            
SendSyntaxMessage(playerid"/bank withdraw [amount]");
            
SendClientMessageEx(playeridCOLOR_INFO"Your balance is %d."FormatNumber(Player[playerid][pBank]));
            return 
1;
        }
        else
        {
            if(
amount[0] > Player[playerid][pBank])
            {
                
SendErrorMessage(playerid"You don't have enough balance to do that.");
                
SendClientMessageEx(playeridCOLOR_INFO"Your balance is %d."FormatNumber(Player[playerid][pBank]));
                return 
1;
            }
            if(
amount[0] <= 0)
            {
                
SendErrorMessage(playerid"You can't withdraw zero or less cash.");
                return 
1;
            }
            else {
                new 
nBanknCash;
                
nBank Player[playerid][pBank] - amount[0];
                
nCash Player[playerid][pCash] + amount[0];
                
Player[playerid][pBank] = nBank;
                
Player[playerid][pCash] = nCash;
                
GivePlayerMoney(playeridamount[0]);
                
SendClientMessageEx(playeridCOLOR_SUCCESS"You've withdrawn %d from your bank account."FormatNumber(amount[0]));
                
SendClientMessageEx(playeridCOLOR_INFO"Your new balance is %d."FormatNumber(Player[playerid][pBank]));
                return 
1;
            }
        }
    }
    
    
// Savings
    
if (!strcmp(type"savings"true7))
    {
        new 
subtype[20];
        
        
// Main command
        
if (sscanf(params"s[20]s[20]"typesubtype))
        {
            
SendSyntaxMessage(playerid"/bank savings [info/create/abort]");
            
            if(
Player[playerid][pSavings] == 0)
            {
                
SendClientMessageEx(playeridCOLOR_INFO"You haven't created a savings account yet.");
                
SendClientMessageEx(playeridCOLOR_INFO"You can create one by using [/bank savings create]");
                return 
1;
            }
            
            if(
Player[playerid][pSavings] >= SAVINGS_MIN || Player[playerid][pSavings] <= SAVINGS_MAX)
            {
                
SendClientMessageEx(playeridCOLOR_INFO"You have a savings account with plan %d."Player[playerid][pSavingsPlan]);
                
SendClientMessageEx(playeridCOLOR_INFO"Your savings initial deposit is %d, which has an interest rate of %d percent."FormatNumber(Player[playerid][pSavings]), GetSavingsRate(Player[playerid][pSavingsPlan]));
                
SendClientMessageEx(playeridCOLOR_INFO"You currently have %d hours on the savings. Your goal is %d hours."Player[playerid][pSavingsTime], GetSavingsPaychecks(Player[playerid][pSavingsPlan]));
            }
            
            return 
1;
        }
        
        else
        {
            
// Savings info
            
if (!strcmp(subtype"info"true4))
            {
                
SendClientMessageEx(playeridCOLOR_INFO"Info about savings and the plans.");
                return 
1;
            }
            
            
// Savings create
            
if (!strcmp(subtype"create"true6))
            {
                new 
amount[20], plan[20];
                
                if (
sscanf(params"s[20]s[20]d[20]d[20]"typesubtypeamountplan))
                {
                    
SendSyntaxMessage(playerid"/bank savings create [%d-%d] [1-4]"FormatNumber(SAVINGS_MIN), FormatNumber(SAVINGS_MAX));
                    
SendClientMessageEx(playeridCOLOR_INFO"Use '/bank savings info' for more information about savings.");
                    return 
1;
                }
                
                else
                {
                    if(
Player[playerid][pSavings] == 0)
                    {
                        if(
plan[0] <= || plan[0] > 4)
                        {
                            
SendErrorMessage(playerid"The savings plan you've entered does not exist.");
                            
SendSyntaxMessage(playerid"/bank savings create [%d-%d] [1-4]"FormatNumber(SAVINGS_MIN), FormatNumber(SAVINGS_MAX));
                            return 
1;
                        }
                    
                          if(
amount[0] < SAVINGS_MIN || amount[0] > SAVINGS_MAX)
                        {
                            
SendErrorMessage(playerid"The deposit value for your savings have to be between %d and %d."FormatNumber(SAVINGS_MIN), FormatNumber(SAVINGS_MAX));
                            return 
1;
                        }
                        
                        if(
amount[0] > Player[playerid][pBank])
                        {
                            
SendErrorMessage(playerid"You do not have enough money on your bank account to do that.");
                            return 
1;
                        }
                        
                        else
                        {
                            new 
nBanknSavingsnPlan;
                            
                            
nBank Player[playerid][pBank] - amount[0];
                            
nSavings Player[playerid][pSavings] + amount[0];
                            
nPlan plan[0];
                            
Player[playerid][pBank] = nBank;
                            
Player[playerid][pSavings] = nSavings;
                            
Player[playerid][pSavingsPlan] = nPlan;
                            
Player[playerid][pSavingsTime] = 0;
                            
SendClientMessageEx(playeridCOLOR_SUCCESS"You've created a savings account with %d using plan %d."FormatNumber(amount[0]), plan);
                            
SendClientMessageEx(playeridCOLOR_INFO"Your new main bank balance is %d."FormatNumber(Player[playerid][pBank]));
                            
                            return 
1;
                        }
                    }
                    else
                    {
                        
SendErrorMessage(playerid"You already have a savings account.");
                        return 
1;
                    }
                }
            }
            
            
// Savings abort
            
if (!strcmp(subtype"abort"true5))
            {
                if(
Player[playerid][pSavings] == 0)
                {
                    
SendErrorMessage(playerid"You haven't got an savings account yet.");
                      return 
1;
                }
                
                else
                {
                    new 
confirm[20];
                    
                    if (
sscanf(params"s[20]s[20]s[20]"typesubtypeconfirm))
                    {
                        
SendWarningMessage(playerid"You'll lose all your gained interest if you abort now. Are you sure to abort your savings account?");
                        
SendSyntaxMessage(playerid"/bank savings abort yes");
                        return 
1;
                    }
                    
                    else
                    {
                        if (!
strcmp(confirm"yes"true3))
                        {
                            new 
nBanknSavingsinitialSavings;
                            
nBank Player[playerid][pBank] + Player[playerid][pSavings];
                            
nSavings 0;
                            
initialSavings Player[playerid][pSavings];
                            
// Tax?
                            
Player[playerid][pBank] = nBank;
                            
Player[playerid][pSavings] = nSavings;
                            
Player[playerid][pSavingsPlan] = 0;
                            
Player[playerid][pSavingsTime] = 0;
                            
SendClientMessageEx(playeridCOLOR_SUCCESS"You've successfully aborted your savings account.");
                            
SendClientMessageEx(playeridCOLOR_SUCCESS"There has been %d deposited to your main bank account."FormatNumber(initialSavings));
                            
SendClientMessageEx(playeridCOLOR_INFO"Your new bank account balance is %d."FormatNumber(Player[playerid][pBank]));
                            return 
1;
                        }
                        
                        else
                        {
                            
SendSyntaxMessage(playerid"/bank savings abort yes");
                            return 
1;
                        }
                    }
                }
            }
        }
    }
    
    if (!
strcmp(type"loan"true4))
    {
        new 
subtype[20];
        
// Main command
        
if (sscanf(params"s[20]s[20]"typesubtype))
        {
            
SendSyntaxMessage(playerid"/bank loan [info/new]");
            if(
Player[playerid][pLoan] == 0)
            {
                
SendClientMessageEx(playeridCOLOR_INFO"You haven't got a loan yet.");
                
SendClientMessageEx(playeridCOLOR_INFO"You can request one by using [/bank loan new]");
                return 
1;
            }
            if(
Player[playerid][pLoan] >= LOAN_MIN || Player[playerid][pLoan] <= LOAN_MAX)
            {
                
SendClientMessageEx(playeridCOLOR_INFO"You currently have a loan with plan %d."Player[playerid][pLoanPlan]);
                
SendClientMessageEx(playeridCOLOR_INFO"Your initial loan is %d, which has an interest rate of %d percent."FormatNumber(Player[playerid][pLoan]), GetLoanRate(Player[playerid][pLoanPlan]));
                
SendClientMessageEx(playeridCOLOR_INFO"You currently have paid %d times. You need a total of %d times to pay it off completely."Player[playerid][pLoanTime], GetLoanPaychecks(Player[playerid][pLoanPlan]));
            }
            return 
1;
        }
        else
        {
            
// Loan info
            
if (!strcmp(subtype"info"true4))
            {
                
SendClientMessageEx(playeridCOLOR_INFO"Info about loan and the plans.");
                return 
1;
            }
            
// Loan new
            
if (!strcmp(subtype"new"true3))
            {
                new 
amount[20], plan[20];
                if (
sscanf(params"s[20]s[20]d[20]d[20]"typesubtypeamountplan))
                {
                    
SendSyntaxMessage(playerid"/bank loan new [%d-%d] [1-4]"FormatNumber(LOAN_MIN), FormatNumber(LOAN_MAX));
                    
SendClientMessageEx(playeridCOLOR_INFO"Use '/bank loan info' for more information about loans.");
                    return 
1;
                }
                else
                {
                    if(
Player[playerid][pLoan] == 0)
                    {
                        if(
plan[0] <= || plan[0] > 4)
                        {
                            
SendErrorMessage(playerid"The loan plan you've entered does not exist.");
                            
SendSyntaxMessage(playerid"/bank loan new [%d-%d] [1-4]"FormatNumber(LOAN_MIN), FormatNumber(LOAN_MAX));
                            return 
1;
                        }
                          if(
amount[0] < LOAN_MIN || amount[0] > SAVINGS_MAX)
                        {
                            
SendErrorMessage(playerid"The amount of loan has to be between %d and %d."FormatNumber(LOAN_MIN), FormatNumber(LOAN_MAX));
                            return 
1;
                        }
                        else
                        {
                            new 
nBanknLoannPlan;
                            
nBank Player[playerid][pBank] + amount[0];
                            
nLoan Player[playerid][pLoan] + amount[0];
                            
nPlan plan[0];
                            
Player[playerid][pBank] = nBank;
                            
Player[playerid][pLoan] = nLoan;
                            
Player[playerid][pLoanPlan] = nPlan;
                            
Player[playerid][pLoanTime] = 0;
                            
SendClientMessageEx(playeridCOLOR_SUCCESS"You've successfully received a loan of %d using plan %d."FormatNumber(amount[0]), plan);
                            
SendClientMessageEx(playeridCOLOR_INFO"Your new main bank balance is %d."FormatNumber(Player[playerid][pBank]));
                            return 
1;
                        }
                    }
                    else
                    {
                        
SendErrorMessage(playerid"You already have a loan.");
                        return 
1;
                    }
                }
            }
        }
    }
    
    else
    {
        return 
SendSyntaxMessage(playerid"/bank [balance/withdraw/deposit/savings/loan]");
    }
    
    return 
1;

Reply
#2

I'd suggest using a dialog when command /bank is used, I can try to help but I am on phone right now so yeah, you'll have to wait a little bit. Other than that, not bad at all!
Reply
#3

Quote:
Originally Posted by Rufio
Посмотреть сообщение
I'd suggest using a dialog when command /bank is used, I can try to help but I am on phone right now so yeah, you'll have to wait a little bit. Other than that, not bad at all!
Hello Rufio,

Thanks for the quick reply. Yes, I have considered using dialogs.
However, I wonder wether using dialogs for such actions is really benefitial for a user.
Another idea could be to use both; like a user could use both a dialog or a command.

Greetings
Reply
#4

Alrighty let me point a few things out:

Код:
new amount[20];
You are using amount as integers only so you shouldn't place [20] behind it becouse thats how you would define it as a string (inb4 array).
So remove all the [...] after all 'amount' variables and dont forget to remove it from the sscanf lines aswell! (Now you also dont need to format that variable to display it.)
(So this also counts with the new plan[..] and other variables that should only contain numbers)

I find your usage of strcmp considered as pretty weird.
You should just use
Код:
if(strcmp(type, "savings") == 0)
Reply
#5

Quote:
Originally Posted by justinnater
Посмотреть сообщение
Alrighty let me point a few things out:

Код:
new amount[20];
You are using amount as integers only so you shouldn't place [20] behind it becouse thats how you would define it as a string (inb4 array).
So remove all the [...] after all 'amount' variables and dont forget to remove it from the sscanf lines aswell! (Now you also dont need to format that variable to display it.)
Hey,

Thanks for the quick reply. This is exactly the kind of response I am looking for.
How would my sscanf statement look like if I do like you say? Perhaps "s[20]s[20]dd"?

PHP код:
if(strcmp(type"savings") == 0
About the 'strcmp', shouldn't I define the string length?

Thanks
Reply
#6

s[20]d as simple as that, there is also another way to do that, that is sscanf(type,"'deposit'd",amount)
What that does is simply look for word "deposit" in type, then get the amount which was entered after it, so there wouldn't be a need to get the deposit again in sscanf (I hope you got the idea of how to do it).
I love these type of topics, I usually take a quick look at topics, and when I feel like topic isn't a "I have no brain, please exhaust yours for my benefit" I reply fast as I can to help them.
Feel free to ask any more questions you want, will be happy to help.

Quote:
Originally Posted by Josh_Lotus
Посмотреть сообщение
PHP код:
if(strcmp(type"savings") == 0
About the 'strcmp', shouldn't I define the string length?

Thanks
about that, if you want to check whole string to another one, no you don't need to specify the length, but as your CMD works, first sscanf would store "deposit 1000" in the type string, which wouldn't be equal to "deposit".
However if you just check the first 7 letters of it, it does, therefore you should use string length for partial checks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)