1

I'm creating a game bot on telegram using node js.

Currently I'm facing a problem on shared variable (module.exports). I'm storing some of the data on the variable. And the problem is, the shared variable index always change. For example, please refer to my code below

var sharedVar = [];

createNewRoom = function(res) { 

 var index = sharedVar.length;

 sharedVar.push({ groupId : res.chat.id }); // every time this function is invoked, it will create a new array inside sharedVar object

//Here comes the problem, it's about the index, 
//because I'm using sharedVar to store arrays, then it will become a problem, 
//if one array is deleted (the index will change)

 var groupId = sharedVar[index].groupId; // it runs OK, if the structure of array doesn't change, but the structure of array change, the index will be a wrong number

        }

As you can see, i got callGameData function, when i call it, it will show the last value of sharedVar, it's supposed to show the current room values / data.

As i mention on the code above, it's all about the dynamic array in the sharedVar object, the index will change dynamically

Any thoughts to tackle this kind of issue? I was thinking about using a new sharedVar object everytime the createNewRoom function is invoked, but the thing is, i have to use sharedVar in many different function, and i still can't figure it out on using that method.

EDIT

This is the second method

var gameData = undefined;

createNewRoom = function() {
 this.gameData = new myConstructor([]); // it will instantiate a new object for each new room

}

myConstructor = function(data) {
 var _data = data;
 this.object = function() {
  return _data;
 }
}

callGameData = function() {
 console.log(gameData);
}
5
  • What exactly do you need the array for? And why do you need to share it if each room is supposed to have its own id (index)? Commented Dec 6, 2016 at 14:58
  • to store the data. yeah that's what i wrote on my post, i was thinking to create an independent variable for each room, for example using constructor, but the problem is, still i have to define the variable on the global, because i have many function that use the variable Commented Dec 6, 2016 at 15:05
  • Please show us those functions, without the code we hardly can advise you to do anything. Are they methods of the rooms? Commented Dec 6, 2016 at 15:15
  • @Bergi, please see my edited post above dude, thanks Commented Dec 6, 2016 at 15:30
  • I still don't get it. What does callGameData do? Does it need to iterate over all rooms? Does it need to access individual rooms? By id? Commented Dec 6, 2016 at 17:16

2 Answers 2

1

An array is fundamentally the wrong data type to use if you want to keep indices the same even in the face of removing entries.

A better method is to use properties of an object. For example:

var roomCache = { nextId: 1 };

createNewRoom = function(res) { 
    roomCache[roomCache.nextId++] = {groupId: res.chat.id}; // Add a new object to the cache and increment the next ID
}

After adding two elements, you'll have the rooms in roomCache[1] and roomCache[2] - if you want to start at zero just change the original value of nextId. You can delete elements in this object and it won't shift any keys for any other objects - just use delete roomCache[1] for example to get rid of that entry.

This assumes there isn't a better ID out there to use for the cache - if, for example, it made more sense to lookup by res.chat.id you could certainly use that as the key into roomCache rather than an auto-incrementing number. Here's how it would look like to cache the values by the group ID instead:

var roomCache = { };

createNewRoom = function(res) { 
    roomCache[res.chat.id] = {groupId: res.chat.id}; // Assumes res.chat.id is not a duplicate of an already cached obhect
}

Now you could just look up by group ID in the cache.

Sign up to request clarification or add additional context in comments.

4 Comments

hi dude, okay, let's assume the roomCache[1] is deleted, meanwhile i have to know which roomCache index stored which group data value. Doesn't it the same concept of what I did before? because i still have to iterate through roomCache array to find out which index that store the right group data value? isn't it?
That's what I was referring to when I spoke about "a better ID out there to use". If you're primarily going to look up by the res.chat.id value that's very simple to do, I'll add that to the answer.
hold up, after i saw your edited post, i think i know what do you mean. gimme 30 mins till 1 hour, want to try it first, and then will get back to you with the update. Thanks
this is what im looking for, just use the chat id as the key, thanks man!!
1

Yes, it's definitely a problem cause you are not keeping track of the index in a logical way, you are relying on position on the array which it changes, you need something that doesn't change over time to keep consistency and supports deletition of the element without affecting the rest of the elements. You could use mongo to store the generated rooms by id or maybe redis or some kind of key value pair database to store that kind of information.

3 Comments

ehm, okay, i've been using mariadb before. when i used mariadb to store all the data (id, etc). It has no problem, so why did i change to use cache instead? performance, yup, i don't have to run a select query or something, it's superfast, but yeah, now i found this problem.
this time you need some sort of id, you can't rely on array indexes.....or you could use objects instead of a simple array with a property id if you want to avoid databases...... {id: 23, otherFields......} and push it into an array, in that way you can delete the object without any problem and keep the consistency of the rest of the objects ids....(I'm assuming you don't want to persist any of this data using this solution)
sorry, i still don't get it haha, pardon me. But doesn't it will be the same concept like what i did? or may be i don't get it dude

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.