2021-02-02 22:26:55 -08:00
|
|
|
import beeline from "honeycomb-beeline";
|
|
|
|
import fetch from "node-fetch";
|
2020-08-20 22:25:41 -07:00
|
|
|
|
2020-04-25 04:33:05 -07:00
|
|
|
function capitalize(str) {
|
|
|
|
return str[0].toUpperCase() + str.slice(1);
|
|
|
|
}
|
|
|
|
|
2020-05-23 12:47:06 -07:00
|
|
|
function getPoseFromPetState(petState) {
|
|
|
|
const { moodId, female, unconverted } = petState;
|
|
|
|
|
|
|
|
if (unconverted) {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "UNCONVERTED";
|
2020-05-23 12:47:06 -07:00
|
|
|
} else if (moodId == null || female == null) {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "UNKNOWN";
|
2020-05-23 12:47:06 -07:00
|
|
|
} else if (String(moodId) === "1" && String(female) === "0") {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "HAPPY_MASC";
|
2020-05-23 12:47:06 -07:00
|
|
|
} else if (String(moodId) === "1" && String(female) === "1") {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "HAPPY_FEM";
|
2020-05-23 12:47:06 -07:00
|
|
|
} else if (String(moodId) === "2" && String(female) === "0") {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "SAD_MASC";
|
2020-05-23 12:47:06 -07:00
|
|
|
} else if (String(moodId) === "2" && String(female) === "1") {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "SAD_FEM";
|
2020-05-23 12:47:06 -07:00
|
|
|
} else if (String(moodId) === "4" && String(female) === "0") {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "SICK_MASC";
|
2020-05-23 12:47:06 -07:00
|
|
|
} else if (String(moodId) === "4" && String(female) === "1") {
|
2020-05-23 11:48:53 -07:00
|
|
|
return "SICK_FEM";
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`could not identify pose: ` +
|
|
|
|
`moodId=${moodId}, ` +
|
2020-05-23 12:47:06 -07:00
|
|
|
`female=${female}, ` +
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
`unconverted=${unconverted}`,
|
2020-05-23 11:48:53 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 00:32:17 -07:00
|
|
|
function getPetStateFieldsFromPose(pose) {
|
|
|
|
if (pose === "UNCONVERTED") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: null, female: null, unconverted: true, labeled: true };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else if (pose === "UNKNOWN") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: null, female: null, unconverted: false, labeled: false };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else if (pose === "HAPPY_MASC") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: "1", female: false, unconverted: false, labeled: true };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else if (pose === "HAPPY_FEM") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: "1", female: true, unconverted: false, labeled: true };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else if (pose === "SAD_MASC") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: "2", female: false, unconverted: false, labeled: true };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else if (pose === "SAD_FEM") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: "2", female: true, unconverted: false, labeled: true };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else if (pose === "SICK_MASC") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: "4", female: false, unconverted: false, labeled: true };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else if (pose === "SICK_FEM") {
|
2020-10-03 04:05:25 -07:00
|
|
|
return { moodId: "4", female: true, unconverted: false, labeled: true };
|
2020-08-31 00:32:17 -07:00
|
|
|
} else {
|
|
|
|
throw new Error(`unexpected pose ${pose}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const POSE_NAMES = {
|
|
|
|
HAPPY_MASC: "Happy Masc",
|
|
|
|
SAD_MASC: "Sad Masc",
|
|
|
|
SICK_MASC: "Sick Masc",
|
|
|
|
HAPPY_FEM: "Happy Fem",
|
|
|
|
SAD_FEM: "Sad Fem",
|
|
|
|
SICK_FEM: "Sick Fem",
|
|
|
|
UNCONVERTED: "Unconverted",
|
|
|
|
UNKNOWN: "Unknown",
|
|
|
|
};
|
|
|
|
|
|
|
|
function getPoseName(pose) {
|
|
|
|
return POSE_NAMES[pose];
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:23:56 -07:00
|
|
|
function getRestrictedZoneIds(zonesRestrict) {
|
|
|
|
const restrictedZoneIds = [];
|
|
|
|
for (const [i, bit] of Array.from(zonesRestrict).entries()) {
|
|
|
|
if (bit === "1") {
|
|
|
|
restrictedZoneIds.push(i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return restrictedZoneIds;
|
|
|
|
}
|
|
|
|
|
2020-08-20 23:23:33 -07:00
|
|
|
async function loadBodyName(bodyId, db) {
|
|
|
|
if (String(bodyId) === "0") {
|
|
|
|
return "All bodies";
|
|
|
|
}
|
|
|
|
|
|
|
|
const [rows] = await db.execute(
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
`SELECT pt.body_id, s.name AS species_name,
|
|
|
|
c.name AS color_name, c.standard FROM pet_types pt
|
|
|
|
INNER JOIN species s ON pt.species_id = s.id
|
2020-08-20 23:23:33 -07:00
|
|
|
INNER JOIN colors c ON c.id = pt.color_id
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
WHERE pt.body_id = ?
|
|
|
|
ORDER BY c.standard DESC, color_name, species_name LIMIT 1;`,
|
|
|
|
[bodyId],
|
2020-08-20 23:23:33 -07:00
|
|
|
);
|
|
|
|
const row = normalizeRow(rows[0]);
|
|
|
|
const speciesName = capitalize(row.speciesName);
|
|
|
|
const colorName = row.standard ? "Standard" : capitalize(row.colorName);
|
|
|
|
return `${colorName} ${speciesName}`;
|
|
|
|
}
|
|
|
|
|
2020-08-20 22:25:41 -07:00
|
|
|
async function logToDiscord(body) {
|
|
|
|
const span = beeline.startSpan({ name: "logToDiscord" });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch(process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"], {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
const resText = await res.text();
|
|
|
|
throw new Error(
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
`Discord returned ${res.status} ${res.statusText}: ${resText}`,
|
2020-08-20 22:25:41 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
beeline.finishSpan(span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 12:47:06 -07:00
|
|
|
function normalizeRow(row) {
|
|
|
|
const normalizedRow = {};
|
|
|
|
for (let [key, value] of Object.entries(row)) {
|
|
|
|
key = key.replace(/_([a-z])/gi, (m) => m[1].toUpperCase());
|
|
|
|
if ((key === "id" || key.endsWith("Id")) && typeof value === "number") {
|
|
|
|
value = String(value);
|
|
|
|
}
|
|
|
|
normalizedRow[key] = value;
|
|
|
|
}
|
|
|
|
return normalizedRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
capitalize,
|
|
|
|
getPoseFromPetState,
|
2020-08-31 00:32:17 -07:00
|
|
|
getPetStateFieldsFromPose,
|
|
|
|
getPoseName,
|
2020-08-31 19:23:56 -07:00
|
|
|
getRestrictedZoneIds,
|
2020-08-20 23:23:33 -07:00
|
|
|
loadBodyName,
|
2020-08-20 22:25:41 -07:00
|
|
|
logToDiscord,
|
2020-05-23 12:47:06 -07:00
|
|
|
normalizeRow,
|
2021-02-02 17:51:54 -08:00
|
|
|
|
|
|
|
// For Apollo's @cacheControl maxAge: time in seconds.
|
|
|
|
oneWeek: 604800,
|
|
|
|
oneDay: 86400,
|
|
|
|
oneHour: 3600,
|
2021-02-02 19:07:38 -08:00
|
|
|
oneMinute: 60,
|
2020-05-23 12:47:06 -07:00
|
|
|
};
|