-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinventory.js
102 lines (90 loc) · 3.03 KB
/
inventory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import fs from "fs";
import chalk from "chalk";
import { load } from "cheerio";
import { readFile } from "fs/promises";
export const processInventory = async () => {
console.log(chalk.yellow("Processing inventory history..."));
try {
const data = await readFile("./input/input.html", "utf8");
const $ = load(data);
const events = [];
const rows = $(".tradehistoryrow").toArray();
for (const row of rows) {
const date = $(row).find(".tradehistory_date").text().trim();
const time = $(row).find(".tradehistory_timestamp").text().trim();
const eventDescription = $(row).find(".tradehistory_event_description").text().trim();
const items = [];
$(row)
.find(".tradehistory_items_group")
.each((j, itemGroup) => {
$(itemGroup)
.find(".history_item")
.each((k, item) => {
const itemName = $(item).find(".history_item_name").text().trim();
if ($(item).data("appid") !== 730) return;
items.push(itemName);
});
});
if (["Unlocked a container", "Crafted"].includes(eventDescription)) {
const richEvent = await richEventInfo(eventDescription, items);
events.push({
date: date.split("\t")[0],
time,
...richEvent,
});
}
}
await fs.promises.writeFile("./data/inventory_history.json", JSON.stringify(events, null, 2));
console.log("Transactions successfully saved to data/inventory_history.json");
return {
success: true,
error: null,
};
} catch (error) {
console.error(chalk.red("Error processing inventory history:"), error);
return {
success: false,
error,
};
}
};
const richEventInfo = async (event, items) => {
if (event === "Unlocked a container") {
const caseName = items.find((item) => item.includes("Key")) ?? "";
const weapon = items.find((item) => item.includes(" | ")) ?? "";
return {
event: "Unboxed",
case: caseName.split("Key")[0]?.trim() ?? "",
weapon: weapon.split(" | ")[0]?.trim() ?? "",
skin: weapon.split(" | ")[1]?.trim() ?? "",
full_name: weapon,
rarity: await rarityLookup(
weapon.replace("StatTrak™ ", "").replace("Souvenir ", "").replace("★ ", "")
),
};
} else if (event === "Crafted") {
const winner = items[0];
return {
event: "Trade-up Contract",
winning_item: winner,
traded_items: items.slice(1),
};
}
};
const rarityLookup = async (skinName) => {
try {
const file_data_raw = await readFile("./data/case_data.json", "utf8");
const case_data = JSON.parse(file_data_raw);
for (const caseItem of case_data.Cases) {
for (const marketPlace of caseItem.MarketPlaces) {
const skin = marketPlace.Skins.find((s) => s.Name === skinName);
if (skin) return skin.Rarity;
}
}
} catch (err) {
console.error(
"Error reading or parsing case data file. Try running the update:cases script and try again."
);
}
return null;
};