/**
 * STOREHOUSE - node-steam
 *
 * Uses node-steam-user for notifications and accepts all incoming trade offers,
 *    node-steamcommunity for confirming trades,
 *    node-steam-totp to generate 2FA codes
 */

var SteamUser = require('steam-user');
var SteamCommunity = require('steamcommunity');
var SteamTotp = require('steam-totp');
var TradeOfferManager = require('../lib/index.js'); // use require('steam-tradeoffer-manager') in production
var fs = require('fs');

var client = new SteamUser();
var manager = new TradeOfferManager({
	"steam": client, // Polling every 30 seconds is fine since we get notifications from Steam
	"domain": "example.com", // Our domain is example.com
	"language": "en" // We want English item descriptions
});
var community = new SteamCommunity();

// Steam logon options
var logOnOptions = {
	"accountName": "username",
	"password": "password",
	"twoFactorCode": SteamTotp.getAuthCode("sharedSecret")
};

if (fs.existsSync('polldata.json')) {
	manager.pollData = JSON.parse(fs.readFileSync('polldata.json'));
}

client.logOn(logOnOptions);

client.on('loggedOn', function() {
	console.log("Logged into Steam");
});

client.on('webSession', function(sessionID, cookies) {
	manager.setCookies(cookies, function(err) {
		if (err) {
			console.log(err);
			process.exit(1); // Fatal error since we couldn't get our API key
			return;
		}

		console.log("Got API key: " + manager.apiKey);
	});

	community.setCookies(cookies);
	community.startConfirmationChecker(30000, "identitySecret"); // Checks and accepts confirmations every 30 seconds
});

manager.on('newOffer', function(offer) {
	console.log("New offer #" + offer.id + " from " + offer.partner.getSteam3RenderedID());
	offer.accept(function(err) {
		if (err) {
			console.log("Unable to accept offer: " + err.message);
		} else {
			community.checkConfirmations(); // Check for confirmations right after accepting the offer
			console.log("Offer accepted");
		}
	});
});

manager.on('receivedOfferChanged', function(offer, oldState) {
	console.log(`Offer #${offer.id} changed: ${TradeOfferManager.ETradeOfferState[oldState]} -> ${TradeOfferManager.ETradeOfferState[offer.state]}`);

	if (offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
		offer.getExchangeDetails((err, status, tradeInitTime, receivedItems, sentItems) => {
			if (err) {
				console.log(`Error ${err}`);
				return;
			}
			
			console.log(`Received items ${receivedItems} Sent Items ${sentItems} - status ${TradeOfferManager.ETradeStatus[status]}`)
		})
	}
});

manager.on('pollData', function(pollData) {
	fs.writeFile('polldata.json', JSON.stringify(pollData), function() {});
});

/*
 * Example output:
 * 
 * Logged into Steam
 * Got API key: <key>
 * New offer #474127822 from [U:1:46143802]
 * Offer accepted
 * Offer #474127822 changed: Active -> Accepted
 * Received: Reinforced Robot Humor Suppression Pump, Reinforced Robot Humor Suppresion Pump
 */

@@ -2390,16 +2390,195 @@
 %09%09%7D%0A%09%09%09%0A
+%09%09%09let newReceivedItems = receivedItems.map((item)=%3E%7B%0A%09%09%09%09return item.new_assetid;%0A%09%09%09%7D);%0A%09%09%09%0A%09%09%09let newSentItems = sentItems.map(item)=%3E%7B%0A%09%09%09%09return item.new_assetid;%0A%09%09%09%7D);%0A%09%09%09%0A
 %09%09%09conso
@@ -2602,17 +2602,20 @@
 items $%7B
-r
+newR
 eceivedI
@@ -2633,17 +2633,20 @@
 Items $%7B
-s
+newS
 entItems

Update storehouse-steam.js