Skip to content

Commit

Permalink
fix army weight when exploring, add weigh + capacity in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
bob0005 committed Jul 15, 2024
1 parent 1e40ae5 commit 594ab5a
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 10 deletions.
28 changes: 26 additions & 2 deletions client/src/hooks/helpers/useArmies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ export type ArmyInfo = ClientComponents["Army"]["schema"] & {
isMercenary: boolean;
uiPos: UIPosition;
offset: Position;
capacity: number;
weight: number;
} & ClientComponents["Health"]["schema"] &
ClientComponents["Protectee"]["schema"] &
ClientComponents["Quantity"]["schema"] &
ClientComponents["Movable"]["schema"] &
ClientComponents["Capacity"]["schema"] &
ClientComponents["ArrivalTime"]["schema"] &
ClientComponents["Position"]["schema"] &
ClientComponents["EntityOwner"]["schema"] &
Expand All @@ -38,6 +39,7 @@ const formatArmies = (
Quantity: Component,
Movable: Component,
Capacity: Component,
Weight: Component,
ArrivalTime: Component,
Position: Component,
EntityOwner: Component,
Expand All @@ -52,6 +54,7 @@ const formatArmies = (
const quantity = getComponentValue(Quantity, id) as ClientComponents["Quantity"]["schema"];
const movable = getComponentValue(Movable, id) as ClientComponents["Movable"]["schema"];
const capacity = getComponentValue(Capacity, id) as ClientComponents["Capacity"]["schema"];
const weight = getComponentValue(Weight, id) as ClientComponents["Weight"]["schema"];
const arrivalTime = getComponentValue(ArrivalTime, id) as ClientComponents["ArrivalTime"]["schema"];
const position = getComponentValue(Position, id) as ClientComponents["Position"]["schema"];
const entityOwner = getComponentValue(EntityOwner, id) as ClientComponents["EntityOwner"]["schema"];
Expand Down Expand Up @@ -90,7 +93,6 @@ const formatArmies = (
...health,
...quantity,
...movable,
...capacity,
...arrivalTime,
...position,
...entityOwner,
Expand All @@ -102,6 +104,10 @@ const formatArmies = (
isMercenary,
offset,
uiPos: { ...getUIPositionFromColRow(position?.x || 0, position?.y || 0), z: 0.32 },
capacity:
(Number(capacity?.weight_gram) * Number(quantity?.value || 0)) /
EternumGlobalConfig.resources.resourceMultiplier,
weight: Number(weight?.value || 0) / EternumGlobalConfig.resources.resourceMultiplier,

name: name
? shortString.decodeShortString(name.name.toString())
Expand All @@ -121,6 +127,7 @@ export const useArmies = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand Down Expand Up @@ -154,6 +161,7 @@ export const useArmies = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -175,6 +183,7 @@ export const useArmiesByEntityOwner = ({ entity_owner_entity_id }: { entity_owne
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand All @@ -200,6 +209,7 @@ export const useArmiesByEntityOwner = ({ entity_owner_entity_id }: { entity_owne
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -225,6 +235,7 @@ export const useArmiesByBattleId = (battle_id: bigint) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand All @@ -248,6 +259,7 @@ export const useArmiesByBattleId = (battle_id: bigint) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -268,6 +280,7 @@ export const getArmiesByBattleId = (battle_id: bigint) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand All @@ -291,6 +304,7 @@ export const getArmiesByBattleId = (battle_id: bigint) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -311,6 +325,7 @@ export const useArmyByArmyEntityId = (entityId: bigint) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand All @@ -334,6 +349,7 @@ export const useArmyByArmyEntityId = (entityId: bigint) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -355,6 +371,7 @@ export const usePositionArmies = ({ position }: { position: Position }) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand All @@ -380,6 +397,7 @@ export const usePositionArmies = ({ position }: { position: Position }) => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand Down Expand Up @@ -433,6 +451,7 @@ export const getArmyByEntityId = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand All @@ -458,6 +477,7 @@ export const getArmyByEntityId = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -480,6 +500,7 @@ export const getArmyByEntityId = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -503,6 +524,7 @@ export const getArmiesAtPosition = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Realm,
Army,
Expand Down Expand Up @@ -545,6 +567,7 @@ export const getArmiesAtPosition = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand All @@ -562,6 +585,7 @@ export const getArmiesAtPosition = () => {
Quantity,
Movable,
Capacity,
Weight,
ArrivalTime,
Position,
EntityOwner,
Expand Down
2 changes: 2 additions & 0 deletions client/src/ui/components/entities/Entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import clsx from "clsx";
import React, { useState } from "react";
import { DepositResources } from "../resources/DepositResources";
import { TravelEntityPopup } from "./TravelEntityPopup";
import { ArmyCapacity } from "@/ui/elements/ArmyCapacity";

const entityIcon: Record<EntityType, string> = {
[EntityType.DONKEY]: "🫏",
Expand Down Expand Up @@ -119,6 +120,7 @@ export const Entity = ({ entityId, ...props }: EntityProps) => {
</div>
</div>
</div>
{entity.entityType === EntityType.TROOP && <ArmyCapacity army={army} className="my-2 ml-5" />}
<div className="flex items-center gap-2 flex-wrap my-2">{renderResources()}</div>
{entityState !== EntityState.Traveling && (
<DepositResources entityId={entityId} battleInProgress={battleInProgress} />
Expand Down
2 changes: 2 additions & 0 deletions client/src/ui/components/military/ArmyChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useState } from "react";
import { InventoryResources } from "../resources/InventoryResources";
import { ArmyManagementCard, ViewOnMapButton } from "./ArmyManagementCard";
import { TroopMenuRow } from "./TroopChip";
import { ArmyCapacity } from "@/ui/elements/ArmyCapacity";

export const ArmyChip = ({ army, extraButton }: { army: ArmyInfo; extraButton?: JSX.Element }) => {
const [editMode, setEditMode] = useState(false);
Expand Down Expand Up @@ -32,6 +33,7 @@ export const ArmyChip = ({ army, extraButton }: { army: ArmyInfo; extraButton?:
</div>
)}
<StaminaResource entityId={BigInt(army.entity_id)} />
<ArmyCapacity army={army} />
</div>
</div>
<div className="flex flex-col gap-1">
Expand Down
2 changes: 2 additions & 0 deletions client/src/ui/components/military/ArmyList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DepositResources } from "../resources/DepositResources";
import { InventoryResources } from "../resources/InventoryResources";
import { ArmyManagementCard } from "./ArmyManagementCard";
import { ArmyViewCard } from "./ArmyViewCard";
import { ArmyCapacity } from "@/ui/elements/ArmyCapacity";

export const EntityArmyList = ({ structure }: any) => {
const { entityArmies } = useArmiesByEntityOwner({ entity_owner_entity_id: structure?.entity_id });
Expand Down Expand Up @@ -76,6 +77,7 @@ export const EntityArmyList = ({ structure }: any) => {
<React.Fragment key={entity.entity_id}>
{/* <StaminaResource entityId={entity.entity_id} className="mb-3" /> */}
<ArmyManagementCard owner_entity={structure?.entity_id} entity={entity} />
<ArmyCapacity army={entity} className="my-2 ml-5" />
<InventoryResources entityId={entity.entity_id} />
<DepositResources
entityId={entity.entity_id}
Expand Down
12 changes: 7 additions & 5 deletions client/src/ui/components/worldmap/armies/ArmyInfoLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import useBlockchainStore from "../../../../hooks/store/useBlockchainStore";
import { currencyFormat } from "../../../utils/utils";

import { useDojo } from "@/hooks/context/DojoContext";
import { ArmyInfo } from "@/hooks/helpers/useArmies";
import { ArmyInfo, useArmyByArmyEntityId } from "@/hooks/helpers/useArmies";
import { BaseThreeTooltip, Position } from "@/ui/elements/BaseThreeTooltip";
import { ResourceIcon } from "@/ui/elements/ResourceIcon";
import { StaminaResource } from "@/ui/elements/StaminaResource";
import clsx from "clsx";
import { useMemo } from "react";
import { useRealm } from "../../../../hooks/helpers/useRealm";
import { OrderIcon } from "../../../elements/OrderIcon";
import { getRealmNameById, getRealmOrderNameById } from "../../../utils/realms";
import { getRealmNameById } from "../../../utils/realms";
import { formatSecondsLeftInDaysHours } from "../../cityview/realm/labor/laborUtils";
import { InventoryResources } from "../../resources/InventoryResources";
import { Headline } from "@/ui/elements/Headline";
import { ArmyCapacity } from "@/ui/elements/ArmyCapacity";

interface ArmyInfoLabelProps {
army: ArmyInfo;
Expand Down Expand Up @@ -42,10 +43,11 @@ const RaiderInfo = ({ army }: ArmyInfoLabelProps) => {
account: { account },
} = useDojo();

const updatedArmy = useArmyByArmyEntityId(BigInt(army.entity_id));

const { getRealmAddressName } = useRealm();
const nextBlockTimestamp = useBlockchainStore.getState().nextBlockTimestamp as number;
const { entity_id, entity_owner_id, address, arrives_at, realm, troops, battle_id } = army;

const { entity_id, entity_owner_id, address, arrives_at, realm, troops, battle_id } = updatedArmy;
const isPassiveTravel = useMemo(
() => (army.arrives_at && nextBlockTimestamp ? army.arrives_at > nextBlockTimestamp : false),
[army.arrives_at, nextBlockTimestamp],
Expand Down Expand Up @@ -105,7 +107,7 @@ const RaiderInfo = ({ army }: ArmyInfoLabelProps) => {
<div className="text-green text-xs self-center">{currencyFormat(troops.paladin_count, 0)}</div>
</div>
</div>

<ArmyCapacity army={updatedArmy} />
<div className="flex flex-row justify-between">
<InventoryResources max={2} entityId={BigInt(entity_id)} />
</div>
Expand Down
16 changes: 16 additions & 0 deletions client/src/ui/components/worldmap/hexagon/ActionInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useResourceBalance } from "@/hooks/helpers/useResources";
import useRealmStore from "@/hooks/store/useRealmStore";
import { StaminaResourceCost } from "@/ui/elements/StaminaResourceCost";
import { getUIPositionFromColRow } from "@/ui/utils/utils";
import { BuildingThumbs } from "@/ui/modules/navigation/LeftNavigationModule";

export const ActionInfo = () => {
const travelPaths = useUIStore((state) => state.travelPaths);
Expand Down Expand Up @@ -54,6 +55,21 @@ export const ActionInfo = () => {
isExplored={isExplored}
travelLength={travelPath.path.length - 1}
/>
<div className="flex flex-row text-xs">
<div
style={{
backgroundImage: `url(${BuildingThumbs.resources})`,
backgroundSize: "calc(100% - 10px)",
backgroundPosition: "center",
}}
className="w-8 h-8 bg-no-repeat"
></div>

<div className="flex flex-col p-1 text-xs">
<div>+{EternumGlobalConfig.exploration.reward}</div>
<div>Reward</div>
</div>
</div>
</BaseThreeTooltip>
</group>
)}
Expand Down
9 changes: 8 additions & 1 deletion client/src/ui/components/worldmap/hexagon/useTravelPath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HighlightPositions } from "@/types/index.js";
import { ACCESSIBLE_POSITIONS_COLOUR } from "@/ui/config.js";
import { useResourceBalance } from "@/hooks/helpers/useResources.js";
import { useRealm } from "@/hooks/helpers/useRealm.js";
import { getArmyByEntityId } from "@/hooks/helpers/useArmies.js";

export const useTravelPath = () => {
const selectedEntity = useUIStore((state) => state.selectedEntity);
Expand All @@ -19,9 +20,15 @@ export const useTravelPath = () => {
const { getFoodResources } = useResourceBalance();
const { useStaminaByEntityId } = useStamina();
const { getEntityOwner } = useRealm();
const { getArmy } = getArmyByEntityId();

const stamina = useStaminaByEntityId({ travelingEntityId: selectedEntity?.id || 0n });

const army = getArmy(selectedEntity?.id || 0n);
const hasCapacity =
(army?.capacity * Number(army?.value)) / EternumGlobalConfig.resources.resourcePrecision - army?.weight >=
EternumGlobalConfig.exploration.reward;

useEffect(() => {
if (!selectedEntity || !stamina) return;

Expand All @@ -33,7 +40,7 @@ export const useTravelPath = () => {
selectedEntity.position,
exploredHexes,
maxTravelPossible,
canExplore(stamina?.amount, food),
canExplore(stamina?.amount, food, hasCapacity),
);

const path = Array.from(pathMap.entries()).map(([key, path]) => {
Expand Down
5 changes: 4 additions & 1 deletion client/src/ui/components/worldmap/hexagon/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const getPositionsAtIndex = (mesh: InstancedMesh<any, any>, index: number
return positions;
};

export const canExplore = (stamina: number | undefined, food: Resource[]) => {
export const canExplore = (stamina: number | undefined, food: Resource[], hasCapacity: boolean) => {
if (stamina && stamina < EternumGlobalConfig.stamina.exploreCost) {
return false;
}
Expand All @@ -55,6 +55,9 @@ export const canExplore = (stamina: number | undefined, food: Resource[]) => {
if ((wheat?.amount || 0) < EternumGlobalConfig.exploration.wheatBurn) {
return false;
}
if (!hasCapacity) {
return false;
}

return true;
};
Expand Down
16 changes: 16 additions & 0 deletions client/src/ui/elements/ArmyCapacity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ArmyInfo } from "@/hooks/helpers/useArmies";
import { EternumGlobalConfig } from "@bibliothecadao/eternum";
import clsx from "clsx";

export const ArmyCapacity = ({ army, className }: { army: ArmyInfo; className?: string }) => {
return (
<div
className={clsx(
army.capacity - army.weight < EternumGlobalConfig.exploration.reward ? "text-red" : "",
className,
)}
>
Capacity : {army.weight} / {army.capacity}
</div>
);
};
3 changes: 2 additions & 1 deletion sdk/packages/eternum/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ export const setCapacityConfig = async (account: Account, provider: EternumProvi
const txArmy = await provider.set_capacity_config({
signer: account,
entity_type: ARMY_ENTITY_TYPE,
weight_gram: EternumGlobalConfig.carryCapacity.army * EternumGlobalConfig.resources.resourcePrecision,
// No precision mod used because weight check in contract uses army qty x precision
weight_gram: EternumGlobalConfig.carryCapacity.army,
});

console.log(`Configuring capacity Army config ${txArmy.statusReceipt}...`);
Expand Down

0 comments on commit 594ab5a

Please sign in to comment.