AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] Bank (https://forums.alliedmods.net/showthread.php?t=294511)

Arkarr 02-28-2017 16:21

[INC] Bank
 
11 Attachment(s)
[INC] Bank

Sooo... I was a bit annoyed when I had to create a bank/point/money system each time I wanted to create a money system or just storing points (well, anything that can be counted). And also 'cause I wanted to try the inc and native stuff wich I never did before.

Basically, you just create banks where you can store points for each player. Simple stuff, really.

Surprisingly, it gained a bit of audience and therefore, I made it work a little bit better. Sadly, I don't have a huge test-data collection, so I can only estimate and do so much with optimisation. If anyone is kind enough to report issue, I'll force myself to fix them as soon as possible.

Include file (for scripters) :
Most up-to-date version can be found on github, here : https://github.com/Arkarr/SM-Bank/bl...clude/bank.inc
PHP Code:

  /*
    Include file for Bank.sp
    Provide simple functions to manage a player's bank.
    
    Created by Arkarr (Alliedmodders)
*/

#if defined _bank_included
 #endinput
#endif
#define _bank_included


/*********************************************************
 * Called when the banks are ready to be manipulated
 *
 * @NoReturn
 *********************************************************/
forward void Bank_DatabaseReady();


/*********************************************************
 * Called when a freshly created bank as been created.
 *
 * @param bankName    The name of the bank. I don't provide bank ID here to prevent plugin from grabbing the wrong ID. 
 *
 * @NoReturn
 *********************************************************/
forward void Bank_Created(const char[] bankName);

/**
 * Create a new bank. Can't create bank with same name at once.
 *
 * @param bankName    The name of the bank
 * @return            True on sucess, false otherwise.
 */
native bool Bank_Create(const char[] bankName)

/**
 * Add or substract a certain ammount of credits of a player's balance.
 *
 * @param bank             The name of the bank
 * @param client        The client to add/substract to.
 * @param ammount       The ammount to add/substract.
 * @param forcecreate   Create the user if not found in the bank.
 * @return            True on sucess, false otherwise.
 */
native void Bank_EditBalance(const char[] bankint clientint ammount)

/**
 * Get the balance of a client in a specific bank.
 *
 * @param bank             The name of the bank
 * @param client    The client to get the balance of.
 * @return            The ammount of credits. -1 if no account found.
 */
native int Bank_GetBalance(const char[] bankint client)

/**
 * Set the balance of a client in a specific bank.
 *
 * @param bank          The name of the bank
 * @param client        The client to set the balance of.
 * @param ammount       The ammount to set the balance of the player. That wasn't english.
 * @param forcecreate   Create the user if not found in the bank.
 * @return            True on sucess, false otherwise.
 */
native void Bank_SetBalance(const char[] bankint clientint ammount

In order to test the functionally of the plugin, you can use the test plugin I wrote, it doesn't do much.
Simply allows you to create a bank with the command sm_createbank and manipulate credits using sm_addcredits, sm_subcredit and sm_setcreddit

PHP Code:

#include <sourcemod>
#include <sdktools>
#include <bank>

//Plugin Info
#define PLUGIN_TAG            "[Bank]"
#define PLUGIN_NAME            "Bank - test plugin"
#define PLUGIN_AUTHOR         "Arkarr"
#define PLUGIN_VERSION         "1.0"
#define PLUGIN_DESCRIPTION     "A simple plugin to test the bank plugin."

public Plugin myinfo 
{
    
name PLUGIN_NAME,
    
author PLUGIN_AUTHOR,
    
description PLUGIN_DESCRIPTION,
    
version PLUGIN_VERSION,
    
url "http://www.sourcemod.net"
};

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_balance"CMD_ShowBalance"Display your current ammount of credits in a specific bank");
    
    
RegAdminCmd("sm_addcredit"CMD_AddCreditsADMFLAG_CHEATS"Add a specific ammount of credits to an accounts");
    
RegAdminCmd("sm_subcredit"CMD_SubCreditsADMFLAG_CHEATS"Substract a specific ammount of credits to an accounts");
    
RegAdminCmd("sm_setcredit"CMD_SetCreditsADMFLAG_CHEATS"Set a specific ammount of credits to an accounts");
    
RegAdminCmd("sm_createbank"CMD_CreateBankADMFLAG_CHEATS"Create a new bank");
    
    
Bank_Create("TestBank");
}

public 
void OnClientPostAdminCheck(int client)
{
    
Bank_EditBalance("TestBank"client10);
}

//Command Callback

public Action CMD_CreateBank(int clientint args)
{
    if(
args 1)
    {
        if(
client != 0)
            
PrintToChat(client"Usage : sm_balance [BANK NAME]");
        else
            
PrintToServer("Usage : sm_balance [BANK NAME]");
        
        return 
Plugin_Handled;
    }
    
    
char bank[40];
    
GetCmdArg(1banksizeof(bank));
    
    
Bank_Create(bank);
    
    return 
Plugin_Handled;
}

public 
Action CMD_ShowBalance(int clientint args)
{
    if(
args 1)
    {
        
PrintToChat(client"Usage : sm_balance [BANK NAME]");
        return 
Plugin_Handled;
    }
    
    
char bank[40];
    
GetCmdArg(1banksizeof(bank));
    
    
int credits Bank_GetBalance(bankclient);
    
    if(
credits == -1)
        
PrintToChat(client"You are not registred in the bank %s"bank);
    else
        
PrintToChat(client"You have %i credits in bank %s"creditsbank);
    
    return 
Plugin_Handled;
}

public 
Action CMD_AddCredits(int clientint args)
{
    if(
args 3)
    {
        if(
client != 0)
            
PrintToChat(client"Usage : sm_balance [BANK NAME] [TARGET] [AMMOUNT]");
        else
            
PrintToServer("Usage : sm_balance [BANK NAME] [TARGET] [AMMOUNT]");
        return 
Plugin_Handled;
    }
    
    
char bank[40];
    
char strTarget[40];
    
char strAmmount[40];
    
GetCmdArg(1banksizeof(bank));
    
GetCmdArg(2strTargetsizeof(strTarget));
    
GetCmdArg(3strAmmountsizeof(strAmmount));
    
    
int target FindTarget(clientstrTarget);
    
int ammount StringToInt(strAmmount);
    
    if(
ammount 1)
    {
        if(
client != 0)
            
PrintToChat(client"You need to put at least more than 1 unit !");
        else
            
PrintToServer("You need to put at least more than 1 unit !");
            
        return 
Plugin_Handled;    
    }
    
    if(
target != -1)
    {
        
Bank_EditBalance(banktargetammount);
    }
    else
    {
        if(
client != 0)
            
PrintToChat(client"Target not found.");
        else
            
PrintToServer("Target not found.");
    }    
    

    return 
Plugin_Handled;
}

public 
Action CMD_SubCredits(int clientint args)
{
    if(
args 3)
    {
        if(
client != 0)
            
PrintToChat(client"Usage : sm_balance [BANK NAME] [TARGET] [AMMOUNT]");
        else
            
PrintToServer("Usage : sm_balance [BANK NAME] [TARGET] [AMMOUNT]");
            
        return 
Plugin_Handled;
    }
    
    
char bank[40];
    
char strTarget[40];
    
char strAmmount[40];
    
GetCmdArg(1banksizeof(bank));
    
GetCmdArg(2strTargetsizeof(strTarget));
    
GetCmdArg(3strAmmountsizeof(strAmmount));
    
    
int target FindTarget(clientstrTarget);
    
int ammount StringToInt(strAmmount);
    
    if(
ammount 0)
    {
        if(
client != 0)
            
PrintToChat(client"You need to put at least less than 0 unit !");
        else
            
PrintToServer("You need to put at least less than 0 unit !");
            
        return 
Plugin_Handled;    
    }
    
    if(
target != -1)
    {
        
Bank_EditBalance(banktargetammount);
    }
    else
    {
        if(
client != 0)
            
PrintToChat(client"Target not found.");
        else
            
PrintToServer("Target not found.");
    }
    
    return 
Plugin_Handled;
}

public 
Action CMD_SetCredits(int clientint args)
{
    if(
args 3)
    {
        if(
client != 0)
            
PrintToChat(client"Usage : sm_setcredit [BANK NAME] [TARGET] [AMMOUNT]");
        else
            
PrintToServer("Usage : sm_setcredit [BANK NAME] [TARGET] [AMMOUNT]");
            
        return 
Plugin_Handled;
    }
    
    
char bank[40];
    
char strTarget[40];
    
char strAmmount[40];
    
GetCmdArg(1banksizeof(bank));
    
GetCmdArg(2strTargetsizeof(strTarget));
    
GetCmdArg(3strAmmountsizeof(strAmmount));
    
    
int target FindTarget(clientstrTarget);
    
int ammount StringToInt(strAmmount);
    
    
    if(
target != -1)
    {
        
Bank_SetBalance(banktargetammount);
    }
    else
    {
        if(
client != 0)
            
PrintToChat(client"Target not found.");
        else
            
PrintToServer("Target not found.");
    }
    
    return 
Plugin_Handled;


Installation

In order to make the plugin work, simply drag and drop the smx in the plugins folder of sourcemod and configure database.cfg like this :
PHP Code:

    "Bank"
    
{
        
"driver"            "default"
        "host"            "YOUR HOST"
        "database"            "YOUR DB NAME"
        "user"            "YOUR HOST USERNAME"
        "pass"            "YOUR HOST PASSWORD"
    


And you are good to go !

All files can be found on Github here : https://github.com/Arkarr/SM-Bank

ShawnCZek 05-31-2017 14:40

Re: [INC] Bank
 
I have some problems, can you fix them, please?

Code:

L 05/31/2017 - 20:20:04: [SM] Exception reported: Unknown column 'STEAM_ID_STOP_IGNORING_RETVALS' in 'where clause'
L 05/31/2017 - 20:20:04: [SM] Blaming: Bank.smx
L 05/31/2017 - 20:20:04: [SM] Call stack trace:
L 05/31/2017 - 20:20:04: [SM]  [0] SetFailState
L 05/31/2017 - 20:20:04: [SM]  [1] Line 263, Bank.sp::ClientFromBank
L 05/31/2017 - 20:20:04: [SM]  [2] Line 175, Bank.sp::Native_BankGetBalance
L 05/31/2017 - 20:20:04: [SM]  [4] Bank_GetBalance
L 05/31/2017 - 20:20:04: [SM]  [5] Line 38, Shop.sp::OnClientPostAdminCheck
L 05/31/2017 - 20:20:04: [SM]  [7] NotifyPostAdminCheck
L 05/31/2017 - 20:20:04: [SM]  [8] Line 339, /home/builds/sourcemod/linux-1.8/build/plugins/admin-sql-threaded.sp::OnReceiveUser
L 05/31/2017 - 20:23:58: [SM] Exception reported: Invalid database Handle 0 (error: 4)
L 05/31/2017 - 20:23:58: [SM] Blaming: Bank.smx
L 05/31/2017 - 20:23:58: [SM] Call stack trace:
L 05/31/2017 - 20:23:58: [SM]  [0] SQL_Query
L 05/31/2017 - 20:23:58: [SM]  [1] Line 287, Bank.sp::GetBankID
L 05/31/2017 - 20:23:58: [SM]  [2] Line 314, Bank.sp::BankExist
L 05/31/2017 - 20:23:58: [SM]  [3] Line 87, Bank.sp::Native_BankCreate
L 05/31/2017 - 20:23:58: [SM]  [5] Bank_Create
L 05/31/2017 - 20:23:58: [SM]  [6] Line 32, Shop.sp::OnPluginStart
L 05/31/2017 - 20:23:58: [SM] Unable to load plugin "Shop.smx": Error detected in plugin startup (see error logs)


Mitchell 06-02-2017 12:24

Re: [INC] Bank
 
lol, looks like Arkarr needs to stop ignoring the steamid returning "STEAM_ID_STOP_IGNORING_RETVALS"

Arkarr 06-02-2017 14:28

Re: [INC] Bank
 
Quote:

Originally Posted by Mitchell (Post 2525489)
lol, looks like Arkarr needs to stop ignoring the steamid returning "STEAM_ID_STOP_IGNORING_RETVALS"

Nope. It's not my fault.

@ShawnCZek
You need to pass a valid steamID in the function Native_BankGetBalance() ! You are trying to pass "STEAM_ID_STOP_IGNORING_RETVALS" wich create this error.

Mitchell 06-02-2017 14:56

Re: [INC] Bank
 
Quote:

Originally Posted by Arkarr (Post 2525523)
Nope. It's not my fault.

@ShawnCZek
You need to pass a valid steamID in the function Native_BankGetBalance() ! You are trying to pass "STEAM_ID_STOP_IGNORING_RETVALS" wich create this error.

Actually you could have gotten a completely different type of error if your query was quoted.
#define QUERY_SELECT_CLIENT_BANK "SELECT * FROM `clients` WHERE steamid=%s AND bankID=%i"
should be
#define QUERY_SELECT_CLIENT_BANK "SELECT * FROM `clients` WHERE steamid='%s' AND bankID=%i"

Arkarr 06-02-2017 15:48

Re: [INC] Bank
 
Quote:

Originally Posted by Mitchell (Post 2525529)
Actually you could have gotten a completely different type of error if your query was quoted.
#define QUERY_SELECT_CLIENT_BANK "SELECT * FROM `clients` WHERE steamid=%s AND bankID=%i"
should be
#define QUERY_SELECT_CLIENT_BANK "SELECT * FROM `clients` WHERE steamid='%s' AND bankID=%i"

My mistake. Thanks.

ShawnCZek 06-03-2017 10:43

Re: [INC] Bank
 
Nice, now it's working. Thank you very much. :)

ShawnCZek 07-28-2017 09:26

Re: [INC] Bank
 
Last time I had again some problems with this plugin and they are linked to your plugin.
Many users use my plugin which uses your plugin. But sometimes the plugin run down because of memory leak. Here are the errors.
Quote:

L 07/28/2017 - 15:17:42: [SM] MEMORY LEAK DETECTED IN PLUGIN (file "Bank.smx")
L 07/28/2017 - 15:17:42: [SM] Unloading plugin to free 15263 handles.
L 07/28/2017 - 15:17:42: [SM] Contact the author(s) of this plugin to correct this error.
L 07/28/2017 - 15:17:42: --------------------------------------------------------------------------
L 07/28/2017 - 15:17:42: Type IDatabase | Count 1
L 07/28/2017 - 15:17:42: Type Trie | Count 15262
L 07/28/2017 - 15:17:42: -- Approximately 5311176 bytes of memory are in use by (15263) Handles.
Thanks in advance for help.

eyal282 01-05-2018 06:46

Re: [INC] Bank
 
Amount*

Arkarr 01-05-2018 09:50

Re: [INC] Bank
 
Quote:

Originally Posted by eyal282 (Post 2569883)
Amount*

That thing has to be pushed into trash.


All times are GMT -4. The time now is 20:58.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.