-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(animals): create animal relationships parser
- Loading branch information
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters