AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   FVault - A new vault system! (https://forums.alliedmods.net/showthread.php?t=76453)

Exolent[jNr] 08-24-2008 17:23

FVault - A new vault system!
 
2 Attachment(s)
I created this for a few reasons:
  • This does not require any special modules. All that is required is:
    Code:
    #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:
  • PHP Code:

    /** 
     * 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 keynumkey[], len

  • PHP Code:

    /** 
     * 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[]) 

  • PHP Code:

    /** 
     * 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

  • PHP Code:

    /** 
     * 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[]) 

  • PHP Code:

    /** 
     * 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[]) 

  • PHP Code:

    /** 
     * 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[]) 

  • PHP Code:

    /**
     * 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

  • PHP Code:

    /**
     * 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

  • PHP Code:

    /** 
     * 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[]) 

  • PHP Code:

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

  • PHP Code:

    /** 
     * 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 vaultnumvaultname[], len

  • PHP Code:

    /** 
     * 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[]) 

  • PHP Code:

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

  • PHP Code:

    /**
     * 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:
    // 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:
    // 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:
    // 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:
    // 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:
    // 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); }

If there are any problems, please tell me so that I can fix them.

Brad 08-24-2008 17:46

Re: FVault - A new vault system!
 
[1] You didn't test it? Beyond wow.

[2] If you're going to post a new vault system, make damn sure you list the advantages and disadvantages compared to existing methods.

:)

Alka 08-24-2008 17:58

Re: FVault - A new vault system!
 
I remember that i maked something like this too, but was named "fdata.inc", anyway good job.

minimiller 08-24-2008 18:04

Re: FVault - A new vault system!
 
Brad +1
What are the advantages of this over nVault?

danielkza 08-24-2008 18:56

Re: FVault - A new vault system!
 
Just a suggestion,on _FormatVaultName, the invalid characters can be a one dimension array if you make the identifiers characters instead of strings(' instead of ").
Edit: Just saw,you're using replace,that needs a string. Just ignore it.

Hawk552 08-24-2008 19:22

Re: FVault - A new vault system!
 
Vaults are ridiculously slow. It's much better to use celltravtrie for this purpose in general. Also, celltravtrie uses iterators rather than a static nth value for iteration.

Exolent[jNr] 08-25-2008 15:40

Re: FVault - A new vault system!
 
Updated:
  • Added fvault_remove_key()
  • Added fvault_clear()
  • Fixed compile error "define"
    - Thanks atomen for pointing out
  • Fixed bug in fvault_get_keyname()
    - Thanks atomen for pointing out

Exolent[jNr] 09-01-2008 12:59

Re: FVault - A new vault system!
 
Updated:
  • Fixed fvault_set_data()
    - Thanks atomen
  • Fixed description for fvault_remove_key()
    - Thanks atomen

atomen 09-01-2008 13:06

Re: FVault - A new vault system!
 
Great, thanks for the quick update!

Exolent[jNr] 09-02-2008 17:56

Re: FVault - A new vault system!
 
Updated!
  • Fixed fvault_set_data()
  • Fixed fvault_get_keyname()
  • Fixed fvault_get_keynum()
  • Fixed fvault_get_vaultname()
  • Fixed fvault_get_vaultnum()
  • Changed fvault_size()


All times are GMT -4. The time now is 19:05.

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