From 13035edc40b59f47a6f6dd1d909e650edd50f085 Mon Sep 17 00:00:00 2001 From: ulysia Date: Sat, 28 Dec 2024 14:05:03 +0100 Subject: [PATCH] moved to types --- AuctionHandler.worker.ts | 2 +- index.ts | 26 ++++++++------------------ src/{Item.ts => Types.ts} | 18 ++++++++++++++++++ src/errorHandler.ts | 13 +++++++++++++ src/helperFunctions.ts | 2 +- 5 files changed, 41 insertions(+), 20 deletions(-) rename src/{Item.ts => Types.ts} (75%) create mode 100644 src/errorHandler.ts diff --git a/AuctionHandler.worker.ts b/AuctionHandler.worker.ts index c801103..5098c49 100644 --- a/AuctionHandler.worker.ts +++ b/AuctionHandler.worker.ts @@ -1,7 +1,7 @@ import axios from 'axios'; import { getParsed, getProfit, splitNumber, getRawCraft } from './src/helperFunctions'; import { parentPort, workerData, isMainThread } from 'worker_threads'; -import { Item } from './src/Item'; +import { Item } from './src/Types'; import { AuctionResponse, Auction, Bid } from './src/auctionType'; import { loadConfig } from './src/configLoader'; diff --git a/index.ts b/index.ts index 2615df8..cccafda 100644 --- a/index.ts +++ b/index.ts @@ -4,30 +4,20 @@ import { Worker } from 'worker_threads'; import { asyncInterval, addNotation } from './src/helperFunctions'; import { string } from 'prismarine-nbt'; import { SqlSystem } from './src/sqlFunctions'; +import { setupErrorHandlers } from './src/errorHandler'; +import { ItemCompactData } from './src/Types'; + +setupErrorHandlers(); + import { loadConfig } from './src/configLoader'; const config = loadConfig(); -class ItemData { - public sales: number; - public lbin: number; - public cleanPrice: number; - public price: number; - - constructor(sales: number = 0, lbin: number = 0, cleanPrice: number = 0, price: number = 0) { - this.sales = sales; - this.lbin = lbin; - this.cleanPrice = cleanPrice; - this.price = price; - } - -} - let worker_count = config.data.worker_count ?? 1; let lastUpdated = 0; let doneWorkers = 0; let startingTime: number; let maxPrice = 0; -let itemDatas: Record = {}; +let itemDatas: Record = {}; const workers: Worker[] = []; const webhookRegex = /https:\/\/discord.com\/api\/webhooks\/(.+)\/(.+)/; @@ -160,7 +150,7 @@ async function getLBINs(): Promise { const lbins = await axios.get('https://moulberry.codes/lowestbin.json'); const lbinData = lbins.data; for (const item of Object.keys(lbinData)) { - if (!itemDatas[item]) itemDatas[item] = new ItemData(); + if (!itemDatas[item]) itemDatas[item] = new ItemCompactData(); itemDatas[item].lbin = lbinData[item]; } } @@ -177,7 +167,7 @@ async function getMoulberry(): Promise { const cleanPriceData = cleanPriceAvgs.data; for (const item of Object.keys(avgData)) { - if (!itemDatas[item]) itemDatas[item] = new ItemData(); + if (!itemDatas[item]) itemDatas[item] = new ItemCompactData(); const itemInfo = avgData[item]; itemDatas[item].sales = itemInfo.sales !== undefined ? itemInfo.sales : 0; diff --git a/src/Item.ts b/src/Types.ts similarity index 75% rename from src/Item.ts rename to src/Types.ts index 36b9aa1..3bdb8cf 100644 --- a/src/Item.ts +++ b/src/Types.ts @@ -1,5 +1,6 @@ // item.ts +//#region ItemData Interface export interface ItemData { name: string; id: string; @@ -76,3 +77,20 @@ export class Item { }; } } +//#endregion +//#region ItemCompactData +export class ItemCompactData { + public sales: number; + public lbin: number; + public cleanPrice: number; + public price: number; + + constructor(sales: number = 0, lbin: number = 0, cleanPrice: number = 0, price: number = 0) { + this.sales = sales; + this.lbin = lbin; + this.cleanPrice = cleanPrice; + this.price = price; + } + +} +//#endregion \ No newline at end of file diff --git a/src/errorHandler.ts b/src/errorHandler.ts new file mode 100644 index 0000000..3e50c4f --- /dev/null +++ b/src/errorHandler.ts @@ -0,0 +1,13 @@ +// errorHandler.ts + +export function setupErrorHandlers(): void { + process.on('uncaughtException', (error) => { + console.error('Uncaught Exception:', error); + process.exit(1); + }); + + process.on('unhandledRejection', (reason, promise) => { + console.error('Unhandled Rejection at:', promise, 'reason:', reason); + process.exit(1); + }); +} diff --git a/src/helperFunctions.ts b/src/helperFunctions.ts index 0d7e391..ee0e648 100644 --- a/src/helperFunctions.ts +++ b/src/helperFunctions.ts @@ -1,6 +1,6 @@ // utils.ts import nbt from 'prismarine-nbt'; -import { Item } from './Item'; +import { Item } from './Types'; import { loadConfig } from './configLoader'; const config = loadConfig();