diff --git a/AuctionHandler.worker.ts b/AuctionHandler.worker.ts index 2a90d06..c801103 100644 --- a/AuctionHandler.worker.ts +++ b/AuctionHandler.worker.ts @@ -2,6 +2,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 { AuctionResponse, Auction, Bid } from './src/auctionType'; import { loadConfig } from './src/configLoader'; const config = loadConfig(); @@ -10,8 +11,7 @@ if (isMainThread || !parentPort) { throw new Error('This module can only be run in a Worker thread.'); } -const threadsToUse: number = config.data['threadsToUse/speed']; - +const worker_count: number = config.data.worker_count; let minProfit = config.data.minSnipeProfit; let minPercentProfit = config.data.minSnipePP; let ignoredAuctions: any[] = []; @@ -31,10 +31,10 @@ parentPort.on('message', async (message) => { } }); -async function parsePage(i) { +async function parsePage(i: number) { console.log(`[Worker ${workerData.workerNumber}] Parsing page ${i}`); try { - const auctionPage = await axios.get( + const auctionPage = await axios.get( `https://api.hypixel.net/skyblock/auctions?page=${i}` ); for (const auction of auctionPage.data.auctions) { @@ -144,12 +144,12 @@ async function parsePage(i) { } } -async function doTask(totalPages) { +async function doTask(totalPages: number) { console.log( `[Worker ${workerData.workerNumber}] Starting task for ${totalPages} pages` ); let startingPage = 0; - const pagePerThread = splitNumber(totalPages, threadsToUse); + const pagePerThread = splitNumber(totalPages, worker_count); if (workerData.workerNumber !== 0 && startingPage === 0) { const clonedStarting = pagePerThread.slice(); diff --git a/config.json b/config.json index c9973b6..23cd625 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "data": { - "thread_count/speed": 48, + "worker_count": 48, "minSnipeProfit": 900000, "minAvgProfit": 500000, diff --git a/index.ts b/index.ts index 0aafccd..8847a94 100644 --- a/index.ts +++ b/index.ts @@ -7,13 +7,27 @@ import { InitTable } from './src/sqlFunctions'; import { loadConfig } from './src/configLoader'; const config = loadConfig(); +class ItemData { + public sales: number; + public lbin: number; + public cleanPrice: number; + public price: number; -let thread_count = config.data['thread_count/speed'] ?? 1; + 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; +let startingTime: number; let maxPrice = 0; -let itemDatas = {}; +let itemDatas: Record = {}; const workers: Worker[] = []; const webhookRegex = /https:\/\/discord.com\/api\/webhooks\/(.+)\/(.+)/; @@ -33,7 +47,7 @@ async function initialize() { await getMoulberry(); await getLBINs(); - for (let j = 0; j < thread_count; j++) { + for (let j = 0; j < worker_count; j++) { workers[j] = new Worker('/app/dist/AuctionHandler.worker.js', { workerData: { itemDatas: itemDatas, @@ -45,11 +59,11 @@ async function initialize() { workers[j].on('message', async (result) => { if (result.itemData !== undefined) { - let averagePrice = itemDatas[result.itemData.id]?.cleanPrice || 'N/A'; + let averagePrice: number | null = itemDatas[result.itemData.id]?.cleanPrice || null; if ( result.auctionData.lbin - result.auctionData.price >= config.data.minSnipeProfit && - averagePrice - result.auctionData.price >= config.data.minAvgProfit + (averagePrice || averagePrice! - result.auctionData.price >= config.data.minAvgProfit) ) { let mustBuyMessage = ''; const embed = new EmbedBuilder() @@ -70,7 +84,7 @@ async function initialize() { result.auctionData.sales )}\` \nType: \`${result.auctionData.ahType}\` - \nAverage Price: \`${addNotation('oneLetters', averagePrice)}\`` + \nAverage Price: \`${averagePrice ? addNotation('oneLetters', averagePrice) : 'N/A'}\`` ); await webhook.send({ @@ -81,7 +95,7 @@ async function initialize() { } } else if (result === 'finished') { doneWorkers++; - if (doneWorkers === thread_count) { + if (doneWorkers === worker_count) { doneWorkers = 0; console.log( `Completed in ${(Date.now() - startingTime) / 1000} seconds` @@ -146,7 +160,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] = {}; + if (!itemDatas[item]) itemDatas[item] = new ItemData(); itemDatas[item].lbin = lbinData[item]; } } @@ -163,7 +177,7 @@ async function getMoulberry(): Promise { const cleanPriceData = cleanPriceAvgs.data; for (const item of Object.keys(avgData)) { - if (!itemDatas[item]) itemDatas[item] = {}; + if (!itemDatas[item]) itemDatas[item] = new ItemData(); const itemInfo = avgData[item]; itemDatas[item].sales = itemInfo.sales !== undefined ? itemInfo.sales : 0; diff --git a/package.json b/package.json index 0ebb551..a417188 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "copy-paste": "^1.5.3", "discord.js": "^14.16.3", "express": "^4.21.2", - "node": "^23.5.0", "node-notifier": "^10.0.1", "open": "^8.4.2", "prismarine-nbt": "^2.7.0", diff --git a/src/Item.ts b/src/Item.ts index ee054a7..36b9aa1 100644 --- a/src/Item.ts +++ b/src/Item.ts @@ -6,7 +6,7 @@ export interface ItemData { stars: number; rarity: string; recomb: boolean; - enchants: string[]; + enchants: Record; hpbs: number; fpbs: number; gemstones: string[]; @@ -34,7 +34,7 @@ export class Item { auctionID: string, price: number, rarity: string, - enchants: string[], + enchants: Record, hpbs: number, fpbs: number, recomb: boolean, diff --git a/src/auctionType.ts b/src/auctionType.ts new file mode 100644 index 0000000..4c97f5b --- /dev/null +++ b/src/auctionType.ts @@ -0,0 +1,39 @@ +export type AuctionResponse = { + success: boolean + page: number + totalPages: number + totalAuctions: number + lastUpdated: number + auctions: Auction[] +} + +export type Auction = { + uuid: string + auctioneer: string + profile_id: string + coop: string[] + start: number + end: number + item_name: string + item_lore: string + extra: string + category: string + tier: string + starting_bid: number + item_bytes: string + claimed: boolean + claimed_bidders: any[] + highest_bid_amount: number + last_updated: number + bin: boolean + bids: Bid[] + item_uuid?: string +} + +export type Bid = { + auction_id: string + bidder: string + profile_id: string + amount: number + timestamp: number +} \ No newline at end of file diff --git a/src/configLoader.ts b/src/configLoader.ts index c477fde..9e3a83c 100644 --- a/src/configLoader.ts +++ b/src/configLoader.ts @@ -3,7 +3,7 @@ import * as path from 'path'; export interface ConfigType { data: { - 'threadsToUse/speed': number; + worker_count: number; minSnipeProfit: number; minAvgProfit: number; minCraftProfit: number; @@ -11,14 +11,7 @@ export interface ConfigType { rawCraftMaxWeightPP: number; minSnipePP: number; minCraftPP: number; - ignoreCategories: { - weapon: boolean; - accessories: boolean; - armor: boolean; - misc: boolean; - blocks: boolean; - consumables: boolean; - } + ignoreCategories: Record; minSales: number; includeCraftCost: boolean; minPriceForRawcraft: number; diff --git a/src/sqlFunctions.ts b/src/sqlFunctions.ts index 496642c..d673b68 100644 --- a/src/sqlFunctions.ts +++ b/src/sqlFunctions.ts @@ -22,7 +22,7 @@ async function InitTable() { // Retrieve all rows const rows = await allQuery(db, 'SELECT * FROM users'); console.log('All rows:', rows); - } catch (err) { + } catch (err: any) { console.error('Database error:', err.message); } finally { db.close();