Skip to content

Commit

Permalink
fixed ships levels on colony
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Feb 2, 2024
1 parent 668912f commit bcbd66c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
33 changes: 22 additions & 11 deletions packages/frontend/src/components/modals/PlanetOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {
useCollectibleResources,
} from '../../hooks/ResourcesHooks';
import { useShipsLevels, useDefencesLevels } from '../../hooks/LevelsHooks';
import { numberWithCommas } from '../../shared/utils';
import { getPlanetAndColonyIds, numberWithCommas } from '../../shared/utils';
import {
useGetColonyMotherPlanet,
useGetColonyResources,
useGetColonyDefences,
} from '../../hooks/ColoniesHooks';
import { useGetColonyShips } from '../../hooks/ColoniesHooks';

export const StyledBox = styled(Box)({
fontWeight: 400,
Expand Down Expand Up @@ -125,13 +125,24 @@ interface Props {
export default function PlanetModal({ planetId, image, position }: Props) {
const spendableResources = useSpendableResources(Number(planetId));
const collectibleResources = useCollectibleResources(Number(planetId));
console.log('planetId', planetId);
const [planet, colony] = getPlanetAndColonyIds(planetId);
console.log('planet', planet);
console.log('colony', colony);

const shipsLevels = useShipsLevels(Number(planetId));
const colonyShips = useGetColonyShips(planet, colony);
const actualShips =
colonyShips && shipsLevels && colony === 0 ? shipsLevels : colonyShips;

const defencesLevels = useDefencesLevels(Number(planetId));
const colonyDefences = useGetColonyDefences(planet, colony);
const actualDefences =
defencesLevels && colonyDefences && colony === 0
? defencesLevels
: colonyDefences;

const motherPlanet = Number(useGetColonyMotherPlanet(planetId));
const colonyId = planetId - motherPlanet * 1000;
const colonyResources = useGetColonyResources(motherPlanet, colonyId);
const colonyDefences = useGetColonyDefences(motherPlanet, colonyId);
const colonyResources = useGetColonyResources(planet, colony);

const [isModalOpen, setIsModalOpen] = React.useState(false);

Expand Down Expand Up @@ -204,11 +215,13 @@ export default function PlanetModal({ planetId, image, position }: Props) {
<GridSection>
<SubTitle>Fleet</SubTitle>
<DetailGrid>
{Object.keys(shipsLevels ?? {}).map((key) => (
{Object.keys(
(planetId > 500 ? shipsLevels : colonyShips) ?? {}
).map((key) => (
<h6 key={key}>
{key}:{' '}
<Value>
{numberWithCommas(shipsLevels[key as keyof ShipsLevels])}
{numberWithCommas(actualShips[key as keyof ShipsLevels])}
</Value>
</h6>
))}
Expand All @@ -225,9 +238,7 @@ export default function PlanetModal({ planetId, image, position }: Props) {
{key}:{' '}
<Value>
{numberWithCommas(
(planetId > 500 ? colonyDefences : defencesLevels)[
key as keyof DefenceLevels
]
actualDefences[key as keyof DefenceLevels]
)}
</Value>
</h6>
Expand Down
9 changes: 9 additions & 0 deletions packages/frontend/src/shared/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,12 @@ export const formatAccount = (account: string) => {
}
return account;
};

export function getPlanetAndColonyIds(planetId: number): [number, number] {
if (planetId > 500) {
const planet = Math.floor(planetId / 1000);
const colony = planetId % 1000;
return [planet, colony];
}
return [planetId, 0];
}

0 comments on commit bcbd66c

Please sign in to comment.