Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: calculate available slots in ShipSnapshotProvider #39

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 36 additions & 60 deletions src/ShipFit/ShipFit.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from "react";

import { EveDataContext } from '../EveDataProvider';
import { ShipSnapshotContext } from '../ShipSnapshotProvider';

import { FitLink } from './FitLink';
Expand All @@ -19,37 +18,14 @@ export interface ShipFitProps {
export const ShipFit = (props: ShipFitProps) => {
const radius = props.radius ?? 365;

const eveData = React.useContext(EveDataContext);
const shipSnapshot = React.useContext(ShipSnapshotContext);
const slots = shipSnapshot.slots;

const scaleStyle = {
"--radius": `${radius}px`,
"--scale": `${radius / 365}`
} as React.CSSProperties;

const slots = {
"hislot": 0,
"medslot": 0,
"lowslot": 0,
"subsystem": 0,
"rig": 0,
};

if (shipSnapshot?.loaded) {
slots.hislot = shipSnapshot.hull?.attributes.get(eveData?.attributeMapping?.hiSlots || 0)?.value || 0;
slots.medslot = shipSnapshot.hull?.attributes.get(eveData?.attributeMapping?.medSlots || 0)?.value || 0;
slots.lowslot = shipSnapshot.hull?.attributes.get(eveData?.attributeMapping?.lowSlots || 0)?.value || 0;
slots.subsystem = shipSnapshot.hull?.attributes.get(eveData?.attributeMapping?.maxSubSystems || 0)?.value || 0;
slots.rig = shipSnapshot.hull?.attributes.get(eveData?.attributeMapping?.rigSlots || 0)?.value || 0;

const items = shipSnapshot.items || [];
for (const item of items) {
slots.hislot += item.attributes.get(eveData?.attributeMapping?.hiSlotModifier || 0)?.value || 0;
slots.medslot += item.attributes.get(eveData?.attributeMapping?.medSlotModifier || 0)?.value || 0;
slots.lowslot += item.attributes.get(eveData?.attributeMapping?.lowSlotModifier || 0)?.value || 0;
}
}

return <div className={styles.fit} style={scaleStyle}>
<div className={styles.outerBand} />
<div className={styles.innerBand} />
Expand All @@ -58,41 +34,41 @@ export const ShipFit = (props: ShipFitProps) => {
<FitLink />

<div className={styles.slots}>
<Slot type="subsystem" index={1} fittable={slots.subsystem >= 1} rotation="-125deg" />
<Slot type="subsystem" index={2} fittable={slots.subsystem >= 2} rotation="-114deg" />
<Slot type="subsystem" index={3} fittable={slots.subsystem >= 3} rotation="-103deg" />
<Slot type="subsystem" index={4} fittable={slots.subsystem >= 4} rotation="-92deg" />

<Slot type="rig" index={1} fittable={slots.rig >= 1} rotation="-73deg" />
<Slot type="rig" index={2} fittable={slots.rig >= 2} rotation="-63deg" />
<Slot type="rig" index={3} fittable={slots.rig >= 3} rotation="-53deg" />

<Slot type="hislot" index={1} fittable={slots.hislot >= 1} rotation="-34deg" />
<Slot type="hislot" index={2} fittable={slots.hislot >= 2} rotation="-24deg" />
<Slot type="hislot" index={3} fittable={slots.hislot >= 3} rotation="-14deg" />
<Slot type="hislot" index={4} fittable={slots.hislot >= 4} rotation="-4deg" />
<Slot type="hislot" index={5} fittable={slots.hislot >= 5} rotation="6deg" />
<Slot type="hislot" index={6} fittable={slots.hislot >= 6} rotation="16deg" />
<Slot type="hislot" index={7} fittable={slots.hislot >= 7} rotation="26deg" />
<Slot type="hislot" index={8} fittable={slots.hislot >= 8} rotation="36deg" />

<Slot type="medslot" index={1} fittable={slots.medslot >= 1} rotation="55deg" />
<Slot type="medslot" index={2} fittable={slots.medslot >= 2} rotation="65deg" />
<Slot type="medslot" index={3} fittable={slots.medslot >= 3} rotation="75deg" />
<Slot type="medslot" index={4} fittable={slots.medslot >= 4} rotation="85deg" />
<Slot type="medslot" index={5} fittable={slots.medslot >= 5} rotation="95deg" />
<Slot type="medslot" index={6} fittable={slots.medslot >= 6} rotation="105deg" />
<Slot type="medslot" index={7} fittable={slots.medslot >= 7} rotation="115deg" />
<Slot type="medslot" index={8} fittable={slots.medslot >= 8} rotation="125deg" />

<Slot type="lowslot" index={1} fittable={slots.lowslot >= 1} rotation="144deg" />
<Slot type="lowslot" index={2} fittable={slots.lowslot >= 2} rotation="154deg" />
<Slot type="lowslot" index={3} fittable={slots.lowslot >= 3} rotation="164deg" />
<Slot type="lowslot" index={4} fittable={slots.lowslot >= 4} rotation="174deg" />
<Slot type="lowslot" index={5} fittable={slots.lowslot >= 5} rotation="184deg" />
<Slot type="lowslot" index={6} fittable={slots.lowslot >= 6} rotation="194deg" />
<Slot type="lowslot" index={7} fittable={slots.lowslot >= 7} rotation="204deg" />
<Slot type="lowslot" index={8} fittable={slots.lowslot >= 8} rotation="214deg" />
<Slot type="subsystem" index={1} fittable={slots?.subsystem >= 1} rotation="-125deg" />
<Slot type="subsystem" index={2} fittable={slots?.subsystem >= 2} rotation="-114deg" />
<Slot type="subsystem" index={3} fittable={slots?.subsystem >= 3} rotation="-103deg" />
<Slot type="subsystem" index={4} fittable={slots?.subsystem >= 4} rotation="-92deg" />

<Slot type="rig" index={1} fittable={slots?.rig >= 1} rotation="-73deg" />
<Slot type="rig" index={2} fittable={slots?.rig >= 2} rotation="-63deg" />
<Slot type="rig" index={3} fittable={slots?.rig >= 3} rotation="-53deg" />

<Slot type="hislot" index={1} fittable={slots?.hislot >= 1} rotation="-34deg" />
<Slot type="hislot" index={2} fittable={slots?.hislot >= 2} rotation="-24deg" />
<Slot type="hislot" index={3} fittable={slots?.hislot >= 3} rotation="-14deg" />
<Slot type="hislot" index={4} fittable={slots?.hislot >= 4} rotation="-4deg" />
<Slot type="hislot" index={5} fittable={slots?.hislot >= 5} rotation="6deg" />
<Slot type="hislot" index={6} fittable={slots?.hislot >= 6} rotation="16deg" />
<Slot type="hislot" index={7} fittable={slots?.hislot >= 7} rotation="26deg" />
<Slot type="hislot" index={8} fittable={slots?.hislot >= 8} rotation="36deg" />

<Slot type="medslot" index={1} fittable={slots?.medslot >= 1} rotation="55deg" />
<Slot type="medslot" index={2} fittable={slots?.medslot >= 2} rotation="65deg" />
<Slot type="medslot" index={3} fittable={slots?.medslot >= 3} rotation="75deg" />
<Slot type="medslot" index={4} fittable={slots?.medslot >= 4} rotation="85deg" />
<Slot type="medslot" index={5} fittable={slots?.medslot >= 5} rotation="95deg" />
<Slot type="medslot" index={6} fittable={slots?.medslot >= 6} rotation="105deg" />
<Slot type="medslot" index={7} fittable={slots?.medslot >= 7} rotation="115deg" />
<Slot type="medslot" index={8} fittable={slots?.medslot >= 8} rotation="125deg" />

<Slot type="lowslot" index={1} fittable={slots?.lowslot >= 1} rotation="144deg" />
<Slot type="lowslot" index={2} fittable={slots?.lowslot >= 2} rotation="154deg" />
<Slot type="lowslot" index={3} fittable={slots?.lowslot >= 3} rotation="164deg" />
<Slot type="lowslot" index={4} fittable={slots?.lowslot >= 4} rotation="174deg" />
<Slot type="lowslot" index={5} fittable={slots?.lowslot >= 5} rotation="184deg" />
<Slot type="lowslot" index={6} fittable={slots?.lowslot >= 6} rotation="194deg" />
<Slot type="lowslot" index={7} fittable={slots?.lowslot >= 7} rotation="204deg" />
<Slot type="lowslot" index={8} fittable={slots?.lowslot >= 8} rotation="214deg" />
</div>
</div>
};
57 changes: 53 additions & 4 deletions src/ShipSnapshotProvider/ShipSnapshotProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";

import { DogmaEngineContext } from '../DogmaEngineProvider';
import { EveDataContext } from "../EveDataProvider";

export interface ShipSnapshotItemAttributeEffect {
operator: string,
Expand Down Expand Up @@ -38,10 +39,21 @@ export interface EsiFit {
}[];
}

interface ShipSnapshotSlots {
hislot: number;
medslot: number;
lowslot: number;
subsystem: number;
rig: number;
};

export type ShipSnapshotSlotsType = keyof ShipSnapshotSlots;

interface ShipSnapshot {
loaded?: boolean;
hull?: ShipSnapshotItem;
items?: ShipSnapshotItem[];
slots: ShipSnapshotSlots;

fit?: EsiFit;

Expand All @@ -52,6 +64,13 @@ interface ShipSnapshot {

export const ShipSnapshotContext = React.createContext<ShipSnapshot>({
loaded: undefined,
slots: {
"hislot": 0,
"medslot": 0,
"lowslot": 0,
"subsystem": 0,
"rig": 0,
},
changeHull: () => {},
changeFit: () => {},
setItemState: () => {},
Expand All @@ -70,8 +89,16 @@ export interface ShipSnapshotProps {
* Calculates the current attrbitues and applied effects of a ship fit.
*/
export const ShipSnapshotProvider = (props: ShipSnapshotProps) => {
const eveData = React.useContext(EveDataContext);
const [shipSnapshot, setShipSnapshot] = React.useState<ShipSnapshot>({
loaded: undefined,
slots: {
"hislot": 0,
"medslot": 0,
"lowslot": 0,
"subsystem": 0,
"rig": 0,
},
changeHull: () => {},
changeFit: () => {},
setItemState: () => {},
Expand All @@ -80,10 +107,10 @@ export const ShipSnapshotProvider = (props: ShipSnapshotProps) => {
const dogmaEngine = React.useContext(DogmaEngineContext);

const setItemState = React.useCallback((flag: number, state: string) => {
if (!currentFit) return;
if (currentFit === undefined) return;

setCurrentFit((oldFit: EsiFit | undefined) => {
if (!oldFit) return oldFit;
if (oldFit === undefined) return undefined;

return {
...oldFit,
Expand Down Expand Up @@ -112,20 +139,42 @@ export const ShipSnapshotProvider = (props: ShipSnapshotProps) => {

React.useEffect(() => {
if (!dogmaEngine.loaded) return;
if (!currentFit || !props.skills) return;
if (currentFit === undefined || props.skills === undefined) return;

const snapshot = dogmaEngine.engine?.calculate(currentFit, props.skills);

const slots = {
"hislot": 0,
"medslot": 0,
"lowslot": 0,
"subsystem": 0,
"rig": 0,
};

slots.hislot = snapshot.hull.attributes.get(eveData?.attributeMapping?.hiSlots || 0)?.value || 0;
slots.medslot = snapshot.hull.attributes.get(eveData?.attributeMapping?.medSlots || 0)?.value || 0;
slots.lowslot = snapshot.hull.attributes.get(eveData?.attributeMapping?.lowSlots || 0)?.value || 0;
slots.subsystem = snapshot.hull.attributes.get(eveData?.attributeMapping?.maxSubSystems || 0)?.value || 0;
slots.rig = snapshot.hull?.attributes.get(eveData?.attributeMapping?.rigSlots || 0)?.value || 0;

const items = snapshot.items;
for (const item of items) {
slots.hislot += item.attributes.get(eveData?.attributeMapping?.hiSlotModifier || 0)?.value || 0;
slots.medslot += item.attributes.get(eveData?.attributeMapping?.medSlotModifier || 0)?.value || 0;
slots.lowslot += item.attributes.get(eveData?.attributeMapping?.lowSlotModifier || 0)?.value || 0;
}

setShipSnapshot({
loaded: true,
hull: snapshot.hull,
items: snapshot.items,
slots,
fit: currentFit,
changeHull,
changeFit: setCurrentFit,
setItemState,
});
}, [dogmaEngine, currentFit, props.skills, changeHull, setItemState]);
}, [eveData, dogmaEngine, currentFit, props.skills, changeHull, setItemState]);

React.useEffect(() => {
setCurrentFit(props.fit);
Expand Down
2 changes: 1 addition & 1 deletion src/ShipSnapshotProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { ShipSnapshotContext, ShipSnapshotProvider } from "./ShipSnapshotProvider";
export type { EsiFit, ShipSnapshotItem, ShipSnapshotItemAttribute, ShipSnapshotItemAttributeEffect } from "./ShipSnapshotProvider";
export type { EsiFit, ShipSnapshotItem, ShipSnapshotItemAttribute, ShipSnapshotItemAttributeEffect, ShipSnapshotSlotsType } from "./ShipSnapshotProvider";