From cefba8c30c6967c8664f1c3827125ed92c0ea356 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 3 Apr 2021 14:15:10 -0700 Subject: [PATCH] Key Waka data by ID, not name --- api/allWakaValues.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/api/allWakaValues.js b/api/allWakaValues.js index 14e5e70..6c158f1 100644 --- a/api/allWakaValues.js +++ b/api/allWakaValues.js @@ -9,10 +9,14 @@ const beeline = require("honeycomb-beeline")({ import fetch from "node-fetch"; +import connectToDb from "../src/server/db"; + async function handle(req, res) { - let itemValues; + const allNcItemNamesAndIdsPromise = loadAllNcItemNamesAndIds(); + + let itemValuesByName; try { - itemValues = await loadWakaValues(); + itemValuesByName = await loadWakaValuesByName(); } catch (e) { console.error(e); res.setHeader("Content-Type", "text/plain"); @@ -20,6 +24,15 @@ async function handle(req, res) { 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 (name in itemValuesByName) { + itemValues[id] = itemValuesByName[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, @@ -32,7 +45,21 @@ async function handle(req, res) { return res.send(itemValues); } -async function loadWakaValues() { +async function loadAllNcItemNamesAndIds() { + const db = await connectToDb(); + + const [rows] = await db.query(` + SELECT items.id, item_translations.name FROM items + INNER JOIN item_translations ON item_translations.item_id = items.id + WHERE + items.rarity_index IN (0, 500) OR is_manually_nc = 1 + AND item_translations.locale = "en" + `); + + return rows; +} + +async function loadWakaValuesByName() { if (!process.env["GOOGLE_API_KEY"]) { throw new Error(`GOOGLE_API_KEY environment variable must be provided`); } @@ -63,12 +90,12 @@ async function loadWakaValues() { // Reformat the rows as a map from item name to value. We offer the item data // as an object with a single field `value` for extensibility, but we omit // the spreadsheet columns that we don't use on DTI, like Notes. - const itemValues = {}; + const itemValuesByName = {}; for (const [itemName, value] of rows) { - itemValues[itemName] = { value }; + itemValuesByName[itemName] = { value }; } - return itemValues; + return itemValuesByName; } export default async (req, res) => {