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

Use command once a day


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
wicho
Veteran Member
Join Date: Feb 2012
Location: GuateAmala
Old 11-23-2018 , 14:45   Use command once a day
Reply With Quote #1

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()

wicho is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 11-23-2018 , 15:10   Re: Use command once a day
Reply With Quote #2

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.
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-23-2018 , 18:52   Re: Use command once a day
Reply With Quote #3

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().
__________________

Last edited by fysiks; 11-23-2018 at 18:53.
fysiks is offline
Kushfield
Member
Join Date: Jan 2017
Location: Estonia
Old 11-23-2018 , 19:04   Re: Use command once a day
Reply With Quote #4

Quote:
Originally Posted by iceeedr View Post
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 
Kushfield 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 19:09.


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