Skip to content

Commit

Permalink
Merge pull request #2431 from BibliothecaDAO/next
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
ponderingdemocritus authored Dec 11, 2024
2 parents aab7250 + 6f7e906 commit c51a01c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 32 deletions.
6 changes: 3 additions & 3 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
},
"dependencies": {
"@bibliothecadao/eternum": "workspace:^",
"@cartridge/connector": "0.5.4",
"@cartridge/controller": "0.5.4",
"@cartridge/connector": "0.5.5",
"@cartridge/controller": "0.5.5",
"@dojoengine/core": "1.0.1",
"@dojoengine/create-burner": "1.0.1",
"@dojoengine/react": "1.0.1",
Expand Down Expand Up @@ -104,4 +104,4 @@
"vitest": "^2.0.5",
"workbox-window": "^7.3.0"
}
}
}
20 changes: 19 additions & 1 deletion client/src/dojo/modelManager/BattleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ArmyInfo } from "@/hooks/helpers/useArmies";
import { Structure } from "@/hooks/helpers/useStructures";
import { Health } from "@/types";
import { multiplyByPrecision } from "@/ui/utils/utils";
import { BattleSide, EternumGlobalConfig, ID } from "@bibliothecadao/eternum";
import { BattleSide, EternumGlobalConfig, ID, MIN_TROOPS_BATTLE } from "@bibliothecadao/eternum";
import {
ComponentValue,
Components,
Expand Down Expand Up @@ -38,6 +38,7 @@ export enum RaidStatus {
OwnStructure = "Can't raid your own structure",
NoArmy = "No army selected",
ArmyNotInBattle = "Selected army not in this battle",
MinTroops = "Minimum 100 troops required",
}

export enum LeaveStatus {
Expand All @@ -48,6 +49,7 @@ export enum LeaveStatus {
}

export enum BattleStartStatus {
MinTroops = "Minimum 100 troops required",
BattleStart = "Start battle",
ForceStart = "Force start",
NothingToAttack = "Nothing to attack",
Expand Down Expand Up @@ -312,6 +314,13 @@ export class BattleManager {

if (structure.isMine) return RaidStatus.OwnStructure;

// Calculate total troops
const totalTroops = selectedArmy.troops
? Object.values(selectedArmy.troops).reduce((sum, count) => sum + Number(count), 0)
: 0;

if (totalTroops < MIN_TROOPS_BATTLE) return RaidStatus.MinTroops;

const staminaManager = new StaminaManager(this.dojo.setup, selectedArmy.entity_id);
if (staminaManager.getStamina(currentArmiesTick).amount === 0) return RaidStatus.NoStamina;

Expand All @@ -323,6 +332,15 @@ export class BattleManager {
defender: ArmyInfo | undefined,
currentTimestamp: number,
): BattleStartStatus {
if (!selectedArmy) return BattleStartStatus.NothingToAttack;

// Calculate total troops
const totalTroops = selectedArmy.troops
? Object.values(selectedArmy.troops).reduce((sum, count) => sum + Number(count), 0)
: 0;

if (totalTroops < MIN_TROOPS_BATTLE) return BattleStartStatus.MinTroops;

if (!defender) return BattleStartStatus.NothingToAttack;

if (!this.isBattle() && defender.health.current > 0n) return BattleStartStatus.BattleStart;
Expand Down
39 changes: 25 additions & 14 deletions client/src/hooks/context/starknet-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { ColorMode } from "@cartridge/controller";
import { mainnet, sepolia } from "@starknet-react/chains";
import { Connector, StarknetConfig, jsonRpcProvider, voyager } from "@starknet-react/core";
import { env } from "../../../env";
import { mainnetPolicies } from "./mainnet-policies";
import { policies } from "./policies";
import { signingPolicy } from "./signing-policy";

const preset: string = "eternum";
const theme: string = "eternum";
const slot: string = env.VITE_PUBLIC_SLOT;
const namespace: string = "s0_eternum";
Expand All @@ -20,19 +20,30 @@ const vrfPolicy = {
description: "Allows requesting random numbers from the VRF provider",
};

const signingPolicies = env.VITE_PUBLIC_CHAIN === "mainnet" ? mainnetPolicies : policies;

const controller = new ControllerConnector({
rpc: env.VITE_PUBLIC_NODE_URL,
namespace,
slot,
policies: [...signingPolicies, ...signingPolicy, vrfPolicy],
theme,
tokens: {
erc20: ["0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49"],
},
colorMode,
});
const controller =
env.VITE_PUBLIC_CHAIN === "mainnet"
? new ControllerConnector({
rpc: env.VITE_PUBLIC_NODE_URL,
namespace,
slot,
preset,
tokens: {
erc20: ["0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49"],
},
colorMode,
})
: new ControllerConnector({
rpc: env.VITE_PUBLIC_NODE_URL,
namespace,
slot,
preset,
policies: [...signingPolicy, ...policies, vrfPolicy],
theme,
tokens: {
erc20: ["0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49"],
},
colorMode,
});

export function StarknetProvider({ children }: { children: React.ReactNode }) {
const rpc = useCallback(() => {
Expand Down
13 changes: 10 additions & 3 deletions client/src/ui/modules/military/battle-view/BattleActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,16 @@ export const BattleActions = ({
];

if (raidStatus !== RaidStatus.isRaidable) {
setTooltip({ content: <div className="">{raidStatus}</div>, position: "top" });
} else if (selectedArmy?.battle_id !== 0) {
content.push(<div>Raiding will make you leave and lose 25% of your army</div>);
setTooltip({ content: <div className="text-center">{raidStatus}</div>, position: "top" });
return;
}

if (selectedArmy?.battle_id !== 0) {
content.push(
<div key="warning" className="text-center text-red mt-2">
Raiding will make you leave and lose 25% of your army
</div>,
);
}

setTooltip({
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tailwindcss": "^3.4.1"
}
},
"packageManager": "[email protected]+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
}
45 changes: 35 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/packages/eternum/src/constants/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TROOPS_FOOD_CONSUMPTION, TROOPS_STAMINAS } from "./troops";

import { ResourcesIds } from ".";

export const MIN_TROOPS_BATTLE = 100_000;
export const FELT_CENTER = 2147483646;
export const WORLD_CONFIG_ID = 999999999n;
export const HYPERSTRUCTURE_CONFIG_ID = 999999992n;
Expand Down

0 comments on commit c51a01c

Please sign in to comment.