Skip to content

Commit 99a3186

Browse files
committed
Refactor crop achievement logic and props
Simplifies Crops component by using precomputed achievement data and updates its props. Refactors crop achievement calculation in Utility.tsx to centralize logic, add maxMono and achievement flags, and improve maintainability.
1 parent f683583 commit 99a3186

2 files changed

Lines changed: 63 additions & 83 deletions

File tree

src/Components/AchieveTabs/Crops.tsx

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,29 @@
11
import React, { useState, useEffect } from 'react'
22
import type { cropsShippedType } from 'types/savefile';
33

4-
interface CropsProps {
4+
interface CropsShippedWrapperType {
55
cropsShipped: cropsShippedType;
66
}
77

8-
const Crops= ({ cropsShipped }: CropsProps) => {
9-
const [maxShipped, setMaxShipped] = useState<{name: string, times: number}>({name: '', times: 0});
10-
11-
const timesShipped = (arr: cropsShippedType) => {
12-
let maxPoly = arr.poly_crops.sort((a, b) => {
13-
const aTimes = a.shipped ? a.shipped : 0;
14-
const bTimes = b.shipped ? b.shipped : 0;
15-
return bTimes - aTimes; // Sort in descending order
16-
});
17-
let maxMono = arr.mono_extras.sort((a, b) => {
18-
const aTimes = a.shipped ? a.shipped : 0;
19-
const bTimes = b.shipped ? b.shipped : 0;
20-
return bTimes - aTimes; // Sort in descending order
21-
});
22-
console.log('maxPoly', maxPoly)
23-
if ((maxPoly === undefined || maxPoly.length === 0) && (maxMono === undefined || maxMono.length === 0)) {
24-
setMaxShipped({name: 'None', times: 0});
25-
return;
26-
} else {
27-
const polyShipped = maxPoly[0]?.shipped || 0;
28-
const monoShipped = maxMono[0]?.shipped || 0;
29-
if (polyShipped > monoShipped) {
30-
setMaxShipped({
31-
name: maxPoly[0]?.name || 'Unknown',
32-
times: maxPoly[0]?.shipped || 0
33-
});
34-
} else {
35-
setMaxShipped({
36-
name: maxMono[0]?.name || 'Unknown',
37-
times: maxMono[0]?.shipped || 0
38-
});
39-
}
40-
}
41-
};
42-
43-
useEffect(() => {
44-
console.log('cs', cropsShipped)
45-
timesShipped(cropsShipped);
46-
}, [cropsShipped]);
47-
48-
console.log(maxShipped)
49-
let _monocultureAchieved = maxShipped.times >= 300;
50-
let _totalShipped = 0 //cropsShipped.poly_crops.reduce((acc, crop) => acc + (crop.shipped ? crop.shipped : 0), 0);
51-
let _polycultureAchieved = _totalShipped >= 15;
52-
let _monocultureText = _monocultureAchieved ? "You already have the 'Monoculture' Achievement" : `You've shipped ${ maxShipped.name } the most and you require ${300 - maxShipped.times} more of it to get the 'Monoculture' Achievement`;
53-
let _polycultureText = _polycultureAchieved ? "You already have the 'Polyculture' Achievement" : `You need to ship ${15 - _totalShipped} more crops to get the 'Polyculture' achievement`;
54-
8+
const Crops= (cropsShipped : CropsShippedWrapperType) => {
9+
console.log('CropsShipped', cropsShipped);
10+
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`;
11+
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`;
12+
5513
return (
5614
<div className="progress-container">
57-
<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>
15+
{/* <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> */}
5816
<br />
5917
<h2>Farming Achievements</h2>
6018
<ul className="a-List">
61-
<li>Monoculture: <span className={_monocultureAchieved ? "completed" : "pending"}>{_monocultureText}</span> </li>
62-
<li>Polyculture: <span className={_polycultureAchieved ? "completed" : "pending"}>{_polycultureText}</span></li>
19+
<li>Monoculture: <span className={cropsShipped.cropsShipped.hasMonoculture ? "completed" : "pending"}>{_monocultureText}</span> </li>
20+
<li>Polyculture: <span className={cropsShipped.cropsShipped.hasPolyculture ? "completed" : "pending"}>{_polycultureText}</span></li>
6321
</ul>
6422
<span className="a-title"><p>Ship 15 of the following crops to get the 'Polyculture' achievement</p></span>
65-
{cropsShipped.poly_crops.map((crop, i) =><a href={`https://stardewvalleywiki.com/${crop.image}`} target="blank" 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+
{cropsShipped.cropsShipped.poly_crops.map((crop, i) =><a href={`https://stardewvalleywiki.com/${crop.image}`} target="blank" 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>)}
6624

6725
<span className="a-title"><p>This crops are not counted for the 'Polyculture' achievement</p></span>
68-
{cropsShipped.mono_extras.map((crop, i) =><a href={`https://stardewvalleywiki.com/${crop.image}`} target="blank" 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>)}
26+
{cropsShipped.cropsShipped.mono_extras.map((crop, i) =><a href={`https://stardewvalleywiki.com/${crop.image}`} target="blank" 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>)}
6927
</div>
7028
);
7129
};

src/Components/Utility/Utility.tsx

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
itemFoundType,
2424
itemsType,
2525
itemType,
26+
maxMonoType,
2627
playerType,
2728
professionsType,
2829
questType,
@@ -69,7 +70,8 @@ const GetDetailedInfo = ({playerData , collectionStatus, specialRequests, availa
6970
if(Array.isArray(playerData)){
7071
playerData.forEach(p => {
7172
let playerFull = {
72-
...p, ...parseData({playerData: p, collectionStatus, specialRequests, availableSpecialRequests})
73+
//...p,
74+
...parseData({playerData: p, collectionStatus, specialRequests, availableSpecialRequests})
7375
}
7476
fullPlayerData.push(playerFull)
7577
})
@@ -90,7 +92,7 @@ const parseData = ({playerData, collectionStatus, specialRequests, availableSpec
9092
moneyEarned: playerData.totalMoneyEarned || 0, //DONE
9193
professions: GetProfessionData(playerData.professions.int) , //DONE?
9294
shippedItems: GetShippedItems(playerData.basicShipped) || [],//DONE
93-
cropsShipped: GetShippedCrops(playerData.basicShipped?.item),//DONE
95+
cropsShipped: GetCropsAchievements(playerData.basicShipped?.item),//DONE
9496
mineralsFound: GetArrayData(playerData.mineralsFound?.item) || [], //DONE
9597
cookedItems: GetCookingData(playerData.recipesCooked, playerData.cookingRecipes.item) || [], //DONE
9698
fishCaught: GetFishes(playerData.fishCaught.item) || [],
@@ -185,38 +187,58 @@ const GetShippedItems = (allShipped: itemType) :generalFormatedItemType[] => {
185187
return data;
186188
}
187189

188-
const GetShippedCrops = (allShipped: itemsType[]) : cropsShippedType => {
190+
/* Crop Related Achievements */
191+
const GetCropsAchievements = (allShipped: itemsType[]) : cropsShippedType => {
189192
const poly_crops: generalFormatedItemType[] = []
190193
const mono_extras: generalFormatedItemType[] = []
191-
192-
// Process poly crops
193-
ShipCrops.poly_crops.forEach(polycropItem => {
194-
const shippedItem = (allShipped && allShipped.length > 0) ?
195-
allShipped.find(i => i.key.int === polycropItem.id) : null;
196-
197-
poly_crops.push({
198-
name: polycropItem.name,
199-
image: GetImages(polycropItem.name),
200-
id: polycropItem.id,
201-
shipped: shippedItem?.value?.int || 0
202-
});
203-
});
204-
205-
// Process mono extras
206-
ShipCrops.mono_extras.forEach(monoCropItem => {
207-
const shippedItem = (allShipped && allShipped.length > 0) ?
208-
allShipped.find(i => i.key.int === monoCropItem.id) : null;
209-
210-
mono_extras.push({
211-
name: monoCropItem.name,
212-
image: GetImages(monoCropItem.name),
213-
id: monoCropItem.id,
214-
shipped: shippedItem?.value?.int || 0
215-
});
194+
let polycultureCount = 0;
195+
let maxMono: maxMonoType = { name: "undefined", shipped: 0 };
196+
197+
ShipCrops.forEach(cropItem => {
198+
const shippedCount = getShippedCount(allShipped, cropItem.id);
199+
const cropData = createCropData(cropItem, shippedCount);
200+
201+
if (!maxMono || shippedCount > maxMono.shipped) {
202+
maxMono = {
203+
name: cropItem.name,
204+
shipped: shippedCount
205+
};
206+
}
207+
208+
if (cropItem.isPolyCrop) {
209+
if (shippedCount >= 15) polycultureCount++;
210+
poly_crops.push(cropData);
211+
} else {
212+
mono_extras.push(cropData);
213+
}
216214
});
217-
218-
return { poly_crops, mono_extras };
215+
216+
const cropsAchievements = {
217+
hasPolyculture: polycultureCount === 28,
218+
hasMonoculture: maxMono ? maxMono?.shipped >= 300 : false,
219+
maxMono,
220+
poly_crops,
221+
mono_extras
222+
};
223+
224+
return cropsAchievements;
219225
}
226+
227+
const getShippedCount = (allShipped: itemsType[], cropId: number): number => {
228+
if (!allShipped?.length) return 0;
229+
const shippedItem = allShipped.find(item => item.key.int === cropId);
230+
return shippedItem?.value?.int || 0;
231+
};
232+
233+
const createCropData = (cropItem: any, shippedCount: number): generalFormatedItemType => ({
234+
name: cropItem.name,
235+
image: GetImages(cropItem.name),
236+
id: cropItem.id,
237+
shipped: shippedCount
238+
});
239+
240+
/* End of Crop Related Achievements */
241+
220242
const GetFishes = (allFished: itemsType[]) => {
221243
let data: generalFormatedItemType[] = []
222244

0 commit comments

Comments
 (0)