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

add lost troops in pillage #2122

Merged
merged 10 commits into from
Nov 20, 2024
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
20 changes: 19 additions & 1 deletion client/src/dojo/contractComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,16 @@ const eventsComponents = (world: World) => {
pillager_army_entity_id: RecsType.Number,
pillaged_structure_owner: RecsType.BigInt,
pillaged_structure_entity_id: RecsType.Number,
attacker_lost_troops: {
knight_count: RecsType.BigInt,
paladin_count: RecsType.BigInt,
crossbowman_count: RecsType.BigInt,
},
structure_lost_troops: {
knight_count: RecsType.BigInt,
paladin_count: RecsType.BigInt,
crossbowman_count: RecsType.BigInt,
},
pillaged_structure_owner_name: RecsType.BigInt,
winner: RecsType.String,
x: RecsType.Number,
Expand All @@ -1640,6 +1650,14 @@ const eventsComponents = (world: World) => {
"u32",
"ContractAddress",
"u32",
// attacking troops
"u64",
"u64",
"u64",
// structure troops
"u64",
"u64",
"u64",
"felt252",
"BattleSide",
"u32",
Expand All @@ -1649,7 +1667,7 @@ const eventsComponents = (world: World) => {
"enum",
"u64",
],
customTypes: ["BuildingCategory"],
customTypes: ["Troops", "BuildingCategory"],
},
},
);
Expand Down
6 changes: 3 additions & 3 deletions client/src/ui/components/battles/BattleListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useUIStore from "@/hooks/store/useUIStore";
import { getComponentValue, HasValue, runQuery } from "@dojoengine/recs";
import React, { useMemo, useState } from "react";
import { ViewOnMapIcon } from "../military/ArmyManagementCard";
import { TroopMenuRow } from "../military/TroopChip";
import { TroopDisplay } from "../military/TroopChip";
import { InventoryResources } from "../resources/InventoryResources";

type BattleListItemProps = {
Expand Down Expand Up @@ -94,7 +94,7 @@ export const BattleListItem = ({ battle, ownArmySelected, showCompass = false }:
<div className={`flex flex-col w-[27rem] h-full justify-between bg-red/20 rounded-md border-gold/20 p-2`}>
<div className="flex w-full justify-between">
<div className="flex flex-col w-[40%]">
<TroopMenuRow troops={updatedBattle?.attack_army?.troops} />
<TroopDisplay troops={updatedBattle?.attack_army?.troops} />
</div>
<div className="flex flex-col font-bold m-auto relative">
{showCompass && <ViewOnMapIcon hideTooltip={true} position={battle?.position} />}
Expand All @@ -119,7 +119,7 @@ export const BattleListItem = ({ battle, ownArmySelected, showCompass = false }:
/>
</div>
<div className="flex flex-col content-center w-[40%]">
<TroopMenuRow troops={updatedBattle?.defence_army?.troops} />
<TroopDisplay troops={updatedBattle?.defence_army?.troops} />
</div>
</div>
{showInventory && (
Expand Down
4 changes: 2 additions & 2 deletions client/src/ui/components/military/ArmyChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useLocation } from "wouter";
import { InventoryResources } from "../resources/InventoryResources";
import { Exchange } from "../structures/worldmap/StructureCard";
import { ArmyManagementCard, ViewOnMapIcon } from "./ArmyManagementCard";
import { TroopMenuRow } from "./TroopChip";
import { TroopDisplay } from "./TroopChip";

export const NavigateToPositionIcon = ({
position,
Expand Down Expand Up @@ -186,7 +186,7 @@ export const ArmyChip = ({
)}
</div>
<div className="flex flex-col content-center w-[55%]">
<TroopMenuRow troops={updatedArmy!.troops} />
<TroopDisplay troops={updatedArmy!.troops} />
{showInventory && (
<InventoryResources
entityIds={[updatedArmy!.entity_id]}
Expand Down
88 changes: 61 additions & 27 deletions client/src/ui/components/military/PillageHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { divideByPrecision, formatResources, formatTime } from "@/ui/utils/utils
import { BattleSide, ID, Resource } from "@bibliothecadao/eternum";
import { ComponentValue, defineQuery, getComponentValue, HasValue, isComponentUpdate } from "@dojoengine/recs";
import { useEffect, useMemo, useState } from "react";
import { TroopDisplay } from "./TroopChip";

type PillageEvent = ComponentValue<ClientComponents["events"]["BattlePillageData"]["schema"]>;

Expand All @@ -14,38 +15,72 @@ const PillageHistoryItem = ({ addressName, history }: { addressName: string; his
const formattedResources = useMemo(() => formatResources(history.pillaged_resources), [history.pillaged_resources]);

return (
<div className="group hover:bg-gold/10 relative bg-gold/20 text-gold p-3">
<div className="flex w-full justify-between font-bold ">
<div className={` ${isSuccess ? "text-order-brilliance" : "text-order-giants"}`}>
{isSuccess ? "success" : "fail"}
<div className="group relative bg-gold/20 hover:bg-gold/10 rounded-lg p-4 transition-all duration-300">
<div className="flex items-center justify-between mb-4">
<div className={`text-base font-semibold uppercase tracking-wider ${isSuccess ? "text-green" : "text-red"}`}>
{isSuccess ? "Successful Raid" : "Failed Raid"}
</div>
<div>{`player: ${addressName}`}</div>
<div className="text-gold/80 font-medium">by: {addressName}</div>
</div>
<div className="flex text-xs justify-between mb-2 mt-2">
<div className="flex flex-col text-xs items-center">
<div>Stolen Resources</div>
<div className="flex mt-1 flex-wrap gap-2 justify-center">
{formattedResources.length > 0
? formattedResources.map((resource: Resource) => (
<ResourceCost
size="sm"
textSize="xs"
key={resource.resourceId}
resourceId={resource.resourceId}
amount={divideByPrecision(resource.amount)}
/>
))
: "None"}

<div className="grid grid-cols-4 gap-6 mb-4">
{/* Stolen Resources */}
<div className="flex flex-col items-center">
<div className="text-xs font-semibold mb-2 text-gold/90">Stolen Resources</div>
<div className="flex flex-wrap gap-2 justify-center min-h-[60px] items-center">
{formattedResources.length > 0 ? (
aymericdelab marked this conversation as resolved.
Show resolved Hide resolved
formattedResources.map((resource: Resource) => (
<ResourceCost
size="sm"
textSize="xs"
key={resource.resourceId}
resourceId={resource.resourceId}
amount={divideByPrecision(resource.amount)}
/>
))
) : (
<span className="text-gold/60 text-sm">None</span>
)}
</div>
</div>
<div className="flex flex-col text-xs items-center">
<div>Destroyed Building</div>
<div className="text-center mt-1">
{history.destroyed_building_category.replace(/([A-Z])/g, " $1").trim()}

{/* Pillager Troops Lost */}
<div className="flex flex-col">
<div className="text-xs font-semibold mb-2 text-gold/90">Pillagers</div>
<TroopDisplay
troops={history.attacker_lost_troops}
className="origin-top-left"
negative
iconSize="sm"
direction="column"
/>
</div>

{/* Structure Troops Lost */}
<div className="flex flex-col">
<div className="text-xs font-semibold mb-2 text-gold/90">Structure</div>
<TroopDisplay
troops={history.structure_lost_troops}
className="origin-top-left"
negative
iconSize="sm"
direction="column"
/>
</div>

{/* Destroyed Building */}
<div className="flex flex-col items-center">
<div className="text-xs font-semibold mb-2 text-gold/90">Destroyed Building</div>
<div className="flex flex-wrap gap-2 justify-center min-h-[60px] items-center">
<div className="text-center text-sm text-gold/80">
{history.destroyed_building_category.replace(/([A-Z])/g, " $1").trim()}
</div>
</div>
</div>
</div>
<div className="absolute bottom-1 right-2 text-xs text-gold/60">

{/* Timestamp */}
<div className="absolute bottom-2 right-4 text-xs text-gold/60 italic">
{`${formatTime(Date.now() / 1000 - history.timestamp)} ago`}
</div>
</div>
Expand All @@ -60,7 +95,6 @@ export const PillageHistory = ({ structureId }: { structureId: ID }) => {
} = useDojo();

const [pillageHistory, setPillageHistory] = useState<PillageEvent[]>([]);

const { getAddressNameFromEntity } = useEntitiesUtils();

useEffect(() => {
Expand All @@ -79,7 +113,7 @@ export const PillageHistory = ({ structureId }: { structureId: ID }) => {
}, [events.BattlePillageData, structureId]);

return (
<div className="p-6 h-full pt-2">
<div className="p-1 h-full">
<div className="overflow-auto h-full">
<div className="overflow-scroll-y grid grid-cols-1 gap-4">
{pillageHistory
Expand Down
63 changes: 52 additions & 11 deletions client/src/ui/components/military/TroopChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,69 @@ import { ResourceIcon } from "@/ui/elements/ResourceIcon";
import { currencyFormat } from "@/ui/utils/utils";
import { ComponentValue } from "@dojoengine/recs";

export const TroopMenuRow = ({
export const TroopDisplay = ({
troops,
className,
direction = "row",
iconSize = "lg",
negative = false,
}: {
troops: ComponentValue<ClientComponents["Army"]["schema"]["troops"]> | undefined;
className?: string;
direction?: "row" | "column";
iconSize?: "sm" | "md" | "lg";
negative?: boolean;
}) => {
return (
<div className={`grid grid-cols-3 gap-2 relative w-full text-gold font-bold ${className}`}>
<div className="px-2 py-1 bg-gold/10 flex flex-col justify-between gap-2 border border-gold/10">
<ResourceIcon withTooltip={false} resource={"Crossbowman"} size="lg" />
<div className="text-green text-xs self-center">
<div
className={`grid ${
direction === "row" ? "grid-cols-3" : "grid-cols-1"
} gap-2 relative w-full text-gold font-bold ${className}`}
>
<div
className={`px-2 py-1 bg-gold/10 flex ${
direction === "row" ? "flex-col" : "flex-row"
} justify-between gap-2 border border-gold/10`}
>
<ResourceIcon withTooltip={false} resource={"Crossbowman"} size={iconSize} />
<div
className={`${
Number(troops?.crossbowman_count || 0) === 0 ? "text-gold" : negative ? "text-red" : "text-green"
} text-xs self-center`}
>
{Number(troops?.crossbowman_count || 0) === 0 ? "" : negative ? "-" : ""}
{currencyFormat(Number(troops?.crossbowman_count || 0), 0)}
</div>
aymericdelab marked this conversation as resolved.
Show resolved Hide resolved
</div>
<div className="px-2 py-1 bg-gold/10 flex flex-col justify-between gap-2 border border-gold/10">
<ResourceIcon withTooltip={false} resource={"Knight"} size="lg" />
<div className="text-green text-xs self-center">{currencyFormat(Number(troops?.knight_count || 0), 0)}</div>
<div
className={`px-2 py-1 bg-gold/10 flex ${
direction === "row" ? "flex-col" : "flex-row"
} justify-between gap-2 border border-gold/10`}
>
<ResourceIcon withTooltip={false} resource={"Knight"} size={iconSize} />
<div
className={`${
Number(troops?.knight_count || 0) === 0 ? "text-gold" : negative ? "text-red" : "text-green"
} text-xs self-center`}
>
{Number(troops?.knight_count || 0) === 0 ? "" : negative ? "-" : ""}
{currencyFormat(Number(troops?.knight_count || 0), 0)}
</div>
</div>
<div className="px-2 py-1 bg-gold/10 flex flex-col justify-between gap-2 border border-gold/10">
<ResourceIcon withTooltip={false} resource={"Paladin"} size="lg" />
<div className="text-green text-xs self-center">{currencyFormat(Number(troops?.paladin_count || 0), 0)}</div>
<div
className={`px-2 py-1 bg-gold/10 flex ${
direction === "row" ? "flex-col" : "flex-row"
} justify-between gap-2 border border-gold/10`}
>
<ResourceIcon withTooltip={false} resource={"Paladin"} size={iconSize} />
<div
className={`${
Number(troops?.paladin_count || 0) === 0 ? "text-gold" : negative ? "text-red" : "text-green"
} text-xs self-center`}
>
{Number(troops?.paladin_count || 0) === 0 ? "" : negative ? "-" : ""}
{currencyFormat(Number(troops?.paladin_count || 0), 0)}
</div>
</div>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions client/src/ui/components/trading/MarketModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ID, ResourcesIds } from "@bibliothecadao/eternum";
import { useComponentValue } from "@dojoengine/react";
import { Suspense, lazy, useMemo, useState } from "react";
import { HintModal } from "../hints/HintModal";
import { TroopMenuRow } from "../military/TroopChip";
import { TroopDisplay } from "../military/TroopChip";
import { ModalContainer } from "../ModalContainer";

const MarketResourceSidebar = lazy(() =>
Expand Down Expand Up @@ -320,7 +320,7 @@ export const MarketModal = () => {
<h3 className="text-xl font-bold mt-2">Bank Defence</h3>
<div className="flex-grow">
<div className="flex items-center text-green">
<TroopMenuRow troops={armyInfo.troops} />
<TroopDisplay troops={armyInfo.troops} />
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useUIStore from "@/hooks/store/useUIStore";
import { ResourcesIds, StructureType } from "@bibliothecadao/eternum";
import clsx from "clsx";
import { useMemo } from "react";
import { TroopMenuRow } from "../../military/TroopChip";
import { TroopDisplay } from "../../military/TroopChip";
import { InventoryResources } from "../../resources/InventoryResources";
import { RealmResourcesIO } from "../../resources/RealmResourcesIO";

Expand Down Expand Up @@ -179,7 +179,7 @@ export const StructureListItem = ({
)}
</div>
<div className="flex flex-col content-center w-[55%]">
<TroopMenuRow troops={updatedBattle?.defence_army?.troops || structure.protector?.troops} />
<TroopDisplay troops={updatedBattle?.defence_army?.troops || structure.protector?.troops} />
<InventoryResources
max={maxInventory}
entityIds={[structure.entity_id]}
Expand Down
2 changes: 1 addition & 1 deletion contracts/dojo_dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ website = "https://alpha-eternum.realms.world/"
"eternum-hyperstructure_systems",
"eternum-liquidity_systems",
"eternum-map_systems",
"eternum-map_generation_systems",
aymericdelab marked this conversation as resolved.
Show resolved Hide resolved
"eternum-name_systems",
"eternum-ownership_systems",
"eternum-realm_systems",
Expand All @@ -37,6 +38,5 @@ website = "https://alpha-eternum.realms.world/"
"eternum-trade_systems",
"eternum-travel_systems",
"eternum-troop_systems",
"eternum-map_generation_systems",
"eternum-battle_utils_systems"
]
Loading
Loading