use PetService to determine the correct pose
This commit is contained in:
parent
bcdd9af806
commit
29b9fe48c5
7 changed files with 59 additions and 12 deletions
|
@ -121,6 +121,7 @@ function SubmitPetForm() {
|
|||
species {
|
||||
id
|
||||
}
|
||||
pose
|
||||
items {
|
||||
id
|
||||
}
|
||||
|
@ -132,12 +133,12 @@ function SubmitPetForm() {
|
|||
onCompleted: (data) => {
|
||||
if (!data) return;
|
||||
|
||||
const { species, color, items } = data.petOnNeopetsDotCom;
|
||||
const { species, color, pose, items } = data.petOnNeopetsDotCom;
|
||||
const params = new URLSearchParams({
|
||||
name: petName,
|
||||
species: species.id,
|
||||
color: color.id,
|
||||
pose: "HAPPY_FEM", // TODO: Ask PetService
|
||||
pose,
|
||||
});
|
||||
for (const item of items) {
|
||||
params.append("objects[]", item.id);
|
||||
|
|
|
@ -103,7 +103,6 @@ function SpeciesColorPicker({
|
|||
const validPoses = getValidPoses(valids, speciesId, newColorId);
|
||||
const isValid = validPoses.size > 0;
|
||||
const closestPose = getClosestPose(validPoses, idealPose);
|
||||
console.log(idealPose, closestPose, validPoses);
|
||||
onChange(species, newColor, isValid, closestPose);
|
||||
};
|
||||
|
||||
|
@ -117,7 +116,6 @@ function SpeciesColorPicker({
|
|||
const validPoses = getValidPoses(valids, newSpeciesId, colorId);
|
||||
const isValid = validPoses.size > 0;
|
||||
const closestPose = getClosestPose(validPoses, idealPose);
|
||||
console.log(idealPose, closestPose, validPoses);
|
||||
onChange(newSpecies, color, isValid, closestPose);
|
||||
};
|
||||
|
||||
|
@ -181,7 +179,6 @@ function pairIsValid(valids, speciesId, colorId) {
|
|||
|
||||
function getValidPoses(valids, speciesId, colorId) {
|
||||
const pairByte = getPairByte(valids, speciesId, colorId);
|
||||
console.log("pair byte", pairByte.toString(2).padStart(8, "0"));
|
||||
|
||||
const validPoses = new Set();
|
||||
if (pairByte & 0b00000001) validPoses.add("HAPPY_MASC");
|
||||
|
|
|
@ -6,6 +6,7 @@ const neopets = require("./neopets");
|
|||
const {
|
||||
capitalize,
|
||||
getPoseFromPetState,
|
||||
getPoseFromPetData,
|
||||
getEmotion,
|
||||
getGenderPresentation,
|
||||
} = require("./util");
|
||||
|
@ -124,6 +125,7 @@ const typeDefs = gql`
|
|||
type Outfit {
|
||||
species: Species!
|
||||
color: Color!
|
||||
pose: Pose!
|
||||
items: [Item!]!
|
||||
}
|
||||
|
||||
|
@ -354,11 +356,15 @@ const resolvers = {
|
|||
return petStates.map((petState) => ({ petType, petState }));
|
||||
},
|
||||
petOnNeopetsDotCom: async (_, { petName }) => {
|
||||
const petData = await neopets.loadPetData(petName);
|
||||
const [petMetaData, customPetData] = await Promise.all([
|
||||
neopets.loadPetMetaData(petName),
|
||||
neopets.loadCustomPetData(petName),
|
||||
]);
|
||||
const outfit = {
|
||||
species: { id: petData.custom_pet.species_id },
|
||||
color: { id: petData.custom_pet.color_id },
|
||||
items: Object.values(petData.object_info_registry).map((o) => ({
|
||||
species: { id: customPetData.custom_pet.species_id },
|
||||
color: { id: customPetData.custom_pet.color_id },
|
||||
pose: getPoseFromPetData(petMetaData, customPetData),
|
||||
items: Object.values(customPetData.object_info_registry).map((o) => ({
|
||||
id: o.obj_info_id,
|
||||
})),
|
||||
};
|
||||
|
|
|
@ -1,13 +1,28 @@
|
|||
const fetch = require("node-fetch");
|
||||
|
||||
async function loadPetData(petName) {
|
||||
async function loadPetMetaData(petName) {
|
||||
const url =
|
||||
`http://www.neopets.com/amfphp/json.php/PetService.getPet` + `/${petName}`;
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`for pet meta data, neopets.com returned: ` +
|
||||
`${res.status} ${res.statusText}. (${url})`
|
||||
);
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
return json;
|
||||
}
|
||||
|
||||
async function loadCustomPetData(petName) {
|
||||
const url =
|
||||
`http://www.neopets.com/amfphp/json.php/CustomPetService.getViewerData` +
|
||||
`/${petName}`;
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`for pet data, neopets.com returned: ` +
|
||||
`for custom pet data, neopets.com returned: ` +
|
||||
`${res.status} ${res.statusText}. (${url})`
|
||||
);
|
||||
}
|
||||
|
@ -56,4 +71,4 @@ function convertSwfUrlToManifestUrl(swfUrl) {
|
|||
return `http://images.neopets.com/cp/${type}/data/${folders}/manifest.json`;
|
||||
}
|
||||
|
||||
module.exports = { loadPetData, loadAssetManifest };
|
||||
module.exports = { loadPetMetaData, loadCustomPetData, loadAssetManifest };
|
||||
|
|
|
@ -13,6 +13,7 @@ describe("Pet", () => {
|
|||
color {
|
||||
id
|
||||
}
|
||||
pose
|
||||
items {
|
||||
id
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ Object {
|
|||
"id": "48313",
|
||||
},
|
||||
],
|
||||
"pose": "SAD_MASC",
|
||||
"species": Object {
|
||||
"id": "54",
|
||||
},
|
||||
|
|
|
@ -57,6 +57,31 @@ function getPoseFromPetState(petState) {
|
|||
}
|
||||
}
|
||||
|
||||
function getPoseFromPetData(petMetaData, petCustomData) {
|
||||
// TODO: Use custom data to decide if Unconverted.
|
||||
const moodId = petMetaData.mood;
|
||||
const genderId = petMetaData.gender;
|
||||
if (String(moodId) === "1" && String(genderId) === "1") {
|
||||
return "HAPPY_MASC";
|
||||
} else if (String(moodId) === "1" && String(genderId) === "2") {
|
||||
return "HAPPY_FEM";
|
||||
} else if (String(moodId) === "2" && String(genderId) === "1") {
|
||||
return "SAD_MASC";
|
||||
} else if (String(moodId) === "2" && String(genderId) === "2") {
|
||||
return "SAD_FEM";
|
||||
} else if (String(moodId) === "4" && String(genderId) === "1") {
|
||||
return "SICK_MASC";
|
||||
} else if (String(moodId) === "4" && String(genderId) === "2") {
|
||||
return "SICK_FEM";
|
||||
} else {
|
||||
throw new Error(
|
||||
`could not identify pose: ` +
|
||||
`moodId=${moodId}, ` +
|
||||
`genderId=${genderId}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeRow(row) {
|
||||
const normalizedRow = {};
|
||||
for (let [key, value] of Object.entries(row)) {
|
||||
|
@ -74,5 +99,6 @@ module.exports = {
|
|||
getEmotion,
|
||||
getGenderPresentation,
|
||||
getPoseFromPetState,
|
||||
getPoseFromPetData,
|
||||
normalizeRow,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue