Skip to content

Commit

Permalink
🔀 Merge pull request #1271 from TF2Autobot/refactor
Browse files Browse the repository at this point in the history
🔨 Refactor codes
  • Loading branch information
idinium96 authored Jul 31, 2022
2 parents 0f49e6c + 53587eb commit ffa361b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
15 changes: 1 addition & 14 deletions src/classes/Commands/sub-classes/Misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import CommandParser from '../../CommandParser';
import Bot from '../../Bot';
import { Discord, Stock } from '../../Options';
import { pure, timeNow, uptime, testPriceKey } from '../../../lib/tools/export';
import { killstreakersData, sheensData, spellsData } from '../../../lib/data';
import { Paints, StrangeParts } from '@tf2autobot/tf2-schema';
import getAttachmentName from '../../../lib/tools/getAttachmentName';

type Misc = 'time' | 'uptime' | 'pure' | 'rate' | 'owner' | 'discord' | 'stock';
type CraftUncraft = 'craftweapon' | 'uncraftweapon';
Expand Down Expand Up @@ -374,15 +373,3 @@ export default class MiscCommands {
return stock;
}
}

function getKeyByValue(object: { [key: string]: any }, value: any): string {
return Object.keys(object).find(key => object[key] === value);
}

function getAttachmentName(attachment: string, pSku: string, paints: Paints, parts: StrangeParts): string {
if (attachment === 's') return getKeyByValue(spellsData, pSku);
else if (attachment === 'sp') return getKeyByValue(parts, pSku);
else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSku);
else if (attachment === 'ks') return getKeyByValue(sheensData, pSku);
else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSku.replace('p', '')));
}
16 changes: 2 additions & 14 deletions src/classes/Listings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import Bot from './Bot';
import Pricelist, { Entry, PricesObject } from './Pricelist';
import log from '../lib/logger';
import { exponentialBackoff } from '../lib/helpers';
import { noiseMakers, spellsData, killstreakersData, sheensData } from '../lib/data';
import { noiseMakers } from '../lib/data';
import { DictItem } from './Inventory';
import { PaintedNames } from './Options';
import { Paints, StrangeParts } from '@tf2autobot/tf2-schema';
import ListingManager from '@tf2autobot/bptf-listings';
import getAttachmentName from '../lib/tools/getAttachmentName';

export default class Listings {
private checkingAllListings = false;
Expand Down Expand Up @@ -717,18 +717,6 @@ export default class Listings {

type Attachment = 's' | 'sp' | 'ke' | 'ks' | 'p';

function getKeyByValue(object: { [key: string]: any }, value: any): string {
return Object.keys(object).find(key => object[key] === value);
}

function getAttachmentName(attachment: string, pSKU: string, paints: Paints, parts: StrangeParts): string {
if (attachment === 's') return getKeyByValue(spellsData, pSKU);
else if (attachment === 'sp') return getKeyByValue(parts, pSKU);
else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSKU);
else if (attachment === 'ks') return getKeyByValue(sheensData, pSKU);
else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSKU.replace('p', '')));
}

function boldDetails(str: string, style: number): string {
// https://lingojam.com/BoldTextGenerator

Expand Down
19 changes: 19 additions & 0 deletions src/lib/tools/getAttachmentName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { spellsData, killstreakersData, sheensData } from '../data';
import { Paints, StrangeParts } from '@tf2autobot/tf2-schema';

function getKeyByValue(object: { [key: string]: any }, value: any): string {
return Object.keys(object).find(key => object[key] === value);
}

export default function getAttachmentName(
attachment: string,
pSKU: string,
paints: Paints,
parts: StrangeParts
): string {
if (attachment === 's') return getKeyByValue(spellsData, pSKU);
else if (attachment === 'sp') return getKeyByValue(parts, pSKU);
else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSKU);
else if (attachment === 'ks') return getKeyByValue(sheensData, pSKU);
else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSKU.replace('p', '')));
}
17 changes: 1 addition & 16 deletions src/lib/tools/getHighValue.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import SKU from '@tf2autobot/tf2-sku';
import { Items } from '@tf2autobot/tradeoffer-manager';
import { spellsData, killstreakersData, sheensData } from '../data';
import Bot from '../../classes/Bot';
import { Paints, StrangeParts } from '@tf2autobot/tf2-schema';
import { testPriceKey } from '../tools/export';
import getAttachmentName from './getAttachmentName';

interface ItemsWithName {
[name: string]: string;
Expand Down Expand Up @@ -75,17 +74,3 @@ export default function getHighValueItems(items: Items, bot: Bot): ItemsWithName
}

type Attachment = 's' | 'sp' | 'ke' | 'ks' | 'p';

function getAttachmentName(attachment: string, pSKU: string, paints: Paints, parts: StrangeParts): string {
if (attachment === 's') return getKeyByValue(spellsData, pSKU);
else if (attachment === 'sp') return getKeyByValue(parts, pSKU);
else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSKU);
else if (attachment === 'ks') return getKeyByValue(sheensData, pSKU);
else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSKU.replace('p', '')));
}

function getKeyByValue(object: { [key: string]: any }, value: any): string {
const keys = Object.keys(object);
const key = keys.find(key => object[key] === value);
return key;
}

0 comments on commit ffa361b

Please sign in to comment.