AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Saving ip's into nvault (https://forums.alliedmods.net/showthread.php?t=189397)

Santaaa 07-06-2012 17:38

Saving ip's into nvault
 
Hello there,

i've tryed to save ip's into nvault but it wont work at all. Can someone help me?

Loaddata:
PHP Code:

public LoadData(id)
{
    new 
ip[33]
    
get_user_ip(idip321)
    
    new 
vaultkey[64],vaultdata[256]
    
format(vaultkey,63,"%s-KDRATIO"ip)
    
format(vaultdata,255,"%i#%i#"Kills[id], Deaths[id])
    
nvault_get(g_vault,vaultkey,vaultdata,255)
    
    
replace_all(vaultdata255"#"" ")
    
    new 
kills[32], deaths[32]
    
    
parse(vaultdatakills31deaths31)
    
    
Kills[id] = str_to_num(kills)
    
Deaths[id] = str_to_num(deaths)
    
    return 
PLUGIN_CONTINUE


Savedata:
PHP Code:

public SaveData(id)
{
    new 
ip[33]
    
get_user_ip(idip321)
    
    new 
vaultkey[64],vaultdata[256]
    
format(vaultkey,63,"%s-KDRATIO"ip)
    
format(vaultdata,255,"%i#%i#"Kills[id], Deaths[id])
    
nvault_set(g_vault,vaultkey,vaultdata)
    
    return 
PLUGIN_CONTINUE



Liverwiz 07-06-2012 19:59

Re: Saving ip's into nvault
 
You need to initialize a person into the vault before you can save, or get their data. Check out my code from a plugin i use nvault for.

PHP Code:

retrieveFrags(id)    // gets the frags for the given clientID from vualt  @RETURNS player's frags or -1 if not authorized
{
    if(
get_pcvar_num(frags_pcvar) == || get_pcvar_num(toggle_pcvar) == 0)
    {
        
client_print(idprint_console"[AMX] %L"id"DISABLED")
        return 
PLUGIN_HANDLED
    
}
    if(!
is_user_connected(id) || is_user_bot(id))
        return -
1
    
    
new name[32], sz_vaultKey[34], data[5], timestamp
    
static frags
    
    get_user_name
(idnamecharsmax(name) )        // Used to show who we're talking about in logFile
        // This makes sure the user has a valid AuthID
    
if( !g_playerAuth[id] )
    {
        
g_idPending[id] = true
        client_print
(idprint_console"[BKF] %L"id"AUTH_FAIL_MSG")
        
gi_playerFrags[id] = 0
        
if(get_pcvar_num(log_pcvar) == 1)
            
log_to_file(nVaultLogFile"[BKF] User %s has not been authorized. No vault data created."name)
        return -
1
    
}
    
    
get_user_authid(idsz_vaultKeycharsmax(sz_vaultKey))  // we are usest the steamID as vault key
    
    
if(!nvault_lookup(g_vaultHandlesz_vaultKeydatacharsmax(data), timestamp))
    {
        
frags 0
        nvault_set
(g_vaultHandlesz_vaultKey"0")
        if(
get_pcvar_num(log_pcvar) == 1)
            
log_to_file(nVaultLogFile"[BKF] User %s (%s) is a new client and has been added to the vault."namesz_vaultKey)
    }
    else
        
frags nvault_get(g_vaultHandlesz_vaultKey)
        
    
gi_playerFrags[id] = frags
    g_idPending
[id] = false
    
if(get_pcvar_num(log_pcvar) == 1)
        
log_to_file(nVaultLogFile"[BKF] User %s (%s) has been authorized and loaded with %d frags."namesz_vaultKeyfrags)
    
client_print_color(idDontChange"[BKF] %L"id"WELCOME_MSG"frags)
    
    return 
frags


P.S. Read and understand the code. Don't just copy/paste it.
This code is run at client_putinserver

also....don't use parse and replace all to seperate your data use strtok, it'll save you some native calls.
Code:

strtok(vaultdata, kills, charsmax(kills), deaths, charsmax(deaths), '#', 1)
This will not remove the final #. Take it out. you don't need it, unless you have plans for it later.

Santaaa 07-06-2012 20:23

Re: Saving ip's into nvault
 
Well, what did i wrong with my version? it works for authid.

Liverwiz 07-06-2012 20:41

Re: Saving ip's into nvault
 
Quote:

Originally Posted by Santaaa (Post 1745034)
Well, what did i wrong with my version? it works for authid.

Only thing i can think of is that you never initialized the value if they're a new player.
But it could be (forgive techno-babble) that IP addresses are not static. (They can change whenever required by your ISP) and you may be connecting with a different IP. Or you could be running through a proxy which will mask your IP, or even some DNS servers and firewalls will mask IPs if you have them set to operate as such.
Long story short....IPs can be quite unreliable especially in situations like this. SteamIDs will never change. Unless the user gets a different account.

hornet 07-06-2012 22:01

Re: Saving ip's into nvault
 
Quote:

Originally Posted by Santaaa (Post 1745034)
Well, what did i wrong with my version? it works for authid.

There should be nothing wrong with the code posted. Having said the above post, ( many IP's not being static ) you should save by Steam ID - you have no reason not to.

EDIT: FYI the returns in your functions are not required.

Santaaa 07-07-2012 20:22

Re: Saving ip's into nvault
 
Then why doesnt it save ?:O

hornet 07-07-2012 22:16

Re: Saving ip's into nvault
 
Did you not read any of the above posts? Save by Steam ID. There's no reason to use IP.

You should also provide the full code because the error may be elsewhere.

Santaaa 07-08-2012 00:30

Re: Saving ip's into nvault
 
PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <colorchat>
#include <nvault>

#define PLUGIN "K/D Ratio"
#define VERSION "1.0"
#define AUTHOR "Santaa"

new const Prefix[] = "[Test]";

new 
Kills[33], Deaths[33]

new 
g_vault

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
g_vault nvault_open("KDRATIO")
    
    if (
g_vault == INVALID_HANDLE)
    {
        
set_fail_state("Error loading logs")
    }
    
    
register_clcmd("say /ratio""Showratio")
    
    
register_event("DeathMsg""eDeath""a")
}

public 
client_putinserver(id)
{
    
LoadData(id)
}

public 
client_disconnect(id)
{
    
SaveData(id)
}

public 
Showratio(id)
{
    new 
name[32]
    
get_user_name(idname31)
    
    new 
Float:Ratio float(Kills[id]) / float(Deaths[id])
    
    
ColorChat(idGREY"%s ^1You have ^4%i^1 kills, ^4%i^1 deaths, ratio: ^4%.2f"PrefixKills[id], Deaths[id], Ratio);
    
ColorChat(0GREY"%s ^4%s ^1has a ratio of: ^4%.2f"PrefixnameRatio);
}

public 
eDeath()
{
    new 
killer read_data(1)
    new 
victim read_data(2)
    
    
Kills[killer]++
    
    
Deaths[victim]++
}

public 
SaveData(id)
{
    new 
ip[46]
    
get_user_ip(idipcharsmax(ip), 1)
    
    new 
vaultkey[64],vaultdata[256]
    
format(vaultkey,63,"%s-KDRATIO"ip)
    
format(vaultdata,255,"%i#%i#"Kills[id], Deaths[id])
    
nvault_set(g_vault,vaultkey,vaultdata)
    return 
PLUGIN_CONTINUE
}

public 
LoadData(id)
{
    new 
ip[46]
    
get_user_ip(idipcharsmax(ip), 1)
    
    new 
vaultkey[64],vaultdata[256]
    
format(vaultkey,63,"%s-PointMod"ip)
    
format(vaultdata,255,"%i#%i#"Kills[id], Deaths[id])
    
nvault_get(g_vault,vaultkey,vaultdata,255)
    
    
replace_all(vaultdata255"#"" ")
    
    new 
kills[32], deaths[32]
    
    
parse(vaultdatakills31deaths31)
    
    
Kills[id] = str_to_num(kills)
    
Deaths[id] = str_to_num(deaths)
    
    return 
PLUGIN_CONTINUE



Liverwiz 07-08-2012 00:36

Re: Saving ip's into nvault
 
if you load ip-PointMod and save ip-KDRATIO as the keys you're not going to get consistent results. Just in case you didn't know that....
Also, not functionally different, but use formatex, its faster. And, again, use strtok instead of replace and parse. It'll be faster.

and....
Code:

ColorChat(0, GREY, "%s ^4%s ^1has a ratio of: ^4%.2f", Prefix, name, Ratio);
:arrow:
PHP Code:

ColorChat(0GREY"%s ^4%s ^1has a ration of: ^4%f"PrefixnameRatio

%f displays a float value. %.2f displays "%.2f". This will also cause the program to not have proper displays. Because Ratio is trying to display to nothing the engine will ignore this call and continue execution.

P.S. There is no point in adding a "KDRATIO" or "PointMod" to your key. If you need a difference, just use different vaults. It'll make it better organized and each vault smaller. (i don't know if vaultkeys are sorted or hashed, but if they're sorted....that will in turn make it faster. If they are hashed....well done developers.

hornet 07-08-2012 01:30

Re: Saving ip's into nvault
 
Quote:

Originally Posted by Liverwiz (Post 1745817)
if you load ip-PointMod and save ip-KDRATIO as the keys you're not going to get consistent results.

In fact, he's actually not going to get any results.

Quote:

P.S. There is no point in adding a "KDRATIO" or "PointMod" to your key. If you need a difference, just use different vaults. It'll make it better organized and each vault smaller. (i don't know if vaultkeys are sorted or hashed, but if they're sorted....that will in turn make it faster. If they are hashed....well done developers.
If he's extending his plugin, then having different identifiers in the key may be useful, rather than mixing all kinds of data. No, vault entries are not sorted or hashed ( if they were hashed the value would not be able to be read ).

@Santaaa hopefully you've learnt and next time will show the correct the code, not write examples on the spot, hence giving helpers the wrong information. And I'll say it once again, your going to have many problems on your server if you continue to save by IP.


All times are GMT -4. The time now is 15:13.

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