ZumeZero

INCLUDE Bank system v0.01 MySQL R7

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