added more types

This commit is contained in:
Ulysia 2024-12-27 22:58:39 +01:00
parent 96baf5eca1
commit d8dbe8ee97
8 changed files with 75 additions and 30 deletions

View file

@ -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<AuctionResponse>(
`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();

View file

@ -1,6 +1,6 @@
{
"data": {
"thread_count/speed": 48,
"worker_count": 48,
"minSnipeProfit": 900000,
"minAvgProfit": 500000,

View file

@ -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<string, ItemData> = {};
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<void> {
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<void> {
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;

View file

@ -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",

View file

@ -6,7 +6,7 @@ export interface ItemData {
stars: number;
rarity: string;
recomb: boolean;
enchants: string[];
enchants: Record<string, number>;
hpbs: number;
fpbs: number;
gemstones: string[];
@ -34,7 +34,7 @@ export class Item {
auctionID: string,
price: number,
rarity: string,
enchants: string[],
enchants: Record<string, number>,
hpbs: number,
fpbs: number,
recomb: boolean,

39
src/auctionType.ts Normal file
View file

@ -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
}

View file

@ -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<string, boolean>;
minSales: number;
includeCraftCost: boolean;
minPriceForRawcraft: number;

View file

@ -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();