Jump to content


Photo

[HELP] how to continue?

node.js node-steam-tradeoffer-manager

  • Please log in to reply
7 replies to this topic

#1 FallingLight

FallingLight

    Newbie

  • Member
  • Pip
  • 6 posts

Posted 15 December 2018 - 12:42 PM

So, i would like to search for specific items (keys).
 
Code:
 

community.getUserInventoryContents(steamid, 730, 2, true, (err, inventory) => {
if (err) {
throw err;
}else{
for(var i = 0, len = inventory.length; i < len; i++){
var itemnames = inventory[i].market_hash_name;
}
var found = itemnames.search("R8 Revolver | Bone Mask (Well-Worn)");
if(found != -1) {
console.log();
}
console.log(itemnames);
console.log (found);
 
}
 
I would like to write a code which is search for a specific item, then check this item's amount and then do something, but i'm new at nodeJS so i don't know so many things.
Please Help, or give me some docs. Tnanks

 



#2 SnaBe

SnaBe

    Advanced Member

  • Member
  • PipPipPip
  • 31 posts
  • LocationDenmark

Posted 15 December 2018 - 03:05 PM

You're very close, here's how I would do it.

//Load the inventory
manager.getUserInventoryContents(steamID, 730, 2, true, function(err, inventory) {
  if(err) {
    console.log('Error getting their inventory: ' + err);
  } else {
    console.log('Checking if user: ' + steamID + ' has any CS:GO keys we accept.');
    for(var n = 0; n < inventory.length; n++) {
      if(config.csgo.acceptedKeys.indexOf(inventory[n].market_hash_name) >= 0) {
        console.log(inventory[n].market_hash_name);
      }
    }
  }
});

acceptedKeys is an array containing all the current CS:GO keys, you can always remove or add keys to meet your needs.

"acceptedKeys": [
  "Glove Case Key",
  "Spectrum Case Key",
  "Gamma 2 Case Key",
  "Clutch Case Key",
  "Huntsman Case Key",
  "Gamma Case Key",
  "Spectrum 2 Case Key",
  "Operation Breakout Case Key",
  "Shadow Case Key",
  "Chroma 3 Case Key",
  "Falchion Case Key",
  "Winter Offensive Case Key",
  "Revolver Case Key",
  "Chroma 2 Case Key"
] 

To store the keys for a possible trade offer, you would have to add them in a new array. Like so: 

(This should be done within the for-loop)

//New empty array
var theirKeys = [];
//Add the keys to our array 
theirKeys.push(inventory[n]);	

After that, you can see how many keys the array contains.

console.log('User has ' + theirKeys.length + ' key(s).');

I would suggest a custom function for easier use.

function checkKeys(steamID) {
  //Their keys in a new array
  var theirKeys = [];
  //Load the inventory
  manager.getUserInventoryContents(steamID, 730, 2, true, function(err, inventory) {
    if(err) {
      console.log('Error getting their inventory: ' + err);
    } else {
      console.log('Checking if user: ' + steamID + ' has CS:GO keys.');
      for(var n = 0; n < inventory.length; n++) {
        if(config.csgo.acceptedKeys.indexOf(inventory[n].market_hash_name) >= 0) {
          theirKeys.push(inventory[n]); 
          console.log(inventory[n].market_hash_name);
        }
      }
      console.log('User has ' + theirKeys.length + ' key(s).');
    }
  });
}

Chat command example:

client.on('friendMessage', function(steamID, message) {
  if(message.match('!check')) {
    client.chatMessage(steamID, 'Handling your request...');
    checkKeys(steamID);
  } else {
  	client.chatMessage(steamID, 'I don\'t know that command.');
  }
});

Edited by SnaBe, 15 December 2018 - 03:08 PM.

  • FallingLight likes this

"Truth can only be found in one place: the code." - Robert C. Martin


#3 FallingLight

FallingLight

    Newbie

  • Member
  • Pip
  • 6 posts

Posted 18 December 2018 - 12:57 PM

Thanks, it is helped a lot. Sorry but, if i want to add for example x keys to offer?

So i know how to create an offer, but how i can get the key's "id" and add it to the offer?

Thanks



#4 SnaBe

SnaBe

    Advanced Member

  • Member
  • PipPipPip
  • 31 posts
  • LocationDenmark

Posted 18 December 2018 - 01:46 PM

To add an 'x' amount of keys to the array, just add an extra check in the if statement. (The one inside the for-loop)

//Loop
for(var n = 0; n < inventory.length; n++) {
  if(theirKeys.length < amountOfKeysToAddVariable && config.csgo.acceptedKeys.indexOf(inventory[n].market_hash_name) >= 0) {
    theirKeys.push(inventory[n]); 
    console.log(inventory[n].market_hash_name);
  }
}

The item ids are stored inside the theirKeys array, adding them to a trade offer is very simple.

offer.addTheirItems(theirKeys);

I hope this helps!


"Truth can only be found in one place: the code." - Robert C. Martin


#5 FallingLight

FallingLight

    Newbie

  • Member
  • Pip
  • 6 posts

Posted 22 December 2018 - 05:09 AM

Big thanks! You helped a lot. I have only one more question: How can i check if the offer is accepted?

I heared about  "sentOfferChanged" but i don't know how to use.


Edited by FallingLight, 23 December 2018 - 08:37 AM.


#6 SnaBe

SnaBe

    Advanced Member

  • Member
  • PipPipPip
  • 31 posts
  • LocationDenmark

Posted 25 December 2018 - 01:19 PM

Here's how you can check if an offer you sent has been accepted or declined.

//When one of our trade offers changes states.
manager.on('sentOfferChanged', function(offer, oldState) {
  //Alert us when an outgoing offer is accepted.
  if(offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
    console.log('Our offer #' + offer.id + ' has been accepted.');
    client.chatMessage(offer.partner.getSteamID64(), 'Thanks for the trade!');
  } else if(offer.state == TradeOfferManager.ETradeOfferState.Declined) {
    console.log('Our offer #' + offer.id + ' has been declined.');
    client.chatMessage(offer.partner.getSteamID64(), 'If you believe there\'s an error with your trade offer, please contact a staff member.');
  }
});


"Truth can only be found in one place: the code." - Robert C. Martin


#7 FallingLight

FallingLight

    Newbie

  • Member
  • Pip
  • 6 posts

Posted 27 December 2018 - 08:11 AM

Thanks Man. Meanwhile, I realized a few things.

Check if the offer is accepted

And if he/she is accept the offer -> bot receive of course items ->

 

for(var i = 0, len = items.length; i < len; i++){
var itemnames = items[i].market_hash_name;
}
 
console.log("Received items: " + itemnames);
 
Big Thanks to you man. You helped me so much and i learned so much thing about javascript.
I know it's late, but Happy Christmas & New Year.

  • SnaBe likes this

#8 SnaBe

SnaBe

    Advanced Member

  • Member
  • PipPipPip
  • 31 posts
  • LocationDenmark

Posted 27 December 2018 - 12:59 PM

Thank you and good luck in your future endeavors.


"Truth can only be found in one place: the code." - Robert C. Martin






Also tagged with one or more of these keywords: node.js, node-steam-tradeoffer-manager

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users