Skip to content

Commit 8b8a0ee

Browse files
committed
Add crafting items parser utility
Introduces parseCraftingItems.ts to process crafting recipes, track crafted and known items, and compute crafting achievements. This utility provides formatted data for display and achievement progress based on crafting activity.
1 parent 8b38ffc commit 8b8a0ee

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { CraftingRec } from "@utility/JSON"
2+
import type { achievementType, generalFormatedItemType, itemsCraftedType } from "types/displayDataTypes"
3+
import type { itemsType } from "types/savefile"
4+
import { GetImages, ValidateKnown } from "./Utils"
5+
import { DIY, Artisan, MasterCraft } from "@media/Achievements";
6+
7+
let CraftingAchievements = [{
8+
goal: 15,
9+
image: DIY,
10+
name: 'D.I.Y.',
11+
done: false,
12+
description: 'Craft a total of 15 items',
13+
hoverDesc: ''
14+
},{
15+
goal: 30,
16+
image: Artisan ,
17+
name: 'Artisan ',
18+
done: false,
19+
description: 'Craft a total of 30 items',
20+
hoverDesc: ''
21+
},{
22+
goal: CraftingRec.recipes.length,
23+
image: MasterCraft,
24+
name: 'Craft Master',
25+
done: false,
26+
description: 'Craft All items',
27+
hoverDesc: ''
28+
}
29+
];
30+
31+
export const GetCraftingRecipes = (recipes: itemsType[]): itemsCraftedType => {
32+
let data: generalFormatedItemType[] = []
33+
let totalCrafted = 0;
34+
let totalKnown = 0;
35+
if(Array.isArray(recipes)) {
36+
CraftingRec.recipes.forEach(item => {
37+
let timesCrafted = recipes.find(i => i.key.string === item)?.value.int || 0;
38+
let known = ValidateKnown(recipes, item) || false;
39+
if(known) totalKnown++;
40+
if(timesCrafted > 0) totalCrafted++;
41+
let d = {
42+
name: item,
43+
image: GetImages(item),
44+
times: timesCrafted,
45+
known: known
46+
}
47+
data.push(d)
48+
})
49+
}
50+
return {
51+
knownItems: totalKnown,
52+
alreadyCraftedItems: totalCrafted,
53+
craftedItems: [...data],
54+
totalRecipes: CraftingRec.recipes.length,
55+
achievements: GetAchievementData(totalCrafted),
56+
};
57+
}
58+
59+
const GetAchievementData = (alreadyCrafted: number) : achievementType[] => {
60+
let achievements = CraftingAchievements.map(ach => {
61+
return {
62+
...ach,
63+
done: alreadyCrafted >= ach.goal,
64+
hoverDesc: alreadyCrafted >= ach.goal ?
65+
'Achievement unlocked!' :
66+
`You need to craft ${ach.goal - alreadyCrafted} more items to unlock this achievement.`
67+
}
68+
});
69+
return achievements;
70+
}

0 commit comments

Comments
 (0)