Advertisement
Lorenc

LorencBank

Jul 5th, 2011
1,457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.66 KB | None | 0 0
  1. /*
  2.  *      LorencBank created by Lorenc
  3.  *
  4.  *          Uses SQLite and ZCMD
  5.  *
  6.  *       Released on the SAMP Forums.
  7.  *
  8. */
  9.  
  10. #include                        <a_samp>
  11. #include                        <zcmd>
  12.  
  13. /* ** Configuration ** */
  14. #define SERVER_DATABASE         "lorBank.db"
  15. #define SERVER_NAME             "ApplesDeathmatch"
  16.  
  17. //#define DIALOG_VERSION
  18. #define COMMAND_VERSION
  19. #define SHOW_CLICKED_BANK       // Cool feature, tab to click the player and show them there bank acc
  20.  
  21. #define DIALOG_TITLE            "{FFFFFF}"#SERVER_NAME""
  22. #define DIALOG_BANK_MENU        12          +1000
  23. #define DIALOG_BANK_WITHDRAW    13          +1000
  24. #define DIALOG_BANK_DEPOSIT     14          +1000
  25. #define DIALOG_BANK_INFO        15          +1000
  26.  
  27. #if defined COMMAND_VERSION
  28.     #if defined DIALOG_VERSION
  29.         #error You cannot have both versions defined!
  30.     #endif
  31. #endif
  32.  
  33. #if !defined COMMAND_VERSION
  34.     #if !defined DIALOG_VERSION
  35.         #error One version must be marked!
  36.     #endif
  37. #endif
  38.  
  39.  
  40. /* ** Colours ** */
  41. #define COL_GREEN               "{6EF83C}"
  42. #define COL_RED                 "{F81414}"
  43. #define COL_LIGHTBLUE           "{00C0FF}"
  44. #define COL_LGREEN              "{C9FFAB}"
  45. #define COL_LRED                "{FFA1A1}"
  46.  
  47. #define COLOR_GREEN             0x00CC00FF
  48. #define COLOR_RED               0xFF0000FF
  49. #define COLOR_YELLOW            0xFFFF00FF
  50. #define COLOR_ORANGE            0xEE9911FF
  51. #define COLOR_BLUE              0x60CED4FF
  52.  
  53. /* ** Player Data ** */
  54. enum PLAYER_DATA
  55. {
  56.     P_BANK_MONEY,
  57. }
  58.  
  59. new gPlayerData                 [MAX_PLAYERS][PLAYER_DATA],
  60.     DB:Database;
  61.  
  62. public OnFilterScriptInit()
  63. {
  64.     print("\nBank Script by Lorenc\n");
  65.     Database = db_open(SERVER_DATABASE);
  66.     db_free_result(db_query(Database, "CREATE TABLE IF NOT EXISTS `USERS` (`NAME`, `BANKMONEY`)"));
  67.     return 1;
  68. }
  69.  
  70. public OnPlayerConnect(playerid)
  71. {
  72.     new Query[128], DBResult:Result;
  73.     format(Query, sizeof(Query), "SELECT * FROM `USERS` WHERE `NAME` = '%s'", ReturnPlayerName(playerid));
  74.     Result = db_query(Database, Query);
  75.     if(db_num_rows(Result))
  76.     {
  77.         new Field[20];
  78.         db_get_field_assoc(Result, "BANKMONEY", Field, 20);
  79.         gPlayerData[playerid][P_BANK_MONEY] = strval(Field);
  80.     }
  81.     else
  82.     {
  83.         format(Query, sizeof(Query), "INSERT INTO `USERS` (`NAME`, `BANKMONEY`) VALUES('%s','0')", ReturnPlayerName(playerid));
  84.         db_free_result(db_query(Database, Query));
  85.     }
  86.     return 1;
  87. }
  88.  
  89. public OnPlayerDisconnect(playerid, reason)
  90. {
  91.     SavePlayerBankAccount(playerid);
  92.     return 1;
  93. }
  94.  
  95. CMD:bank(playerid, params[])
  96. {
  97.     #if defined DIALOG_VERSION
  98.         ShowPlayerDialog(playerid, DIALOG_BANK_MENU, DIALOG_STYLE_LIST,""#DIALOG_TITLE" - Bank","Withdraw\nDeposit\nAccount Information","Select", "Cancel");
  99.     #endif
  100.     #if defined COMMAND_VERSION
  101.     new string[128];
  102.     if(isnull(params)) SendLBankMessage(playerid, "/bank [WITHDRAW/DEPOSIT/ACCOUNT]");
  103.     else if(!strcmp(params, "withdraw", true, 8))
  104.     {
  105.         new amount[24];
  106.         strcpy2(amount, params, 0, 9);
  107.        
  108.         if(!IsNumeric(amount)) return SendLBankMessage(playerid, "Numeric Digits allowed only.");
  109.         else if(strval(amount) > gPlayerData[playerid][P_BANK_MONEY]) return SendLBankMessage(playerid, "You cannot withdraw this much.");
  110.         else if(strval(amount) < 0) return SendLBankMessage(playerid, "You cannot withdraw this much.");
  111.         else
  112.         {
  113.             GivePlayerMoney(playerid, strval(amount));
  114.             gPlayerData[playerid][P_BANK_MONEY] = gPlayerData[playerid][P_BANK_MONEY] - strval(amount);
  115.             format(string, sizeof(string), "You have withdrawed $%d dollars.", strval(amount));
  116.             SendLBankMessage(playerid, string);
  117.         }
  118.     }
  119.     else if(!strcmp(params, "deposit", true, 7))
  120.     {
  121.         new amount[24];
  122.         strcpy2(amount, params, 0, 8);
  123.  
  124.         if(!IsNumeric(amount)) return SendLBankMessage(playerid, "Numeric Digits allowed only.");
  125.         else if(strval(amount) > GetPlayerMoney(playerid)) return SendLBankMessage(playerid, "You cannot withdraw this much.");
  126.         else if(strval(amount) < 0) return SendLBankMessage(playerid, "You cannot withdraw this much.");
  127.         else
  128.         {
  129.             GivePlayerMoney(playerid, -strval(amount));
  130.             gPlayerData[playerid][P_BANK_MONEY] = gPlayerData[playerid][P_BANK_MONEY] + strval(amount);
  131.             format(string, sizeof(string), "You have deposited $%d dollars.", strval(amount));
  132.             SendLBankMessage(playerid, string);
  133.         }
  134.     }
  135.     else if(!strcmp(params, "account", true, 7))
  136.     {
  137.         format(string, sizeof(string), "{FFFFFF}Your current bank account:\n\nBank Money: $%d\nCurrent Money: $%d", gPlayerData[playerid][P_BANK_MONEY], GetPlayerMoney(playerid));
  138.         ShowPlayerDialog(playerid, 69+1000, DIALOG_STYLE_MSGBOX,""#DIALOG_TITLE" - Bank", string, "Ok", "");
  139.     }
  140.     else return SendLBankMessage(playerid, "/bank [WITHDRAW/DEPOSIT/ACCOUNT]");
  141.     #endif
  142.     return 1;
  143. }
  144.  
  145. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  146. {
  147.     new string[128];
  148.     if(dialogid == DIALOG_BANK_MENU)
  149.     {
  150.         if(response)
  151.         {
  152.             switch(listitem)
  153.             {
  154.                 case 0:
  155.                 {
  156.                     format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to withdraw\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  157.                     ShowPlayerDialog(playerid, DIALOG_BANK_WITHDRAW, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Take", "Back");
  158.                 }
  159.                 case 1:
  160.                 {
  161.                     format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to deposit\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  162.                     ShowPlayerDialog(playerid, DIALOG_BANK_DEPOSIT, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Give", "Back");
  163.                 }
  164.                 case 2:
  165.                 {
  166.                     format(string, sizeof(string), "{FFFFFF}Your Bank Information\n\nBalance: %d\nCurrent Money: %d", gPlayerData[playerid][P_BANK_MONEY], GetPlayerMoney(playerid));
  167.                     ShowPlayerDialog(playerid, DIALOG_BANK_INFO, DIALOG_STYLE_MSGBOX,""#DIALOG_TITLE" - Bank",string, "Ok", "Back");
  168.                 }
  169.             }
  170.         }
  171.     }
  172.     if(dialogid == DIALOG_BANK_WITHDRAW)
  173.     {
  174.         if(response)
  175.         {
  176.             if(!strlen(inputtext))
  177.             {
  178.                 SendLBankMessage(playerid, "Invalid Amount!");
  179.                 format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to withdraw\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  180.                 ShowPlayerDialog(playerid, DIALOG_BANK_WITHDRAW, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Take", "Back");
  181.             }
  182.             else if(strval(inputtext) > gPlayerData[playerid][P_BANK_MONEY])
  183.             {
  184.                 SendLBankMessage(playerid, "You don't have enough money!");
  185.                 format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to withdraw\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  186.                 ShowPlayerDialog(playerid, DIALOG_BANK_WITHDRAW, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Take", "Back");
  187.             }
  188.             else if(!IsNumeric(inputtext))
  189.             {
  190.                 SendLBankMessage(playerid, "Letters are forbidden and are not numbers.");
  191.                 format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to withdraw\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  192.                 ShowPlayerDialog(playerid, DIALOG_BANK_WITHDRAW, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Take", "Back");
  193.             }
  194.             else
  195.             {
  196.                 gPlayerData[playerid][P_BANK_MONEY] = gPlayerData[playerid][P_BANK_MONEY] - strval(inputtext);
  197.                 GivePlayerMoney(playerid, strval(inputtext));
  198.                 format(string, sizeof(string), "{FFFFFF}Your Bank Information\n\nBalance: %d\nCurrent Money: %d", gPlayerData[playerid][P_BANK_MONEY], GetPlayerMoney(playerid));
  199.                 ShowPlayerDialog(playerid, DIALOG_BANK_INFO, DIALOG_STYLE_MSGBOX,""#DIALOG_TITLE" - Bank",string, "Ok", "Back");
  200.             }
  201.         }
  202.         else ShowPlayerDialog(playerid, DIALOG_BANK_MENU,DIALOG_STYLE_LIST,""#DIALOG_TITLE" - Bank","Withdraw\nDeposit\nAccount Information","Select", "Cancel");
  203.     }
  204.     if(dialogid == DIALOG_BANK_DEPOSIT)
  205.     {
  206.         if(response)
  207.         {
  208.             if(!strlen(inputtext))
  209.             {
  210.                 SendLBankMessage(playerid, "Invalid Amount!");
  211.                 format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to deposit\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  212.                 ShowPlayerDialog(playerid, DIALOG_BANK_DEPOSIT, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Give", "Back");
  213.             }
  214.             else if(strval(inputtext) > GetPlayerMoney(playerid))
  215.             {
  216.                 SendLBankMessage(playerid, "You don't have enough money!");
  217.                 format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to deposit\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  218.                 ShowPlayerDialog(playerid, DIALOG_BANK_DEPOSIT, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Give", "Back");
  219.             }
  220.             else if(!IsNumeric(inputtext))
  221.             {
  222.                 SendLBankMessage(playerid, "Letters are forbidden and are not numbers.");
  223.                 format(string, sizeof(string), "{FFFFFF}Enter the amount you are willing to deposit\nfrom your banking account\n\nCurrent Balance: $%d", gPlayerData[playerid][P_BANK_MONEY]);
  224.                 ShowPlayerDialog(playerid, DIALOG_BANK_DEPOSIT, DIALOG_STYLE_INPUT,""#DIALOG_TITLE" - Bank",string, "Give", "Back");
  225.             }
  226.             else
  227.             {
  228.                 gPlayerData[playerid][P_BANK_MONEY] = gPlayerData[playerid][P_BANK_MONEY] + strval(inputtext);
  229.                 GivePlayerMoney(playerid, -strval(inputtext));
  230.                 format(string, sizeof(string), "{FFFFFF}Your Bank Information\n\nBalance: %d\nCurrent Money: %d", gPlayerData[playerid][P_BANK_MONEY], GetPlayerMoney(playerid));
  231.                 ShowPlayerDialog(playerid, DIALOG_BANK_INFO, DIALOG_STYLE_MSGBOX,""#DIALOG_TITLE" - Bank",string, "Ok", "Back");
  232.             }
  233.         }
  234.         else ShowPlayerDialog(playerid, DIALOG_BANK_MENU,DIALOG_STYLE_LIST,""#DIALOG_TITLE" - Bank","Withdraw\nDeposit\nAccount Information","Select", "Cancel");
  235.     }
  236.     if(dialogid == DIALOG_BANK_INFO)
  237.     {
  238.         if(!response) ShowPlayerDialog(playerid, DIALOG_BANK_MENU, DIALOG_STYLE_LIST,""#DIALOG_TITLE" - Bank","Withdraw\nDeposit\nAccount Information","Select", "Cancel");
  239.     }
  240.     return 1;
  241. }
  242.  
  243. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  244. {
  245.     new string[128];
  246.     #if defined SHOW_CLICKED_BANK
  247.         format(string, sizeof(string), "{FFFFFF}%s's Bank Information\n\nBalance: %d\nCurrent Money: %d", ReturnPlayerName(playerid), gPlayerData[clickedplayerid][P_BANK_MONEY], GetPlayerMoney(clickedplayerid));
  248.         ShowPlayerDialog(playerid, 69+1000, DIALOG_STYLE_MSGBOX,""#DIALOG_TITLE" - Bank",string, "Ok", "");
  249.     #endif
  250.     return 1;
  251. }
  252.  
  253. ///////////////////////////////////
  254. ///         Functions           ///
  255. //////////////////////////////////
  256.  
  257. stock SendLBankMessage(playerid, const Message[])
  258. {
  259.     new string[128];
  260.     format(string, sizeof(string), ""COL_LGREEN"LorencBank: {FFFFFF}%s", Message);
  261.     SendClientMessage(playerid, -1, string);
  262.     return 1;
  263. }
  264.  
  265. SavePlayerBankAccount(playerid)
  266. {
  267.     new Query[128];
  268.     format(Query, sizeof(Query), "UPDATE `USERS` SET BANKMONEY = '%d' WHERE `NAME` = '%s'", gPlayerData[playerid][P_BANK_MONEY], ReturnPlayerName(playerid));
  269.     return db_free_result(db_query(Database, Query));
  270. }
  271.  
  272. stock ReturnPlayerName(playerid)
  273. {
  274.     new pname[MAX_PLAYER_NAME];
  275.     GetPlayerName(playerid, pname, sizeof(pname));
  276.     return pname;
  277. }
  278.  
  279. stock strcpy2(dest[], src[], startdest = 0, startsrc = 0)
  280. {
  281.     for(new i=startsrc,j=strlen(src);i<j;i++) {
  282.         dest[startdest++]=src[i];
  283.     }
  284.     dest[startdest]=0;
  285. }
  286.  
  287. stock IsNumeric(const str[])
  288. {
  289.     new len = strlen(str);
  290.  
  291.     if(!len) return false;
  292.     for(new i; i < len; i++)
  293.     {
  294.         if(!('0' <= str[i] <= '9')) return false;
  295.     }
  296.     return true;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement