new endpoint for blob of valid pet poses
This commit is contained in:
parent
2214fe2815
commit
04a851a138
5 changed files with 95 additions and 6 deletions
6
api/validPetPoses.js
Normal file
6
api/validPetPoses.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import getValidPetPoses from "../src/server/getValidPetPoses";
|
||||||
|
|
||||||
|
export default async (req, res) => {
|
||||||
|
const buffer = await getValidPetPoses();
|
||||||
|
res.status(200).send(buffer);
|
||||||
|
};
|
BIN
src/server/__snapshots__/getValidPetPoses.test.js.snap
Normal file
BIN
src/server/__snapshots__/getValidPetPoses.test.js.snap
Normal file
Binary file not shown.
73
src/server/getValidPetPoses.js
Normal file
73
src/server/getValidPetPoses.js
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
import connectToDb from "./db";
|
||||||
|
|
||||||
|
import { getEmotion, getGenderPresentation } from "./util";
|
||||||
|
|
||||||
|
export default async function getValidPetPoses() {
|
||||||
|
const db = await connectToDb();
|
||||||
|
|
||||||
|
const numSpeciesPromise = getNumSpecies(db);
|
||||||
|
const numColorsPromise = getNumColors(db);
|
||||||
|
const poseTuplesPromise = getPoseTuples(db);
|
||||||
|
|
||||||
|
const [numSpecies, numColors, poseTuples] = await Promise.all([
|
||||||
|
numSpeciesPromise,
|
||||||
|
numColorsPromise,
|
||||||
|
poseTuplesPromise,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const poseStrs = new Set();
|
||||||
|
for (const poseTuple of poseTuples) {
|
||||||
|
const { species_id, color_id, mood_id, female } = poseTuple;
|
||||||
|
const emotion = getEmotion(mood_id);
|
||||||
|
const genderPresentation = getGenderPresentation(female);
|
||||||
|
const poseStr = `${species_id}-${color_id}-${emotion}-${genderPresentation}`;
|
||||||
|
poseStrs.add(poseStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasPose(speciesId, colorId, emotion, genderPresentation) {
|
||||||
|
const poseStr = `${speciesId}-${colorId}-${emotion}-${genderPresentation}`;
|
||||||
|
return poseStrs.has(poseStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
const numPairs = numSpecies * numColors;
|
||||||
|
const buffer = Buffer.alloc(numPairs);
|
||||||
|
|
||||||
|
for (let speciesId = 1; speciesId <= numSpecies; speciesId++) {
|
||||||
|
for (let colorId = 1; colorId <= numColors; colorId++) {
|
||||||
|
let byte = 0;
|
||||||
|
byte += hasPose(speciesId, colorId, "HAPPY", "MASCULINE") ? 1 : 0;
|
||||||
|
byte <<= 1;
|
||||||
|
byte += hasPose(speciesId, colorId, "SAD", "MASCULINE") ? 1 : 0;
|
||||||
|
byte <<= 1;
|
||||||
|
byte += hasPose(speciesId, colorId, "SICK", "MASCULINE") ? 1 : 0;
|
||||||
|
byte <<= 1;
|
||||||
|
byte += hasPose(speciesId, colorId, "HAPPY", "FEMININE") ? 1 : 0;
|
||||||
|
byte <<= 1;
|
||||||
|
byte += hasPose(speciesId, colorId, "SAD", "FEMININE") ? 1 : 0;
|
||||||
|
byte <<= 1;
|
||||||
|
byte += hasPose(speciesId, colorId, "SICK", "FEMININE") ? 1 : 0;
|
||||||
|
|
||||||
|
buffer.writeUInt8(byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getNumSpecies(db) {
|
||||||
|
const [rows, _] = await db.query(`SELECT count(*) FROM species`);
|
||||||
|
return rows[0]["count(*)"];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getNumColors(db) {
|
||||||
|
const [rows, _] = await db.query(`SELECT count(*) FROM colors`);
|
||||||
|
return rows[0]["count(*)"];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getPoseTuples(db) {
|
||||||
|
const [rows, _] = await db.query(`
|
||||||
|
SELECT DISTINCT species_id, color_id, mood_id, female FROM pet_states
|
||||||
|
INNER JOIN pet_types ON pet_types.id = pet_states.pet_type_id
|
||||||
|
WHERE mood_id IS NOT NULL AND female IS NOT NULL AND color_id >= 1`);
|
||||||
|
return rows;
|
||||||
|
}
|
8
src/server/getValidPetPoses.test.js
Normal file
8
src/server/getValidPetPoses.test.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import getValidPetPoses from "./getValidPetPoses";
|
||||||
|
|
||||||
|
describe("getValidPetPoses", () => {
|
||||||
|
it("gets them and writes them to a buffer", async () => {
|
||||||
|
const buffer = await getValidPetPoses();
|
||||||
|
expect(buffer.toString()).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
|
@ -3,13 +3,14 @@ function capitalize(str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getEmotion(moodId) {
|
function getEmotion(moodId) {
|
||||||
if (moodId === "1") {
|
const moodIdStr = String(moodId);
|
||||||
|
if (moodIdStr === "1") {
|
||||||
return "HAPPY";
|
return "HAPPY";
|
||||||
} else if (moodId === "2") {
|
} else if (moodIdStr === "2") {
|
||||||
return "SAD";
|
return "SAD";
|
||||||
} else if (moodId === "4") {
|
} else if (moodIdStr === "4") {
|
||||||
return "SICK";
|
return "SICK";
|
||||||
} else if (moodId === null) {
|
} else if (moodIdStr === null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`unrecognized moodId ${JSON.stringify(moodId)}`);
|
throw new Error(`unrecognized moodId ${JSON.stringify(moodId)}`);
|
||||||
|
@ -17,9 +18,10 @@ function getEmotion(moodId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getGenderPresentation(modelPetWasFemale) {
|
function getGenderPresentation(modelPetWasFemale) {
|
||||||
if (modelPetWasFemale === 1) {
|
const modelPetWasFemaleStr = String(modelPetWasFemale);
|
||||||
|
if (modelPetWasFemaleStr === "1") {
|
||||||
return "FEMININE";
|
return "FEMININE";
|
||||||
} else if (modelPetWasFemale === 0) {
|
} else if (modelPetWasFemaleStr === "0") {
|
||||||
return "MASCULINE";
|
return "MASCULINE";
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue