AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Saving variable per variable (https://forums.alliedmods.net/showthread.php?t=241486)

Kia 06-04-2014 09:16

Saving variable per variable
 
Hello there,

I add items to a menu using an API using sub-plugins. Now, I want to save the time when someone clicked on a menu item and remove that entry after e.g. 1 hour.
Time should be saved per player and per item and removed per time per item, means I need to save a dynamic amount of variables which get removed dynamically and independent of each other.

Any suggestions on how to realize that?

Backstabnoob 06-04-2014 09:38

Re: Saving variable per variable
 
That's not enough info. You obviously need some sort of 2-D array.

Kia 06-04-2014 09:57

Re: Saving variable per variable
 
Okay, I will try to explain.

I have a Shop (menu) which content is added by sub-plugins.
When a player buys one of the items, the item is added into his "list" of saved items.
This bought item should expire after a certain time.

Backstabnoob 06-04-2014 10:12

Re: Saving variable per variable
 
You already said that. How do you identify items? By a generated integer? The simple answer would be new g_ItemBuyTimestamp[ MaxPlayers ][ MaxItems ]. When a player buys an item you just do g_ItemBuyTimestamp[ Id ][ ItemId ] = get_systime(). Then on menu display/callback you check if g_ItemByTimestamp[ id ][ ItemId ] + 3600 < get_systime() and disable the item accordingly.

Since you want it to last this long you somehow need to save the time stamp.

Although this is really simple so I'm unsure if that's what you want.

Kia 06-04-2014 10:23

Re: Saving variable per variable
 
Each item has it's own index yes, but how do I save that array efficiently?

Backstabnoob 06-04-2014 10:28

Re: Saving variable per variable
 
SQL would be your best bet in this case. Or JSON string, but there's no efficient way to deal with JSON in amxx unless someone ports SMJansson.

Kia 06-04-2014 10:33

Re: Saving variable per variable
 
Wouldn't I need a table for each item?
Or how do you handle arrays in SQL?

Backstabnoob 06-04-2014 10:58

Re: Saving variable per variable
 
No, why? Just create a table such as:
Code:

CREATE TABLE table_itemlist (
    authid VARCHAR ( 34 ),
    itemid SMALLINT ( 5 ) UNSIGNED,
    unixtime INT( 11 ),
    INDEX( authid )
)

Then you can freely put any data in it. In the end you'll have something like this:

Code:

+------------------+--------+---------------+
| authid          | itemid | unixtime      |
+------------------+--------+---------------+
| STEAM_0:1:12345  | 1      | 1415781811    |
| STEAM_0:1:12345  | 2      | 1415781848    |
| STEAM_0:1:12345  | 3      | 1415781875    |
| STEAM_0:1:12345  | 5      | 1415781654    |
| STEAM_0:1:45678  | 1      | 1415781823    |
| STEAM_0:1:45678  | 3      | 1415781835    |
| STEAM_0:1:91234  | 1      | 1415787551    |
+------------------+--------+---------------+


Kia 06-04-2014 11:04

Re: Saving variable per variable
 
Quote:

Originally Posted by Backstabnoob (Post 2146676)
No, why? Just create a table such as:
Code:

CREATE TABLE table_itemlist (
    authid VARCHAR ( 34 ),
    itemid SMALLINT ( 5 ) UNSIGNED,
    unixtime INT( 11 ),
    INDEX( authid )
)

Then you can freely put any data in it. In the end you'll have something like this:

Code:

+------------------+--------+---------------+
| authid          | itemid | unixtime      |
+------------------+--------+---------------+
| STEAM_0:1:12345  | 1      | 1415781811    |
| STEAM_0:1:12345  | 2      | 1415781848    |
| STEAM_0:1:12345  | 3      | 1415781875    |
| STEAM_0:1:12345  | 5      | 1415781654    |
| STEAM_0:1:45678  | 1      | 1415781823    |
| STEAM_0:1:45678  | 3      | 1415781835    |
| STEAM_0:1:91234  | 1      | 1415787551    |
+------------------+--------+---------------+


But how do I handle the content if I want something like this :

Code:

+------------------+--------+---------------+
| authid          | itemid | unixtime      |
+------------------+--------+---------------+
| STEAM_0:1:12345  | 1,4,6 | 1415781811, 1415781215, 1415781856    |
| STEAM_0:1:12345  | 2      | 1415781848    |
| STEAM_0:1:12345  | 3      | 1415781875    |
| STEAM_0:1:12345  | 5      | 1415781654    |
| STEAM_0:1:45678  | 1      | 1415781823    |
| STEAM_0:1:45678  | 3      | 1415781835    |
| STEAM_0:1:91234  | 1      | 1415787551    |
+------------------+--------+---------------+

People can have multiple items at once.

Backstabnoob 06-04-2014 11:12

Re: Saving variable per variable
 
What don't you understand? In the post I showed the player with steam ID STEAM_0:1:12345 has items 1, 2, 3 and 5 at the same time (until get_systime( ) is higher than unixtime from the table)

Anyway, using numbered indexes for items is not the best idea because if the plugin order changes, items will also have different identifiers, so you should think about some sort of unique string identifiers.


All times are GMT -4. The time now is 09:47.

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