-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added win loss ration to universe view
- Loading branch information
Showing
5 changed files
with
102 additions
and
6 deletions.
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
82 changes: 82 additions & 0 deletions
82
packages/testnet/frontend/src/components/battlereports/utils.ts
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { FetchData } from './BattleReport'; | ||
|
||
export function useGetBattleReportsData(planetId: number | null) { | ||
const [battleReports, setBattleReports] = useState<FetchData[]>([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
if (planetId === null) { | ||
// If planetId is not set, clear the current data and do not fetch | ||
setBattleReports([]); | ||
setError(null); | ||
setIsLoading(false); | ||
return; | ||
} | ||
|
||
const apiUrl = `https://www.api.testnet.no-game.xyz/battle-reports?planet_id=${planetId}`; | ||
const fetchData = async () => { | ||
setIsLoading(true); | ||
setError(null); | ||
|
||
try { | ||
const response = await fetch(apiUrl); | ||
if (!response.ok) { | ||
throw new Error('Error fetching battle reports'); | ||
} | ||
const data = await response.json(); | ||
|
||
if (data.length === 0) { | ||
setError('No Battle Reports to show'); | ||
setBattleReports([]); | ||
} else { | ||
setBattleReports(data); | ||
} | ||
} catch (err) { | ||
setError( | ||
err instanceof Error ? err.message : 'An unexpected error occurred' | ||
); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}, [planetId]); // Re-fetch data when planetId changes | ||
|
||
return { battleReports, isLoading, error }; | ||
} | ||
|
||
export function useCalculateWinsAndLosses(planetId: number) { | ||
const [winLoss, setWinLoss] = useState({ wins: 0, losses: 0 }); | ||
|
||
const { battleReports, isLoading, error } = useGetBattleReportsData(planetId); | ||
console.log(battleReports); | ||
console.log(error); | ||
|
||
useEffect(() => { | ||
if (!isLoading && !error && battleReports) { | ||
let wins = 0; | ||
let losses = 0; | ||
|
||
for (const report of battleReports) { | ||
const totalLoot = | ||
report.loot.steel + report.loot.quartz + report.loot.tritium; | ||
console.log(totalLoot); | ||
|
||
if (report.attacker_planet_id === planetId && totalLoot > 0) { | ||
wins++; | ||
} else if (report.defender_planet_id === planetId && totalLoot === 0) { | ||
wins++; | ||
} else { | ||
losses++; | ||
} | ||
} | ||
|
||
setWinLoss({ wins, losses }); | ||
} | ||
}, [battleReports, isLoading, error, planetId]); | ||
|
||
return winLoss; | ||
} |
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
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