Julian90

gBank

May 25th, 2012
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.39 KB | None | 0 0
  1. /*
  2.  
  3.     [J]ulian (c) 2012 - Julián Agüero.
  4.                 sa-mp
  5.     Sistema de cuentas bancarias - MySQL.
  6.  
  7.     Requiere: http://forum.sa-mp.com/showthread.php?t=56564
  8.     Ejemplo: http://adf.ly/900cL
  9.    
  10. */
  11. #if !defined gBank_included
  12.     #define gBank_included
  13. #endif
  14.  
  15. #if !defined mysql_included
  16.     #include <a_mysql>
  17. #endif
  18.  
  19. #if !defined bank_table
  20.     #define bank_table "bank"
  21. #endif
  22.  
  23. static
  24.     IsBankLogged[MAX_PLAYERS],
  25.     bString[128],
  26.     deposit;
  27.  
  28. #define INVALID_BANK_ACCOUNT -1
  29.  
  30.  
  31. /*
  32.     native BankSystemInit()
  33. */
  34.  
  35. stock BankSystemInit()
  36. {
  37.     mysql_query("CREATE TABLE IF NOT EXISTS "bank_table" (account INT(5) NOT NULL AUTO_INCREMENT, user VARCHAR(34) NOT NULL, pass VARCHAR(34) NOT NULL, balance INT(5) NOT NULL, PRIMARY KEY (account))");
  38. }
  39.  
  40. /*
  41.     native ResetPlayerLogged(playerid)
  42. */
  43.  
  44. stock ResetPlayerLogged(playerid)
  45. {
  46.     IsBankLogged[playerid] = INVALID_BANK_ACCOUNT;
  47. }
  48.  
  49. /*
  50.     native GetTotalAccounts()
  51. */
  52.  
  53. stock GetTotalAccounts()
  54. {
  55.     mysql_query("SELECT COUNT(*) FROM "bank_table"");
  56.     mysql_store_result();
  57.     mysql_fetch_row(bString);
  58.     return strval(bString);
  59. }
  60.  
  61. /*
  62.     native CreateBankAccount(user[], pass[], balance = 0)
  63. */
  64.  
  65. stock CreateBankAccount(user[], pass[], balance = 0)
  66. {
  67.     format(bString, sizeof(bString), "INSERT INTO "bank_table" (user, pass, balance) VALUES ('%s', '%s', %d)", user, pass, balance);
  68.     mysql_query(bString);
  69. }
  70.  
  71. /*
  72.     native DeleteBankAccount(account)
  73. */
  74.  
  75. stock DeleteBankAccount(account)
  76. {
  77.     format(bString, sizeof(bString), "DELETE FROM "bank_table" WHERE account = %d LIMIT 1", account);
  78.     mysql_query(bString);
  79. }
  80.  
  81. /*
  82.     native GetAccountBalance(account)
  83. */
  84.  
  85. stock GetAccountBalance(account)
  86. {
  87.     format(bString, sizeof(bString), "SELECT balance FROM "bank_table" WHERE account = %d", account);
  88.     mysql_query(bString);
  89.     mysql_store_result();
  90.     mysql_fetch_row(bString);
  91.     return strval(bString);
  92. }
  93.  
  94. /*
  95.     native SetAccountBalance(account, amount)
  96. */
  97.  
  98. stock SetAccountBalance(account, amount)
  99. {
  100.     format(bString, sizeof(bString), "UPDATE "bank_table" SET balance = %d WHERE account = %d", amount, account);
  101.     mysql_query(bString);
  102. }
  103.  
  104. /*
  105.     native IsPlayerLoggedInAccount(playerid, account)
  106. */
  107.  
  108. stock IsPlayerLoggedInAnyAccount(playerid)
  109. {
  110.     if(IsBankLogged[playerid] != INVALID_BANK_ACCOUNT) return 1;
  111.     return 0;
  112. }
  113.  
  114. /*
  115.     native IsPlayerLoggedInAnyAccount(playerid)
  116. */
  117.  
  118. stock IsPlayerLoggedInAccount(playerid, account)
  119. {
  120.     if(IsBankLogged[playerid] == account && IsBankLogged[playerid] != INVALID_BANK_ACCOUNT) return 1;
  121.     return 0;
  122. }
  123.  
  124. /*
  125.     native IsPlayerHasAccount(user[])
  126. */
  127.  
  128. stock IsPlayerHasAccount(user[])
  129. {
  130.     format(bString, sizeof(bString), "SELECT * FROM "bank_table" WHERE user = '%s'", user);
  131.     mysql_query(bString);
  132.     mysql_store_result();
  133.     if(mysql_num_rows() != 0) return 1;
  134.     return 0;
  135. }
  136.  
  137. /*
  138.     native DepositBankMoney(account, amount)
  139. */
  140.  
  141. stock DepositBankMoney(account, amount)
  142. {
  143.     deposit = (GetAccountBalance(account) + amount);
  144.     format(bString, sizeof(bString), "UPDATE "bank_table" SET balance = %d WHERE account = %d", deposit, account);
  145.     mysql_query(bString);
  146.     return deposit;
  147. }
  148.  
  149. /*
  150.     native WithdrawBankMoney(account, amount)
  151. */
  152.  
  153. stock WithdrawBankMoney(account, amount)
  154. {
  155.     deposit = (GetAccountBalance(account) - amount);
  156.     format(bString, sizeof(bString), "UPDATE "bank_table" SET balance = %d WHERE account = %d", deposit, account);
  157.     mysql_query(bString);
  158.     return deposit;
  159. }
  160.  
  161. /*
  162.     native WireTransfer(account, toaccount, amount)
  163. */
  164.  
  165. stock WireTransfer(account, toaccount, amount)
  166. {
  167.     WithdrawBankMoney(account, amount);
  168.     DepositBankMoney(toaccount, amount);
  169.     return amount;
  170. }
  171.  
  172. /*
  173.     native PlayerLoggedInAccount(playerid, account, pass[]);
  174. */
  175.  
  176. stock PlayerLoggedInAccount(playerid, account, pass[])
  177. {
  178.     format(bString, sizeof(bString), "SELECT * FROM "bank_table" WHERE account = %d AND pass = '%s'", account, pass);
  179.     mysql_query(bString);
  180.     mysql_store_result();
  181.     if(mysql_num_rows() != 0)
  182.     {
  183.         CallLocalFunction("OnPlayerLoginAccount", "iii", playerid, account, true);
  184.         IsBankLogged[playerid] = account;
  185.     }
  186.     else CallLocalFunction("OnPlayerLoginAccount", "iii", playerid, account, false);
  187. }
  188.  
  189. /*
  190.     native PlayerLogoutOfAccount(playerid)
  191. */
  192.  
  193. stock PlayerLogoutOfAccount(playerid)
  194. {
  195.     CallLocalFunction("OnPlayerLogoutAccount", "ii", playerid, account);
  196.     IsBankLogged[playerid] = INVALID_BANK_ACCOUNT;
  197. }
  198.  
  199. forward OnPlayerLoginAccount(playerid, account, success);
  200. forward OnPlayerLogoutAccount(playerid, account);
Add Comment
Please, Sign In to add comment