Skip to content

Commit

Permalink
feat(Ship): Extract resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasmirnov03 committed Dec 11, 2023
1 parent 5c66aec commit e2a2b25
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAuth } from '../../../../hooks/auth/useAuth.tsx';
import "./waypoint-info.css";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { callApi } from '../../../../utils/api/api-caller.ts';
import { useShip } from '../../../../hooks/ship/useShip.ts';

interface WaypointInfoProps {
nav: Nav | undefined,
Expand All @@ -16,6 +17,7 @@ export const WaypointInfo: FC<WaypointInfoProps> = ({ nav }) => {
const [marketplace, setMarketplace] = useState<Market>();

const auth = useAuth();
const shipContext = useShip();

const getLocation = useCallback(() => {
if (!nav) {
Expand All @@ -38,6 +40,18 @@ export const WaypointInfo: FC<WaypointInfoProps> = ({ nav }) => {
return Boolean(waypoint?.traits.some(trait => trait.symbol === 'MARKETPLACE'));
}

function canMine(): boolean {
if (!waypoint) {
return false;
}
const waypointTypes = [
"ASTEROID",
"ASTEROID_FIELD",
"ENGINEERED_ASTEROID"
];
return waypointTypes.includes(waypoint.type);
}

useEffect(() => {
getLocation();
}, [getLocation]);
Expand All @@ -50,6 +64,8 @@ export const WaypointInfo: FC<WaypointInfoProps> = ({ nav }) => {
<p>{waypoint?.traits.map(trait => trait.name + ', ')}</p>
{hasMarketplace() &&
<button className="button" onClick={getMarketplaceInfo}>Get marketplace info</button>}
{canMine() &&
<button className='button' onClick={shipContext.extractResources}>Extract resources</button>}
<ul className="tradeGoods">
{marketplace?.tradeGoods?.map(good => (
<li key={good.symbol} className="tradeGood">
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/ship/ShipContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ interface ShipContextInterface {
ship: ShipModel | undefined;
waypoints: Waypoint[];
scanWaypoints: () => void
navigateToWaypoint: (w: Waypoint) => void
navigateToWaypoint: (w: Waypoint) => void,
extractResources: () => void,
cooldown: number,
fuel: Fuel | undefined,
nav: Nav | undefined,
Expand All @@ -21,6 +22,7 @@ export const ShipContext = createContext<ShipContextInterface>({
},
navigateToWaypoint: () => {
},
extractResources: () => { },
cooldown: 0,
nav: undefined,
cargo: undefined,
Expand Down
18 changes: 18 additions & 0 deletions src/models/api-response/extract-resources-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Cooldown } from "../cooldown.model";
import { Cargo } from "../ship.model";

interface Yield {
symbol: string;
units: number;
}

interface Extraction {
shipSymbol: string;
yield: Yield;
}

export interface ExtractResourcesResponse {
cooldown: Cooldown;
extraction: Extraction;
cargo: Cargo;
}
12 changes: 11 additions & 1 deletion src/providers/ship/ShipContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Cargo, Fuel, Nav, ShipModel } from '../../models/ship.model.ts';
import { Waypoint, WaypointResponse } from '../../models/waypoint.model.ts';
import { callApi } from '../../utils/api/api-caller.ts';
import { NavigateResponse } from '../../models/api-response/navigate-response.ts';
import { ExtractResourcesResponse } from '../../models/api-response/extract-resources-response.ts';

interface ShipContextProviderProps {
children: ReactElement;
Expand Down Expand Up @@ -95,6 +96,14 @@ export function ShipContextProvider({ children }: ShipContextProviderProps) {
});
}

function extractResources(): void {
callApi<ExtractResourcesResponse>(`/my/ships/${ship?.symbol}/extract`, auth.token, 'post')
.then((res) => {
setCooldown(res.data.cooldown.remainingSeconds);
setCargo(res.data.cargo);
});
}

return <ShipContext.Provider value={{
ship,
waypoints,
Expand All @@ -103,7 +112,8 @@ export function ShipContextProvider({ children }: ShipContextProviderProps) {
navigateToWaypoint,
fuel,
nav,
cargo
cargo,
extractResources
}}>
{children}
</ShipContext.Provider>;
Expand Down

0 comments on commit e2a2b25

Please sign in to comment.