Fix Juppie Swirl bug in /api/validPetPoses

Oops, the new Juppie Swirl color has ID 114, and there's no released color #113 yet. But our `/api/validPetPoses` code, when deciding how large to make the byte array, uses the _number_ of colors in the database.

This meant that, when Juppie Swirl was released, there wasn't a 114th slot allocated, and the loop stopped at color ID #113—so the new Juppie Swirl color #114 wasn't included in the results. This made it impossible to select Juppie Swirl as a starting color. (You could, however, model a Juppie Swirl Chia, and the wardrobe would load it successfully; and you would see the color/species picker with the correct options selected, but in a red "invalid" state.)

Now, we instead use the largest ID in the database to determine the size of the array. This means Juppie Swirl is now included correctly!

There would be network perf implications if the color IDs were a sparser space, but it's dense enough to be totally fine in practice. (But let's not release an April Fools color #9999 or anything!)
This commit is contained in:
Emi Matchu 2022-02-23 00:01:10 -08:00
parent c1eef6222b
commit cc9a540cec

View file

@ -12,13 +12,17 @@ import { getPoseFromPetState, normalizeRow } from "../../src/server/util";
export async function getValidPetPoses() {
const db = await connectToDb();
const numSpeciesPromise = getNumSpecies(db);
const numColorsPromise = getNumColors(db);
const largestSpeciesIdPromise = getLargestSpeciesId(db);
const largestColorIdPromise = getLargestColorId(db);
const distinctPetStatesPromise = getDistinctPetStates(db);
const [numSpecies, numColors, distinctPetStates] = await Promise.all([
numSpeciesPromise,
numColorsPromise,
const [
largestSpeciesId,
largestColorId,
distinctPetStates,
] = await Promise.all([
largestSpeciesIdPromise,
largestColorIdPromise,
distinctPetStatesPromise,
]);
@ -35,14 +39,23 @@ export async function getValidPetPoses() {
return poseStrs.has(poseStr);
}
const numPairs = numSpecies * numColors;
// NOTE: We base the size of the array on the _largest_ color and species IDs
// in the database, not the _number_ of colors and species. This is
// because TNT sometimes skips IDs when working on multiple colors at
// once, so it's possible to have e.g. 113 released colors, but have
// the largest color ID be #114, because #113 was skipped. In that case,
// we leave an empty byte at skipped IDs, and that's okay because they
// don't skip a whole bunch of color IDs at once. (This would be a bad
// idea for network perf if e.g. TNT skipped to color #9999, because we
// would then leave a LOT of empty bytes in the array!)
const numPairs = largestSpeciesId * largestColorId;
const buffer = Buffer.alloc(numPairs + 2);
buffer.writeUInt8(numSpecies, 0);
buffer.writeUInt8(numColors, 1);
buffer.writeUInt8(largestSpeciesId, 0);
buffer.writeUInt8(largestColorId, 1);
for (let speciesId = 1; speciesId <= numSpecies; speciesId++) {
for (let speciesId = 1; speciesId <= largestSpeciesId; speciesId++) {
const speciesIndex = speciesId - 1;
for (let colorId = 1; colorId <= numColors; colorId++) {
for (let colorId = 1; colorId <= largestColorId; colorId++) {
const colorIndex = colorId - 1;
// We fill in the high bits first, and shift left as we go!
@ -63,21 +76,21 @@ export async function getValidPetPoses() {
byte <<= 1;
byte += hasPose(speciesId, colorId, "HAPPY_MASC") ? 1 : 0;
buffer.writeUInt8(byte, speciesIndex * numColors + colorIndex + 2);
buffer.writeUInt8(byte, speciesIndex * largestColorId + colorIndex + 2);
}
}
return buffer;
}
async function getNumSpecies(db) {
const [rows] = await db.query(`SELECT count(*) FROM species`);
return rows[0]["count(*)"];
async function getLargestSpeciesId(db) {
const [rows] = await db.query(`SELECT max(id) FROM species`);
return rows[0]["max(id)"];
}
async function getNumColors(db) {
const [rows] = await db.query(`SELECT count(*) FROM colors WHERE prank = 0`);
return rows[0]["count(*)"];
async function getLargestColorId(db) {
const [rows] = await db.query(`SELECT max(id) FROM colors WHERE prank = 0`);
return rows[0]["max(id)"];
}
async function getDistinctPetStates(db) {