Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔨 Refactor codes #1271

Merged
merged 1 commit into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}