Skip to content

Commit

Permalink
feat: ✨ add gram support to item data extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca180 committed Jan 17, 2025
1 parent ce3ea7d commit 76da70f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 14 deletions.
19 changes: 15 additions & 4 deletions pages/api/v1/items/[id_name]/drops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ export const getItemDrops = async (
item_iid: number,
isNC = false
): Promise<ItemOpenable | null> => {
const drops = await prisma.openableItems.findMany({
const itemProm = prisma.items.findFirst({
where: {
internal_id: item_iid,
},
});

const dropsProm = prisma.openableItems.findMany({
where: {
parent_iid: item_iid,
parent_item: {
Expand All @@ -104,6 +110,8 @@ export const getItemDrops = async (
},
});

const [item, drops] = await Promise.all([itemProm, dropsProm]);

const prizePools: { [name: string]: PrizePoolData } = {};
const dropsData: { [id: number]: ItemDrop } = {};
const poolsData: { [id: string]: { [note: string]: number } } = {};
Expand All @@ -121,6 +129,8 @@ export const getItemDrops = async (
return;
}

if (drop.notes?.includes('gramOption')) return;

openingSet[drop.opening_id] = [...(openingSet[drop.opening_id] ?? []), drop.item_iid];
});

Expand All @@ -131,7 +141,8 @@ export const getItemDrops = async (
const manualItems: number[] = [];
let isChoice = false;
let isZoneCat = false;
let isGram = false;
let isGram = !!item?.name.toLowerCase().split(' ').includes('gram');

drops
.sort((a, b) => (a.notes?.length ?? 0) - (b.notes?.length ?? 0))
.map((drop) => {
Expand Down Expand Up @@ -345,9 +356,9 @@ export const getItemDrops = async (
openableData.hasLE = true;
}

let dropRate = (drop.dropRate / itemDropCount) * 100;
let dropRate = isGram ? 0 : (drop.dropRate / itemDropCount) * 100;

if (drop.dropRate / pool.openings >= 1) {
if (drop.dropRate / pool.openings >= 1 && !isGram) {
if ([65354, 17434, 860].includes(drop.item_iid)) openableData.isGBC = true;
dropRate = 100;
}
Expand Down
28 changes: 24 additions & 4 deletions pages/api/v1/items/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const POST = async (req: NextApiRequest, res: NextApiResponse) => {
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
const items = data.items;
const parentItem = data.parentItem;
const gramInfo = data.gramInfo;
const lang = data.lang;

if (lang !== 'en') return res.status(400).json({ error: 'Language not supported' });
Expand Down Expand Up @@ -59,7 +60,7 @@ const POST = async (req: NextApiRequest, res: NextApiResponse) => {
name_image_id: [...name_image_id, parent_name_image_id],
});

const opening_id = chance.hash({ length: 15 });
const opening_id = gramInfo?.cash_id || chance.hash({ length: 15 });

const ip_address = requestIp.getClientIp(req);

Expand Down Expand Up @@ -93,9 +94,28 @@ const POST = async (req: NextApiRequest, res: NextApiResponse) => {
});

const openableItems = (await Promise.all(openableItemsPromise)).filter(
(item) => item,
(item) => item
) as Prisma.OpenableItemsUncheckedCreateInput[];

if (gramInfo.options.length) {
const itemNameData = await getManyItems({
name: gramInfo.options as string[],
});

Object.values(itemNameData).map((itemData) => {
const data = {
opening_id: opening_id,
item_iid: itemData.internal_id,
parent_iid: parentData.internal_id,
limitedEdition: false,
notes: 'gramOption',
ip_address: ip_address,
};

openableItems.push(data);
});
}

const openableItemData = await prisma.openableItems.createMany({
data: openableItems,
});
Expand Down Expand Up @@ -168,7 +188,7 @@ export const processOpenableItems = async (openableItem: OpenableQueue) => {
});

const openableItems = (await Promise.all(openableItemsPromise)).filter(
(item) => item,
(item) => item
) as Prisma.OpenableItemsUncheckedCreateInput[];

const openableItemData = await prisma.openableItems.createMany({
Expand All @@ -182,7 +202,7 @@ const addToQueue = async (
item: any,
parent: any,
opening_id: string,
ip_address: string | null,
ip_address: string | null
) => {
return prisma.openableQueue.create({
data: {
Expand Down
81 changes: 75 additions & 6 deletions userscripts/itemDataExtractor.user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name itemdb - Item Data Extractor
// @version 1.8.1
// @version 1.9.0
// @author itemdb
// @namespace itemdb
// @description Feeds itemdb.com.br with neopets item data
Expand Down Expand Up @@ -46,6 +46,11 @@ const originalFetch = window.fetch;

let resItemData = [];

const script_info = {
version: GM_info.script.version,
versionCode: Number(GM_info.script.version.replaceAll(".", ""))
}

const itemdb_script = function() {
if(typeof $ === 'undefined') return;
// Check if we are on the beta site
Expand Down Expand Up @@ -999,6 +1004,64 @@ const itemdb_script = function() {
});
}

function handleNCGram() {
$(document).on('ajaxSuccess.ncGram', () => {
const isGram = $(".popup-body__2020 input[name='popup_title']").val()?.toLowerCase().includes("gram") || $("#invItemName").text()?.toLowerCase().includes("gram");
if(!isGram) return;
const itemName = $("#invItemName").text();
const image = $(".inv-itemLg").css("background-image").replace(/^url\(['"](.+)['"]\)/, '$1')

const parentItem = {
name: itemName,
img: image
}

const gramOptions = $("#giftItem-1 option").map(function() {
if($(this).val() < 0) return;
return $(this).text()
}).get();

const gramInfo = {
options: gramOptions,
cash_id: $(".popup-body__2020 input[name='cash_obj_id']").val()
}

const submitButton = $('#iteminfo_select_action > div');
if(!submitButton.length) return;

$(document).off(`ajaxSuccess.ncGram`);

const JQUERY3 = jQuery;
const J$ = $;
let _interval;
submitButton.on(`click.action`, function(){
submitButton.off(`click.action`);

$(document).on(`ajaxSuccess.actionSuccess`, () => {
const result = $('#invResult');

// neopets changes the jquery object and breaks everything.
if(_interval) clearInterval(_interval);
_interval = setInterval(() => {$ = J$; jQuery = JQUERY3;}, 100);

if(!result.length) return;

const item = {
name: result.find('b').first().text(),
img: result.find("img").attr('src'),
isLE: false,
notes: 'gram',
};

$(document).off(`ajaxSuccess.actionSuccess`);

console.log([item], parentItem, gramInfo)
submitOpenable([item], parentItem, gramInfo)
})
})
});
}

function handleNPOpenables(){
const pageLoad = Date.now();
$(document).ajaxSuccess(() => {
Expand Down Expand Up @@ -1119,6 +1182,7 @@ const itemdb_script = function() {
handleNCCapsule();
handleNPOpenables();
handleNPRefresh();
handleNCGram();
}
if (!URLHas('inventory')) discardPrevInventory();
if (URLHas('safetydeposit')) handleSDB();
Expand Down Expand Up @@ -1170,6 +1234,7 @@ const itemdb_script = function() {
url: 'https://itemdb.com.br/api/v1/items',
headers: {
'Content-Type': 'application/json',
'itemdb-version': script_info.versionCode,
},
data: JSON.stringify(rawData),
onload: function (res) {
Expand Down Expand Up @@ -1203,6 +1268,7 @@ const itemdb_script = function() {
url: 'https://itemdb.com.br/api/v1/prices',
headers: {
'Content-Type': 'application/json',
'itemdb-version': script_info.versionCode,
},
data: JSON.stringify(rawData),
onload: function (res) {
Expand Down Expand Up @@ -1236,6 +1302,7 @@ const itemdb_script = function() {
url: 'https://itemdb.com.br/api/v1/trades',
headers: {
'Content-Type': 'application/json',
'itemdb-version': script_info.versionCode,
},
data: JSON.stringify(rawData),
onload: function (res) {
Expand All @@ -1251,23 +1318,28 @@ const itemdb_script = function() {
})
}

async function submitOpenable(items, parentItem) {
async function submitOpenable(items, parentItem, gramInfo) {
if(checkTranslation()) return;

const hash = idb.getTradesHash({items, parentItem, gramInfo});

const pageLang = GM_getValue('pageLang', 'unknown');
if(pageLang !== nl) return console.error('Language error');

const rawData = {
lang: pageLang,
items: items,
parentItem: parentItem,
gramInfo: gramInfo,
hash: hash,
}

GM_xmlhttpRequest({
method: 'POST',
url: 'https://itemdb.com.br/api/v1/items/open',
headers: {
'Content-Type': 'application/json',
'itemdb-version': script_info.versionCode,
},
data: JSON.stringify(rawData),
onload: function (res) {
Expand Down Expand Up @@ -1340,7 +1412,4 @@ if (URLHas('/stylingchamber/')) watchFetchItemRequests('userStyles');
if (URLHas('customise')) watchItemRequests('editordata');

// for troubleshooting use
unsafeWindow.itemdb_script = {
version: GM_info.script.version,
versionCode: Number(GM_info.script.version.replaceAll(".", ""))
}
unsafeWindow.itemdb_script = script_info;

0 comments on commit 76da70f

Please sign in to comment.