diff --git a/src/lib/file.ts b/src/lib/file.ts index a221f31..4a86055 100644 --- a/src/lib/file.ts +++ b/src/lib/file.ts @@ -14,6 +14,7 @@ import { parseShipping, parseSocial, parsePowers, + parseAnimals, } from "@/lib/parsers"; import { GetListOrEmpty, getAllFarmhands } from "@/lib/utils"; import { parseNotes } from "./parsers/notes"; @@ -89,6 +90,19 @@ export function parseSaveFile(xml: string) { // Map of uniqueMultiplayerID to array of children names const children = findChildren(prefix, saveFile.SaveGame); + const findInGameLocation = (location: string) => { + return saveFile.SaveGame.locations.GameLocation.find( + (obj: any) => obj[`@_${prefix}:type`] === location, + ); + } + + const parsedAnimals = parseAnimals( + findInGameLocation("Farm").buildings.Building, + findInGameLocation("Farm").characters.NPC, + findInGameLocation("FarmHouse").characters.NPC, + prefix, + ) + let processedPlayers: any[] = []; // get the saveGame.player's mailReceived, mailForTomorrow, mailbox so we don't @@ -138,6 +152,10 @@ export function parseSaveFile(xml: string) { hostMailForTomorrow, hostMailbox, ), + animals: { + ...parsedAnimals, + horse: player.horseName, + }, }; processedPlayers.push(processedPlayer); }); diff --git a/src/lib/parsers/animals.ts b/src/lib/parsers/animals.ts index ce2d72b..4a93a31 100644 --- a/src/lib/parsers/animals.ts +++ b/src/lib/parsers/animals.ts @@ -1 +1,75 @@ -export function parseAnimals(player: any) {} +interface AnimalsRet { + farmAnimals: FarmAnimal[]; + pets: Pet[]; +} + +type FarmAnimal = { + name: string; + type: string; + age: number; + friendshipTowardFarmer: number; + happiness: number; +}; + +type Pet = { + name: string; + type: string; + friendship: number; +} + +type Building = { + indoors?: { + [key: string]: any; + animals: { + item: Array<{ value: { FarmAnimal: FarmAnimal } }>; + }; + }; +}; + +type Character = { + [key: string]: any; +} + +export function parseAnimals( + buildings: any, + farmCharacters: any, + farmHouseCharacters: any, + prefix: string, +): AnimalsRet { + const farmAnimals = buildings + .filter((building: Building) => building.indoors?.[`@_${prefix}:type`] === "AnimalHouse") + // Get the arrays of animals with the required properties, and flatten them into a single one + .flatMap((building: Building) => { + const animals = building.indoors?.animals?.item || []; + return animals.map((animal) => { + const { name, type, age, friendshipTowardFarmer: friendship, happiness } = animal.value.FarmAnimal; + return { name, type, age, friendship, happiness }; + }); + }) || []; + + const getPets = (characters: Character | Character[]) => { + if (characters) { + // Check for multiple characters + if (Array.isArray(characters)) { + return characters + .filter((character) => character[`@_${prefix}:type`] === "Pet") + .map((pet) => { + // Get the required properties + const { name, petType: type, friendshipTowardFarmer: friendship } = pet; + return { name, type, friendship }; + }); + } else if (characters[`@_${prefix}:type`] === "Pet") { + const { name, petType: type, friendshipTowardFarmer: friendship } = characters; + return [{ name, type, friendship }]; + } + } + + return []; + }; + + const farmPets = getPets(farmCharacters); + const farmHousePets = getPets(farmHouseCharacters); + const pets = farmPets.concat(farmHousePets); + + return { farmAnimals, pets }; +} diff --git a/src/lib/parsers/index.ts b/src/lib/parsers/index.ts index 2fbb98e..51b638a 100644 --- a/src/lib/parsers/index.ts +++ b/src/lib/parsers/index.ts @@ -9,6 +9,7 @@ import { parsePerfection } from "@/lib/parsers/perfection"; import { parseShipping } from "@/lib/parsers/shipping"; import { findChildren, parseSocial } from "@/lib/parsers/social"; import { parsePowers } from "@/lib/parsers/powers"; +import { parseAnimals } from "@/lib/parsers/animals"; export { findChildren, @@ -23,4 +24,5 @@ export { parseShipping, parseSocial, parsePowers, + parseAnimals, };