Skip to content

Commit

Permalink
πŸ”€ Merge pull request #580
Browse files Browse the repository at this point in the history
✨ support processing Non-TF2 items (offer only)
  • Loading branch information
idinium96 authored Apr 22, 2021
2 parents d6a5709 + e4a2ff9 commit a4f8505
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 69 deletions.
6 changes: 4 additions & 2 deletions src/classes/Commands/sub-classes/Review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SKU from 'tf2-sku-2';
import SchemaManager from 'tf2-schema-2';
import Bot from '../../Bot';
import CommandParser from '../../CommandParser';
import { generateLinks } from '../../../lib/tools/export';
import { generateLinks, testSKU } from '../../../lib/tools/export';

// Manual review commands

Expand Down Expand Up @@ -146,7 +146,9 @@ export default class ReviewCommands {
continue;
}

summary.push(schema.getName(SKU.fromString(sku), false) + (dict[sku] > 1 ? ` x${dict[sku]}` : '')); // dict[sku] = amount
const name = testSKU(sku) ? schema.getName(SKU.fromString(sku), false) : sku;

summary.push(name + (dict[sku] > 1 ? ` x${dict[sku]}` : '')); // dict[sku] = amount
}

if (summary.length === 0) {
Expand Down
119 changes: 68 additions & 51 deletions src/classes/MyHandler/MyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { exponentialBackoff } from '../../lib/helpers';

import { noiseMakers } from '../../lib/data';
import { sendAlert, sendStats } from '../../lib/DiscordWebhook/export';
import { summarize, uptime, getHighValueItems } from '../../lib/tools/export';
import { summarize, uptime, getHighValueItems, testSKU } from '../../lib/tools/export';

import genPaths from '../../resources/paths';
import Pricer, { RequestCheckFn } from '../Pricer';
Expand Down Expand Up @@ -758,7 +758,7 @@ export default class MyHandler extends Handler {
let isNoiseMakerNotFullUses = false;
const noiseMakerNotFullSKUs: string[] = [];

let hasInvalidItems = false;
let hasNonTF2Items = false;

const states = [false, true];
for (let i = 0; i < states.length; i++) {
Expand All @@ -770,9 +770,9 @@ export default class MyHandler extends Handler {
continue;
}

if (sku === 'unknown') {
if (!testSKU(sku)) {
// Offer contains an item that is not from TF2
hasInvalidItems = true;
hasNonTF2Items = true;
}

if (sku === '5000;6') {
Expand Down Expand Up @@ -907,10 +907,10 @@ export default class MyHandler extends Handler {
return;
}

if (hasInvalidItems) {
if (hasNonTF2Items && opt.offerReceived.alwaysDeclineNonTF2Items) {
// Using boolean because items dict always needs to be saved
offer.log('info', 'contains items not from TF2, declining...');
return { action: 'decline', reason: '🟨_INVALID_ITEMS_CONTAINS_NON_TF2' };
return { action: 'decline', reason: '🟨_CONTAINS_NON_TF2' };
}

const offerMessage = offer.message.toLowerCase();
Expand Down Expand Up @@ -1089,6 +1089,8 @@ export default class MyHandler extends Handler {

const amount = items[which][sku].length;

let isNonTF2Items = false;

if (sku === '5000;6') {
exchange[which].value += amount;
exchange[which].scrap += amount;
Expand All @@ -1109,10 +1111,23 @@ export default class MyHandler extends Handler {
exchange[which].value += value;
exchange[which].scrap += value;
} else {
const match =
which === 'our'
? this.bot.pricelist.getPrice(sku)
: this.bot.pricelist.getPrice(sku, false, true);
let match: Entry | null = null;

if (hasNonTF2Items) {
if (testSKU(sku)) {
match =
which === 'our'
? this.bot.pricelist.getPrice(sku)
: this.bot.pricelist.getPrice(sku, false, true);
} else {
isNonTF2Items = true;
}
} else {
match =
which === 'our'
? this.bot.pricelist.getPrice(sku)
: this.bot.pricelist.getPrice(sku, false, true);
}

const notIncludeCraftweapons = this.isWeaponsAsCurrency.enable
? !(
Expand Down Expand Up @@ -1227,59 +1242,61 @@ export default class MyHandler extends Handler {
(match !== null && match.intent === (buying ? 1 : 0))
) {
// Offer contains an item that we are not trading
hasInvalidItems = true;
// hasInvalidItems = true;

// If that particular item is on our side, then put to review
if (which === 'our') {
hasInvalidItemsOur = true;
}

// await sleepasync().Promise.sleep(1 * 1000);
const price = await this.bot.pricelist.getItemPrices(sku);
const item = SKU.fromString(sku);

const isCrateOrCases = item.crateseries !== null || ['5737;6', '5738;6'].includes(sku);
// 5737;6 and 5738;6 - Mann Co. Stockpile Crate
let itemSuggestedValue = 'No price';

const isWinterNoiseMaker = ['673;6'].includes(sku);
if (!isNonTF2Items) {
// await sleepasync().Promise.sleep(1 * 1000);
const price = await this.bot.pricelist.getItemPrices(sku);
const item = SKU.fromString(sku);

const isSkinsOrWarPaints = item.wear !== null;
const isCrateOrCases = item.crateseries !== null || ['5737;6', '5738;6'].includes(sku);
// 5737;6 and 5738;6 - Mann Co. Stockpile Crate

let itemSuggestedValue: string;
if (price === null) {
itemSuggestedValue = 'No price';
hasNoPrice = true;
} else {
price.buy = new Currencies(price.buy);
price.sell = new Currencies(price.sell);
const isWinterNoiseMaker = ['673;6'].includes(sku);

itemPrices[sku] = {
buy: price.buy,
sell: price.sell
};
const isSkinsOrWarPaints = item.wear !== null;

if (
opt.offerReceived.invalidItems.givePrice &&
!isSkinsOrWarPaints &&
!isCrateOrCases &&
!isWinterNoiseMaker // all of these (with !) should be false in order to be true
) {
// if offerReceived.invalidItems.givePrice is set to true (enable) and items is not skins/war paint/crate/cases,
// then give that item price and include in exchange
exchange[which].value += price[intentString].toValue(keyPrice.metal) * amount;
exchange[which].keys += price[intentString].keys * amount;
exchange[which].scrap += Currencies.toScrap(price[intentString].metal) * amount;
if (price === null) {
hasNoPrice = true;
} else {
price.buy = new Currencies(price.buy);
price.sell = new Currencies(price.sell);

itemPrices[sku] = {
buy: price.buy,
sell: price.sell
};

if (
opt.offerReceived.invalidItems.givePrice &&
!isSkinsOrWarPaints &&
!isCrateOrCases &&
!isWinterNoiseMaker // all of these (with !) should be false in order to be true
) {
// if offerReceived.invalidItems.givePrice is set to true (enable) and items is not skins/war paint/crate/cases,
// then give that item price and include in exchange
exchange[which].value += price[intentString].toValue(keyPrice.metal) * amount;
exchange[which].keys += price[intentString].keys * amount;
exchange[which].scrap += Currencies.toScrap(price[intentString].metal) * amount;
}
const valueInRef = {
buy: Currencies.toRefined(price.buy.toValue(keyPrice.metal)),
sell: Currencies.toRefined(price.sell.toValue(keyPrice.metal))
};

itemSuggestedValue =
(intentString === 'buy' ? valueInRef.buy : valueInRef.sell) >= keyPrice.metal
? `${valueInRef.buy.toString()} ref (${price.buy.toString()})` +
` / ${valueInRef.sell.toString()} ref (${price.sell.toString()})`
: `${price.buy.toString()} / ${price.sell.toString()}`;
}
const valueInRef = {
buy: Currencies.toRefined(price.buy.toValue(keyPrice.metal)),
sell: Currencies.toRefined(price.sell.toValue(keyPrice.metal))
};

itemSuggestedValue =
(intentString === 'buy' ? valueInRef.buy : valueInRef.sell) >= keyPrice.metal
? `${valueInRef.buy.toString()} ref (${price.buy.toString()})` +
` / ${valueInRef.sell.toString()} ref (${price.sell.toString()})`
: `${price.buy.toString()} / ${price.sell.toString()}`;
}

wrongAboutOffer.push({
Expand Down
10 changes: 3 additions & 7 deletions src/classes/MyHandler/offer/accepted/processAccepted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ export default function processAccepted(
// doing this so it will only executed if includes 🟨_INVALID_ITEMS reason.

(meta.reasons.filter(el => el.reason === '🟨_INVALID_ITEMS') as i.InvalidItems[]).forEach(el => {
accepted.invalidItems.push(
`${
isWebhookEnabled
? `_${bot.schema.getName(SKU.fromString(el.sku), false)}_`
: bot.schema.getName(SKU.fromString(el.sku), false)
} - ${el.price}`
);
const name = t.testSKU(el.sku) ? bot.schema.getName(SKU.fromString(el.sku), false) : el.sku;

accepted.invalidItems.push(`${isWebhookEnabled ? `_${name}_` : name} - ${el.price}`);
});
}
if (meta?.uniqueReasons?.includes('🟧_DISABLED_ITEMS')) {
Expand Down
5 changes: 5 additions & 0 deletions src/classes/MyHandler/offer/accepted/updateListings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EntryData } from '../../../Pricelist';
import log from '../../../../lib/logger';
import { sendAlert } from '../../../../lib/DiscordWebhook/export';
import { PaintedNames } from '../../../Options';
import { testSKU } from '../../../../lib/tools/export';

let itemsFromPreviousTrades: string[] = [];

Expand Down Expand Up @@ -43,6 +44,10 @@ export default function updateListings(
continue;
}

if (!testSKU(sku)) {
continue;
}

const item = SKU.fromString(sku);
const name = bot.schema.getName(item, false);
const pure = ['5000;6', '5001;6', '5002;6'];
Expand Down
3 changes: 2 additions & 1 deletion src/classes/MyHandler/offer/review/reasons/invalidItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import SKU from 'tf2-sku-2';
import pluralize from 'pluralize';
import { Meta, InvalidItems } from '@tf2autobot/tradeoffer-manager';
import Bot from '../../../../Bot';
import { testSKU } from '../../../../../lib/tools/export';

export default function invalidItems(meta: Meta, bot: Bot): { note: string; name: string[] } {
const opt = bot.options.discordWebhook.offerReview;
const invalidForTheir: string[] = []; // Display for trade partner
const invalidForOur: string[] = []; // Display for owner

(meta.reasons.filter(el => el.reason.includes('🟨_INVALID_ITEMS')) as InvalidItems[]).forEach(el => {
const name = bot.schema.getName(SKU.fromString(el.sku), false);
const name = testSKU(el.sku) ? bot.schema.getName(SKU.fromString(el.sku), false) : el.sku;

if (opt.enable && opt.url !== '') {
// show both item name and prices.tf price
Expand Down
2 changes: 2 additions & 0 deletions src/classes/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export const DEFAULTS = {
sendPreAcceptMessage: {
enable: true
},
alwaysDeclineNonTF2Items: true,
// πŸŸ₯_INVALID_VALUE
invalidValue: {
autoDecline: {
Expand Down Expand Up @@ -1269,6 +1270,7 @@ interface Metals extends OnlyEnable {

interface OfferReceived {
sendPreAcceptMessage?: OnlyEnable;
alwaysDeclineNonTF2Items?: boolean;
invalidValue?: InvalidValue;
invalidItems?: InvalidItems;
disabledItems?: AutoAcceptOverpayAndAutoDecline;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/extend/item/getSKU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export = function (
const self = this as EconItem;

if (self.appid != 440) {
if (self.type && self.market_name) {
return `${self.type}: ${self.market_name}`;
}

return 'unknown';
}

Expand Down
4 changes: 3 additions & 1 deletion src/lib/tools/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import listItems from './summarizeItems';
import summarize, { summarizeToChat } from './summarizeOffer';
import profit from './profit';
import itemStats from './itemStats';
import testSKU from './testSKU';

export {
getHighValueItems,
Expand All @@ -26,5 +27,6 @@ export {
uptime,
convertTime,
profit,
itemStats
itemStats,
testSKU
};
5 changes: 5 additions & 0 deletions src/lib/tools/getHighValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Items } from '@tf2autobot/tradeoffer-manager';
import { spellsData, killstreakersData, sheensData } from '../data';
import Bot from '../../classes/Bot';
import { Paints, StrangeParts } from 'tf2-schema-2';
import { testSKU } from '../tools/export';

export default function getHighValueItems(
items: Items,
Expand All @@ -22,6 +23,10 @@ export default function getHighValueItems(
continue;
}

if (!testSKU(sku)) {
continue;
}

let toString = '';

const toJoin: string[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tools/summarizeItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TradeOffer, Prices } from '@tf2autobot/tradeoffer-manager';
import SKU from 'tf2-sku-2';
import Currencies from 'tf2-currencies-2';
import Bot from '../../classes/Bot';
import { replace } from '../tools/export';
import { replace, testSKU } from '../tools/export';

export default function listItems(
offer: TradeOffer,
Expand Down Expand Up @@ -134,7 +134,7 @@ function listPrices(offer: TradeOffer, bot: Bot, isSteamChat: boolean): string {
sellPrice = new Currencies(prices[sku].sell).toString();
}

const name = bot.schema.getName(SKU.fromString(sku), properName);
const name = testSKU(sku) ? bot.schema.getName(SKU.fromString(sku), properName) : sku;

toJoin.push(
`${
Expand Down
15 changes: 10 additions & 5 deletions src/lib/tools/summarizeOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ export function summarizeToChat(
type SummarizeType = 'summary-accepted' | 'declined' | 'review-partner' | 'review-admin' | 'summary-accepting';

import Currencies from 'tf2-currencies-2';
import SKU from 'tf2-sku-2';
import { replace } from '../tools/export';

export default function summarize(
offer: TradeOffer,
Expand Down Expand Up @@ -144,6 +142,9 @@ export default function summarize(
}
}

import SKU from 'tf2-sku-2';
import { replace, testSKU } from '../tools/export';

function getSummary(
dict: OurTheirItemsDict,
bot: Bot,
Expand All @@ -165,9 +166,13 @@ function getSummary(
continue;
}

const isTF2Items = testSKU(sku);

// compatible with pollData from before v3.0.0 / before v2.2.0 and/or v3.0.0 or later ↓
const amount = typeof dict[sku] === 'object' ? (dict[sku]['amount'] as number) : dict[sku];
const generateName = bot.schema.getName(SKU.fromString(sku.replace(/;p\d+/, '')), properName);
const generateName = isTF2Items
? bot.schema.getName(SKU.fromString(sku.replace(/;p\d+/, '')), properName)
: sku; // Non-TF2 items
const name = properName ? generateName : replace.itemName(generateName ? generateName : 'unknown');

if (showStockChanges) {
Expand All @@ -191,7 +196,7 @@ function getSummary(
oldStock = currentStock;
}

if (withLink) {
if (withLink && isTF2Items) {
summary.push(
`[${
bot.options.tradeSummary.showPureInEmoji
Expand Down Expand Up @@ -229,7 +234,7 @@ function getSummary(
);
}
} else {
if (withLink) {
if (withLink && isTF2Items) {
summary.push(
`[${
bot.options.tradeSummary.showPureInEmoji
Expand Down
5 changes: 5 additions & 0 deletions src/lib/tools/testSKU.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function testSKU(sku: string): boolean {
return /^(\d+);([0-9]|[1][0-5])(;((uncraftable)|(untrad(e)?able)|(australium)|(festive)|(strange)|((u|pk|td-|c|od-|oq-|p)\d+)|(w[1-5])|(kt-[1-3])|(n((100)|[1-9]\d?))))*?$/.test(
sku
);
}
Loading

0 comments on commit a4f8505

Please sign in to comment.