Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- MYSQL 39-2
- - DATABASE http://pastebin.com/MM5WNXqi
- - Bank system v0.01 by _ZUME
- - Functions
- - GetAccountID(id) - Retorna el ID de la cuenta
- - IsPlayerNameConnected(name[]) - Comprueba si un jugador esta conectado mediante su nombre
- - GetFounderAccount(id) - Retorna el nombre del fundador de la cuenta bancaria por ID Relativa (Vease GetAccountID(id))
- - GetAccountRelativeID(id) - Retorna el ID relativo de la cuenta bancaria en el servidor
- - GetPassAccount(id) - Retorna en string la password de la cuenta bancaria por ID Relativa.
- - GetMoneyAccount(id) - Retorna el dinero de la cuenta bancaria por ID relativa.
- - ValidatePasswordAccount(id, pass[]) - Comprueba una cadena con la password de la cuenta a verdadero o falso por ID Relativa
- - BankAccount_Delete(id) - Elimina una cuenta bancaria por ID Relativa
- - BankAccount_Count(playerid) - Retorna la cantidad de cuentas bancarias de un playerid
- - AccountIsOfPlayer(id, playerid) - Comprueba si una cuenta bancaria es de un jugador o no retornando verdadero o falso.
- - Init_BankConnection() - Inicia la conexion a la base de datos en donde se encuentre la tabla
- - CreateBankAccount(playerid, password[]) - Crea una nueva cuenta bancaria
- - SaveAccountBank(id) - Actualiza a la base de datos una cuenta bancaria
- - SubstracMoneyAccount(id, amount) - Resta dinero a una cuenta bancaria por ID relativa
- - RaiseMoneyAccount(id, amount) - AUmenta dinero a una cuenta bancaria por ID relativa
- - Callbacks
- - OnPlayerCreateBankAccount(playerid, accountid, action)
- ( Es llamada cuando se crea o elimina una cuenta bancaria, el argumento action tiene disponible ACCOUNT_CREATE ( 1 ) ACCOUNT_DELETE ( 2) )
- */
- #include <a_samp>
- #if !defined mysql_connect
- #include <a_mysql>
- #endif
- // actions
- #define ACCOUNT_CREATE 1
- #define ACCOUNT_DELETE 2
- enum BankEnum
- {
- b_id,
- b_exist,
- b_founder[MAX_PLAYER_NAME],
- b_money,
- };
- new BankInfo[MAX_BANK_ACCOUNT][BankEnum];
- new g_BankConnection = -1;
- forward OnPlayerCreateBankAccount(playerid, accountid, action);
- stock SQL_BANKCONNECT(){
- g_BankConnection = mysql_connect(BANK_LOCALSQL, BANK_USERSQL, BANK_DBSQL, BANK_PASSSQL);
- }
- stock IsPlayerNameConnected(name[])
- {
- for(new i = 0; i != MAX_PLAYERS; i ++) if(IsPlayerConnected(i))
- {
- if(!strcmp(GetNamePlayerBank(i), name, true)){
- return i;
- }
- }
- return INVALID_PLAYER_ID;
- }
- stock GetAccountID(id){
- if(!BankInfo[id][b_exist])
- return 0;
- return (BankInfo[id][b_id]);
- }
- stock GetAccountRelativeID(id){
- for(new i = 0; i != MAX_BANK_ACCOUNT; i ++){
- if(BankInfo[i][b_exist] && BankInfo[i][b_id] == id){
- return i;
- }
- }
- return -1;
- }
- stock GetFounderAccount(id){
- new
- name[MAX_PLAYER_NAME] = {85, 110, 107, 110, 111, 119, 110};
- if(!BankInfo[id][b_exist])
- return name;
- strunpack(name, BankInfo[id][b_founder]);
- return name;
- }
- stock GetPassAccount(id){
- new
- name[MAX_PLAYER_NAME] = {85, 110, 107, 110, 111, 119, 110};
- if(!BankInfo[id][b_exist])
- return name;
- strunpack(name, BankInfo[id][b_pass]);
- return name;
- }
- stock SubstracMoneyAccount(id, amount){
- if(!BankInfo[id][b_exist])
- return 0;
- BankInfo[id][b_money] -= amount;
- return 1;
- }
- stock RaiseMoneyAccount(id, amount){
- if(!BankInfo[id][b_exist])
- return 0;
- BankInfo[id][b_money] += amount;
- return 1;
- }
- stock GetMoneyAccount(id){
- if(!BankInfo[id][b_exist])
- return name;
- return (BankInfo[id][b_money]);
- }
- stock ValidatePasswordAccount(id, pass[]){
- if(!BankInfo[id][b_exist])
- return 0;
- if(!strcmp(pass, GetPassAccount(id), true)){
- return 1;
- }
- return 0;
- }
- cache_get_field_intbank(row, const field_name[]){
- new
- str[12];
- cache_get_field_content(row, field_name, str, g_BankConnection);
- return strval(str);
- }
- forward AccountsBankLoad(); public AccountsBankLoad(){
- if(mysql_errno(g_BankConnection) != 0)
- return printf("The bank database was not connected.");
- new
- rows,
- fields;
- cache_get_data(rows, fields, g_BankConnection);
- for (new i = 0; i < rows; i ++) if (i < MAX_BANK_ACCOUNT){
- BankInfo[i][b_exist] = true;
- BankInfo[i][b_id] = cache_get_field_intbank(i, "b_id");
- BankInfo[i][b_money] = cache_get_field_intbank(i, "b_money");
- cache_get_field_content(i, "b_founder", BankInfo[i][b_founder], g_BankConnection);
- }
- return 1;
- }
- stock SaveAccountBank(id){
- if(mysql_errno(g_BankConnection) != 0)
- return printf("The bank database was not connected.");
- if(!BankInfo[id][b_exist])
- return 0;
- new
- query[200];
- format(query, sizeof(query), "UPDATE `banco` SET `b_money` = '%d' WHERE `b_id` = '%d'", BankInfo[id][b_money], BankInfo[id][b_id]);
- mysql_tquery(g_BankConnection, query, "", "");
- return 1;
- }
- stock BankAccount_Delete(id){
- if(mysql_errno(g_BankConnection) != 0)
- return printf("The bank database was not connected.");
- if(!BankInfo[id][b_exist])
- return 0;
- new playerid = INVALID_PLAYER_ID;
- playerid = IsPlayerNameConnected(GetFounderAccount(id));
- CallLocalFunction("OnPlayerCreateBankAccount", "iid", playerid, id, ACCOUNT_DELETE);
- BankInfo[id][b_exist] = false;
- BankInfo[id][b_money] = 0;
- new
- query[200];
- format(query, sizeof(query), "DELETE FROM `banco` WHERE `b_id` = '%d'", BankInfo[id][b_id]);
- mysql_tquery(g_BankConnection, query, "", "");
- return 1;
- }
- stock BankAccount_Count(playerid){
- new count = 0;
- for(new i = 0; i < MAX_BANK_ACCOUNT; i++){
- if(BankInfo[i][b_exist]){
- if(!strcmp(BankInfo[i][b_founder], GetNamePlayerBank(playerid), true)){
- count++;
- }
- }
- }
- return count;
- }
- stock AccountIsOfPlayer(playerid, id){
- if(!BankInfo[id][b_exist])
- return 0;
- for(new i = 0; i < MAX_BANK_ACCOUNT; i++){
- if(BankInfo[i][b_exist] && BankInfo[i][b_id] == id){
- if(!strcmp(BankInfo[i][b_exist], GetNamePlayerBank(playerid), true)){
- return 1;
- }
- }
- }
- return -1;
- }
- forward SQL_Bnk(playerid, id); public SQL_Bnk(playerid, id){
- if(!BankInfo[id][b_exist])
- return 0;
- BankInfo[id][b_id] = cache_insert_id(g_BankConnection);
- CallLocalFunction("OnPlayerCreateBankAccount", "iid", playerid, id, ACCOUNT_CREATE);
- return 1;
- }
- stock Init_BankConnection(){
- SQL_BANKCONNECT();
- if(mysql_errno(g_BankConnection) != 0)
- return print("The bank database was not connected.");
- mysql_tquery(g_BankConnection, "SELECT * FROM `banco`", "AccountsBankLoad", "");
- return 1;
- }
- stock CreateBankAccount(playerid, password[]){
- new
- query[400];
- for(new i = 0; i != MAX_BANK_ACCOUNT; i ++){
- if(!BankInfo[i][b_exist]){
- BankInfo[i][b_exist] = true;
- BankInfo[i][b_money] = 150;
- format(BankInfo[i][b_founder], MAX_PLAYER_NAME, "%s", GetNamePlayerBank(playerid));
- 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]);
- mysql_tquery(g_BankConnection, query, "SQL_Bnk", "ii", playerid, i);
- return i;
- }
- }
- return -1;
- }
- stock SQL_ReturnEscapedBank(const string[]){
- static
- entry[256];
- mysql_real_escape_string(string, entry, g_BankConnection);
- return entry;
- }
- GetNamePlayerBank(playerid){
- new name[24];
- GetPlayerName(playerid, name, 24);
- return name;
- }
Add Comment
Please, Sign In to add comment