Off Topic > PAWN Scripting

Advanced Bank System.

(1/1)

Life:
Hello today i will be giving you an advanced bank system.

// Define colors
#define COLOR_WHITE 0xFFFFFFFF
#define COLOR_YELLOW 0xFFFF00FF
#define COLOR_GREEN 0x00FF00FF
#define COLOR_RED 0xFF0000FF

// Define dialog IDs
#define DIALOG_ATM 1
#define DIALOG_BANK 2

// Define textdraw IDs
#define TEXTDRAW_BALANCE 1
#define TEXTDRAW_DEPOSIT 2
#define TEXTDRAW_WITHDRAW 3

// Define maximum account balance
#define MAX_BALANCE 1000000

// Include files
#include <a_samp>
#include <sscanf2>
#include <dialog>
#include <textdraw>

// Function to format money values
public format_money(value, output[], len)
{
    new whole = int(value);
    new frac = int((value - whole) * 100);
    format(output, len, "$%d.%02d", whole, frac);
    return 1;
}

// Command to open ATM dialog
CMD:atm(playerid)
{
    if(!IsPlayerConnected(playerid)) return SendClientMessage(playerid, COLOR_RED, "Error: You are not connected to the server.");
   
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
   
    ShowPlayerDialog(playerid, DIALOG_ATM, DIALOG_STYLE_LIST, "ATM", "What would you like to do?", "View balance\nDeposit money\nWithdraw money\nCancel");
    return 1;
}

// Callback function for ATM dialog
public ATMDialog(playerid, response, listitem, inputtext[])
{
    if(response)
    {
        switch(listitem)
        {
            case 0: // View balance
                new Float:balance = GetPlayerScore(playerid);
                new msg[32];
                format_money(balance, msg, sizeof(msg));
                SendClientMessage(playerid, COLOR_GREEN, "Your current balance is:");
                SendClientMessage(playerid, COLOR_WHITE, msg);
                break;
           
            case 1: // Deposit money
                new Float:deposit;
                if(sscanf(inputtext, "f", deposit))
                {
                    if(deposit <= 0) return SendClientMessage(playerid, COLOR_RED, "Error: Invalid deposit amount.");
                    new Float:balance = GetPlayerScore(playerid);
                    if(balance + deposit > MAX_BALANCE) return SendClientMessage(playerid, COLOR_RED, "Error: You cannot deposit more than $1,000,000.");
                    GivePlayerScore(playerid, deposit);
                    new msg[64];
                    format_money(deposit, msg, sizeof(msg));
                    SendClientMessage(playerid, COLOR_GREEN, "You have deposited:");
                    SendClientMessage(playerid, COLOR_WHITE, msg);
                }
                else
                {
                    SendClientMessage(playerid, COLOR_RED, "Error: Invalid input.");
                }
                break;
           
            case 2: // Withdraw money
                new Float:withdraw;
                if(sscanf(inputtext, "f", withdraw))
                {
                    if(withdraw <= 0) return SendClientMessage(playerid, COLOR_RED, "Error: Invalid withdrawal amount.");
                    new Float:balance = GetPlayerScore(playerid);
                    if(balance - withdraw < 0) return SendClientMessage(playerid, COLOR_RED, "Error: Insufficient funds.");
                    TakePlayerScore(playerid, withdraw);
                    new msg[64];
                    format_money(withdraw, msg, sizeof(msg));
                    SendClientMessage(playerid, COLOR_GREEN, "You have withdrawn:");
                    SendClientMessage(playerid, COLOR_WHITE, msg);
}
else
{
SendClientMessage(playerid, COLOR_RED, "Error: Invalid input.");
}
break;
        default: // Cancel
            break;
    }
}
return 1;
}

// Command to open bank dialog
CMD:bank(playerid)
{
if(!IsPlayerConnected(playerid)) return SendClientMessage(playerid, COLOR_RED, "Error: You are not connected to the server.");
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);

TextDrawShowForPlayer(playerid, TEXTDRAW_BALANCE);
TextDrawShowForPlayer(playerid, TEXTDRAW_DEPOSIT);
TextDrawShowForPlayer(playerid, TEXTDRAW_WITHDRAW);
ShowPlayerDialog(playerid, DIALOG_BANK, DIALOG_STYLE_LIST, "Bank", "What would you like to do?", "View balance\nDeposit money\nWithdraw money\nCancel");
return 1;
}

// Callback function for bank dialog
public BankDialog(playerid, response, listitem, inputtext[])
{
TextDrawHideForPlayer(playerid, TEXTDRAW_BALANCE);
TextDrawHideForPlayer(playerid, TEXTDRAW_DEPOSIT);
TextDrawHideForPlayer(playerid, TEXTDRAW_WITHDRAW);
if(response)
{
    switch(listitem)
    {
        case 0: // View balance
            new Float:balance = GetPlayerScore(playerid);
            new msg[32];
            format_money(balance, msg, sizeof(msg));
            SendClientMessage(playerid, COLOR_GREEN, "Your current balance is:");
            SendClientMessage(playerid, COLOR_WHITE, msg);
            break;
       
        case 1: // Deposit money
            new Float:deposit;
            if(sscanf(inputtext, "f", deposit))
            {
                if(deposit <= 0) return SendClientMessage(playerid, COLOR_RED, "Error: Invalid deposit amount.");
                new Float:balance = GetPlayerScore(playerid);
                if(balance + deposit > MAX_BALANCE) return SendClientMessage(playerid, COLOR_RED, "Error: You cannot deposit more than $1,000,000.");
                GivePlayerScore(playerid, deposit);
                new msg[64];
                format_money(deposit, msg, sizeof(msg));
                SendClientMessage(playerid, COLOR_GREEN, "You have deposited:");
                SendClientMessage(playerid, COLOR_WHITE, msg);
            }
            else
            {
                SendClientMessage(playerid, COLOR_RED, "Error: Invalid input.");
            }
            break;
       
        case 2: // Withdraw money
            new Float:withdraw;
            if(sscanf(inputtext, "f", withdraw))
            {
                if(withdraw <= 0) return SendClientMessage(playerid, COLOR_RED, "Error: Invalid withdrawal amount.");
                new Float:balance = GetPlayerScore(playerid);
                if(balance - withdraw < 0) return SendClientMessage(playerid, COLOR_RED, "Error: Insufficient funds.");
                TakePlayerScore(playerid, withdraw);
                new msg[64];
                format_money(withdraw, msg, sizeof(msg));
                SendClientMessage(playerid, COLOR_GREEN, "You have withdrawn:");
                SendClientMessage(playerid, COLOR_WHITE, msg);
            }
            else
            {
                SendClientMessage(playerid, COLOR_RED, "Error: Invalid input.");
            }
            break;
       
        default: // Cancel
            break;
    }
}
return 1;
}

// Initialize textdraws
public OnGameModeInit()
{
new Float:x, Float:y, Float:z;
x = 1462.07
y = -1045.89;
z = 24.1;
TEXTDRAW_BALANCE = TextDrawCreate(x + 0.05, y + 0.17, "$0");
TextDrawLetterSize(TEXTDRAW_BALANCE, 0.2, 1.0);
TextDrawTextSize(TEXTDRAW_BALANCE, 0.25, 1.0);
TextDrawAlignment(TEXTDRAW_BALANCE, 1);
TextDrawColor(TEXTDRAW_BALANCE, 0xFFFFFFFF);
TextDrawBackgroundColor(TEXTDRAW_BALANCE, 0x80000000);
TextDrawSetOutline(TEXTDRAW_BALANCE, 1);
TextDrawSetProportional(TEXTDRAW_BALANCE, 1);
TextDrawFont(TEXTDRAW_BALANCE, 3);

TEXTDRAW_DEPOSIT = TextDrawCreate(x + 0.05, y + 0.27, "Deposit money");
TextDrawLetterSize(TEXTDRAW_DEPOSIT, 0.2, 1.0);
TextDrawTextSize(TEXTDRAW_DEPOSIT, 0.25, 1.0);
TextDrawAlignment(TEXTDRAW_DEPOSIT, 1);
TextDrawColor(TEXTDRAW_DEPOSIT, 0xFFFFFFFF);
TextDrawBackgroundColor(TEXTDRAW_DEPOSIT, 0x80000000);
TextDrawSetOutline(TEXTDRAW_DEPOSIT, 1);
TextDrawSetProportional(TEXTDRAW_DEPOSIT, 1);
TextDrawFont(TEXTDRAW_DEPOSIT, 3);

TEXTDRAW_WITHDRAW = TextDrawCreate(x + 0.05, y + 0.37, "Withdraw money");
TextDrawLetterSize(TEXTDRAW_WITHDRAW, 0.2, 1.0);
TextDrawTextSize(TEXTDRAW_WITHDRAW, 0.25, 1.0);
TextDrawAlignment(TEXTDRAW_WITHDRAW, 1);
TextDrawColor(TEXTDRAW_WITHDRAW, 0xFFFFFFFF);
TextDrawBackgroundColor(TEXTDRAW_WITHDRAW, 0x80000000);
TextDrawSetOutline(TEXTDRAW_WITHDRAW, 1);
TextDrawSetProportional(TEXTDRAW_WITHDRAW, 1);
TextDrawFont(TEXTDRAW_WITHDRAW, 3);

return 1;
}

// Cleanup textdraws
public OnPlayerDisconnect(playerid, reason)
{
TextDrawDestroy(TEXTDRAW_BALANCE);
TextDrawDestroy(TEXTDRAW_DEPOSIT);
TextDrawDestroy(TEXTDRAW_WITHDRAW);
return 1;
}

There may be errors so if there are please reply the errors here.

Navigation

[0] Message Index

Go to full version