FVault - A new vault system!

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1189
Gender:
United States of America

FVault - A new vault system!

Post by Archive-Poster »

I created this for a few reasons:
This does not require any special modules. All that is required is:

Code: Select all

#include <amxmodx>
#include <fvault>
Has the ability to iterate through all vault names and all vault keys within each vault.
Keeps the original functionality of nvault with more features.

Functions:

Code: Select all

/** 
 * Retrieves a key name specified by its number
 * 
 * @param vaultname    Vault name to look in
 * @param keynum    Key number within the vault to find key name
 * @param key        String which key name will be copied to
 * @param len        Length of key name
 * @return        Returns 1 on success, 0 on failue.
 */
fvault_get_keyname(const vaultname[], const keynum, key[], len) 

Code: Select all

/** 
 * Retrieves a key number specified by its name
 * 
 * @param vaultname    Vault name to look in
 * @param key        Key name to search for
 * @return        Returns key number on success, -1 on failure
 */
fvault_get_keynum(const vaultname[], const key[]) 

Code: Select all

/** 
 * Retrieves data specified by a key
 * 
 * @param vaultname    Vault name to look in
 * @param key        Key name to look for the data
 * @param data        String which data will be copied to
 * @param len        Length of data
 * @param timestamp    The unix time of when the data was last set ( -1 if permanent data, 0 if old fvault version ) ( optional param )
 * @return        Returns 1 on success, 0 on failue.
 */
fvault_get_data(const vaultname[], const key[], data[], len, &timestamp=0) 

Code: Select all

/** 
 * Sets data of a key with current timestamp
 * 
 * @param vaultname    Vault name to look in
 * @param key        Key name to which data will be set
 * @param data        Data to set to key
 * @return        Does not return a value.
 */
fvault_set_data(const vaultname[], const key[], const data[]) 

Code: Select all

/** 
 * Sets data of a key permanently (can't be removed with fvault_prune)
 * 
 * @param vaultname    Vault name to look in
 * @param key        Key name to which data will be set
 * @param data        Data to set to key
 * @return        Does not return a value.
 */
fvault_pset_data(const vaultname[], const key[], const data[]) 

Code: Select all

/** 
 * Removes a key from a vault
 * 
 * @param vaultname    Vault name to look in
 * @param key        Key to remove
 * @return        No return
 */
fvault_remove_key(const vaultname[], const key[]) 

Code: Select all

/**
 * Prunes the vault for keys that are within the given timestamps
 * 
 * @param vaultname    Vault name to look in
 * @param start        If timestamp is after this Unix Time (set -1 to prune from very start)
 * @param end        If timestamp is before this Unix Time (set -1 to prune to most time)
 * @return        Returns number of keys pruned
 */
fvault_prune(const vaultname[], const start=-1, const end=-1) 

Code: Select all

/**
 * Updates the timestamp on a key located within the vault
 * 
 * @param vaultname    Vault name to look in
 * @param key        Key to update timestamp (if it doesn't exist, a blank value will be set)
 * @param timestamp    Unix Time to set for the key (-1 for current time)
 * @return        Returns 2 on new entry, 1 on success, 0 on failure for the key having a permanent timestamp
 */
fvault_touch(const vaultname[], const key[], const timestamp=-1) 

Code: Select all

/** 
 * Retrieves total keys located within the vault
 * 
 * @param vaultname    Vault name to look in
 * @return        Returns amount of keys in vault
 */
fvault_size(const vaultname[]) 

Code: Select all

/** 
 * Clears all key entries for a vault
 * 
 * @param vaultname    Vault name to erase
 * @return        No return
 */
fvault_clear(const vaultname[])

Code: Select all

/** 
 * Retrieves a vault name specified by its number
 * 
 * @param vaultnum    Vault number to find the vault name
 * @param vaultname    String which vault name will be copied to
 * @param len        Length of vault name
 * @return        Returns 1 on success, 0 on failue.
 */
fvault_get_vaultname(const vaultnum, vaultname[], len) 

Code: Select all

/** 
 * Retrieves a vault number specified by its name
 * 
 * @param vaultname    Vault name to find the number
 * @return        Returns vault number on success, -1 on failure
 */
fvault_get_vaultnum(const vaultname[]) 

Code: Select all

/** 
 * Retrieves total vaults ever created
 * 
 * @return        Returns amount of vaults
 */
fvault_total() 

Code: Select all

/**
 * Gets all vault keys, data, and timestamps
 * 
 * @param        vaultname - Vault name to look in
 * @param        keys - cellarray holding all of the keys
 * @param        datas - cellarray holding all of the data values
 * @param        timestamps - cellarray holding all of the timestamps
 * 
 * @return        Returns total number of entries in vault
 * 
 * @note        keys needs to be created like this: ArrayCreate(64)
 *             datas needs to be created like this: ArrayCreate(512)
 *             timestamps need to be created like this: ArrayCreate()
 */
fvault_load(const vaultname[], Array:keys=Invalid_Array, Array:datas=Invalid_Array, Array:timestamps=Invalid_Array) 
Some examples:

Code: Select all

// How to find all vaults created:

new total = fvault_total();

new vaultname[32];
for( new i = 0; i < total; i++ )
{
    fvault_get_vaultname(i, vaultname, sizeof(vaultname) - 1);
    server_print("Vault [%i]: %s", i, vaultname);
}

Code: Select all

// How to find all keys within a vault:

new const vaultname[] = "myvault";

new total = fvault_size(vaultname);

new key[32], data[64];
for( new i = 0; i < total; i++ )
{
    fvault_get_keyname(vaultname, i, key, sizeof(key) - 1);
    fvault_get_data(vaultname, key, data, sizeof(data) - 1);
    
    server_print("Key: %s | Data: %s", key, data);
}

Code: Select all

// More efficient method to find all keys within a vault:

new const vaultname[] = "myvault";

new Array:keys = ArrayCreate(64);
new Array:datas = ArrayCreate(512);

new total = fvault_load(vaultname, keys, datas);

new key[64], data[512];
for( new i = 0; i < total; i++ )
{
    ArrayGetString(keys, i, key, 31);
    ArrayGetString(datas, i, data, 511);
    
    server_print("Key: %s | Data: %s", key, data);
}

Code: Select all

// Pruning player agree/decline after 15 days of not being updated

// 15 days * 24 hours * 60 minutes * 60 seconds = 15 days in seconds
new end_prune_time = get_systime() - (15 * 24 * 60 * 60);

fvault_prune("player_agree", _, end_prune_time);

Code: Select all

// A simple exp mod

#include <amxmodx>
#include <fvault>

new const g_vault_name[] = "EXP_MOD";

new g_exp[33];

public client_authorized(plr)
{
    if( !is_user_hltv(plr) && !is_user_bot(plr) )
    {
        LoadExp(plr);
    }
}

public client_disconnect(plr)
{
    if( g_exp[plr] > 0 )
    {
        SaveExp(plr);
        
        g_exp[plr] = 0;
    }
}

LoadExp(plr)
{
    new authid[35];
    get_user_authid(plr, authid, sizeof(authid) - 1);
    
    new data[16];
    if( fvault_get_data(g_vault_name, authid, data, sizeof(data) - 1) )
    {
        g_exp[plr] = str_to_num(data);
    }
    else
    {
        g_exp[plr] = 0;
    }
}

SaveExp(plr)
{
    new authid[35];
    get_user_authid(plr, authid, sizeof(authid) - 1);
    
    new data[16];
    num_to_str(g_exp[plr], data, sizeof(data) - 1);
    
    fvault_set_data(g_vault_name, authid, data);
}
Attachments
fvault.inc.zip
(3.24 KiB) Downloaded 8 times

Who is online

Users browsing this forum: No registered users and 1 guest