-
Notifications
You must be signed in to change notification settings - Fork 4
/
Neopets - SDB Inventarium.user.js
353 lines (322 loc) · 14.5 KB
/
Neopets - SDB Inventarium.user.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// ==UserScript==
// @name Neopets - SDB Inventarium <MettyNeo>
// @version 0.3
// @description Allows you to view all SDB items at once, displaying additional info and allowing for more detailed search and sorts.
// @author Metamagic
// @match https://www.neopets.com/safetydeposit.phtml*
// @match https://www.neopets.com/inventory.phtml*
// @match https://www.neopets.com/quickstock.phtml*
// @icon https://i.imgur.com/RnuqLRm.png
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
/* TO-DO:
- compat with sdb dump script
- display
- display override on main page
- item display - variable row size & detail lvls
- 'back to vanilla' button
- page selection / offset - variable page size
- gui for moving pages
- sorting system
- gui for sort
- search system
- gui for search
- record custom sdb removes
*/
//some items have duplicate names and thus we can't determine which of two ids an item is from its name alone
const DUPLICATE_NAMES = [
"Piece of a treasure map",
"Spooky Treasure Map",
"Space Map",
"Candy Cane Sword",
"Christmas Baby Blu",
"Cloud Umbrella",
"Counting Babaas",
"Fire Umbrella",
"Kass Basher Flag",
"Lost Desert Palm Tree",
"Lunch Tray",
"Rainbow Umbrella",
"Retired Altador Cup Mystery Capsule",
"Retired Dyeworks Mystery Capsule",
"Retired Wonderclaw Mystery Capsule",
"Shining Star Gift Box",
"Snowflake Garland",
"Sparkling Yellow Mystery Capsule",
"The Darkest Faerie (TCG)",
"Usuki Dream Car",
"Usuki Dream Jetski",
"Yooyuball Tattoo",
"Basic Gift Box"
]
let clicked = false //prevents double-inputs
const url = document.location.href
if(url.includes("safetydeposit.phtml")) {
recordSDBPage()
removeVanillaSDB()
addFQCheck()
}
else if(url.includes("quickstock.phtml")) {
depositQuickStock()
}
else if(url.includes("inventory.phtml")) {
depositInventory()
}
function addFQCheck() {
let row = $("#content > table > tbody > tr > td.content > form > table:nth-child(1) > tbody > tr > td:nth-child(2)")[0]
let b = document.createElement("button")
b.innerHTML = "Find r102-r179 Items"
b.addEventListener("click", (e)=>{e.preventDefault();e.stopPropagation();getFQItems();})
row.appendChild(b)
}
function getFQItems() {
let sdb = Array.from(Object.values(GM_getValue("sdbdata"))).filter((sdbitem) => {
return sdbitem.rarity > 101 && sdbitem.rarity < 180
}).sort((a, b) => {
return b.count - a.count
})
let count = sdb.map((a)=>{return a.count}).reduce((a, b) => {return a + b}, 0)
//format list
let str = `<b><u>Results:</u> (Total: ${count})</b>`
for(const item of sdb) {
str += `<br>\t- <a href="/safetydeposit.phtml?obj_name=${encodeURIComponent(item.name)}&category=0">${item.name}</a> <small>x ${item.count}</small>`
}
let display = document.createElement("p")
display.innerHTML = str
$("#content td.content")[0].insertBefore(display, $("#content > table > tbody > tr > td.content > table")[0])
console.log("Items displayed.")
}
//this is technically illegal btw
//todo: remove this
function autoNavigate() {
console.log("Attempting to navigate to next page...")
$("#content > table > tbody > tr > td.content > table > tbody > tr > td:nth-child(3) > a")?.[0]?.click()
}
//==============
// parsing pages
//==============
function recordSDBPage() {
let rows = Array.from($("#content td.content > form > table:nth-of-type(2) > tbody > tr:not(:first-child):not(:last-child)"))
console.log(`[SDB] Reading data for ${rows.length} items.`)
let pagedata = {}
for(const tr of rows) {
let rowdata = parseRowData(tr)
pagedata[JSON.stringify(rowdata[0])] = rowdata[1]
}
getItemDBData(pagedata)
}
function parseRowData(tr) {
let name = tr.querySelector(" td:nth-child(2) > b").childNodes[0].nodeValue
let img = tr.querySelector("td:nth-child(1) > img").getAttribute("src").match(/.*?images\.neopets\.com\/items\/(.+?)\..+/)[1]
let desc = tr.querySelector("td:nth-child(3) > i").innerHTML
let type = tr.querySelector("td:nth-child(4) > b").innerHTML
let count = parseInt(tr.querySelector("td:nth-child(5) > b").innerHTML)
let id = parseInt(tr.querySelector("td:nth-child(6) > input").getAttribute("name").match(/back_to_inv\[([0-9]+)\]/)[1])
let tag = tr.querySelector("span.medText font") || null
let searchhelper = tr.querySelector("td:nth-child(2) > p.search-helper") || null //for compatibility with dice's search helper script
return [id,{name:name, image_id:img, desc:desc, category:type, count:count, tag: tag, searchhelper:searchhelper, id:id}]
}
function depositQuickStock() {
let submit = $(`#content td.content > form tr:last-child input[type=submit]:nth-child(1)`)?.[0]
if(submit) {
submit.addEventListener("click", (event) => {
//event.preventDefault()
//event.stopPropagation()
if(!clicked) {
let selected = Array.from($(`form[name="quickstock"] tr:not(:nth-last-child(-n+2)) input:checked`)).slice(0,70) //quick stock can only do 70 at a time!
let deposited = selected.filter((e) => {return e.value == "deposit"}).map((e) => {return e.parentElement.parentElement.querySelector("td:first-child").childNodes[0].nodeValue})
if(deposited.length > 0) {
console.log(`[SDB] Recording deposit of ${deposited.length} items...`)
recordItemListToSDB(deposited)
}
clicked = true
}
})
console.log("[SDB] Watching quick stock for deposits...")
}
}
function depositInventory() {
const resultObs = new MutationObserver((mutations) => {
if($("#invResult > div.popup-body__2020 > p")[0].innerHTML.includes("into your safety deposit box!")) {
//get name and img to identify item
let name = $("#invResult > div.popup-body__2020 > p > b")[0].innerHTML
let img = $("#invResult > div.popup-body__2020 > div > img")[0].getAttribute("src").match(/.*?images\.neopets\.com\/items\/(.+?)\..+/)[1]
console.log(`[SDB] Recording ${name} deposit...`)
//check if we already have info on this in sdb
let res = Object.values(GM_getValue("sdbdata", {})).find((i) => {return i.name == name && i.image_id == img})
//already have info, increment count
if(res != null) {
res.count += 1
GM_setValue("sdbdata", res)
console.log(`[SDB] Item count incremented.`)
}
//don't have info, get info from itemdb
else getItemDBDataFromPair([name, img])
}
})
resultObs.observe($("#invResult")[0], {characterData: true, childList: true, subtree: true})
console.log("[SDB] Watching inventory for deposits...")
}
//necessary for if items are fully removed and need to be removed from the sdbdata list
function removeVanillaSDB() {
//remove one - simply update count
$("#content td.content > form > table:nth-child(3) > tbody").on("click", "td:nth-child(6) > a", function (event) {
//event.stopPropagation()
//event.preventDefault()
if(!clicked) {
let id = parseInt(this.parentElement.querySelector("input").getAttribute("name").match(/back_to_inv\[([0-9]+)\]/)[1])
let sdb = GM_getValue("sdbdata", {})
sdb[id].count -= 1
console.log(`[SDB] Removed 1 ${sdb[id].name} from SDB.`)
if(sdb[id].count == 0) delete sdb[id]
GM_setValue("sdbdata", sdb)
clicked = true
}
})
//move selected items
$("#content td.content > form input.submit_data")[0].addEventListener("click", (event) => {
if(!clicked) {
let changeditems = []
for(const input of Array.from($("input.remove_safety_deposit"))) {
let count = input.value
//for each input that we're removing something from, decrease the count
if(count > 0) {
let sdb = GM_getValue("sdbdata", {})
let id = input.getAttribute("name").match(/back_to_inv\[([0-9]+)\]/)[1]
sdb[id].count -= count
changeditems.push([sdb[id].name, count])
if(sdb[id].count == 0) delete sdb[id]
GM_setValue("sdbdata", sdb)
}
}
if(changeditems.length > 0) {
console.log("[SDB] The following items were removed:")
for(const item of changeditems) console.log(`\t- ${item[0]} (x${item[1]})`)
}
clicked = true
}
})
}
//==============
// sdb item data
//==============
function savePageData(pagedata) {
//saves data
let sdbdata = GM_getValue("sdbdata", {})
for(const item of Object.keys(pagedata)) {
sdbdata[item] = pagedata[item]
}
GM_setValue("sdbdata", sdbdata)
let items = Object.keys(sdbdata).length
let qty = Object.values(sdbdata).reduce((a, b) => {return a + b.count}, 0)
//shows item count recorded so far
//todo: store these counts as a variable before page modifications
if(url.includes("safetydeposit.phtml")) {
let itemcounts = $("#content td.content > table > tbody > tr > td:nth-child(2)")[0].innerHTML.match(/<b>Items:<\/b> (.+?) \| <b>Qty:<\/b> (.+)/)
console.log(`[SDB] SDB page recorded. (Items: ${items}/${itemcounts[1].replaceAll(",","")}, Qty: ${qty}/${itemcounts[2].replaceAll(",","")})`)
//autoNavigate()
}
else console.log(`[SDB] SDB page recorded. (Items: ${items}, Qty: ${qty})`)
}
function recordItemListToSDB(itemnames) {
let toitemdb = {}
let uncertain = GM_getValue("uncertainitems", {})
let sdbdata = GM_getValue("sdbdata", {})
//handles each item to be deposited
let updated = 0
for(const name of itemnames) {
let item = getSDBItemByName(name)
if(Object.keys(item).length == 0) { //we don't have item info, let's grab it from itemdb
if(toitemdb[name]) toitemdb[name] += 1
else toitemdb[name] = 1
}
else if(item == null) { //multiple items with the same name, can't tell item without manual checking
uncertain[name] = undefined
console.log(`[SDB] WARNING: Can't determine item ID from item with duplicate name! (${name})`)
}
else {
Object.values(sdbdata).find((i) => {return i.name == name}).count += 1 //increment count in sdb
updated += 1
}
}
GM_setValue("sdbdata", sdbdata)
GM_setValue("uncertainitems", uncertain)
if(updated > 1) console.log(`[SDB] Updated SDB item quantity for ${updated} deposited items.`)
else if(updated == 1) console.log(`[SDB] Updated SDB item quantity for ${updated} deposited item.`)
if(Object.keys(toitemdb).length > 0) getItemDBDataFromNames(toitemdb) //fetches data for items from db
}
function getSDBItemByName(name) {
//can't be certain of items that share name, eg map pieces
if(isDuplicateName(name)) return null
return Object.values(GM_getValue("sdbdata", {})).find((i) => {return i.name == name}) || {}
}
function isDuplicateName(name) {
return name.includes("Map Piece") || name.includes("Laboratory Map") || DUPLICATE_NAMES.includes(name)
}
//================
// itemdb requests
//================
function parseItemDBResponse(data, itemlist, pair=false) {
if(Object.keys(data).length < itemlist.length) console.log(`[SDB] WARNING: ItemDB only returned ${Object.keys(data).length} items. Make sure to submit data to itemDB using their userscript!`)
let pagedata = {}
for(let name of Object.keys(data)) {
let key = data[name].item_id
pagedata[key] = {}
pagedata[key].name = data[name].name
pagedata[key].image_id = data[name].image_id
pagedata[key].category = data[name].category
pagedata[key].desc = data[name].description
pagedata[key].tag = data[name].specialType
pagedata[key].rarity = data[name].rarity
pagedata[key].searchhelper = null
pagedata[key].estVal = data[name].estVal
pagedata[key].isNC = data[name].isNC
pagedata[key].isWearable = data[name].isWearable
pagedata[key].isNeohome = data[name].isNeohome
pagedata[key].isNoTrade = data[name].status == "no trade"
pagedata[key].priceData = data[name].price
pagedata[key].owlsData = data[name].owls
if(pair) pagedata[key].count = 1
else pagedata[key].count = itemlist[name]
}
console.log(`[SDB] Item data for ${Object.keys(data).length} items grabbed from ItemDB. Thanks Magnetismo Times!`)
savePageData(pagedata)
}
function getItemDBData(pagedata) {
let itemlist = Object.keys(pagedata)
$.get("https://itemdb.com.br/api/v1/items/many", {item_id:itemlist}, (data, status)=>{
if(Object.keys(data).length < itemlist.length) console.log(`[SDB] WARNING: ItemDB only returned ${Object.keys(data).length} items, expected ${itemlist.length} items.`)
for(const key of Object.keys(data)) {
//adds data from itemdb
pagedata[key].rarity = data[key].rarity
pagedata[key].estVal = data[key].estVal
pagedata[key].isNC = data[key].isNC
pagedata[key].isWearable = data[key].isWearable
pagedata[key].isNeohome = data[key].isNeohome
pagedata[key].isNoTrade = data[key].status == "no trade"
pagedata[key].priceData = data[key].price
pagedata[key].owlsData = data[key].owls
}
console.log(`[SDB] Additional item data for ${Object.keys(data).length} items grabbed from ItemDB. Thanks Magnetismo Times!`)
savePageData(pagedata)
})
}
function getItemDBDataFromNames(itemlist) {
console.log(`[SDB] Fetching item data from ItemDB for ${Object.keys(itemlist).length} items.`)
$.get("https://itemdb.com.br/api/v1/items/many", {name:Object.keys(itemlist)}, (data) => {
parseItemDBResponse(data, itemlist)
})
}
function getItemDBDataFromPair(pair) {
console.log(`[SDB] Fetching item data from ItemDB for ${pair[0]}.`)
$.get("https://itemdb.com.br/api/v1/items/many", {name_image_id:[pair]}, (data) => {
parseItemDBResponse(data, [pair[0]], true)
})
}
/*
//back_to_inv%5B1%5D=1&back_to_inv%5B192%5D=1&obj_name=&category=0&offset=0
$.post("/process_safetydeposit.phtml?checksub=scan", "back_to_inv%5B1%5D=1&back_to_inv%5B478%5D=1&obj_name=&category=0&offset=0", (data, status) => {
console.log("status: "+status)
})
*/