Skip to content

Commit 335dd7d

Browse files
committed
Refactor player data handling and props in stats components
Updated Skills, Utility, and stats components to use more explicit and type-safe props for player and experience data. Refactored stats.tsx to use formattedSaveFileType, improved null handling, and adjusted mapping logic for farmhand data. Updated useLoadSaveFile hook to return formattedSaveFileType for playerData, ensuring consistent data structures across components.
1 parent bc1c5b0 commit 335dd7d

4 files changed

Lines changed: 45 additions & 38 deletions

File tree

src/Components/Skills/Skills.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import SkillContainer from './SkillContainer/SkillContainer'
33
import {Farming, Mining, Foraging, Fishing, Combat} from '@media/Skills'
44
import type { experienceType } from 'types/displayDataTypes'
55

6-
const Skills = (experience: experienceType[]) => {
6+
interface SkillsProps {
7+
experience: experienceType[];
8+
}
9+
10+
const Skills = ({ experience }: SkillsProps) => {
711
const skillImg =[Farming, Fishing, Foraging, Mining, Combat];
812
console.log("Skills data:", experience);
913
return (
@@ -12,7 +16,6 @@ const Skills = (experience: experienceType[]) => {
1216
{experience.map((item, i: number) => (i === 5) ? "" :
1317
<SkillContainer
1418
key={i}
15-
index={i}
1619
skillImg={skillImg[i] || ""}
1720
skillName={item.skill}
1821
xp={item.xp}

src/Components/Utility/Utility.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ type getParsedUserDataType = Omit<getDetailedInfoType, 'playerData'> & {
6666

6767
//Calls the parser and creates an array of players depending on wether it is a single player or multiple
6868
const GetDetailedInfo = ({playerData , collectionStatus, specialRequests, availableSpecialRequests}: getDetailedInfoType) =>{
69-
let fullPlayerData: any = []
69+
let fullPlayerData: fullPlayerDataType[] = []
7070
if(Array.isArray(playerData)){
7171
playerData.forEach(p => {
72-
let playerFull = {
72+
let playerFull: fullPlayerDataType
73+
= {
7374
//...p,
7475
...parseData({playerData: p, collectionStatus, specialRequests, availableSpecialRequests})
7576
}
77+
78+
if(playerFull !== null)
7679
fullPlayerData.push(playerFull)
7780
})
7881
}
@@ -81,12 +84,11 @@ const GetDetailedInfo = ({playerData , collectionStatus, specialRequests, availa
8184
}
8285

8386
//Creates an object per player with the cleanup playerData.from the file
84-
const parseData = ({playerData, collectionStatus, specialRequests, availableSpecialRequests}: getParsedUserDataType) => {
85-
if(!playerData) return null;
87+
const parseData = ({playerData, collectionStatus, specialRequests, availableSpecialRequests}: getParsedUserDataType) : fullPlayerDataType => {
8688
//Not finished
8789
console.log("Parsing data for:", playerData)
8890
let fullPlayerData : fullPlayerDataType = {
89-
playerName: playerData.name,
91+
playerName: playerData.name || "Unknown",
9092
farmName: playerData.farmName, //TODO: Remove and make global if even needed
9193
experience: GetXpInfo(playerData.experiencePoints.int), //DONE
9294
moneyEarned: playerData.totalMoneyEarned || 0, //DONE
@@ -124,7 +126,7 @@ const GetXpInfo = (xp: number[]): experienceType[] => {
124126
levelInfo: Levels.find((level) => level.val >= _skill) || { id: 10, val: 15000 }
125127
})
126128
}
127-
})
129+
})
128130
return skillLevelData
129131
}
130132
/* End of XP Data */

src/Components/stats/stats.tsx

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,23 @@ import Junimo2 from '../../Media/Junimo2B.png'
1111
import Junimo3 from '../../Media/Junimo3B.png'
1212
import Junimo4 from '../../Media/Junimo4B.png'
1313
import Rope from '../../Media/Rope.png'
14-
import type { fullPlayerDataType } from 'types/displayDataTypes';
14+
import type { formattedSaveFileType, fullPlayerDataType } from 'types/displayDataTypes';
1515

16-
type statsPageProps = {
17-
playerData: fullPlayerDataType;
18-
globalFarmName: string;
19-
farmhands: fullPlayerDataType[];
20-
}
2116

22-
const Stats = ({playerData, globalFarmName, farmhands}: statsPageProps) => {
23-
const [farmName] = useState(globalFarmName || "My Farm");
17+
18+
const Stats = ({playerData, farmName, farmhandData}: formattedSaveFileType) => {
19+
const [globalFarmName] = useState(farmName || "My Farm");
2420
const [junimos] = useState([Junimo1, Junimo2, Junimo3, Junimo4]);
2521
const [Activejunimos] = useState([Junimo1a, Junimo2a, Junimo3a, Junimo4a]);
2622

27-
const titles = farmhands.map((item, i) =>
23+
const titles = farmhandData.map((item, i) =>
2824
<Tab key={i}>
2925
<img src={junimos[i + 1]} alt="Jun"></img> {item.playerName}
3026
</Tab>
3127
);
28+
3229

33-
const playerStats = farmhands.map((item, i) => (
30+
const playerStats = farmhandData.map((item, i) => (
3431
<TabPanel key={i}>
3532
<section className="wrapper">
3633
<Skills {...item.experience}></Skills>
@@ -55,42 +52,46 @@ const Stats = ({playerData, globalFarmName, farmhands}: statsPageProps) => {
5552
</section>
5653
</TabPanel>
5754
));
58-
55+
console.log("Rendering Stats component with playerData:", playerData, "experience:", playerData.experience);
5956
return (
6057
<div className="file-container">
61-
<div className="farmName"><h2>Farm: {farmName}</h2></div>
58+
<div className="farmName"><h2>Farm: {globalFarmName}</h2></div>
6259
<section className="scroller">
60+
{ playerData ? (
6361
<Tabs>
6462
<TabList>
65-
<Tab><img src={junimos[0]} alt="Junimo" width="25px"></img>{playerData[0].playerName}</Tab>
63+
<Tab><img src={junimos[0]} alt="Junimo" width="25px"></img>{playerData.playerName}</Tab>
6664
{titles}
6765
</TabList>
6866

6967
<TabPanel>
7068
<section className="wrapper">
71-
<Skills {...playerData[0].experience}></Skills>
69+
<>
70+
<Skills experience={playerData.experience}></Skills>
7271
<div className="ropes">
7372
<img src={Rope} alt=""></img>
7473
<img src={Rope} alt=""></img>
7574
</div>
7675
<Achievements
77-
recipesCooked={playerData[0].cookedItems}
78-
itemsCrafted={playerData[0].itemsCrafted}
79-
cropsShipped={playerData[0].cropsShipped}
80-
fishCaught={playerData[0].fishCaught}
81-
friendship={playerData[0].friendship}
82-
monstersKilled={playerData[0].monstersKilled}
83-
shippedItems={playerData[0].shippedItems}
84-
moneyEarned={playerData[0].moneyEarned}
85-
museumCollection={playerData[0].museumCollection}
86-
questsDone={playerData[0].questsDone}
87-
specialReq={playerData[0].specialRequests}
88-
pendingSpecialReq={playerData[0].pendingSpecialRequests}
89-
></Achievements>
76+
recipesCooked={playerData.cookedItems}
77+
itemsCrafted={playerData.itemsCrafted}
78+
cropsShipped={playerData.cropsShipped}
79+
fishCaught={playerData.fishCaught}
80+
friendship={playerData.friendship}
81+
monstersKilled={playerData.monstersKilled}
82+
shippedItems={playerData.shippedItems}
83+
moneyEarned={playerData.moneyEarned}
84+
museumCollection={playerData.museumCollection}
85+
questsDone={playerData.questsDone}
86+
specialReq={playerData.specialRequests}
87+
//pendingSpecialReq={playerData.pendingSpecialRequests}
88+
></Achievements>
89+
</>
9090
</section>
9191
</TabPanel>
9292
{playerStats}
93-
</Tabs>
93+
</Tabs>) : <p>No player data available.</p>
94+
}
9495
</section>
9596
</div>
9697
);

src/hooks/useLoadSaveFile.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from 'react';
22
import { XMLParser } from 'fast-xml-parser';
33
import { GetDetailedInfo, GetFarmHands } from '@utility/Utility';
44
import type { gameLocationType, itemsType, itemType, playerType, saveFileType, saveGameType, specialOrderType } from 'types/savefile.js';
5+
import type { formattedSaveFileType } from 'types/displayDataTypes';
56

67
export interface UseLoadSaveFileResult {
78
playerData: any;
@@ -15,7 +16,7 @@ const useLoadSaveFile = (): UseLoadSaveFileResult => {
1516
const [fileData, setFileData] = useState<saveGameType | null>(null);
1617
const [isLoading, setIsLoading] = useState(false);
1718
const [error, setError] = useState<string | null>(null);
18-
const [playerData, setPlayerData] = useState<any | null>(null);
19+
const [playerData, setPlayerData] = useState<formattedSaveFileType | null>(null);
1920

2021
useEffect(() => {
2122
if (fileData) {
@@ -74,7 +75,7 @@ const useLoadSaveFile = (): UseLoadSaveFileResult => {
7475
collectionStatus: collectionStatus,
7576
specialRequests: specialRequests,
7677
availableSpecialRequests: availableSpecialRequests
77-
}),
78+
})[0] || null,
7879
farmhandData: GetDetailedInfo({
7980
playerData: farmHands,
8081
collectionStatus: collectionStatus,

0 commit comments

Comments
 (0)