Raised This Month: $ Target: $400
 0% 

Saving variable per variable


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-04-2014 , 09:16   Saving variable per variable
Reply With Quote #1

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?
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-04-2014 , 09:38   Re: Saving variable per variable
Reply With Quote #2

That's not enough info. You obviously need some sort of 2-D array.
Backstabnoob is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-04-2014 , 09:57   Re: Saving variable per variable
Reply With Quote #3

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.
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-04-2014 , 10:12   Re: Saving variable per variable
Reply With Quote #4

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.
Backstabnoob is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-04-2014 , 10:23   Re: Saving variable per variable
Reply With Quote #5

Each item has it's own index yes, but how do I save that array efficiently?
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-04-2014 , 10:28   Re: Saving variable per variable
Reply With Quote #6

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.
Backstabnoob is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-04-2014 , 10:33   Re: Saving variable per variable
Reply With Quote #7

Wouldn't I need a table for each item?
Or how do you handle arrays in SQL?
__________________

Last edited by Kia; 06-04-2014 at 10:35.
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-04-2014 , 10:58   Re: Saving variable per variable
Reply With Quote #8

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    |
+------------------+--------+---------------+
Backstabnoob is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-04-2014 , 11:04   Re: Saving variable per variable
Reply With Quote #9

Quote:
Originally Posted by Backstabnoob View Post
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.
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-04-2014 , 11:12   Re: Saving variable per variable
Reply With Quote #10

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.

Last edited by Backstabnoob; 06-04-2014 at 11:12.
Backstabnoob 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 09:47.


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