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

Enh/merge troops capacity check #1873

Open
wants to merge 2 commits into
base: rc-1
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.7.0
dojo 1.0.0-alpha.12
dojo 1.0.0-alpha.16
2 changes: 1 addition & 1 deletion client/.env.preview
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VITE_PUBLIC_MASTER_ADDRESS="0x1a3e37c77be7de91a9177c6b57956faa6da25607e567b10a25cf64fea5e533b"
VITE_PUBLIC_MASTER_PRIVATE_KEY="0x4ab5a607d92f0870cfd82ef9cecb2fe903830441180fd432b831a8863c08097"
VITE_PUBLIC_WORLD_ADDRESS="0x76ca3dfc3e96843716f882546f0db96b7da0cf988bdba284b469d0defb2f48f"
VITE_PUBLIC_WORLD_ADDRESS="0x320b2713e324fe3125bbc42d85ff69cb3c0908b436fa38a35746dbc45deeb11"
VITE_PUBLIC_ACCOUNT_CLASS_HASH="0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c"
VITE_EVENT_KEY="0x1a2f334228cee715f1f0f54053bb6b5eac54fa336e0bc1aacf7516decb0471d"
VITE_PUBLIC_TORII="https://api.cartridge.gg/x/eternum-42/torii"
Expand Down
2 changes: 1 addition & 1 deletion client/.env.production
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VITE_PUBLIC_MASTER_ADDRESS="0x779c2c098f066ddde5850ec8426511e46e6499adf0b5c77e8961917413b57db"
VITE_PUBLIC_MASTER_PRIVATE_KEY="0x189765c7b9daa2efdd3025d3236a929ce5e510834b8cba4f002a0d5c1accb5a"
VITE_PUBLIC_WORLD_ADDRESS="0x76ca3dfc3e96843716f882546f0db96b7da0cf988bdba284b469d0defb2f48f"
VITE_PUBLIC_WORLD_ADDRESS="0x320b2713e324fe3125bbc42d85ff69cb3c0908b436fa38a35746dbc45deeb11"
VITE_PUBLIC_ACCOUNT_CLASS_HASH="0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c"
VITE_EVENT_KEY="0x1a2f334228cee715f1f0f54053bb6b5eac54fa336e0bc1aacf7516decb0471d"
VITE_PUBLIC_TORII="https://api.cartridge.gg/x/eternum-42/torii"
Expand Down
2 changes: 1 addition & 1 deletion client/src/dojo/modelManager/utils/ArmyMovementUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const getArmyWeight = (weight: ComponentValue<ClientComponents["Weight"]["schema
return weight.value / BigInt(EternumGlobalConfig.resources.resourcePrecision);
};

const getArmyNumberOfTroops = (army: ComponentValue<ClientComponents["Army"]["schema"]>) => {
export const getArmyNumberOfTroops = (army: ComponentValue<ClientComponents["Army"]["schema"]>) => {
const knights = army.troops.knight_count || 0n;
const crossbowmen = army.troops.crossbowman_count || 0n;
const paladins = army.troops.paladin_count || 0n;
Expand Down
6 changes: 6 additions & 0 deletions client/src/ui/components/hyperstructures/StructureCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ArrowRight } from "lucide-react";
import { useMemo, useState } from "react";
import { StructureListItem } from "../worldmap/structures/StructureListItem";
import { ResourceExchange } from "./ResourceExchange";
import { ArmyCapacity } from "@/ui/elements/ArmyCapacity";

export const StructureCard = ({
position,
Expand Down Expand Up @@ -255,6 +256,11 @@ const TroopExchange = ({
<div className="flex flex-row justify-around items-center">
<div className="w-[60%] mr-1 bg-gold/20">
<p className="pt-2 pb-1 text-center">{giverArmyName}</p>
<ArmyCapacity
army={transferDirection === "to" ? getArmy(giverArmyEntityId) : takerArmy || getArmy(protector!.army_id)}
className="flex justify-center"
deductedTroops={Object.values(troopsGiven).reduce((a, b) => a + b, 0n)}
/>
{Object.entries(troopsToFormat(attackerArmyTroops)).map(([resourceId, amount]: [string, bigint]) => {
return (
<div
Expand Down
86 changes: 47 additions & 39 deletions client/src/ui/elements/ArmyCapacity.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ReactComponent as Inventory } from "@/assets/icons/common/bagpack.svg";
import { configManager } from "@/dojo/setup";
import { getArmyNumberOfTroops } from "@/dojo/modelManager/utils/ArmyMovementUtils";
import { ArmyInfo } from "@/hooks/helpers/useArmies";
import useUIStore from "@/hooks/store/useUIStore";
import { useMemo } from "react";
Expand All @@ -11,57 +12,64 @@ enum CapacityColor {
HEAVY = "bg-red",
}

export const ArmyCapacity = ({ army, className }: { army: ArmyInfo | undefined; className?: string }) => {
type ArmyCapacityProps = {
army: ArmyInfo | undefined;
className?: string;
deductedTroops?: bigint;
};

export const ArmyCapacity = ({ army, className, deductedTroops = 0n }: ArmyCapacityProps) => {
if (!army) return null;

const totalTroops = getArmyNumberOfTroops(army);
const remainingTroops = totalTroops - deductedTroops;
const capacityRatio = Number(remainingTroops) / Number(totalTroops);
const armyTotalCapacity = BigInt(Number(army.totalCapacity) * capacityRatio);

const setTooltip = useUIStore((state) => state.setTooltip);
const remainingCapacity = useMemo(() => army.totalCapacity - army.weight, [army]);
const remainingCapacity = useMemo(() => armyTotalCapacity - army.weight, [army]);

const capacityColor = useMemo(() => {
if (army.weight >= army.totalCapacity) return CapacityColor.HEAVY;
if (army.weight >= armyTotalCapacity) return CapacityColor.HEAVY;
if (remainingCapacity < BigInt(configManager.getExploreReward())) return CapacityColor.MEDIUM;
return CapacityColor.LIGHT;
}, [remainingCapacity]);

const weightPercentage = useMemo(() => ((Number(army.weight) / Number(army.totalCapacity)) * 100).toFixed(0), [army]);
const weightPercentage = useMemo(() => ((Number(army.weight) / Number(armyTotalCapacity)) * 100).toFixed(0), [army]);

return (
army.totalCapacity !== 0n && (
<div className={`flex flex-row text-xxs ${className}`}>
<div className="mr-1">{`${formatNumber(Number(army.weight) / 1000, 1)}K/${formatNumber(
Number(army.totalCapacity) / 1000,
1,
)}K`}</div>
<div
onMouseEnter={() => {
setTooltip({
content: (
<>
<div>
Capacity: {formatNumber(Number(army.weight), 0)} / {formatNumber(Number(army.totalCapacity), 0)} kg
</div>
{capacityColor !== CapacityColor.LIGHT && (
<div className="text-red">Offload to continue exploring</div>
)}
</>
),
position: "right",
});
}}
onMouseLeave={() => {
setTooltip(null);
}}
className={`flex flex-col text-xs font-bold uppercase self-center`}
>
<div className="bg-gray-200 rounded-full h-1.5 dark:bg-gray-700 border border-y w-16">
<div
className={`${capacityColor} h-1 rounded-full`}
style={{ width: `${Math.min(Number(weightPercentage), 100)}%` }}
></div>
</div>
<div className={`flex flex-row text-xxs ${className}`}>
<div className="mr-1">{`${formatNumber(Number(army.weight) / 1000, 1)}K/${formatNumber(
Number(armyTotalCapacity) / 1000,
1,
)}K`}</div>
<div
onMouseEnter={() => {
setTooltip({
content: (
<>
<div>
Capacity: {formatNumber(Number(army.weight), 0)} / {formatNumber(Number(armyTotalCapacity), 0)} kg
</div>
{capacityColor !== CapacityColor.LIGHT && <div className="text-red">Offload to continue exploring</div>}
</>
),
position: "right",
});
}}
onMouseLeave={() => {
setTooltip(null);
}}
className={`flex flex-col text-xs font-bold uppercase self-center`}
>
<div className="bg-gray-200 rounded-full h-1.5 dark:bg-gray-700 border border-y w-16">
<div
className={`${capacityColor} h-1 rounded-full`}
style={{ width: `${Math.min(Number(weightPercentage), 100)}%` }}
></div>
</div>
<Inventory className="fill-order-giants w-2 ml-1" />
</div>
)
<Inventory className="fill-order-giants w-2 ml-1" />
</div>
);
};
2 changes: 1 addition & 1 deletion contracts/.tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.7.0
dojo 1.0.0-alpha.12
dojo 1.0.0-alpha.16
58 changes: 40 additions & 18 deletions contracts/manifests/dev/deployment/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@
],
"address": "0x320b2713e324fe3125bbc42d85ff69cb3c0908b436fa38a35746dbc45deeb11",
"transaction_hash": "0x5a4deb5ecebc548f10b969db5496f9605a3b862dc766bb419c42615ff735760",
"block_number": 50,
"block_number": 51,
"seed": "eternum",
"metadata": {
"profile_name": "dev",
Expand Down Expand Up @@ -2016,8 +2016,8 @@
{
"kind": "DojoContract",
"address": "0x6bde5ad6204af5e31e9b66f687f5ccd84fcefa1b61dfba9bda8ca331252e641",
"class_hash": "0x415b7fbec04a92d4b9c0e8609ac23369eb7a321382b832f7d364b99d84170da",
"original_class_hash": "0x415b7fbec04a92d4b9c0e8609ac23369eb7a321382b832f7d364b99d84170da",
"class_hash": "0x2253d2b111c13991bee5248d1eb5b1b98fb69a0af8e3fc7b13383adb0d41bde",
"original_class_hash": "0x2253d2b111c13991bee5248d1eb5b1b98fb69a0af8e3fc7b13383adb0d41bde",
"base_class_hash": "0x2427dd10a58850ac9a5ca6ce04b7771b05330fd18f2e481831ad903b969e6b2",
"abi": [
{
Expand Down Expand Up @@ -4948,8 +4948,8 @@
{
"kind": "DojoContract",
"address": "0x5886dd9b433da22f890ff62d65de352ece2e98e8b5a54fd73dbe5dfa5c212c2",
"class_hash": "0x2119c7396a5673deee312df02e6ec7108a2ea6e080ba01cfc06bd5020887d68",
"original_class_hash": "0x2119c7396a5673deee312df02e6ec7108a2ea6e080ba01cfc06bd5020887d68",
"class_hash": "0x6df101d21ff5eb3855e99b271537f9c5e872fe16e1c8861fab8315c32079252",
"original_class_hash": "0x6df101d21ff5eb3855e99b271537f9c5e872fe16e1c8861fab8315c32079252",
"base_class_hash": "0x2427dd10a58850ac9a5ca6ce04b7771b05330fd18f2e481831ad903b969e6b2",
"abi": [
{
Expand Down Expand Up @@ -5140,14 +5140,18 @@
},
{
"type": "enum",
"name": "core::bool",
"name": "eternum::models::hyperstructure::Access",
"variants": [
{
"name": "False",
"name": "Public",
"type": "()"
},
{
"name": "True",
"name": "Private",
"type": "()"
},
{
"name": "GuildOnly",
"type": "()"
}
]
Expand Down Expand Up @@ -5230,15 +5234,15 @@
},
{
"type": "function",
"name": "set_private",
"name": "set_access",
"inputs": [
{
"name": "hyperstructure_entity_id",
"type": "core::integer::u32"
},
{
"name": "private",
"type": "core::bool"
"name": "access",
"type": "eternum::models::hyperstructure::Access"
}
],
"outputs": [],
Expand Down Expand Up @@ -5331,7 +5335,7 @@
"systems": [
"contribute_to_construction",
"set_co_owners",
"set_private",
"set_access",
"end_game"
],
"manifest_name": "eternum-hyperstructure_systems-3660009a"
Expand Down Expand Up @@ -26194,13 +26198,13 @@
"key": false
},
{
"name": "private",
"type": "bool",
"name": "access",
"type": "Access",
"key": false
}
],
"class_hash": "0x191b6a74aef66c4146874f50a63809c039c6243f2d0d643693fa5690990e0a4",
"original_class_hash": "0x191b6a74aef66c4146874f50a63809c039c6243f2d0d643693fa5690990e0a4",
"class_hash": "0x338d3e6593d7cdd1088edd18bbd44f38598d465f533dac605135b7e6cd754e1",
"original_class_hash": "0x338d3e6593d7cdd1088edd18bbd44f38598d465f533dac605135b7e6cd754e1",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -26583,6 +26587,24 @@
}
]
},
{
"type": "enum",
"name": "eternum::models::hyperstructure::Access",
"variants": [
{
"name": "Public",
"type": "()"
},
{
"name": "Private",
"type": "()"
},
{
"name": "GuildOnly",
"type": "()"
}
]
},
{
"type": "struct",
"name": "eternum::models::hyperstructure::Hyperstructure",
Expand All @@ -26608,8 +26630,8 @@
"type": "core::integer::u64"
},
{
"name": "private",
"type": "core::bool"
"name": "access",
"type": "eternum::models::hyperstructure::Access"
}
]
},
Expand Down
20 changes: 10 additions & 10 deletions contracts/manifests/dev/deployment/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ original_class_hash = "0x6f38d5d9507c5d9546290e1a27e309efe5a9af3770b6cc1627db4a1
abi = "manifests/dev/deployment/abis/dojo-world.json"
address = "0x320b2713e324fe3125bbc42d85ff69cb3c0908b436fa38a35746dbc45deeb11"
transaction_hash = "0x5a4deb5ecebc548f10b969db5496f9605a3b862dc766bb419c42615ff735760"
block_number = 50
block_number = 51
seed = "eternum"
manifest_name = "dojo-world"

Expand Down Expand Up @@ -60,8 +60,8 @@ manifest_name = "eternum-building_systems-4b0f3026"
[[contracts]]
kind = "DojoContract"
address = "0x6bde5ad6204af5e31e9b66f687f5ccd84fcefa1b61dfba9bda8ca331252e641"
class_hash = "0x415b7fbec04a92d4b9c0e8609ac23369eb7a321382b832f7d364b99d84170da"
original_class_hash = "0x415b7fbec04a92d4b9c0e8609ac23369eb7a321382b832f7d364b99d84170da"
class_hash = "0x2253d2b111c13991bee5248d1eb5b1b98fb69a0af8e3fc7b13383adb0d41bde"
original_class_hash = "0x2253d2b111c13991bee5248d1eb5b1b98fb69a0af8e3fc7b13383adb0d41bde"
base_class_hash = "0x2427dd10a58850ac9a5ca6ce04b7771b05330fd18f2e481831ad903b969e6b2"
abi = "manifests/dev/deployment/abis/contracts/eternum-combat_systems-27f37676.json"
reads = []
Expand Down Expand Up @@ -189,8 +189,8 @@ manifest_name = "eternum-guild_systems-38e127d4"
[[contracts]]
kind = "DojoContract"
address = "0x5886dd9b433da22f890ff62d65de352ece2e98e8b5a54fd73dbe5dfa5c212c2"
class_hash = "0x2119c7396a5673deee312df02e6ec7108a2ea6e080ba01cfc06bd5020887d68"
original_class_hash = "0x2119c7396a5673deee312df02e6ec7108a2ea6e080ba01cfc06bd5020887d68"
class_hash = "0x6df101d21ff5eb3855e99b271537f9c5e872fe16e1c8861fab8315c32079252"
original_class_hash = "0x6df101d21ff5eb3855e99b271537f9c5e872fe16e1c8861fab8315c32079252"
base_class_hash = "0x2427dd10a58850ac9a5ca6ce04b7771b05330fd18f2e481831ad903b969e6b2"
abi = "manifests/dev/deployment/abis/contracts/eternum-hyperstructure_systems-3660009a.json"
reads = []
Expand All @@ -200,7 +200,7 @@ tag = "eternum-hyperstructure_systems"
systems = [
"contribute_to_construction",
"set_co_owners",
"set_private",
"set_access",
"end_game",
]
manifest_name = "eternum-hyperstructure_systems-3660009a"
Expand Down Expand Up @@ -1618,8 +1618,8 @@ key = false

[[models]]
kind = "DojoModel"
class_hash = "0x191b6a74aef66c4146874f50a63809c039c6243f2d0d643693fa5690990e0a4"
original_class_hash = "0x191b6a74aef66c4146874f50a63809c039c6243f2d0d643693fa5690990e0a4"
class_hash = "0x338d3e6593d7cdd1088edd18bbd44f38598d465f533dac605135b7e6cd754e1"
original_class_hash = "0x338d3e6593d7cdd1088edd18bbd44f38598d465f533dac605135b7e6cd754e1"
abi = "manifests/dev/deployment/abis/models/eternum-Hyperstructure-767c75e3.json"
tag = "eternum-Hyperstructure"
qualified_path = "eternum::models::hyperstructure::hyperstructure"
Expand Down Expand Up @@ -1651,8 +1651,8 @@ type = "u64"
key = false

[[models.members]]
name = "private"
type = "bool"
name = "access"
type = "Access"
key = false

[[models]]
Expand Down
4 changes: 2 additions & 2 deletions contracts/scripts/env_variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
STARKNET_RPC_URL="http://localhost:5050"
DOJO_ACCOUNT_ADDRESS="0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca"
DOJO_PRIVATE_KEY="0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a"
SOZO_WORLD="0x76ca3dfc3e96843716f882546f0db96b7da0cf988bdba284b469d0defb2f48f"
SOZO_WORLD="0x320b2713e324fe3125bbc42d85ff69cb3c0908b436fa38a35746dbc45deeb11"
KATANA_TOML_PATH="./manifests/dev/deployment/manifest.toml"

# Check if the first argument is provided and set it to "dev" or "prod"
Expand All @@ -14,7 +14,7 @@ if [[ ! -z "$1" ]]; then
STARKNET_RPC_URL="https://api.cartridge.gg/x/eternum-42/katana/"
# DOJO_ACCOUNT_ADDRESS="" # number 4
# DOJO_PRIVATE_KEY="" # number 4
SOZO_WORLD="0x76ca3dfc3e96843716f882546f0db96b7da0cf988bdba284b469d0defb2f48f"
SOZO_WORLD="0x320b2713e324fe3125bbc42d85ff69cb3c0908b436fa38a35746dbc45deeb11"
KATANA_TOML_PATH="./manifests/prod/deployment/manifest.toml"
elif [[ "$1" != "dev" ]]; then
echo "Invalid argument. Use 'dev' or 'prod'."
Expand Down
Loading
Loading