mvp
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { SWAPIPlanet, SWAPIStarship, Planet, Starship } from "@/types/swapi";
|
||||
|
||||
const BASE_URL = "https://swapi.dev/api";
|
||||
|
||||
export async function fetchPlanets(): Promise<Planet[]> {
|
||||
const res = await fetch(`${BASE_URL}/planets/`);
|
||||
if (!res.ok) throw new Error("Galaxy data unavailable");
|
||||
const data = await res.json();
|
||||
|
||||
return data.results.map(
|
||||
(p: SWAPIPlanet): Planet => ({
|
||||
...p,
|
||||
id: p.url,
|
||||
// Add random x/y coordinates for galaxy map
|
||||
x: Math.floor(Math.random() * 80) + 10,
|
||||
y: Math.floor(Math.random() * 70) + 15,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function fetchStarships(): Promise<Starship[]> {
|
||||
const res = await fetch(`${BASE_URL}/starships/`);
|
||||
if (!res.ok) throw new Error("Imperial records unavailable");
|
||||
const data = await res.json();
|
||||
|
||||
return data.results.map((s: SWAPIStarship): Starship => {
|
||||
// Add difficulty score based on ship cost
|
||||
const cost = parseInt(s.cost_in_credits);
|
||||
let ds = 8;
|
||||
if (isNaN(cost) || cost > 1000000) ds = 12;
|
||||
else if (cost > 100000) ds = 10;
|
||||
|
||||
return { ...s, id: s.url, ds };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user