AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Save, read and compare user IP in fvault (https://forums.alliedmods.net/showthread.php?t=333321)

The overrated maniac 07-03-2021 07:21

Save, read and compare user IP in fvault
 
I add my ip that I found in amx_who, with amx_save_ip "ip". Then I reconnect and says the user ip isn't in the file. What can I do?

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <fvault>

#define PLUGIN "fvault test"
#define VERSION "1.0"
#define AUTHOR "Maniatico"

new g_szUserIP[33][191];
new 
g_szSavedIP[191];

new const 
Vault[] = "_ips"

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR);
    
    
register_clcmd("amx_save_ip""save_ip"ADMIN_IMMUNITY);
}


public 
client_putinserver(id){
    
    
get_user_ip(idg_szUserIP[id], charsmax(g_szUserIP[]), 1);
    
set_task(5.0"Load"id);
}

public 
client_disconnect(id){
    
    
remove_task(id);
}

public 
save_ip(id){
    
    if(!(
get_user_flags(id) & ADMIN_IMMUNITY)){
        
        return 
PLUGIN_HANDLED;
    }
    else{
        
read_argv(1g_szSavedIP190);
        
        if( 
equal(g_szSavedIP"" ) || contain(g_szSavedIP" ") != -1){
            
console_print(id"Cant take that ip");
            return 
PLUGIN_HANDLED;
        }
        else {
            static 
SZ_Data[512], SZ_NAME[32];
            
formatex(SZ_Datacharsmax(SZ_Data), "IP: %s"g_szSavedIP)
            
fvault_set_data(VaultSZ_NAMESZ_Data)
            
            
console_print(id"IP %s saved"g_szSavedIP);
            
Save(id);
        }
        return 
PLUGIN_CONTINUE;
    }
    return 
PLUGIN_HANDLED;
}
 
public 
Save(id){
    
    static 
SZ_Data[512], SZ_NAME[32]
    
get_user_name(idSZ_NAME31);
    
    
formatex(SZ_Datacharsmax(SZ_Data), "Name: %s - IP: %s"SZ_NAME[id], g_szSavedIP[id])
    
fvault_set_data(VaultSZ_NAMESZ_Data)
}

public 
Load(id){
    
    static 
SZ_Data[512],  VAULT_IP[191], SZ_NAME[32];
    new 
size_vault fvault_size(Vault);
    
    
get_user_name(idSZ_NAME31);
    
    if( !
fvault_get_dataVault,SZ_DataSZ_NAMEcharsmaxSZ_Data ) ) ){
        return 
0;
    }
    
    
parse(SZ_DataVAULT_IPcharsmax(VAULT_IP))
    
    for (new 
0size_vaulti++){
        
fvault_get_keyname(VaultiSZ_NAMEcharsmax(SZ_NAME));
        
fvault_get_data(VaultSZ_NAMESZ_Datacharsmax(SZ_Data));
        
        if(
equal(g_szUserIP[id], g_szSavedIP[i])){
            
client_print(0print_chat"The user ip is already saved in the file.");
        }
        else{
            
client_print(0print_chat"The user ip is not saved in the file.");
        }
    }
    return 
PLUGIN_HANDLED;



fysiks 07-04-2021 01:14

Re: Save, read and compare user IP in fvault
 
There are several major issues with this plugin. The first is that you aren't looking at the data in the vault at all. You get it from the vault but you don't actually do anything with it. Though I'm confident you're not setting and getting the data to and from the vault properly. You're also wrongly using both of your global variables. In fact, neither of those global variables should exist in this plugin.

It would probably be easier to help you with this if you can explain what you are actually trying to do.

The overrated maniac 07-04-2021 01:39

Re: Save, read and compare user IP in fvault
 
Quote:

Originally Posted by fysiks (Post 2751836)
There are several major issues with this plugin. The first is that you aren't looking at the data in the vault at all. You get it from the vault but you don't actually do anything with it. Though I'm confident you're not setting and getting the data to and from the vault properly. You're also wrongly using both of your global variables. In fact, neither of those global variables should exist in this plugin.

It would probably be easier to help you with this if you can explain what you are actually trying to do.

Okay, then I should make another post in plugins request I guess..

What I'm trying to do is get the user ip when he joins and save it in a string. Then read the vault data and compare if the user IP is in it. The saveds ip are added manually with the command "amx_save_ip <ip>".
If the user IP exist in the file, do something. Else, do other thing.

Napoleon_be 07-04-2021 07:54

Re: Save, read and compare user IP in fvault
 
Quote:

Originally Posted by The overrated maniac (Post 2751837)
Okay, then I should make another post in plugins request I guess..

If you don't know what you're doing, yes.

Bugsy 07-04-2021 10:07

Re: Save, read and compare user IP in fvault
 
There are a few things that need fixing in your code, below is part of your problem. I recommend you instead use nvault with nvault array, it will be easier to store multiple pieces of data for a player, in this case name and IP, and also steam id if you want. With fvault, you'll need to string it all together and then pull the info back out. "Name: bugsy IP: 255.255.255.255:12345 SteamID: STEAM_0:0:12345"
Code:
    for (new i = 0; i < size_vault; i++){         fvault_get_keyname(Vault, i, SZ_NAME, charsmax(SZ_NAME));         fvault_get_data(Vault, SZ_NAME, SZ_Data, charsmax(SZ_Data));                             //You are comparing the wrong variable here, you are reading the value from fvault into SZ_Data while you are         //using g_szUserIP[id] and g_szSavedIP[] in the check. Avoid declaring variables globally when its not needed,         //such as g_szSavedIP[]. This should be local to the function.         //Also, your data is saved in the below format, you will need to parse the IP value from it.         //"Name: %s - IP: %s" - You need to pull just the IP out of this for the compare.         //Do you want to check the name too, if so that will be an additional parse/check.         if(equal(g_szUserIP[id], g_szSavedIP[i])){             client_print(0, print_chat, "The user ip is already saved in the file.");         }         else{             client_print(0, print_chat, "The user ip is not saved in the file.");         }     }     return PLUGIN_HANDLED;

Not tested:
PHP Code:

        parse(SZ_DataVAULT_IPcharsmax(VAULT_IP))
        
        new 
iIPPos bool:bFound;
        for (new 
0size_vaulti++)
        {
            
fvault_get_keyname(VaultiSZ_NAMEcharsmax(SZ_NAME));
            
fvault_get_data(VaultSZ_NAMESZ_Datacharsmax(SZ_Data));
            
            
iIPPos strfindSZ_Data "IP: " );
            
            if ( 
iIPPos > -)
            {
                if ( 
equalg_szUserIPid ] , SZ_DataiIPPos ] ) )
                {
                    
client_print(0print_chat"The user ip is already saved in the file.");
                    
bFound true;
                    break;
                }
            }
        }
    }
    
    if ( 
bFound == false )
    {
        
client_print(0print_chat"The user ip is not saved in the file.");
    }



The overrated maniac 07-05-2021 01:36

Re: Save, read and compare user IP in fvault
 
Quote:

Originally Posted by Bugsy (Post 2751861)
There are a few things that need fixing in your code, below is part of your problem. I recommend you instead use nvault with nvault array, it will be easier to store multiple pieces of data for a player, in this case name and IP, and also steam id if you want. With fvault, you'll need to string it all together and then pull the info back out. "Name: bugsy IP: 255.255.255.255:12345 SteamID: STEAM_0:0:12345"
Code:
    for (new i = 0; i < size_vault; i++){         fvault_get_keyname(Vault, i, SZ_NAME, charsmax(SZ_NAME));         fvault_get_data(Vault, SZ_NAME, SZ_Data, charsmax(SZ_Data));                             //You are comparing the wrong variable here, you are reading the value from fvault into SZ_Data while you are         //using g_szUserIP[id] and g_szSavedIP[] in the check. Avoid declaring variables globally when its not needed,         //such as g_szSavedIP[]. This should be local to the function.         //Also, your data is saved in the below format, you will need to parse the IP value from it.         //"Name: %s - IP: %s" - You need to pull just the IP out of this for the compare.         //Do you want to check the name too, if so that will be an additional parse/check.         if(equal(g_szUserIP[id], g_szSavedIP[i])){             client_print(0, print_chat, "The user ip is already saved in the file.");         }         else{             client_print(0, print_chat, "The user ip is not saved in the file.");         }     }     return PLUGIN_HANDLED;

Not tested:
PHP Code:

        parse(SZ_DataVAULT_IPcharsmax(VAULT_IP))
        
        new 
iIPPos bool:bFound;
        for (new 
0size_vaulti++)
        {
            
fvault_get_keyname(VaultiSZ_NAMEcharsmax(SZ_NAME));
            
fvault_get_data(VaultSZ_NAMESZ_Datacharsmax(SZ_Data));
            
            
iIPPos strfindSZ_Data "IP: " );
            
            if ( 
iIPPos > -)
            {
                if ( 
equalg_szUserIPid ] , SZ_DataiIPPos ] ) )
                {
                    
client_print(0print_chat"The user ip is already saved in the file.");
                    
bFound true;
                    break;
                }
            }
        }
    }
    
    if ( 
bFound == false )
    {
        
client_print(0print_chat"The user ip is not saved in the file.");
    }



Thanks. I dont really want to save the name, the thing is I read something related to a "key" and in the example/tutorial it was the name.

I'll learn nvault if I cant make this work with fvault.

Bugsy 07-05-2021 10:40

Re: Save, read and compare user IP in fvault
 
You do not need to store both the name and IP in the vault data since the name is stored as the key, so you'll always have it. Try this:

An assumption is 1 ip per player name
PHP Code:


#include <amxmodx>
#include <amxmisc>
#include <fvault>

#define PLUGIN "fvault test"
#define VERSION "1.0"
#define AUTHOR "Maniatico"

#define MAX_PLAYERS 32

#if AMXX_VERSION_NUM >= 190
#define client_disconnect client_disconnected
#endif

new g_szUserIPMAX_PLAYERS ][ 27 ];

new const 
Vault[] = "_ips";

public 
plugin_init() 
{
    
register_pluginPLUGIN VERSION AUTHOR );
    
    
register_clcmd"amx_save_ip" "SaveIP" ADMIN_IMMUNITY );
}


public 
client_putinserverid )
{
    
get_user_ipid g_szUserIPid ], charsmaxg_szUserIP[] ) , );
    
set_task5.0 "CheckVault" id );
}

public 
client_disconnectid )
{
    
remove_taskid );
}

public 
SaveIPid )
{
    new 
szIP16 ] , szName32 ];
    
    if ( 
get_user_flagsid ) & ADMIN_IMMUNITY )
    {
        
read_argvszIP charsmaxszIP ) );
        
        
//Ideally should use regex here to check for IP format
        
if ( szIP] == EOS || ( containszIP ".") == -) || ( strlenszIP ) < ) )
        {
            
console_printid "Cant take that ip [%s]" szIP );
        }
        else 
        {
            
get_user_nameid szName charsmaxszName ) );
            
fvault_set_dataVault szName szIP );
            
console_printid "IP %s saved" szIP );
        }
    }
    
    return 
PLUGIN_HANDLED;
}
 
public 
CheckVaultid )
{
    new 
szVaultIP16 ] , szName32 ];
    
    
get_user_nameid szName charsmaxszName ) );
    
    if ( 
fvault_get_dataVault szName szVaultIP charsmaxszVaultIP ) ) && equalszVaultIP g_szUserIPid ] ) )
    {
        
client_printprint_chat "The user ip is saved in the file." );
    }
    else
    {
        
client_printprint_chat "The user ip is not saved in the file." );
    }



The overrated maniac 07-22-2021 02:40

Re: Save, read and compare user IP in fvault
 
Quote:

Originally Posted by Bugsy (Post 2751934)
You do not need to store both the name and IP in the vault data since the name is stored as the key, so you'll always have it. Try this:

An assumption is 1 ip per player name
PHP Code:


#include <amxmodx>
#include <amxmisc>
#include <fvault>

#define PLUGIN "fvault test"
#define VERSION "1.0"
#define AUTHOR "Maniatico"

#define MAX_PLAYERS 32

#if AMXX_VERSION_NUM >= 190
#define client_disconnect client_disconnected
#endif

new g_szUserIPMAX_PLAYERS ][ 27 ];

new const 
Vault[] = "_ips";

public 
plugin_init() 
{
    
register_pluginPLUGIN VERSION AUTHOR );
    
    
register_clcmd"amx_save_ip" "SaveIP" ADMIN_IMMUNITY );
}


public 
client_putinserverid )
{
    
get_user_ipid g_szUserIPid ], charsmaxg_szUserIP[] ) , );
    
set_task5.0 "CheckVault" id );
}

public 
client_disconnectid )
{
    
remove_taskid );
}

public 
SaveIPid )
{
    new 
szIP16 ] , szName32 ];
    
    if ( 
get_user_flagsid ) & ADMIN_IMMUNITY )
    {
        
read_argvszIP charsmaxszIP ) );
        
        
//Ideally should use regex here to check for IP format
        
if ( szIP] == EOS || ( containszIP ".") == -) || ( strlenszIP ) < ) )
        {
            
console_printid "Cant take that ip [%s]" szIP );
        }
        else 
        {
            
get_user_nameid szName charsmaxszName ) );
            
fvault_set_dataVault szName szIP );
            
console_printid "IP %s saved" szIP );
        }
    }
    
    return 
PLUGIN_HANDLED;
}
 
public 
CheckVaultid )
{
    new 
szVaultIP16 ] , szName32 ];
    
    
get_user_nameid szName charsmaxszName ) );
    
    if ( 
fvault_get_dataVault szName szVaultIP charsmaxszVaultIP ) ) && equalszVaultIP g_szUserIPid ] ) )
    {
        
client_printprint_chat "The user ip is saved in the file." );
    }
    else
    {
        
client_printprint_chat "The user ip is not saved in the file." );
    }



Thanks, it worked perfect.


All times are GMT -4. The time now is 02:29.

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