2022-09-04 01:35:05 -07:00
|
|
|
/**
|
|
|
|
* /api/allNCTradeValues returns all the NC trade values OWLS has!
|
|
|
|
*
|
|
|
|
* NOTE: We no longer use API endpoint as a caching layer for individual item
|
|
|
|
* data requests. See `nc-trade-values.js` for the new system we use!
|
|
|
|
* This endpoint is therefore deprecated and might vanish.
|
|
|
|
*/
|
2022-08-15 18:39:29 -07:00
|
|
|
const beeline = require("honeycomb-beeline")({
|
|
|
|
writeKey: process.env["HONEYCOMB_WRITE_KEY"],
|
|
|
|
dataset:
|
|
|
|
process.env["NODE_ENV"] === "production"
|
|
|
|
? "Dress to Impress (2020)"
|
|
|
|
: "Dress to Impress (2020, dev)",
|
|
|
|
serviceName: "impress-2020-gql-server",
|
|
|
|
});
|
|
|
|
|
|
|
|
import fetch from "node-fetch";
|
|
|
|
|
|
|
|
import connectToDb from "../../src/server/db";
|
|
|
|
|
|
|
|
async function handle(req, res) {
|
|
|
|
const allNcItemNamesAndIdsPromise = loadAllNcItemNamesAndIds();
|
|
|
|
|
|
|
|
let itemValuesByIdOrName;
|
|
|
|
try {
|
|
|
|
itemValuesByIdOrName = await loadOWLSValuesByIdOrName();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
res.setHeader("Content-Type", "text/plain; charset=utf8");
|
|
|
|
res.status(500).send("Error loading OWLS Pricer data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restructure the value data to use IDs as keys, instead of names.
|
|
|
|
const allNcItemNamesAndIds = await allNcItemNamesAndIdsPromise;
|
|
|
|
const itemValues = {};
|
|
|
|
for (const { name, id } of allNcItemNamesAndIds) {
|
|
|
|
if (id in itemValuesByIdOrName) {
|
|
|
|
itemValues[id] = itemValuesByIdOrName[id];
|
|
|
|
} else if (name in itemValuesByIdOrName) {
|
|
|
|
itemValues[id] = itemValuesByIdOrName[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache for 1 minute, and immediately serve stale data for a day after.
|
|
|
|
// This should keep it fast and responsive, and stay well within our API key
|
|
|
|
// limits. (This will cause the client to send more requests than necessary,
|
|
|
|
// but the CDN cache should generally respond quickly with a small 304 Not
|
|
|
|
// Modified, unless the data really did change.)
|
|
|
|
res.setHeader(
|
|
|
|
"Cache-Control",
|
2024-02-20 16:27:05 -08:00
|
|
|
"public, max-age=3600, stale-while-revalidate=86400",
|
2022-08-15 18:39:29 -07:00
|
|
|
);
|
|
|
|
return res.send(itemValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadAllNcItemNamesAndIds() {
|
|
|
|
const db = await connectToDb();
|
|
|
|
|
|
|
|
const [rows] = await db.query(`
|
2024-02-20 16:27:05 -08:00
|
|
|
SELECT items.id, items.name FROM items
|
|
|
|
WHERE items.rarity_index IN (0, 500) OR is_manually_nc = 1
|
2022-08-15 18:39:29 -07:00
|
|
|
`);
|
|
|
|
|
|
|
|
return rows.map(({ id, name }) => ({ id, name: normalizeItemName(name) }));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all OWLS Pricer values from the spreadsheet. Returns an object keyed by
|
|
|
|
* ID or name - that is, if the item ID is provided, we use that as the key; or
|
|
|
|
* if not, we use the name as the key.
|
|
|
|
*/
|
|
|
|
async function loadOWLSValuesByIdOrName() {
|
|
|
|
const res = await fetch(
|
2024-02-20 16:27:05 -08:00
|
|
|
`https://neo-owls.herokuapp.com/itemdata/owls_script/`,
|
2022-08-15 18:39:29 -07:00
|
|
|
);
|
|
|
|
const json = await res.json();
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(
|
2024-02-20 16:27:05 -08:00
|
|
|
`Could not load OWLS Pricer data: ${res.status} ${res.statusText}`,
|
2022-08-15 18:39:29 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const itemValuesByIdOrName = {};
|
2022-08-15 23:57:33 -07:00
|
|
|
for (const [itemName, valueText] of Object.entries(json)) {
|
2022-08-15 18:39:29 -07:00
|
|
|
// OWLS returns an empty string for NC Mall items they don't have a trade
|
|
|
|
// value for, to allow the script to distinguish between NP items vs
|
|
|
|
// no-data NC items. We omit it from our data instead, because our UI is
|
|
|
|
// already aware of whether the item is NP or NC.
|
2022-08-15 23:57:33 -07:00
|
|
|
if (valueText.trim() === "") {
|
2022-08-15 18:39:29 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: OWLS doesn't currently provide item IDs ever. Add support for it
|
|
|
|
// if it does! (I'm keeping the rest of the code the same because I
|
2022-08-15 23:57:33 -07:00
|
|
|
// think that might happen for disambiguation, like Waka did.) Until
|
|
|
|
// then, we just always key by name.
|
2022-08-16 13:59:10 -07:00
|
|
|
// HACK: With the exception of the Butterfly Dress, which has a special
|
|
|
|
// name in the OWLS database! We hardcodily disambiguate it here. But
|
|
|
|
// if they start serving item IDs, that would resolve it too!
|
|
|
|
let nameOrId;
|
|
|
|
if (itemName === "Butterfly Dress") {
|
|
|
|
nameOrId = 44775;
|
|
|
|
} else if (itemName === "Butterfly Dress (from Faerie Festival event)") {
|
|
|
|
nameOrId = 76073;
|
|
|
|
} else {
|
|
|
|
nameOrId = normalizeItemName(itemName);
|
|
|
|
}
|
2022-08-15 23:57:33 -07:00
|
|
|
|
|
|
|
// We wrap it in an object with the key `valueText`, just to not break
|
|
|
|
// potential external consumers of this endpoint if we add more fields.
|
|
|
|
// (This is kinda silly and unnecessary, but it should get gzipped out and
|
|
|
|
// shouldn't add substantial time to building or parsing, so like w/e!)
|
2022-08-16 13:59:10 -07:00
|
|
|
itemValuesByIdOrName[nameOrId] = { valueText };
|
2022-08-15 18:39:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return itemValuesByIdOrName;
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeItemName(name) {
|
|
|
|
return (
|
|
|
|
name
|
|
|
|
// Remove all spaces, they're a common source of inconsistency
|
|
|
|
.replace(/\s+/g, "")
|
|
|
|
// Lower case, because capitalization is another common source
|
|
|
|
.toLowerCase()
|
|
|
|
// Remove diacritics: https://stackoverflow.com/a/37511463/107415
|
|
|
|
.normalize("NFD")
|
|
|
|
.replace(/[\u0300-\u036f]/g, "")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleWithBeeline(req, res) {
|
|
|
|
beeline.withTrace(
|
|
|
|
{ name: "api/allNCTradeValues", operation_name: "api/allNCTradeValues" },
|
2024-02-20 16:27:05 -08:00
|
|
|
() => handle(req, res),
|
2022-08-15 18:39:29 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default handleWithBeeline;
|