ZumeZero

INCLUDE Bank system v0.01 MYSQL 39-2

Dec 29th, 2014
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.15 KB | None | 0 0
  1. /*
  2.  
  3. MYSQL 39-2
  4.  
  5. - DATABASE http://pastebin.com/MM5WNXqi
  6. - Bank system v0.01 by _ZUME
  7. - Functions
  8. - GetAccountID(id) - Retorna el ID de la cuenta
  9. - IsPlayerNameConnected(name[]) - Comprueba si un jugador esta conectado mediante su nombre
  10. - GetFounderAccount(id) - Retorna el nombre del fundador de la cuenta bancaria por ID Relativa (Vease GetAccountID(id))
  11. - GetAccountRelativeID(id) - Retorna el ID relativo de la cuenta bancaria en el servidor
  12. - GetPassAccount(id) - Retorna en string la password de la cuenta bancaria por ID Relativa.
  13. - GetMoneyAccount(id) - Retorna el dinero de la cuenta bancaria por ID relativa.
  14. - ValidatePasswordAccount(id, pass[]) - Comprueba una cadena con la password de la cuenta a verdadero o falso por ID Relativa
  15. - BankAccount_Delete(id) - Elimina una cuenta bancaria por ID Relativa
  16. - BankAccount_Count(playerid) - Retorna la cantidad de cuentas bancarias de un playerid
  17. - AccountIsOfPlayer(id, playerid) - Comprueba si una cuenta bancaria es de un jugador o no retornando verdadero o falso.
  18. - Init_BankConnection() - Inicia la conexion a la base de datos en donde se encuentre la tabla
  19. - CreateBankAccount(playerid, password[]) - Crea una nueva cuenta bancaria
  20. - SaveAccountBank(id) - Actualiza a la base de datos una cuenta bancaria
  21. - SubstracMoneyAccount(id, amount) - Resta dinero a una cuenta bancaria por ID relativa
  22. - RaiseMoneyAccount(id, amount) - AUmenta dinero a una cuenta bancaria por ID relativa
  23. - Callbacks
  24. - OnPlayerCreateBankAccount(playerid, accountid, action)
  25. ( Es llamada cuando se crea o elimina una cuenta bancaria, el argumento action tiene disponible ACCOUNT_CREATE ( 1 ) ACCOUNT_DELETE ( 2) )
  26. */
  27.  
  28. #include <a_samp>
  29.  
  30. #if !defined mysql_connect
  31. #include <a_mysql>
  32. #endif
  33.  
  34. // actions
  35. #define ACCOUNT_CREATE 1
  36. #define ACCOUNT_DELETE 2
  37.  
  38. enum BankEnum
  39. {
  40.     b_id,
  41.     b_exist,
  42.     b_founder[MAX_PLAYER_NAME],
  43.     b_money,
  44. };
  45. new BankInfo[MAX_BANK_ACCOUNT][BankEnum];
  46. new g_BankConnection = -1;
  47.  
  48. forward OnPlayerCreateBankAccount(playerid, accountid, action);
  49.  
  50. stock SQL_BANKCONNECT(){
  51.     g_BankConnection = mysql_connect(BANK_LOCALSQL, BANK_USERSQL, BANK_DBSQL, BANK_PASSSQL);
  52. }
  53.  
  54. stock IsPlayerNameConnected(name[])
  55. {
  56.     for(new i = 0; i != MAX_PLAYERS; i ++) if(IsPlayerConnected(i))
  57.     {
  58.  
  59.         if(!strcmp(GetNamePlayerBank(i), name, true)){
  60.             return i;
  61.         }
  62.     }
  63.     return INVALID_PLAYER_ID;
  64. }
  65.  
  66. stock GetAccountID(id){
  67.     if(!BankInfo[id][b_exist])
  68.     return 0;
  69.  
  70.     return (BankInfo[id][b_id]);
  71. }
  72.  
  73. stock GetAccountRelativeID(id){
  74.     for(new i = 0; i != MAX_BANK_ACCOUNT; i ++){
  75.         if(BankInfo[i][b_exist] && BankInfo[i][b_id] == id){
  76.             return i;
  77.         }
  78.     }
  79.     return -1;
  80. }
  81.  
  82. stock GetFounderAccount(id){
  83.     new
  84.     name[MAX_PLAYER_NAME] = {85, 110, 107, 110, 111, 119, 110};
  85.  
  86.     if(!BankInfo[id][b_exist])
  87.     return name;
  88.  
  89.     strunpack(name, BankInfo[id][b_founder]);
  90.     return name;
  91. }
  92.  
  93. stock GetPassAccount(id){
  94.     new
  95.     name[MAX_PLAYER_NAME] = {85, 110, 107, 110, 111, 119, 110};
  96.  
  97.     if(!BankInfo[id][b_exist])
  98.     return name;
  99.  
  100.     strunpack(name, BankInfo[id][b_pass]);
  101.     return name;
  102. }
  103.  
  104. stock SubstracMoneyAccount(id, amount){
  105.     if(!BankInfo[id][b_exist])
  106.     return 0;
  107.  
  108.     BankInfo[id][b_money] -= amount;
  109.     return 1;
  110. }
  111.  
  112. stock RaiseMoneyAccount(id, amount){
  113.     if(!BankInfo[id][b_exist])
  114.     return 0;
  115.  
  116.     BankInfo[id][b_money] += amount;
  117.     return 1;
  118. }
  119.  
  120. stock GetMoneyAccount(id){
  121.     if(!BankInfo[id][b_exist])
  122.     return name;
  123.  
  124.     return (BankInfo[id][b_money]);
  125. }
  126.  
  127. stock ValidatePasswordAccount(id, pass[]){
  128.     if(!BankInfo[id][b_exist])
  129.     return 0;
  130.  
  131.     if(!strcmp(pass, GetPassAccount(id), true)){
  132.         return 1;
  133.     }
  134.     return 0;
  135. }
  136.  
  137. cache_get_field_intbank(row, const field_name[]){
  138.     new
  139.     str[12];
  140.  
  141.     cache_get_field_content(row, field_name, str, g_BankConnection);
  142.     return strval(str);
  143. }
  144.  
  145. forward AccountsBankLoad(); public AccountsBankLoad(){
  146.     if(mysql_errno(g_BankConnection) != 0)
  147.     return printf("The bank database was not connected.");
  148.  
  149.     new
  150.     rows,
  151.     fields;
  152.  
  153.     cache_get_data(rows, fields, g_BankConnection);
  154.     for (new i = 0; i < rows; i ++) if (i < MAX_BANK_ACCOUNT){
  155.         BankInfo[i][b_exist] = true;
  156.         BankInfo[i][b_id] = cache_get_field_intbank(i, "b_id");
  157.         BankInfo[i][b_money] = cache_get_field_intbank(i, "b_money");
  158.         cache_get_field_content(i, "b_founder", BankInfo[i][b_founder], g_BankConnection);
  159.     }
  160.     return 1;
  161. }
  162.  
  163. stock SaveAccountBank(id){
  164.     if(mysql_errno(g_BankConnection) != 0)
  165.     return printf("The bank database was not connected.");
  166.  
  167.     if(!BankInfo[id][b_exist])
  168.     return 0;
  169.  
  170.     new
  171.     query[200];
  172.  
  173.     format(query, sizeof(query), "UPDATE `banco` SET `b_money` = '%d' WHERE `b_id` = '%d'", BankInfo[id][b_money], BankInfo[id][b_id]);
  174.     mysql_tquery(g_BankConnection, query, "", "");
  175.     return 1;
  176. }
  177.  
  178. stock BankAccount_Delete(id){
  179.     if(mysql_errno(g_BankConnection) != 0)
  180.     return printf("The bank database was not connected.");
  181.  
  182.     if(!BankInfo[id][b_exist])
  183.     return 0;
  184.  
  185.     new playerid = INVALID_PLAYER_ID;
  186.     playerid = IsPlayerNameConnected(GetFounderAccount(id));
  187.  
  188.     CallLocalFunction("OnPlayerCreateBankAccount", "iid", playerid, id, ACCOUNT_DELETE);
  189.  
  190.     BankInfo[id][b_exist] = false;
  191.     BankInfo[id][b_money] = 0;
  192.  
  193.     new
  194.     query[200];
  195.  
  196.     format(query, sizeof(query), "DELETE FROM `banco` WHERE `b_id` = '%d'", BankInfo[id][b_id]);
  197.     mysql_tquery(g_BankConnection, query, "", "");
  198.     return 1;
  199. }
  200.  
  201. stock BankAccount_Count(playerid){
  202.     new count = 0;
  203.     for(new i = 0; i < MAX_BANK_ACCOUNT; i++){
  204.         if(BankInfo[i][b_exist]){
  205.             if(!strcmp(BankInfo[i][b_founder], GetNamePlayerBank(playerid), true)){
  206.                 count++;
  207.             }
  208.         }
  209.     }
  210.     return count;
  211. }
  212.  
  213. stock AccountIsOfPlayer(playerid, id){
  214.     if(!BankInfo[id][b_exist])
  215.     return 0;
  216.  
  217.     for(new i = 0; i < MAX_BANK_ACCOUNT; i++){
  218.         if(BankInfo[i][b_exist] && BankInfo[i][b_id] == id){
  219.             if(!strcmp(BankInfo[i][b_exist], GetNamePlayerBank(playerid), true)){
  220.                 return 1;
  221.             }
  222.         }
  223.     }
  224.     return -1;
  225. }
  226.  
  227. forward SQL_Bnk(playerid, id); public SQL_Bnk(playerid, id){
  228.     if(!BankInfo[id][b_exist])
  229.     return 0;
  230.  
  231.     BankInfo[id][b_id] = cache_insert_id(g_BankConnection);
  232.     CallLocalFunction("OnPlayerCreateBankAccount", "iid", playerid, id, ACCOUNT_CREATE);
  233.     return 1;
  234. }
  235.  
  236. stock Init_BankConnection(){
  237.     SQL_BANKCONNECT();
  238.  
  239.     if(mysql_errno(g_BankConnection) != 0)
  240.     return print("The bank database was not connected.");
  241.  
  242.     mysql_tquery(g_BankConnection, "SELECT * FROM `banco`", "AccountsBankLoad", "");
  243.     return 1;
  244. }
  245.  
  246. stock CreateBankAccount(playerid, password[]){
  247.     new
  248.     query[400];
  249.  
  250.     for(new i = 0; i != MAX_BANK_ACCOUNT; i ++){
  251.         if(!BankInfo[i][b_exist]){
  252.             BankInfo[i][b_exist] = true;
  253.             BankInfo[i][b_money] = 150;
  254.             format(BankInfo[i][b_founder], MAX_PLAYER_NAME, "%s", GetNamePlayerBank(playerid));
  255.  
  256.             format(query, sizeof(query), "INSERT INTO `banco` (`b_founder`, `b_pass`, `b_money`) VALUES('%s', '%s', '%d')", SQL_ReturnEscapedBank(BankInfo[i][b_founder]), SQL_ReturnEscapedBank(password), BankInfo[i][b_money]);
  257.             mysql_tquery(g_BankConnection, query, "SQL_Bnk", "ii", playerid, i);
  258.             return i;
  259.         }
  260.     }
  261.     return -1;
  262. }
  263.  
  264. stock SQL_ReturnEscapedBank(const string[]){
  265.     static
  266.     entry[256];
  267.  
  268.     mysql_real_escape_string(string, entry, g_BankConnection);
  269.     return entry;
  270. }
  271.  
  272. GetNamePlayerBank(playerid){
  273.     new name[24];
  274.     GetPlayerName(playerid, name, 24);
  275.     return name;
  276. }
Add Comment
Please, Sign In to add comment