Skip to content

Commit 756770e

Browse files
committed
Refactor crops achievement logic and types
Moved crop achievement logic from Utility to a dedicated parser, updated Crops component to use new props structure, and simplified cropsShippedType. Adjusted Achievements and type definitions to match the new data flow.
1 parent 5599f23 commit 756770e

4 files changed

Lines changed: 68 additions & 76 deletions

File tree

src/Components/AchieveTabs/Crops/index.tsx

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,67 @@
1-
import React, { useState, useEffect } from 'react'
1+
import { AchievementItem, ItemWithCounter } from '@components/common';
22
import type { cropsShippedType } from 'types/displayDataTypes';
33

4-
interface CropsShippedWrapperType {
5-
cropsShipped: cropsShippedType;
6-
}
74

8-
const Crops= (cropsShipped : CropsShippedWrapperType) => {
9-
let _monocultureText = cropsShipped.cropsShipped.hasMonoculture ? "You already have the 'Monoculture' Achievement" : `You've shipped ${ cropsShipped.cropsShipped.maxMono?.name } the most and you require ${300 - cropsShipped.cropsShipped.maxMono?.shipped} more of it to get the 'Monoculture' Achievement`;
10-
let _polycultureText = cropsShipped.cropsShipped.hasPolyculture ? "You already have the 'Polyculture' Achievement" : `You need to ship 15 crops for each polyculture crop to get the 'Polyculture' achievement`;
11-
5+
const Crops= ({poly_crops, mono_extras, achievements} : cropsShippedType) => {
6+
console.log(achievements)
127
return (
138
<div className="progress-container">
14-
{/* <span className="a-title"><h1>{_monocultureAchieved ? `You've shipped ${ maxShipped.name } the most and you require ${300 - maxShipped.times} more of it to get the 'Monoculture' Achievement` : <span className="completed">`You already have the 'Monoculture' Achievement`</span> }</h1></span> */}
159
<br />
16-
<h2>Farming Achievements</h2>
17-
<ul className="a-List">
18-
<li>Monoculture: <span className={cropsShipped.cropsShipped.hasMonoculture ? "completed" : "pending"}>{_monocultureText}</span> </li>
19-
<li>Polyculture: <span className={cropsShipped.cropsShipped.hasPolyculture ? "completed" : "pending"}>{_polycultureText}</span></li>
20-
</ul>
10+
<h2>Farming Achievements</h2>
11+
<div className="section-achievements">
12+
{achievements && achievements.map((ach, i) => (
13+
<AchievementItem
14+
key={i}
15+
done={ach.done}
16+
image={ach.image}
17+
achievementName={ach.name}
18+
achievementDesc={ach.description}
19+
achievementHoverDesc={ach.hoverDesc}
20+
/>))}
21+
</div>
2122
<span className="a-title"><p>Ship 15 of the following crops to get the 'Polyculture' achievement</p></span>
22-
{cropsShipped.cropsShipped.poly_crops.map((crop, i) =><a href={`https://stardewvalleywiki.com/${crop.image}`} target="_blank" rel="noreferrer" key={i}> <img key={i} src={`https://stardew-tracker.s3.amazonaws.com/Crops/${crop.image}.png`} alt={crop.name} className={ (crop.shipped !== undefined) ? ((crop.shipped >= 15) ? "done" : "known" ): "" } title={(crop.shipped !== undefined) ? (crop.shipped >= 15) ? `You have shipped ${crop.name} ${crop.shipped} times` : `You have to ship ${ 15 - crop.shipped} more ${crop.name} ` : `You haven't shipped ${crop.name}`} ></img></a>)}
23+
<div className="item-grid">
24+
{ poly_crops ?
25+
poly_crops.map((d, i) =>
26+
<ItemWithCounter
27+
key={i}
28+
link={`https://stardewvalleywiki.com/${d.link}`}
29+
src={`https://stardew-tracker.s3.amazonaws.com/Crops/${d.image}.png`}
30+
name={d.name}
31+
state={ (d.shipped) ? "done" : "known" }
32+
hoverDesc={(d.shipped) ?
33+
(d.shipped && d.shipped > 0) ? `Cooked ${d.name} ${d.shipped} times`
34+
: `You haven't cooked ${d.name}`
35+
: `You don't know how to cook ${d.name}`}
36+
times={d.shipped ? d.shipped : 0}
37+
/>)
38+
: <div>No cooking data available.</div>
39+
}
40+
</div>
2341

2442
<span className="a-title"><p>These crops are not counted for the 'Polyculture' achievement</p></span>
25-
{cropsShipped.cropsShipped.mono_extras.map((crop, i) =><a href={`https://stardewvalleywiki.com/${crop.image}`} target="_blank" rel="noreferrer" key={i}> <img key={i} src={`https://stardew-tracker.s3.amazonaws.com/Crops/${crop.image}.png`} alt={crop.name} className={ (crop.shipped !== undefined) ? ((crop.shipped > 0) ? "done" : "known" ): "" } title={(crop.shipped !== undefined) ? `You have shipped ${crop.name} ${crop.shipped} times` : `You haven't shipped ${crop.name}`} ></img></a>)}
43+
<div className="item-grid">
44+
{ mono_extras ?
45+
mono_extras.map((d, i) =>
46+
<ItemWithCounter
47+
key={i}
48+
link={`https://stardewvalleywiki.com/${d.link}`}
49+
src={`https://stardew-tracker.s3.amazonaws.com/Crops/${d.image}.png`}
50+
name={d.name}
51+
state={ (d.shipped) ? "done" : "known" }
52+
hoverDesc={(d.shipped) ?
53+
(d.shipped && d.shipped > 0) ? `Cooked ${d.name} ${d.shipped} times`
54+
: `You haven't cooked ${d.name}`
55+
: `You don't know how to cook ${d.name}`}
56+
times={d.shipped ? d.shipped : 0}
57+
/>)
58+
: <div>No cooking data available.</div>
59+
}
60+
</div>
61+
{/* {cropsShipped.cropsShipped.mono_extras.map((crop, i) =>
62+
<a href={`https://stardewvalleywiki.com/${crop.image}`} target="_blank" rel="noreferrer" key={i}>
63+
<img key={i} src={`https://stardew-tracker.s3.amazonaws.com/Crops/${crop.image}.png`}
64+
alt={crop.name} className={ (crop.shipped !== undefined) ? ((crop.shipped > 0) ? "done" : "known" ): "" } title={(crop.shipped !== undefined) ? `You have shipped ${crop.name} ${crop.shipped} times` : `You haven't shipped ${crop.name}`} ></img></a>)} */}
2665
</div>
2766
);
2867
};

src/Components/Achievements/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ const Achievements = ({
5454
<img className="star" alt="star" src={Star}></img>
5555
<Tabs>
5656
<TabList className="achievement">
57-
{TabImg.map((img, i) => <Tab key={i}> <img src={img.img} alt={img.alt} title={img.alt} className="tab-ico"></img></Tab>)}
57+
{TabImg.map((img, i) =>
58+
<Tab key={i}>
59+
<img src={img.img} alt={img.alt} title={img.alt} className="tab-ico" />
60+
</Tab>
61+
)}
5862
</TabList>
5963
<TabPanel>
6064
<section className="achievement-container">
@@ -68,7 +72,7 @@ const Achievements = ({
6872
</TabPanel>
6973
<TabPanel>
7074
<section className="achievement-container">
71-
<Crops cropsShipped={cropsShipped} />
75+
<Crops {...cropsShipped} />
7276
</section>
7377
</TabPanel>
7478
<TabPanel>

src/Utility/index.tsx

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type {
3838
import type { fullPlayerDataType, museumCollectionType } from 'types/displayDataTypes';
3939
import { GetCookingData } from './Parsers/parseCookingItems';
4040
import { GetCraftingRecipes } from './Parsers/parseCraftingItems';
41+
import { GetCropsAchievements } from './Parsers/parseCropItems';
4142

4243
//Gets the info from the farm hands as an array of the same type
4344
const GetFarmHands = (locations: gameLocationType[]): playerType[] => {
@@ -230,56 +231,6 @@ const GetShippedItems = (allShipped: itemType) :generalFormatedItemType[] => {
230231
return data;
231232
}
232233

233-
/* Crop Related Achievements */
234-
const GetCropsAchievements = (allShipped: itemsType[]) : cropsShippedType => {
235-
const poly_crops: generalFormatedItemType[] = []
236-
const mono_extras: generalFormatedItemType[] = []
237-
let polycultureCount = 0;
238-
let maxMono: maxMonoType = { name: "undefined", shipped: 0 };
239-
240-
ShipCrops.forEach(cropItem => {
241-
const shippedCount = getShippedCount(allShipped, cropItem.id);
242-
const cropData = createCropData(cropItem, shippedCount);
243-
244-
if (!maxMono || shippedCount > maxMono.shipped) {
245-
maxMono = {
246-
name: cropItem.name,
247-
shipped: shippedCount
248-
};
249-
}
250-
251-
if (cropItem.isPolyCrop) {
252-
if (shippedCount >= 15) polycultureCount++;
253-
poly_crops.push(cropData);
254-
} else {
255-
mono_extras.push(cropData);
256-
}
257-
});
258-
259-
const cropsAchievements = {
260-
hasPolyculture: polycultureCount === 28,
261-
hasMonoculture: maxMono ? maxMono?.shipped >= 300 : false,
262-
maxMono,
263-
poly_crops,
264-
mono_extras
265-
};
266-
267-
return cropsAchievements;
268-
}
269-
270-
const getShippedCount = (allShipped: itemsType[], cropId: number): number => {
271-
if (!allShipped?.length) return 0;
272-
const shippedItem = allShipped.find(item => item.key.int === cropId);
273-
return shippedItem?.value?.int || 0;
274-
};
275-
276-
const createCropData = (cropItem: any, shippedCount: number): generalFormatedItemType => ({
277-
name: cropItem.name,
278-
image: GetImages(cropItem.name),
279-
id: cropItem.id,
280-
shipped: shippedCount
281-
});
282-
283234
/* End of Crop Related Achievements */
284235

285236
const GetFishes = (allFished: itemsType[]) => {

src/types/displayDataTypes.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export type cookingDataType = {
4141
achievements?: achievementType[];
4242
}
4343

44+
export type cropsShippedType = {
45+
achievements?: achievementType[];
46+
mono_extras: generalFormatedItemType[];
47+
poly_crops: generalFormatedItemType[];
48+
};
49+
4450
export type achievementType = {
4551
done: boolean,
4652
goal: number,
@@ -78,14 +84,6 @@ export type museumCollectionType = {
7884
minerals: generalFormatedItemType[];
7985
}
8086

81-
export type cropsShippedType = {
82-
hasMonoculture: boolean;
83-
hasPolyculture: boolean;
84-
mono_extras: generalFormatedItemType[];
85-
poly_crops: generalFormatedItemType[];
86-
maxMono: null | maxMonoType;
87-
};
88-
8987
export type maxMonoType = {
9088
name: string;
9189
shipped: number;

0 commit comments

Comments
 (0)