Skip to content

Commit

Permalink
feat(animals): create animal relationships parser
Browse files Browse the repository at this point in the history
  • Loading branch information
softy-dev committed Dec 18, 2024
1 parent 2c29a75 commit e85bc36
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/lib/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
parseShipping,
parseSocial,
parsePowers,
parseAnimals,
} from "@/lib/parsers";
import { GetListOrEmpty, getAllFarmhands } from "@/lib/utils";
import { parseNotes } from "./parsers/notes";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -138,6 +152,10 @@ export function parseSaveFile(xml: string) {
hostMailForTomorrow,
hostMailbox,
),
animals: {
...parsedAnimals,
horse: player.horseName,
},
};
processedPlayers.push(processedPlayer);
});
Expand Down
76 changes: 75 additions & 1 deletion src/lib/parsers/animals.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
2 changes: 2 additions & 0 deletions src/lib/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,4 +24,5 @@ export {
parseShipping,
parseSocial,
parsePowers,
parseAnimals,
};

0 comments on commit e85bc36

Please sign in to comment.