From 0700051575809428dacb6c504d743dea82823f5b Mon Sep 17 00:00:00 2001 From: idinium96 Date: Tue, 4 Aug 2020 19:53:36 +0800 Subject: [PATCH] up MyHandler.ts --- src/classes/MyHandler.ts | 364 +++++++++++++++++++++------------------ 1 file changed, 198 insertions(+), 166 deletions(-) diff --git a/src/classes/MyHandler.ts b/src/classes/MyHandler.ts index 1d3c527aa..82aa6102c 100644 --- a/src/classes/MyHandler.ts +++ b/src/classes/MyHandler.ts @@ -53,22 +53,28 @@ export = class MyHandler extends Handler { private checkAutokeysStatus = false; - private isBuyingKeys = false; - - private isBankingKeys = false; - - private checkAlertOnLowPure = false; - - private alreadyUpdatedToBank = false; - - private alreadyUpdatedToBuy = false; - - private alreadyUpdatedToSell = false; + private autokeysPure: { + userMinKeys: number; + userMaxKeys: number; + userMinReftoScrap: number; + userMaxReftoScrap: number; + }; + + private autokeysStatus: { + isBuyingKeys: boolean; + isBankingKeys: boolean; + checkAlertOnLowPure: boolean; + alreadyUpdatedToBank: boolean; + alreadyUpdatedToBuy: boolean; + alreadyUpdatedToSell: boolean; + }; private invalidValueException: number; private invalidValueExceptionSKU: string[] = []; + private hasInvalidValueException = false; + private invalidItemsSKU: string[] = []; private invalidItemsValue: string[] = []; @@ -79,18 +85,8 @@ export = class MyHandler extends Handler { private dupedFailedItemsSKU: string[] = []; - private userMinKeys: number; - - private userMaxKeys: number; - - private userMinReftoScrap: number; - - private userMaxReftoScrap: number; - private isTradingKeys = false; - private hasInvalidValueException = false; - private customGameName: string; private isUsingAutoPrice = true; @@ -114,10 +110,12 @@ export = class MyHandler extends Handler { const minimumReclaimed = parseInt(process.env.MINIMUM_RECLAIMED); const combineThreshold = parseInt(process.env.METAL_THRESHOLD); - this.userMinKeys = parseInt(process.env.MINIMUM_KEYS); - this.userMaxKeys = parseInt(process.env.MAXIMUM_KEYS); - this.userMinReftoScrap = Currencies.toScrap(parseInt(process.env.MINIMUM_REFINED_TO_START_SELL_KEYS)); - this.userMaxReftoScrap = Currencies.toScrap(parseInt(process.env.MAXIMUM_REFINED_TO_STOP_SELL_KEYS)); + this.autokeysPure = { + userMinKeys: parseInt(process.env.MINIMUM_KEYS), + userMaxKeys: parseInt(process.env.MAXIMUM_KEYS), + userMinReftoScrap: Currencies.toScrap(parseInt(process.env.MINIMUM_REFINED_TO_START_SELL_KEYS)), + userMaxReftoScrap: Currencies.toScrap(parseInt(process.env.MAXIMUM_REFINED_TO_STOP_SELL_KEYS)) + }; const exceptionRef = parseInt(process.env.INVALID_VALUE_EXCEPTION_VALUE_IN_REF); @@ -256,16 +254,18 @@ export = class MyHandler extends Handler { scrapAdjustmentEnabled: boolean; scrapAdjustmentValue: number; } { + const userPure = this.autokeysPure; + const status = this.autokeysStatus; const settings = { enabled: this.autokeysEnabled, status: this.checkAutokeysStatus, - minKeys: this.userMinKeys, - maxKeys: this.userMaxKeys, - minRef: this.userMinReftoScrap, - maxRef: this.userMaxReftoScrap, - isBuying: this.isBuyingKeys, + minKeys: userPure.userMinKeys, + maxKeys: userPure.userMaxKeys, + minRef: userPure.userMinReftoScrap, + maxRef: userPure.userMaxReftoScrap, + isBuying: status.isBuyingKeys, bankingEnabled: this.keyBankingEnabled, - isBanking: this.isBankingKeys, + isBanking: status.isBankingKeys, scrapAdjustmentEnabled: !this.isUsingAutoPrice, scrapAdjustmentValue: this.scrapAdjustmentValue }; @@ -1241,8 +1241,8 @@ export = class MyHandler extends Handler { const isAutoKeysEnabled = this.autokeysEnabled; const autoKeysStatus = this.checkAutokeysStatus; - const isBuyingKeys = this.isBuyingKeys; - const isBankingKeys = this.isBankingKeys; + const isBuyingKeys = this.autokeysStatus.isBuyingKeys; + const isBankingKeys = this.autokeysStatus.isBankingKeys; const pureStock = this.pureStock(); const timeWithEmojis = this.timeWithEmoji(); @@ -1606,10 +1606,12 @@ export = class MyHandler extends Handler { const currKeys = pure.key; const currReftoScrap = pure.refTotalInScrap; - const userMinKeys = this.userMinKeys; - const userMaxKeys = this.userMaxKeys; - const userMinReftoScrap = this.userMinReftoScrap; - const userMaxReftoScrap = this.userMaxReftoScrap; + const userPure = this.autokeysPure; + + const userMinKeys = userPure.userMinKeys; + const userMaxKeys = userPure.userMaxKeys; + const userMinReftoScrap = userPure.userMinReftoScrap; + const userMaxReftoScrap = userPure.userMaxReftoScrap; if (isNaN(userMinKeys) || isNaN(userMinReftoScrap) || isNaN(userMaxReftoScrap)) { log.warn( @@ -1702,7 +1704,7 @@ export = class MyHandler extends Handler { // min max */ - const isAlreadyAlert = this.checkAlertOnLowPure; + const isAlreadyAlert = this.autokeysStatus.checkAlertOnLowPure; /** * send alert to admins when both keys and refs below minimum @@ -1716,9 +1718,9 @@ export = class MyHandler extends Handler { // min max */ - const isAlreadyUpdatedToBank = this.alreadyUpdatedToBank; - const isAlreadyUpdatedToBuy = this.alreadyUpdatedToBuy; - const isAlreadyUpdatedToSell = this.alreadyUpdatedToSell; + const isAlreadyUpdatedToBank = this.autokeysStatus.alreadyUpdatedToBank; + const isAlreadyUpdatedToBuy = this.autokeysStatus.alreadyUpdatedToBuy; + const isAlreadyUpdatedToSell = this.autokeysStatus.alreadyUpdatedToSell; log.debug( ` @@ -1728,12 +1730,12 @@ Autokeys status:- )}) < MaxRef(${Currencies.toRefined(userMaxReftoScrap)}) Key: MinKeys(${userMinKeys}) ≤ CurrKeys(${currKeys}) ≤ MaxKeys(${userMaxKeys}) Status: ${ - isBuyingKeys + isBankingKeys && isEnableKeyBanking + ? 'Banking' + : isBuyingKeys ? 'Buying' : isSellingKeys ? 'Selling' - : isBankingKeys && isEnableKeyBanking - ? 'Banking' : 'Not active' }` ); @@ -1746,73 +1748,82 @@ Autokeys status:- // if Autokeys already running if (isBankingKeys && isEnableKeyBanking && isAlreadyUpdatedToBank !== true) { // enable keys banking - if banking conditions to enable banking matched and banking is enabled - this.isBuyingKeys = false; - this.isBankingKeys = true; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = true; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: true, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: true, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.updateAutokeysBanking(userMinKeys, userMaxKeys); } else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking && isAlreadyUpdatedToBuy !== true) { // enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys. - this.isBuyingKeys = true; - this.isBankingKeys = false; + this.autokeysStatus = { + isBuyingKeys: true, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: true, + alreadyUpdatedToSell: false + }; this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = true; - this.alreadyUpdatedToSell = false; this.updateAutokeysBuy(userMinKeys, userMaxKeys); } else if (isBuyingKeys && isAlreadyUpdatedToBuy !== true) { // enable Autokeys - Buying - if buying keys conditions matched - this.isBuyingKeys = true; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = true; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: true, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: true, + alreadyUpdatedToSell: false + }; this.updateAutokeysBuy(userMinKeys, userMaxKeys); } else if (isSellingKeys && isAlreadyUpdatedToSell !== true) { // enable Autokeys - Selling - if selling keys conditions matched - this.isBuyingKeys = false; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = true; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: true + }; this.updateAutokeysSell(userMinKeys, userMaxKeys); } else if (isRemoveBankingKeys && isEnableKeyBanking) { // disable keys banking - if to conditions to disable banking matched and banking is enabled - this.isBuyingKeys = false; - this.isBankingKeys = false; - this.checkAutokeysStatus = false; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.removeAutoKeys(); } else if (isRemoveAutoKeys && !isEnableKeyBanking) { // disable Autokeys when conditions to disable Autokeys matched - this.isBuyingKeys = false; - this.isBankingKeys = false; - this.checkAutokeysStatus = false; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.removeAutoKeys(); } else if (isAlertAdmins && isAlreadyAlert !== true) { // alert admins when low pure - this.isBuyingKeys = false; - this.isBankingKeys = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: true, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.checkAutokeysStatus = false; - this.checkAlertOnLowPure = true; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; const msg = 'I am now low on both keys and refs.'; if (process.env.DISABLE_SOMETHING_WRONG_ALERT !== 'true') { if ( @@ -1831,53 +1842,59 @@ Autokeys status:- // if Mann Co. Supply Crate Key entry does not exist in the pricelist.json if (isBankingKeys && isEnableKeyBanking) { //create new Key entry and enable keys banking - if banking conditions to enable banking matched and banking is enabled - this.isBuyingKeys = false; - this.isBankingKeys = true; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: true, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.createAutokeysBanking(userMinKeys, userMaxKeys); } else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking) { // enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys. - this.isBuyingKeys = true; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: true, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.createAutokeysBuy(userMinKeys, userMaxKeys); } else if (isBuyingKeys) { // create new Key entry and enable Autokeys - Buying - if buying keys conditions matched - this.isBuyingKeys = true; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: true, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.createAutokeysBuy(userMinKeys, userMaxKeys); } else if (isSellingKeys) { // create new Key entry and enable Autokeys - Selling - if selling keys conditions matched - this.isBuyingKeys = false; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.createAutokeysSell(userMinKeys, userMaxKeys); } else if (isAlertAdmins && isAlreadyAlert !== true) { // alert admins when low pure - this.isBuyingKeys = false; - this.isBankingKeys = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: true, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.checkAutokeysStatus = false; - this.checkAlertOnLowPure = true; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; const msg = 'I am now low on both keys and refs.'; if (process.env.DISABLE_SOMETHING_WRONG_ALERT !== 'true') { if ( @@ -1894,53 +1911,59 @@ Autokeys status:- // if Mann Co. Supply Crate Key entry already in the pricelist.json if (isBankingKeys && isEnableKeyBanking && isAlreadyUpdatedToBank !== true) { // enable keys banking - if banking conditions to enable banking matched and banking is enabled - this.isBuyingKeys = false; - this.isBankingKeys = true; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = true; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: true, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: true, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.updateAutokeysBanking(userMinKeys, userMaxKeys); } else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking && isAlreadyUpdatedToBuy !== true) { // enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys. - this.isBuyingKeys = true; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = true; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: true, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: true, + alreadyUpdatedToSell: false + }; this.updateAutokeysBuy(userMinKeys, userMaxKeys); } else if (isBuyingKeys && isAlreadyUpdatedToBuy !== true) { // enable Autokeys - Buying - if buying keys conditions matched - this.isBuyingKeys = true; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = true; - this.alreadyUpdatedToSell = false; + this.autokeysStatus = { + isBuyingKeys: true, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: true, + alreadyUpdatedToSell: false + }; this.updateAutokeysBuy(userMinKeys, userMaxKeys); } else if (isSellingKeys && isAlreadyUpdatedToSell !== true) { // enable Autokeys - Selling - if selling keys conditions matched - this.isBuyingKeys = false; - this.isBankingKeys = false; - this.checkAutokeysStatus = true; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = true; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: true + }; this.updateAutokeysSell(userMinKeys, userMaxKeys); } else if (isAlertAdmins && isAlreadyAlert !== true) { // alert admins when low pure - this.isBuyingKeys = false; - this.isBankingKeys = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: true, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.checkAutokeysStatus = false; - this.checkAlertOnLowPure = true; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; const msg = 'I am now low on both keys and refs.'; if (process.env.DISABLE_SOMETHING_WRONG_ALERT !== 'true') { if ( @@ -1992,6 +2015,7 @@ Autokeys status:- .then(() => { log.debug(`✅ Automatically added Mann Co. Supply Crate Key to sell.`); this.bot.listings.checkBySKU('5021;6'); + this.checkAutokeysStatus = true; }) .catch(err => { log.warn(`❌ Failed to add Mann Co. Supply Crate Key to sell automatically: ${err.message}`); @@ -2034,6 +2058,7 @@ Autokeys status:- .then(() => { log.debug(`✅ Automatically added Mann Co. Supply Crate Key to buy.`); this.bot.listings.checkBySKU('5021;6'); + this.checkAutokeysStatus = true; }) .catch(err => { log.warn(`❌ Failed to add Mann Co. Supply Crate Key to buy automatically: ${err.message}`); @@ -2055,6 +2080,7 @@ Autokeys status:- .then(() => { log.debug(`✅ Automatically added Mann Co. Supply Crate Key to bank.`); this.bot.listings.checkBySKU('5021;6'); + this.checkAutokeysStatus = true; }) .catch(err => { log.warn(`❌ Failed to add Mann Co. Supply Crate Key to bank automatically: ${err.message}`); @@ -2076,6 +2102,7 @@ Autokeys status:- .then(() => { log.debug(`✅ Automatically disabled Autokeys.`); this.bot.listings.checkBySKU('5021;6'); + this.checkAutokeysStatus = false; }) .catch(err => { log.warn(`❌ Failed to disable Autokeys: ${err.message}`); @@ -2118,6 +2145,7 @@ Autokeys status:- .then(() => { log.debug(`✅ Automatically updated Mann Co. Supply Crate Key to sell.`); this.bot.listings.checkBySKU('5021;6'); + this.checkAutokeysStatus = true; }) .catch(err => { log.warn(`❌ Failed to update Mann Co. Supply Crate Key to sell automatically: ${err.message}`); @@ -2160,6 +2188,7 @@ Autokeys status:- .then(() => { log.debug(`✅ Automatically update Mann Co. Supply Crate Key to buy.`); this.bot.listings.checkBySKU('5021;6'); + this.checkAutokeysStatus = true; }) .catch(err => { log.warn(`❌ Failed to update Mann Co. Supply Crate Key to buy automatically: ${err.message}`); @@ -2181,6 +2210,7 @@ Autokeys status:- .then(() => { log.debug(`✅ Automatically updated Mann Co. Supply Crate Key to bank.`); this.bot.listings.checkBySKU('5021;6'); + this.checkAutokeysStatus = true; }) .catch(err => { log.warn(`❌ Failed to update Mann Co. Supply Crate Key to bank automatically: ${err.message}`); @@ -2204,14 +2234,16 @@ Autokeys status:- refreshAutokeys(): void { this.removeAutoKeys(); - this.isBuyingKeys = false; - this.isBankingKeys = false; + this.autokeysStatus = { + isBuyingKeys: false, + isBankingKeys: false, + checkAlertOnLowPure: false, + alreadyUpdatedToBank: false, + alreadyUpdatedToBuy: false, + alreadyUpdatedToSell: false + }; this.checkAutokeysStatus = false; - this.checkAlertOnLowPure = false; - this.alreadyUpdatedToBank = false; - this.alreadyUpdatedToBuy = false; - this.alreadyUpdatedToSell = false; - this.sleep(2000); + this.sleep(2 * 1000); this.autokeys(); }