-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSRS.js
190 lines (178 loc) · 4.47 KB
/
OSRS.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var glob = require('glob');
var fs = require('fs');
var items = require('./items.json');
const osrs = require('osrs-wrapper');
const osbw = require('osbuddy-api-wrapper');
const api = new osbw({
cache: {
// The cache will expire in 15 minutes
'max-age': 15
}
});
class OSRS {
constructor() {
}
compareMargin(a, b) {
if (a.margin < b.margin) return -1;
if (a.margin > b.margin) return 1;
else return 0;
}
compareQtyMargin(a, b) {
if ((a['buying-quantity'] - a['selling-quantity']) < (b['buying-quantity'] - b['selling-quantity'])) return -1;
if ((a['buying-quantity'] - a['selling-quantity']) > (b['buying-quantity'] - b['selling-quantity'])) return 1;
else return 0;
}
Item(id, name) {
this.name = name;
this.id = id;
this.hist = [];
this.fetchData = async function () {
let obj = await api.item(this.id)
this.updateHist();
this.stashData(obj);
this.writeData(obj);
}
this.stashData = function (data) {
this.hist.push(data)
}
this.writeData = function (obj) {
if (fs.existsSync("./ge_items/" + this.name + ".json")) {
let itf = require("./ge_items/" + this.name + ".json");
itf.push(obj);
} else {
fs.writeFile("./ge_items/" + itemName + ".json", this.hist, err => {});
}
}
this.getData = function () {
this.updateHist();
return this.hist;
}
this.updateHist = function () {
if (fs.existsSync("./ge_items/" + this.name + ".json")) {
let itf = require("./ge_items/" + this.name + ".json");
this.hist = itf;
} else {
fs.writeFile("./ge_items/" + itemName + ".json", "[]", err => {});
this.hist = [];
}
}
}
getItemName(itemID) {
let itemName;
items.forEach(obj => {
if (obj.id == itemID) {
itemName = obj.name;
}
});
return itemName;
}
getItemID(itemName) {
let id;
console.log(itemName);
items.forEach(obj => {
console.log(obj);
if (obj.name.toUpperCase == itemName.toUpperCase) {
id = obj.id;
return id;
}
});
if (id != undefined) {
return id;
} else {
throw "item not found";
}
return;
}
async getItemObj(itemID) {
if (Number(itemID) == NaN) {
return this
} else {
let itemObj = await api.item(itemID);
return itemObj;
}
}
queryAll() {
api.names().then(names => {
saveQuery(names);
});
}
async saveQuery(names) {
for (j in names) {
try {
let itemID = names[j].id;
let itemName = await getItemName(itemID);
let itemObj = await getItemObj(itemID);
let date = new Date();
itemObj.name = itemName;
itemObj.qTime = date.getTime();
if (itemObj['buying-quantity'] == 0 || itemObj['selling-quantity'] == 0) {
ge.push(itemObj);
}
console.log(itemObj.name);
} catch (error) {
console.error(error);
fails.push({
ID: names[j].id,
Error: error
});
fs.writeFile("fails.json", JSON.stringify(fails), err => {})
}
}
console.log("writing final");
fs.writeFile("ge.json", JSON.stringify(ge), err => {
if (err != null) console.log(err);
});
}
async saveItem(itemObj, itemID) {
let itemName = await getItemName(itemID);
itemObj.name = itemName;
itemObj.qTime = d.getTime();
if (!fs.existsSync("./ge_items/" + itemName + ".json")) {
fs.writeFile("./ge_items/" + itemName + ".json", "[" + JSON.stringify(itemObj) + "]", err => {});
} else {
console.log("already exists");
let file = require("./ge_items/" + itemName + ".json");
file.push(itemObj);
file = removeDuplicates(file);
fs.writeFile("./ge_items/" + itemName + ".json", JSON.stringify(file), err => {
if (err != null) console.log(err);
});
}
}
async sortData(args) {
if (args == "margin") {
ge.sort(compareMargin);
await fs.writeFile("randoms.json", JSON.stringify(ge), err => console.log(err));
}
if (args == "qtymargin") {
ge.sort(compareQtyMargin);
await fs.writeFile("randoms.json", JSON.stringify(ge), err => console.log(err));
}
}
async wipe() {
glob.sync('./ge_items/*.json').forEach(function (file) {
fs.unlink(path.resolve(file), () => {});
});
}
getGrid(itmid, itmname) {
console.log("ITEM ID: " + itmid);
const sixHoursAgo = new Date();
sixHoursAgo.setHours(sixHoursAgo.getHours() - 6);
let obj = {
id: itmid,
interval: 30,
start: sixHoursAgo.getTime()
}
api.graph({
id: itmid,
interval: 30,
start: sixHoursAgo.getTime()
}).then(thing => {
let string = "Item: " + itmname;
for (d in thing) {
string = string + "\nBuying Complete: " + thing[d].buyingCompleted + "";
}
homeChannel.send(string);
});
}
}