AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   nVault Tutorial (https://forums.alliedmods.net/showthread.php?t=91503)

Bugsy 05-02-2009 01:04

nVault Tutorial
 
nVault Tutorial

Intro
nVault is an AMX-X tool designed for an easy method of saving and retrieving data from an external file. I have seen many people ask how to save data in a plugin so it can be retrieved after a map change or server restart. nVault will allow you to do this and it is very simple to use. nVault stores data using a key:data system; each chunk of data that you save has a unique key that corresponds to it. Both key and data are always strings when saving but data can be retrieved as integer, float, or string (see nvault_get). Data is saved to a nvault file in binary format, not plain text; this means that you cannot edit the file in [your favorite text editor here] but there is an nVault Editor that can be used. Also note that you cannot iterate through entries in a nvault file with the native nvault functions. You can iterate through entries using the nVault Utility include file. The ability the store arrays in nVault can also be accomplished with the nVault Utility.

nVault info from AMX-X documentation:
http://www.amxmodx.org/funcwiki.php?go=module&id=20
Code:

nVault is a binary vault implementation. It allows you to open named vaults (which are stored in
data\vault) and read/write to them at any time.

The vault is implemented with a simple hash lookup, and the binary file format is primitive. However, it
should be much faster than AMX Mod X's built-in vault for most things. Each entry is also timestamped,
so you can prune for old entries.

Furthermore, the vault attempts to be journaled. That is, it keeps a log of everything it does, so if the
server crashes, it can "replay" the journal and recover damage. The journal is erased on synchronization
to the vault binary, which occurs on vault close/mapchange.

These are the available nvault functions:
Code:

nvault_open - Opens a vault by name (such as "myvault").
nvault_close - Closes a vault.
nvault_lookup - Looks up a vault value for full information.
nvault_get - Gets a vault value by returning an int setting a byref float or setting a string & maxlength.
nvault_set - Sets a vault value with current timestamp.
nvault_pset - Sets a permanent vault value with no timestamp.
nvault_remove - Remove an entry from vault.
nvault_touch - "Touches" a key to update its timestamp value.
nvault_prune - Prunes the vault for entries that are within the given timestamps.

nvault_open
Before doing any transactions with a vault, it must first be opened. This is done with the nvault_open command; it takes 1 string parameter which is the name of the file that is created on the servers hard-disk ([name].vault). When you call the nvault_open() command, it will return a handle (integer value) that is used by all of the other nvault functions as a reference to that particular vault. If an error occurs when opening the vault, nvault_open() will return -1 (INVALID_HANDLE) so it is a good idea to verify if it opened correctly prior to using any other commands.

Code:

nvault_open( const szVaultName[] )

szVaultName = This will be the name of your vault. Every time you want to communicate with this vault you must always use the same name to access that vault. A file will be created in data\vault folder named [szVaultName].vault

Return value: Vault id on success, -1 on failure.

PHP Code:

//Open a vault and throw a plugin error if INVALID_HANDLE is returned.
new iVaultHandle nvault_open"testvault" );

if ( 
iVaultHandle == INVALID_HANDLE )
    
set_fail_state"Error opening nVault" ); 

nvault_close
This function will close the vault. It takes the vault handle to close as a parameter. Once a vault is closed you cannot do any transactions with the vault until it is re-opened. In most circumstances it is best to put nvault_close() in plugin_end(). Failure to close a vault file will cause it to be corrupt and you will lose all data.
Code:

nvault_close( iHandle )

iHandle = The handle of the vault that we want to close

Return value: 1 on success, 0 on failure

PHP Code:

//Open and close a vault.
new iVaultHandle nvault_open"testvault" );

nvault_closeiVaultHandle ); 

nvault_lookup
This function will lookup data using a string key and will place the found data into szValue by-reference for a max of iMaxLength characters. If data is found, the function will return 1 (true), if no data found 0 (false). Timestamp will hold the time at which the entry was written/updated or touched last. This function can only retrieve data from the vault and store it into a string variable. See timestamps section below for an explanation of how timestamps work. This function should be used if you would like to see if a particular key exists in the vault.

Code:

nvault_lookup( iHandle , const szKey[] , szValue[] , iMaxLength , iTimestamp )

iHandle = The handle returned from nvault_open.
szKey = The key you want to use for the data lookup.
szValue = The string variable that will store the data we are retrieving.
iMaxLength = The max length of the string we want read into szValue.
iTimestamp = Will hold a timestamp for when entry was written or last touched.

Return value: 1 if data exists, 0 if no data exists.

PHP Code:

//Lookup a string stored in a vault using the players steamid as the key.
new szAuthID[35];
new 
szData[30];
new 
iTimestamp;
new 
iVault;
new 
iDataExists

get_user_authid
id szAuthID charsmaxszAuthID ) );

iVault nvault_open"testvault" );
iDataExists nvault_lookupiVault szAuthID szData charsmaxszData ) , iTimestamp );

//An entry was found, the data for this key is now stored in szData.
if ( iDataExists )
{
    
client_printid print_chat "Data=%s Timestamp=%d" szData iTimestamp );
}
else
{
    
client_printid print_chat "No entry found with key %s" szAuthID );


nvault_get
This function is very similar to nvault_lookup; it looks up data in a vault using the specified key. However, nvault_get is a bit more flexible than nvault_lookup because it allows you to retrieve data directly into a variable other than string. This will negate the need for you to do any data conversion once the data is retrieved as you would need to do if you use nvault_lookup to retrieve a float or int. Note, nvault_get does not allow you to retrieve the timestamp so if you do need the timestamp then you will need to use nvault_lookup.

Code:

nvault_get( handle , const szKey[] , ... )

handle = The handle returned from nvault_open.
key = The key you want to use for the data lookup.
[...] = [Variable # of parameters] This can either be a float variable, string & maxlen, or empty. See below

The number of parameters tells the function what type of data you wish to retrieve. Parameter count starts after szKey[]
0 parameters = nvault_get will return the integer value.
1 parameters = the float value will be passed by reference.
2 parameters = the string value will be passed by ref for maxlen characters.

Integer: iValue = nvault_get( iHandle , szKey[] );
Return value: Integer value that was found, or 0 if the key was not found or if the data value is non-numerical (ie. a string).

Float: nvault_get( iHandle , szKey[] , float );
Return value: 1 if data found, or 0 if key was not found.

String: nvault_get( iHandle , szKey[] , szValue[] , iMaxLen );
Return value: Length of string data retrieved, or 0 if key was not found.

PHP Code:

//Retrieve an integer, float, and string value from a vault.
new iVault;
new 
szAuthID[35];
new 
szData[30];

iVault nvault_open"testvault" );
get_user_authidid szAuthID charsmaxszAuthID ) );

//To retrieve a number that is stored as the data
new iNumber nvault_getiVault szAuthID );

//To retrieve a float that is stored as the data
new FloatfValue;
nvault_getiVault szAuthID fValue );

//To retrieve a string that is stored as the data
new szDataString[10];
nvault_getiVault szAuthID szDataString charsmaxszDataString ) ); 

nvault_set
This will write data to the vault and timestamp the entry.

Code:

nvault_set( handle , const key[] , const value[] )

handle = The handle returned from nvault_open.
key = The key you want to use for the data lookup.
value = The string you want to write to vault

Return value: 1 on success, 0 on failure.

PHP Code:

//Save the phrase "hello world" to vault using steamid as the key.
new iVault;
new 
szAuthID[35];
new 
szData[30];

iVault nvault_open"testvault" );
get_user_authidid szAuthID charsmaxszAuthID ) );

//Pass the value directly with no variable
nvault_setiVault szAuthID "hello world" );

//Pass the value with a string variable
new szText[] = "hello world";
nvault_setiVault szAuthID szText ); 

nvault_pset
The same as nvault_set() except this writes an entry without a timestamp so the entry will not be removed by the nvault_prune() function (if called). The timestamp value is 0 when permanent.
Code:

Return value: 1 on success, 0 on failure.
nvault_remove
This removes an entry from the vault that has the specified key value.
Code:

nvault_remove( handle , const key[] )

handle = The handle returned from nvault_open.
key = The key of the vault entry to remove.

Return value: 1 on success, 0 on failure.

PHP Code:

//Remove an entry from vault
new iVault;

iVault nvault_open"testvault" );

//Set a test entry with key "test entry"
nvault_setiVault "test entry" "test value" );

//Remove the entry that was just saved
nvault_removeiVault "test entry" ); 

nvault_touch
This function will 'touch' an entry, updating it's timestamp; the entry value will remain unchanged. If the key does not exist that is being touched, an empty entry for that key will be created. See timestamps section below for an explanation of timestamps.

Code:

nvault_touch( iHandle , const szKey[] , [ iTimestamp = -1 ] )

iHandle = The handle of the vault
szKey = The key for which we want to update timestamp
iTimeStamp - [Optional param] If not passed to the function (or -1 is passed)
it will update the key with the current time. Otherwise it will update the
key with the timestamp specified.

Return value: 1 on success, 0 on failure.

PHP Code:

//Touch the entry
new iVault;
new 
szAuthID[35];

iVault nvault_open"testvault" );

get_user_authidid szAuthID charsmaxszAuthID ) );

//Touch the entry with current timestamp
nvault_touchiVault szAuthID );

//Touch entry with timestamp 1 hour ago. (60 * 60) = 3600
nvault_touchiVault szAuthID get_systime() - 3600 ); 

nvault_prune
This function is used to remove entries that fall within the specified start and end timestamps. A timestamp value is in seconds so keep this in consideration when pruning entries. See timestamps section below for an explanation of timestamps.

Code:

nvault_prune( iHandle , iStart , iEnd )

iHandle = The handle returned from nvault_open.
iStart = The timestamp where to begin removing entries.
iEnd = The timestamp where to stop removing entries.

Return value: Number of vault entries pruned on success, 0 on failure.

PHP Code:

new iVault;

iVault nvault_open"testvault" );

//Remove all entries in vault. 0 = earliest possible time, get_systime() = now.
nvault_pruneiVault get_systime() );

//Remove entries in vault that are 15 days old or more.
//get_systime() returns the current timestamp
//1 day = 86400 seconds = 60 secs * 60 mins * 24 hours
nvault_pruneiVault get_systime() - ( 15 86400 ) ); 

Timestamps
Timestamps in nvault are in unix-time format (aka POSIX time) which is equal to the number of seconds that have elapsed since midnight January 1, 1970. To prune or compare a timestamp retrieved in nvault_lookup, you can do simple multiplication to convert seconds to minutes, hours, days, or weeks. Example: Day = 60 * 60 * 24 = 86400.

In the above nvault_prune example it shows how to prune entries that are 15 days old or more. If you wanted to prune entries that are 10+ minutes old, you would do nvault_prune( iHandle , 0 , get_systime() - ( 60 * 10 ) ).

See this link for info on Unix Time: http://en.wikipedia.org/wiki/Unix_time
AMX-X include file for converting Unix Time <-> Normal Time: http://forums.alliedmods.net/showthread.php?t=91915
Convert Unix Time <-> Normal Time on the web: http://www.onlineconversion.com/unix_time.htm

Using nVault in your plugin
If you will be using nvault functions constantly throughout your plugin, it is best to keep the nvault file open while your plugin is running. To do this, use a global variable to hold the handle and call nvault_open() in plugin_init() or plugin_config() (or anywhere you choose). To close the vault, call nvault_close() in plugin_end().

nVault Limitations
The major things (IMO) that are missing in nVault is the ability to determine the number of entries in a vault, the ability to cycle through entries in a vault, and storing arrays. I created an include file, nVault Utility which provides this missing functionality. Just be advised, the nVault handle used with nVault and nVault Utility are *NOT* interchangeable, which means you will have 2 separate handles if you are working with both at the same time (ie. you cannot use the handle returned by nvault_open() with functions in nVault utility and vice-versa. The only exception for this is for the two functions nvault_set_array() and nvault_get_array(), which do use the handle returned by nvault_open().

Example Plugin
The below plugin shows how to do save a players money and score to the vault. When a user disconnects, these items are saved and when he re-connects they get restored.
Plugin

fysiks 05-02-2009 01:14

Re: nVault
 
Sweet, I was just looking for this tutorial the other day :). I hope it's good :|.

EDIT: Read it.

Would there be file access issues if you tried to access a single nVault from two plugins? Just curious really.

--kml-- 05-02-2009 03:30

Re: nVault
 
wooT thx for this tuto :O

+karma

edit:

seems like i cant give now, will try give later

and thx for the tut

SchlumPF* 05-02-2009 07:46

Re: nVault
 
so many tutorials in such a small periode of time... gj although i never needed to use nvault, sql <3 :D

Nextra 05-02-2009 08:22

Re: nVault
 
Well done. Nice to know but I will stay with (My)SQL(ite) :)

Bugsy 05-02-2009 10:18

Re: nVault
 
The overhead for SQL isn't always necessary for basic data storage. nVault also does not require the knowledge of how to write queries. nVault provides a very basic and easy to understand means of saving\retrieving data to an external file. :grrr:

SchlumPF* 05-02-2009 13:11

Re: nVault
 
Quote:

Originally Posted by Bugsy (Post 819363)
The overhead for SQL isn't always necessary for basic data storage. nVault also does not require the knowledge of how to write queries. nVault provides a very basic and easy to understand means of saving\retrieving data to an external file. :grrr:

ofc it does but most things i did were very simple to create using ordinary textfiles. i mostly use sql cuz the data is shown in huge motd which are php files... i think you understand :D

Exolent[jNr] 05-02-2009 13:34

Re: nVault
 
Good job! I always wanted to know how to use the prune function.

PHP Code:

public cmdSaveScore(id)
{
    
//Save 2 items into the value of the entry.
    //Example: STEAM_0:0:1234 15 5

    
new szData[8];
    new 
szKey[40];
    
formatexszKey 39 "%sMONEY" g_szAuthID[id] );
    
formatexszData "%d %d" get_user_kills(id) , get_user_deaths(id) );

    
nvault_setg_Vault szKey szData );

    
client_printid print_chat "* Your score was saved to vault." );
}

public 
cmdGetScore(id)
{
    
//Read 2 items that that are saved in the same entry
    //Example: STEAM_0:0:1234 15 5

    
new szData[8];
    new 
szKey[40];
    
formatexszKey 39 "%sMONEY" g_szAuthID[id] );

    if ( 
nvault_getg_Vault szKey szData ) )
    {
        new 
iSpacePos containszData " " );
        new 
szKills[4];
        new 
szDeaths[4];
        
        if ( 
iSpacePos > -)
        {    
            
formatexszKills iSpacePos "%s" szData );
            
formatexszDeaths "%s" szDataiSpacePos ] );

            
set_user_killsid str_to_numszKills );
            
set_user_deathsid str_to_numszDeaths );

            
client_printid print_chat "* Your score was loaded: %s kills, %s deaths" szKills szDeaths );
        }
    }
    else
    {
        
client_printid print_chat "* You have no score entry in vault." );
    }


Don't you mean:
PHP Code:

formatexszKey 39 "%sSCORE" g_szAuthID[id] ); 


Nextra 05-02-2009 14:59

Re: nVault
 
Quote:

Originally Posted by Bugsy (Post 819363)
The overhead for SQL isn't always necessary for basic data storage. nVault also does not require the knowledge of how to write queries. nVault provides a very basic and easy to understand means of saving\retrieving data to an external file. :grrr:

This is why I said I'm going to stick with SQL, since I know how to query it is not a problem for me :wink:. It's definitely a helpful and nice tutorial for everyone else but as long as it is not extremely incomplex, temporary or limited data that has to be stored I will always use SQL.

Question: I have a bad opinion about vaults ever since my old WC3 Server suffered from a huge XP-database and crashed on every mapchange. Is this still possible with nVault?

Bugsy 05-02-2009 15:32

Re: nVault
 
Quote:

Originally Posted by Nextra (Post 819544)
This is why I said I'm going to stick with SQL, since I know how to query it is not a problem for me :wink:. It's definitely a helpful and nice tutorial for everyone else but as long as it is not extremely incomplex, temporary or limited data that has to be stored I will always use SQL.

Question: I have a bad opinion about vaults ever since my old WC3 Server suffered from a huge XP-database and crashed on every mapchange. Is this still possible with nVault?

Even if querying isn't a problem for you it could still be overkill depending on what you are storing. If you are only storing a single item and only need to retrieve it based on a single key then it is a waste of CPU to use SQL. Whatever floats your boat though. :grrr:

I've never had any issues with nVault. I have used it for all of my data saving needs.

Nextra 05-02-2009 15:36

Re: nVault
 
Quote:

Originally Posted by Bugsy (Post 819582)
Even if querying isn't a problem for you it could still be overkill depending on what you are storing. If you are only storing a single item and only need to retrieve it based on a single key then it is a waste of CPU to use SQL. Whatever floats your boat though. :grrr:

Of course. As I said: If it's uncomplex or temporary data I wouldn't bother doing a SQL-implementation.

Bugsy 05-02-2009 16:20

Re: nVault
 
Thanks Exolent for catching the typo. +k

Bugsy 05-25-2009 11:33

Re: nVault
 
Made corrections to example plugin. There were a few minor typo's and added required modules to make it compile via copy and paste.

Dygear 01-14-2010 00:03

Re: nVault
 
If you could add a function to return just the timestamp for a vault key that would be great.

Code:
nvault_timestamp(vault, key[]); // Finds when the item was last touched.

Also, it would be awesome it we could write as to the save to the vault in the same data type that it was intended to be in.

Exolent[jNr] 01-14-2010 00:39

Re: nVault
 
Code:
stock nvault_timestamp(const vault, const key[]) {     static data[2], timestamp;     return nvault_lookup(vault, key, data, charsmax(data), timestamp) ? timestamp : 0; }

r4ndomz 02-21-2010 09:56

Re: nVault
 
Thanks for the tut!

tm. 04-22-2010 02:26

Re: nVault
 
Thanks for the tutorial, it has been useful for me.
Quote:

Originally Posted by fysiks (Post 819100)
Would there be file access issues if you tried to access a single nVault from two plugins? Just curious really.

Same question.
And how many entries is to much for nvault?

Seta00 04-22-2010 16:08

Re: nVault
 
Quote:

Originally Posted by fysiks (Post 819100)
Would there be file access issues if you tried to access a single nVault from two plugins?

File access protection is based on the process, and there's only one process accessing all the vaults: hl.exe, so no problems.

Of course there's the problem two or more threads can't write to one file at the same time, but this isn't an issue, since AMXx runs on a single thread.

fysiks 04-22-2010 16:35

Re: nVault
 
Yeah, I figured that was the case.

NiHiLaNTh 08-18-2010 13:32

Re: nVault
 
Can anyone help me with storing multiple data.
Code:

#include < amxmodx >
#include < nvault >

new g_iVaultID
new g_szSteamID[ 33 ][ 34 ]
new g_iData[ 33 ]
new g_iData2[ 33 ]
new g_iData3[ 33 ]
new g_iData4[ 33 ]
new g_iData5[ 33 ]
new g_iData6[ 33 ]

public plugin_init( )
{
    register_plugin( "Shit", "Crap", "Lol" )
   
    register_clcmd( "say /show", "fn_Show" )
    register_clcmd( "say /save", "fn_Save" )
}

public plugin_end( )
{
    nvault_close( g_iVaultID )
}

public plugin_cfg( )
{
    g_iVaultID = nvault_open( "test_vault" )
   
    if( g_iVaultID == INVALID_HANDLE )
    {
        set_fail_state( "Error opening Test Nvault" )
    }
}

public client_putinserver( Player )
{
    get_user_authid( Player, g_szSteamID[ Player ], charsmax( g_szSteamID[ ] ) )
   
    Load_Stuff( Player )
}

public fn_Show( Player )
{
    client_print( Player, print_chat, "Data1: %d; Data2: %d; Data3: %d; Data4: %d; Data5:%d; Data6: %d", g_iData[ Player ], g_iData2[ Player ], g_iData3[ Player ], g_iData4[ Player ], g_iData5[ Player ], g_iData6[ Player ] )
}

public fn_Save( Player )
{
    new szSteamID[ 34 ]
    formatex( szSteamID, charsmax( szSteamID ), "%sTEST", g_szSteamID[ Player ] )
   
    g_iData[ Player ] = random_num( 0, 200 )
    g_iData2[ Player ] = random_num( 201, 400 )
    g_iData3[ Player ] = random_num( 401, 600 )
    g_iData4[ Player ] = random_num( 601, 800 )
    g_iData5[ Player ] = random_num( 801, 999 )
    g_iData6[ Player ] = random_num( 1000, 1099 )
   
    new szData[ 50 ]
    formatex( szData, charsmax( szData ), "%d %d %d %d %d %d", g_iData[ Player ], g_iData2[ Player ], g_iData3[ Player ], g_iData4[ Player ], g_iData5[ Player ], g_iData6[ Player ] )
   
    nvault_set( g_iVaultID, szSteamID, szData )
   
    client_print( Player, print_chat, "Data1: %d; Data2: %d; Data3: %d; Data4: %d; Data5:%d; Data6: %d", g_iData[ Player ], g_iData2[ Player ], g_iData3[ Player ], g_iData4[ Player ], g_iData5[ Player ], g_iData6[ Player ]  )
}

Load_Stuff( Player )
{
    new szData[ 50 ], szSteamID[ 34 ]
    formatex( szSteamID, charsmax( szSteamID ), "%sTEST", g_szSteamID[ Player ] )
   
    if( nvault_get( g_iVaultID, szSteamID, szData, 49 ) )
    {
        new iSpacePos = contain( szData, " " )
       
        if( iSpacePos > -1 )
        {
            new szData1[ 4 ], szData2[ 4 ], szData3[ 4 ], szData4[ 4 ], szData5[ 4 ], szData6[ 5 ]
            formatex( szData1, iSpacePos, "%s", szData )
            formatex( szData2, 3, "%s", szData[ iSpacePos + 1 ] )
            formatex( szData3, 3, "%s", szData2[ iSpacePos + 1 ] )
            formatex( szData4, 3, "%s", szData3[ iSpacePos + 1 ] )
            formatex( szData5, 3, "%s", szData4[ iSpacePos + 1 ] )
            formatex( szData6, 4, "%s", szData5[ iSpacePos + 1 ] )
           
           
            g_iData[ Player ] = str_to_num( szData1 )
            g_iData2[ Player ] = str_to_num( szData2 )
            g_iData3[ Player ] = str_to_num( szData3 )
            g_iData4[ Player ] = str_to_num( szData4 )
            g_iData5[ Player ] = str_to_num( szData5 )
            g_iData6[ Player ] = str_to_num( szData6 )
        }
    }
}

I dont why, but g_iData[ ] values all the time are 0. :(

Bugsy 08-18-2010 19:16

Re: nVault
 
PHP Code:

            new szData1], szData2], szData3], szData4], szData5], szData6]
            
formatexszData1iSpacePos"%s"szData )
            
formatexszData23"%s"szDataiSpacePos ] )
            
formatexszData33"%s"szData2iSpacePos ] )
            
formatexszData43"%s"szData3iSpacePos ] )
            
formatexszData53"%s"szData4iSpacePos ] )
            
formatexszData64"%s"szData5iSpacePos ] ) 

I haven't looked over everything closely but the above code is most likely the problem. Try this:

PHP Code:

            new szData1], szData2], szData3], szData4], szData5], szData6]
        
            
parseszData szData1 charsmaxszData1 ) ,
              
szData2 charsmaxszData2 ) ,
              
szData3 charsmaxszData3 ) ,
              
szData4 charsmaxszData4 ) ,
              
szData5 charsmaxszData5 ) ,
              
szData6 charsmaxszData6 ) ) 


NiHiLaNTh 08-19-2010 08:14

Re: nVault
 
Thank you Bugsy, now it works ;]

lashsh 08-28-2010 17:48

Re: nVault
 
XP title e.g 1 kill 10 XP, and 800 XP title serious player
XP

PHP Code:

0 XP 
100 XP 
200 XP 
400 XP 
800 XP 
1600 XP 
3200 XP 

Tittles

PHP Code:

0 XP lamer 
100 XP Beginner 
200 XP Amateur 
400 XP Professional 
800 XP Master 
1600 XP Igrok 
3200 XP Counter
-Strike Pather 

pleace give me this plugin or create

Bugsy 08-29-2010 08:38

Re: nVault
 
Quote:

Originally Posted by lashsh (Post 1284561)
XP title e.g 1 kill 10 XP, and 800 XP title serious player
XP

pleace give me this plugin or create

http://forums.alliedmods.net/showpos...54&postcount=6

johnally 05-11-2011 14:43

Re: nVault
 
Sockets + php to handle real important database stuff.
nVault to logs less important temporary data.

What more?:) gj

Bugsy 05-11-2011 20:56

Re: nVault
 
Quote:

Originally Posted by johnally (Post 1467607)
Sockets + php to handle real important database stuff.
nVault to logs less important temporary data.

What more?:) gj

Thanks for your informative contribution to the thread! Contrary to your opinion, nVault can be used for storing "important permanent data" as well as "less important temporary data."

andre neves 10-14-2011 18:01

Re: nVault
 
PHP Code:

nvault_prune (g_iVault0get_systime() - ( 86400 10) ) 

this will remove all data that are inactive for 10 days?

Bugsy 10-14-2011 18:29

Re: nVault
 
That will delete entries that are older than 10 days.

nvault_prune( Vault , start time , end time )

All entries that fall within starting and ending time are deleted.

0 = start of time
get_systime() = now
86400 * 10 = 10 days

bibu 01-11-2012 14:04

Re: nVault
 
Is there a way, that it loads the nvault but doesn't create a file in data/vault ?

I've tried this in CS and it did create one but it seems like I can't get this to working in TFC. Could you maybe have a look bugsy?

bibu 02-11-2012 06:03

Re: nVault
 
Is there a function to prune only one key in a vault file? For example the scores in the example plugin.

Exolent[jNr] 02-11-2012 15:46

Re: nVault
 
Code:
/* Prunes a specific key in the vault if it is within the given timestampps  * This will not erase values set with pset  */ stock nvault_prune_key(vault, const key[], start, end) {     new timestamp;     if(nvault_lookup(vault, key, "", 0, timestamp) && timestamp > 0 && (start <= timestamp <= end)) {         nvault_remove(vault, key);         return 1;     }         return 0; }

Infernuz 02-16-2012 12:10

Re: nVault
 
What's the point of nvault_touch if it only updates timestamp? I would as well want it to update my data.

Do I have to first make a check if data exists and only then use nvault_set? (Finds first and replaces the data after)
Or does nvault_set do it automatically without creating a new record?

Nothing is mentioned if nvault_set updates or not.

Just interested.

Exolent[jNr] 02-16-2012 12:28

Re: nVault
 
Quote:

Originally Posted by Infernuz (Post 1651640)
What's the point of nvault_touch if it only updates timestamp? I would as well want it to update my data.

Use nvault_set() to update your data and the timestamp will update, too.

Quote:

Originally Posted by Infernuz (Post 1651640)
Do I have to first make a check if data exists and only then use nvault_set? (Finds first and replaces the data after)
Or does nvault_set do it automatically without creating a new record?

Nothing is mentioned if nvault_set updates or not.

Well the whole point of "keys" are that they are unique in the vault.
If it were to create new entries instead of updating, those keys would not be unique.
So the answer is: yes, nvault_set() updates the data if it already exists.

bibu 02-18-2012 11:35

Re: nVault
 
Thanks for the stock exolent. However, how should I check if the days expired, and then prune that key for the user? A general pruning doesn't work, since I don't know how to do that with your stock. Right now I have this:

PHP Code:

        new szDeathKey[41]
        
        
formatex(szDeathKeycharsmax(szDeathKey), "%s_DEATH"g_szAuthID[id])
    
        
//check here the days expired, and if so, prune that key
        
nvault_prune_key(g_score_vaultszDeathKeyget_systime(), 86400 get_pcvar_num(amx_death_expire_days)) 

Is that right like that?

Bugsy 02-18-2012 12:41

Re: nVault
 
Quote:

Originally Posted by bibu (Post 1648269)
Is there a function to prune only one key in a vault file? For example the scores in the example plugin.

There is a score key for every player so there will not only be one score key. If you are referring to deleting all players 'score' key then no, the native 'prune' function works only with the timestamp. You will need to use the nvault utility include and delete keys containing "score".

bibu 02-18-2012 13:07

Re: nVault
 
See my post in #34. It just only for one player and does work IMO.

Bugsy 02-18-2012 13:56

Re: nVault
 
Sorry about that bibu, I didn't see page 2 for some reason and that this has already been resolved. Exolents function will do just fine, the only thing I think you have wrong is the window of time for pruning entries. Correct me if I'm wrong, I'm a little rusty.

PHP Code:

nvault_prune_key(g_score_vaultszDeathKeyget_systime(), 86400 get_pcvar_num(amx_death_expire_days)) 

Start: get_sys_time() = Now
End: 86400 * days = Days to remove in seconds (which is days starting from 01/01/1970 or w\e).

Should be

Start: 0 (01/01/1970)
End: get_systime() - ( get_pcvar_num( amx_death_expire_days ) * 86400 ) ) (Now - X days)

bibu 02-18-2012 17:22

Re: nVault
 
Bugsy I am thinking abit wrong and couldn't really understand your explanation. Could you rephrase that?

Exolent[jNr] 02-18-2012 19:33

Re: nVault
 
Quote:

Originally Posted by bibu (Post 1652980)
Could you rephrase that?

Change
PHP Code:

nvault_prune_key(g_score_vaultszDeathKeyget_systime(), 86400 get_pcvar_num(amx_death_expire_days)) 

to
PHP Code:

nvault_prune_key(g_score_vaultszDeathKey0get_systime() - (86400 get_pcvar_num(amx_death_expire_days))) 


Bugsy 02-18-2012 19:42

Re: nVault
 
Quote:

Originally Posted by Exolent[jNr] (Post 1653028)
Change
PHP Code:

nvault_prune_key(g_score_vaultszDeathKeyget_systime(), 86400 get_pcvar_num(amx_death_expire_days)) 

to
PHP Code:

nvault_prune_key(g_score_vaultszDeathKey0get_systime() + (86400 get_pcvar_num(amx_death_expire_days))) 


That will delete all entries. start=0 with end=get_systime() would delete all; in addition you are adding excess days onto now which says delete everything from the start of time until 15 days from now. Change + to - and it is good, as I posted in #37.

Quote:

Originally Posted by bibu (Post 1652980)
Bugsy I am thinking abit wrong and couldn't really understand your explanation. Could you rephrase that?

When you prune, you are deleting entries that have a time stamp that falls within Start and End range. So for start time you use 0 (beginning of Epoch time) and for the end date you use now minus the number of days for expired entries. A timestamp is the number of seconds that have elapsed since 01/01/70 (IIRC), so the lower the number, the older the time is from now. Suppose you wanted to delete items that are older than 15 days:

0 < 17 day old entry to delete < 15 days from now
0 < 1328148921(now minus 17 days) < 1328321721(now minus 15 days)

Here's some math:

Start = 0

Calculate End time value (Now - 15 days)
  • Now = 1329610846
  • 15 Days = 1296000
  • 1329610846 - 1296000 = 1328314846

Outdated entry example to delete (17 days old, Now - 17 days)
  • 17 Days = 1468800
  • 1329610846 - 1468800 = 1328142046

Delete this 17 day old entry since:
0(Start) < 1328142046(17 day old entry) < 1328314846(now - 15 days)

So what you want is this:
PHP Code:

nvault_prune_keyg_score_vault szDeathKey get_systime() - ( 86400 get_pcvar_numamx_death_expire_days ) ) ) 



All times are GMT -4. The time now is 09:55.

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