You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
6.3 KiB
161 lines
6.3 KiB
'use strict';
|
|
////////////////////////////////////////////////////////////////
|
|
var backend_integration = require("./backend_integration")
|
|
var trade_checker_imp = require("./trade_checker")
|
|
var backend = new backend_integration();
|
|
var trade_checker = new trade_checker_imp();
|
|
const VERSION = require("./version");
|
|
var SteamTradeOfferManager = require('steam-tradeoffer-manager');
|
|
|
|
const consoleTime = () => {
|
|
return (new Date()).toLocaleString();
|
|
}
|
|
|
|
async function sync(timeout) {
|
|
console.log(consoleTime(), `Build date: ${VERSION}`);
|
|
let sleep = function sleep (howLong) {
|
|
return new Promise(function (resolve) {
|
|
setTimeout(() => {resolve()}, howLong)
|
|
})
|
|
}
|
|
var steam_client;
|
|
while (true) {
|
|
try {
|
|
await backend.pulse().then(async () => {
|
|
if (backend.up) {
|
|
backend.getPrices(trade_checker);
|
|
if (steam_client === undefined) {
|
|
console.log(consoleTime(), "Setup Steam Client...")
|
|
steam_client = setupClient();
|
|
}
|
|
} else {
|
|
console.log(consoleTime(), `wait ${timeout} seconds after destroy, where pulse failed`);
|
|
await sleep(timeout * 1000);
|
|
if (!backend.up) {
|
|
process.exit(228);
|
|
}
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.log(consoleTime(), e);
|
|
}
|
|
}
|
|
}
|
|
|
|
function calculate(offer) {
|
|
if (offer.state !== SteamTradeOfferManager.ETradeOfferState.Active) {
|
|
console.log(consoleTime(), `[${offer.id}] trade have not active stats`)
|
|
return false;
|
|
}
|
|
if (offer.itemsToGive.length>0) {
|
|
console.log(consoleTime(), `[${offer.id}] partner wanna get bot items, decine trade`);
|
|
return false;
|
|
}
|
|
if (offer.itemsToReceive.length>50) {
|
|
console.log(consoleTime(), `[${offer.id}] cannot accept trade with more 50 items`)
|
|
return false;
|
|
}
|
|
const seconds = trade_checker.Items2Seconds(offer.itemsToReceive);
|
|
if (seconds === 0) {
|
|
console.log(consoleTime(), `[${offer.id}] cannot accept trade not valid items`)
|
|
return false;
|
|
}
|
|
return seconds > 0;
|
|
}
|
|
|
|
function setupClient() {
|
|
const config = require("./auth_data/auth.json")
|
|
/////////////////////////////////////////////////////////////////
|
|
var SteamUser = require('steam-user');
|
|
var SteamCommunity = require('steamcommunity');
|
|
var SteamTOTP = require('steam-totp');
|
|
/////////////////////////////////////////////////////////////////
|
|
var client = new SteamUser();
|
|
var community = new SteamCommunity();
|
|
var manager = new SteamTradeOfferManager({
|
|
steam: client,
|
|
language: 'en'
|
|
});
|
|
/////////////////////////////////////////////////////////////////
|
|
console.log(consoleTime(), 'connecting to steam account...');
|
|
client.logOn({
|
|
accountName: config.login,
|
|
password: config.password,
|
|
twoFactorCode: SteamTOTP.getAuthCode(config.shared_secret)
|
|
});
|
|
/////////////////////////////////////////////////////////////////
|
|
client.on('loggedOn', function(details, parental) {
|
|
console.log(consoleTime(), 'SteamID64 :', client.steamID.getSteamID64());
|
|
client.setPersona(SteamUser.EPersonaState.Online);
|
|
});
|
|
|
|
client.on('webSession', function(sessionID, cookies) {
|
|
manager.setCookies(cookies, function(err) {
|
|
if (err) {
|
|
console.log(consoleTime(), err);
|
|
process.exit(1); // Fatal error since we couldn't get our API key
|
|
return;
|
|
}
|
|
console.log(consoleTime(), "Web session created!");
|
|
});
|
|
|
|
community.setCookies(cookies);
|
|
});
|
|
|
|
client.on('error', function(error) {
|
|
|
|
console.log(consoleTime(), 'connection error\n');
|
|
console.log(consoleTime(), error);
|
|
});
|
|
////////////////////////////////////////////////////////////////////
|
|
manager.on('newOffer', function(offer) {
|
|
console.log(consoleTime(), `[${offer.id}] Incoming trade from ${offer.partner.getSteamID64()}`);
|
|
if (calculate(offer)) {
|
|
console.log(consoleTime(), `[${offer.id}] Try accept trade`);
|
|
offer.accept(false, function(error, status) {
|
|
if (error == null) {
|
|
console.log(consoleTime(), "Success accept trade");
|
|
return;
|
|
}
|
|
if ("message" in error && error.message === "Not Logged In") {
|
|
console.log(consoleTime(), "Need relog to accept");
|
|
client.relog();
|
|
} else {
|
|
console.log(consoleTime(), "Cannot accept trade", error, status);
|
|
}
|
|
});
|
|
} else {
|
|
console.log(consoleTime(), `[${offer.id}] Try decline trade`);
|
|
offer.decline(function(error, status) {
|
|
if (error == null) {
|
|
console.log(consoleTime(), "Success decline trade");
|
|
return;
|
|
}
|
|
if ("message" in error && error.message === "Not Logged In") {
|
|
console.log(consoleTime(), "Need relog to decline");
|
|
client.relog();
|
|
} else {
|
|
console.log(consoleTime(), "Cannot decline trade", error, status);
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
manager.on('receivedOfferChanged', function(offer, oldState) {
|
|
console.log(consoleTime(), `[${offer.id}] changed: ${SteamTradeOfferManager.ETradeOfferState[oldState]} -> ${SteamTradeOfferManager.ETradeOfferState[offer.state]}`);
|
|
if (offer.state === SteamTradeOfferManager.ETradeOfferState.Accepted) {
|
|
offer.getExchangeDetails((err, status, tradeInitTime, receivedItems, sentItems) => {
|
|
const seconds = trade_checker.Items2Seconds(receivedItems);
|
|
if (seconds > 0) {
|
|
const extra = `keys=${trade_checker.mannco_key(receivedItems)};metal=${trade_checker.pure_metal(receivedItems)};`;
|
|
backend.vip(offer.partner.getSteamID64(), seconds, extra, offer.id);
|
|
} else {
|
|
console.log(consoleTime(), `[${offer.id}] Trade accepted, but vip not gived!`);
|
|
}
|
|
})
|
|
}
|
|
});
|
|
return client;
|
|
}
|
|
|
|
sync(60);
|