Raised This Month: $51 Target: $400
 12% 

storing multiple values in nvault?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-27-2015 , 06:45   storing multiple values in nvault?
Reply With Quote #1

title

For example... store player's money, armor and a custom currency.. Can this be done by storing them in an array?

new gArray[33]= { gMoney, gArmor, gCredits }

Just save gArray in the savevalue function?
redivcram is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 10-27-2015 , 06:58   Re: storing multiple values in nvault?
Reply With Quote #2

PHP Code:
//SaveData:

format(szVaultDatacharsmax(szVaultData), "%i#%i#%i#"gMoney[id], gArmor[id], gCredits[id])
nvault_set(g_VaultszVaultKeyszVaultData)

//LoadData:
format(szVaultDatacharsmax(szVaultData), "%i#%i#%i#"gMoney[id], gArmor[id], gCredits[id])
nvault_get(g_VaultszVaultKeyszVaultDatacharsmax(szVaultData))
replace_all(szVaultDatacharsmax(szVaultData), "#"" ")

new 
szMoney[10], szArmor[10], szCredits[10]
parse(szVaultDataszMoneycharsmax(szMoney), szArmorcharsmax(szArmor), szCreditscharsmax(szCredits))

gMoney[id] = str_to_num(szMoney)
gArmor[id] = str_to_num(szArmor)
gCredits[id] = str_to_num(szCredits
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-27-2015 , 07:21   Re: storing multiple values in nvault?
Reply With Quote #3

You can do it an easier way. Let me know how many numbers you need to store and the max possible value of each.
__________________
Bugsy is offline
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-27-2015 , 12:52   Re: storing multiple values in nvault?
Reply With Quote #4

Save them permanently even if the server crashes... if It's even possible. So max possible value of... 200 for one value, for the other value let it be played time... so I don't know the limit But I also want it to be permanently saved.

@OcixCrom I'll see Bugsy's way and decide which is more suitable for me
redivcram is offline
silentscope
Junior Member
Join Date: Jul 2011
Old 10-27-2015 , 13:19   Re: storing multiple values in nvault?
Reply With Quote #5

Quote:
Originally Posted by redivcram View Post
Save them permanently even if the server crashes...
good point, how would it affect on performance if its saved permanently?
silentscope is offline
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-27-2015 , 15:32   Re: storing multiple values in nvault?
Reply With Quote #6

Quote:
Originally Posted by silentscope View Post
good point, how would it affect on performance if its saved permanently?
What about saving data in a text document for each player? That is.. each document for each steamid.. will that affect performance and how?
redivcram is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-27-2015 , 18:54   Re: storing multiple values in nvault?
Reply With Quote #7

My original plan was to store the values within 1 cell since there are 4 bytes to work with. This is useful if you are storing multiple booleans or multiple small value numbers. Play time can get high so this approach would not be good because you would probably need 16 bits just for play time alone, leaving only 16 for the remainder of the values. Also, it could be confusing unless you are familiar with bit operations.

Instead here is something easier. For this you will need nVault Utility
PHP Code:

#include <amxmodx>
#include <nvault_util>

new const Version[] = "0.1";

const 
MAXPLAYERS 32;
const 
TASK_DELAY_PRINT 34214;

enum _:PlayerStuff
{
    
Money,
    
Armor,
    
Credits,
    
PlayTime,
    
SteamID32 ]
}
new 
psPlayerDataMAXPLAYERS ][ PlayerStuff ];
new 
g_Vault;

public 
plugin_init() 
{
    
register_plugin"nVault Array Example" Version "bugsy" );
    
    if ( ( 
g_Vault nvault_open"TestVault" ) ) == INVALID_HANDLE )
        
set_fail_state"Error opening vault" );
}

public 
plugin_end()
{
    
nvault_closeg_Vault );
}

public 
client_authorizedid )
{
    
get_user_authidid psPlayerDataid ][ SteamID ] , charsmaxpsPlayerData[] ) );
    
    
LoadDataid );
}

public 
client_disconnectid )
{
    
SaveDataid );
    
remove_taskid TASK_DELAY_PRINT );
    
    
arraysetpsPlayerDataid ] , sizeofpsPlayerData[] ) );
}

public 
SaveDataid )
{
    
//Test data
    
psPlayerDataid ][ Money ] = 15333;
    
psPlayerDataid ][ Armor ] = 100;
    
psPlayerDataid ][ Credits ] = 23132123;
    
psPlayerDataid ][ PlayTime ] = 664243;
    
    
nvault_set_arrayg_Vault psPlayerDataid ][ SteamID ] , psPlayerDataid ] , sizeofpsPlayerData[] ) );
}

public 
LoadDataid )
{
    new 
iTimeStamp;
    
    if ( 
nvault_get_arrayg_Vault psPlayerDataid ][ SteamID ] , psPlayerDataid ] , sizeofpsPlayerData[] ) , iTimeStamp ) )
    {
        
set_task7.0 "DelayPrint" id TASK_DELAY_PRINT );
    }
}

public 
DelayPrintid )
{
    
id -= TASK_DELAY_PRINT;
    
    
client_printid print_chat "Data loaded: Money=%d Armor=%d Credits=%d PlayTime=%d SteamID=%s" psPlayerDataid ][ Money ] ,
                                                             
psPlayerDataid ][ Armor ] ,
                                                             
psPlayerDataid ][ Credits ] ,
                                                             
psPlayerDataid ][ PlayTime ] ,
                                                             
psPlayerDataid ][ SteamID ] );

__________________

Last edited by Bugsy; 10-27-2015 at 18:56.
Bugsy is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 10-27-2015 , 19:42   Re: storing multiple values in nvault?
Reply With Quote #8

Bear in mind that the data is saved when the client successfully disconnects from the server, so if the server crashes, none of it will get saved. You can avoid this by saving the data in another way, for example when the player spawns, which will have a far less impact when it crashes, but I'm not sure how this will affect the performance.

Last edited by OciXCrom; 10-27-2015 at 19:43.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-27-2015 , 19:49   Re: storing multiple values in nvault?
Reply With Quote #9

I'll keep that in mind. But can I add inside plugin_end an index for online players with get_players and when it crashes (ends) it saves it for every player? Yes.. confusing indeed
redivcram is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-27-2015 , 19:49   Re: storing multiple values in nvault?
Reply With Quote #10

Yes, you can call the save function any time you want. Doing it at disconnect is normally done because it avoids redundant saves, plus it gives you the accurate last known data for the player.

How often does the average persons server crash? I ran one for years and it was very rare for me. If you have something that is causing a crash, address it. Don't let it be and work around the fact that you will get crashes.

Quote:
Originally Posted by redivcram View Post
I'll keep that in mind. But can I add inside plugin_end an index for online players with get_players and when it crashes (ends) it saves it for every player? Yes.. confusing indeed
No, when a crash occurs nothing gets called. It's like a light switch. plugin_end() only gets called on graceful shutdown and map change.
__________________

Last edited by Bugsy; 10-27-2015 at 19:50.
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:52.


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