AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Use command once a day (https://forums.alliedmods.net/showthread.php?t=312257)

wicho 11-23-2018 14:45

Use command once a day
 
Hi all, well I want to add a command /get that gives 50 ammo packs but I want to only use it once a day, I did this but it does not work, I can use it once on each map, can someone tell me what is the problem? ... thx in advance

PHP Code:

#include <amxmodx>
#include <zp50_ammopacks>
#include <nvault>

#define PLUGIN "command"
#define VERSION "1.0"
#define AUTHOR "kha"

//Name of vault
#define VAULT_NAME "getap"

new g_last_used[33], g_iVault
new g_szAuthID[33][34]

public 
plugin_init() 
{
         
register_plugin(PLUGINVERSIONAUTHOR)

         
register_clcmd("say /get""block_use")
}

public 
plugin_cfg()
{
         
g_iVault nvault_open(VAULT_NAME)
         if(
g_iVault == INVALID_HANDLE)
         {
             new 
szText[128]
             
formatex(szText127"Error opening '%s' nVault."VAULT_NAME)
             
set_fail_state(szText)
         }
}

public 
plugin_end()
{
         
nvault_close(g_iVault)
}

public 
client_putinserver(id)
{
         
get_user_authid(idg_szAuthID[id], charsmax(g_szAuthID[]))
         
LoadTime(id)
}

public 
block_use(id
{
         if(
get_systime() - g_last_used[id] < 86400
         {
         
client_print(idprint_chat"This command can only be used once a day!")
                  return 
PLUGIN_HANDLED
         
}

         
zp_ammopacks_set(idzp_ammopacks_get(id) + 50)
         
client_print(idprint_chat"You got 50 ammo packs enjoy them!!")
     
         
SaveTime(id)
         return 
PLUGIN_HANDLED


public 
LoadTime(id)
{
         
g_last_used[id] = nvault_get(g_iVaultg_szAuthID[id])  


public 
SaveTime(id)
{
         new 
szTime[5]
         
num_to_str(g_last_used[id], szTimecharsmax(szTime))
         
nvault_set(g_iVaultg_szAuthID[id], szTime)
         
g_last_used[id] = get_systime()



iceeedr 11-23-2018 15:10

Re: Use command once a day
 
I really do not understand much of nvault, but here is a mistake, because you are comparing the global last use with steamid, and that does not make sense.

I will not know how to exemplify it, but you have to use nvault's "timestamp" feature, and only then do you compare it to the latest usage, I think.

fysiks 11-23-2018 18:52

Re: Use command once a day
 
The issue is that the value that you read from nvault is the same value that you write to nvault. Therefore, you are never updating the value stored in nvault. So, you need to update g_last_used[id] before you write the value to nvault.

Also, players are only guaranteed to have a valid SteamID after client_authorized() so you need to use client_authorized() instead of client_putinserver().

Kushfield 11-23-2018 19:04

Re: Use command once a day
 
Quote:

Originally Posted by iceeedr (Post 2625400)
I really do not understand much of nvault, but here is a mistake, because you are comparing the global last use with steamid, and that does not make sense.

I will not know how to exemplify it, but you have to use nvault's "timestamp" feature, and only then do you compare it to the latest usage, I think.

I don't see him comparing last use time with SteamID anywhere.

As far as I can tell, the problem is in your SaveTime function:
PHP Code:

public SaveTime(id)
{
         new 
szTime[5]
         
num_to_str(g_last_used[id], szTimecharsmax(szTime))
         
nvault_set(g_iVaultg_szAuthID[id], szTime)
         
g_last_used[id] = get_systime()


The first mistake is using size 5 for szTime: get_systime() returns a Unix timestamp, which these days is a 10-digit number. This means you're only saving the first 4 digits of the timestamp, as that's how many characters your string can hold.
The second mistake is updating g_last_used[id] after saving it's value in the vault. This means the value you're saving is the old last use time, not the new one, so the saved value actually never gets updated.

Using nVault's own timestamps probably would be more efficient for your use case though, and would save you from all this trouble. nVault automatically stores a timestamp together with each key in the vault, so all you would have to do to update a key to current timestamp (or create a new entry with the current timestamp if it doesn't already exist) is:
PHP Code:

nvault_touch(g_iVaultg_szAuthID[id]); 

and to get the timestamp:
PHP Code:

if(!nvault_lookup(g_iVaultg_szAuthID[id], ""0g_last_used[id])) // returns 0 if key doesn't exist
    
g_last_used[id] = 0// set last use time to 0 if the player has never used before, otherwise previous player's value would be kept 



All times are GMT -4. The time now is 07:37.

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