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

ref: combat #1120

Merged
merged 3 commits into from
Jul 18, 2024
Merged

ref: combat #1120

merged 3 commits into from
Jul 18, 2024

Conversation

edisontim
Copy link
Collaborator

@edisontim edisontim commented Jul 16, 2024

PR Type

Enhancement, Tests, Bug fix


Description

  • Refactored and enhanced multiple hooks and utilities related to armies, battles, structures, and entities.
  • Updated various components to use new data structures and improved logic.
  • Added new components and unit tests for better coverage and functionality.
  • Improved battle data handling and army management across the application.

Changes walkthrough 📝

Relevant files
Enhancement
13 files
tailwind.config.js
Add utility class to hide scrollbars                                         

client/tailwind.config.js

  • Added new utility class .no-scrollbar to hide scrollbars across
    different browsers.
  • +12/-0   
    useArmies.tsx
    Refactor and enhance army-related hooks and utilities       

    client/src/hooks/helpers/useArmies.tsx

  • Refactored imports and type definitions.
  • Updated formatArmies function to improve data handling.
  • Renamed useArmies to useMovableArmies.
  • Improved army filtering logic.
  • +135/-118
    useBattles.tsx
    Refactor and enhance battle-related hooks and utilities   

    client/src/hooks/helpers/battles/useBattles.tsx

  • Refactored imports and type definitions.
  • Added getBattle function to fetch and process battle data.
  • Updated battle-related hooks to use new utility functions.
  • +74/-77 
    ArmyManagementCard.tsx
    Update ArmyManagementCard component to use new army data structure

    client/src/ui/components/military/ArmyManagementCard.tsx

  • Renamed entity prop to army.
  • Updated component to use new army data structure.
  • Added ViewOnMapIcon component.
  • +68/-27 
    BattleManager.ts
    Refactor BattleManager class for improved battle data handling

    client/src/dojo/modelManager/BattleManager.ts

  • Refactored BattleManager class to improve battle data handling.
  • Added methods to update army and battle health.
  • +122/-45
    useStructures.tsx
    Refactor and enhance structure-related hooks and utilities

    client/src/hooks/helpers/useStructures.tsx

  • Refactored imports and type definitions.
  • Updated structure-related hooks to use new utility functions.
  • +42/-58 
    useEntities.tsx
    Refactor and enhance entity-related hooks and utilities   

    client/src/hooks/helpers/useEntities.tsx

  • Split useEntities into useEntities and useEntitiesUtils.
  • Updated entity-related hooks to use new utility functions.
  • +67/-71 
    EntityDetails.tsx
    Add EntityDetails component to display entity information

    client/src/ui/modules/entity-details/EntityDetails.tsx

  • Added new component EntityDetails.
  • Implemented logic to display entity details based on selected entity
    or clicked hex.
  • +184/-4 
    ArmyChip.tsx
    Update ArmyChip component to use new army data structure 

    client/src/ui/components/military/ArmyChip.tsx

  • Updated ArmyChip component to use new army data structure.
  • Added logic to handle updated army and battle data.
  • +47/-28 
    Entity.tsx
    Update Entity component to use new entity and battle data structure

    client/src/ui/components/entities/Entity.tsx

  • Updated Entity component to use new entity and battle data structure.
  • Improved logic for determining entity state and battle status.
  • +23/-15 
    BattleActions.tsx
    Update BattleActions component to use new battle data structure

    client/src/ui/modules/military/battle-view/BattleActions.tsx

  • Updated BattleActions component to use new battle data structure.
  • Improved logic for handling battle actions.
  • +16/-11 
    ArmyInfoLabel.tsx
    Update ArmyInfoLabel component to use new army data structure

    client/src/ui/components/worldmap/armies/ArmyInfoLabel.tsx

  • Updated ArmyInfoLabel component to use new army data structure.
  • Improved logic for displaying army information.
  • +13/-15 
    Army.tsx
    Update Army and ArmySelectionOverlay components to use new army data
    structure

    client/src/ui/components/worldmap/armies/Army.tsx

  • Updated Army and ArmySelectionOverlay components to use new army data
    structure.
  • Improved logic for handling army selection and battle status.
  • +20/-15 
    Tests
    2 files
    useBattlesUtils.test.tsx
    Add unit tests for useBattlesUtils functions                         

    client/src/hooks/helpers/battles/test/useBattlesUtils.test.tsx

    • Added unit tests for useBattlesUtils functions.
    +161/-0 
    useBattles.test.tsx
    Add unit tests for useBattles functions                                   

    client/src/hooks/helpers/battles/test/useBattles.test.tsx

    • Added unit tests for useBattles functions.
    +86/-0   
    Additional files (token-limit)
    66 files
    ProductionManager.ts
    ...                                                                                                           

    client/src/dojo/modelManager/ProductionManager.ts

    ...

    +26/-10 
    BattleView.tsx
    ...                                                                                                           

    client/src/ui/modules/military/battle-view/BattleView.tsx

    ...

    +11/-9   
    Battle.tsx
    ...                                                                                                           

    client/src/ui/modules/military/battle-view/Battle.tsx

    ...

    +8/-7     
    useHyperstructures.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useHyperstructures.tsx

    ...

    +9/-11   
    TroopChip.tsx
    ...                                                                                                           

    client/src/ui/components/military/TroopChip.tsx

    ...

    +5/-5     
    useGuilds.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useGuilds.tsx

    ...

    +6/-6     
    TopMiddleNavigation.tsx
    ...                                                                                                           

    client/src/ui/modules/navigation/TopMiddleNavigation.tsx

    ...

    +6/-6     
    useTrade.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useTrade.tsx

    ...

    +9/-9     
    Battles.tsx
    ...                                                                                                           

    client/src/ui/components/models/buildings/worldmap/Battles.tsx

    ...

    +4/-5     
    StructureCard.tsx
    ...                                                                                                           

    client/src/ui/components/hyperstructures/StructureCard.tsx

    ...

    +3/-11   
    GroundGrid.tsx
    ...                                                                                                           

    client/src/ui/components/construction/GroundGrid.tsx

    ...

    +9/-9     
    ShardsMines.tsx
    ...                                                                                                           

    client/src/ui/components/models/buildings/worldmap/ShardsMines.tsx

    ...

    +8/-5     
    useHexPosition.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useHexPosition.tsx

    ...

    +4/-4     
    useTravel.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useTravel.tsx

    ...

    +7/-3     
    ArmyViewCard.tsx
    ...                                                                                                           

    client/src/ui/components/military/ArmyViewCard.tsx

    ...

    +5/-3     
    MarketManager.ts
    ...                                                                                                           

    client/src/dojo/modelManager/MarketManager.ts

    ...

    +13/-5   
    RealmListItem.tsx
    ...                                                                                                           

    client/src/ui/components/worldmap/realms/RealmListItem.tsx

    ...

    +2/-11   
    Battle.tsx
    ...                                                                                                           

    client/src/ui/components/military/Battle.tsx

    ...

    +4/-5     
    InstancedCastles.tsx
    ...                                                                                                           

    client/src/ui/components/models/buildings/worldmap/InstancedCastles.tsx

    ...

    +6/-6     
    TradeHistoryEvent.tsx
    ...                                                                                                           

    client/src/ui/components/trading/TradeHistoryEvent.tsx

    ...

    +3/-3     
    Armies.tsx
    ...                                                                                                           

    client/src/ui/components/worldmap/armies/Armies.tsx

    ...

    +5/-13   
    LeftNavigationModule.tsx
    ...                                                                                                           

    client/src/ui/modules/navigation/LeftNavigationModule.tsx

    ...

    +4/-4     
    useResources.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useResources.tsx

    ...

    +4/-2     
    HexceptionViewScene.tsx
    ...                                                                                                           

    client/src/ui/modules/scenes/HexceptionViewScene.tsx

    ...

    +8/-8     
    useBuildings.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useBuildings.tsx

    ...

    +5/-5     
    ArmyHitBox.tsx
    ...                                                                                                           

    client/src/ui/components/worldmap/armies/ArmyHitBox.tsx

    ...

    +14/-2   
    utils.tsx
    ...                                                                                                           

    client/src/ui/components/worldmap/armies/utils.tsx

    ...

    +6/-3     
    useRoads.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useRoads.tsx

    ...

    +5/-5     
    useBanks.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useBanks.tsx

    ...

    +3/-3     
    RightNavigationModule.tsx
    ...                                                                                                           

    client/src/ui/modules/navigation/RightNavigationModule.tsx

    ...

    +2/-2     
    WorldStructuresMenu.tsx
    ...                                                                                                           

    client/src/ui/modules/world-structures/WorldStructuresMenu.tsx

    ...

    +4/-4     
    BattleDetails.tsx
    ...                                                                                                           

    client/src/ui/modules/military/battle-view/BattleDetails.tsx

    ...

    +2/-2     
    StructureListItem.tsx
    ...                                                                                                           

    client/src/ui/components/worldmap/structures/StructureListItem.tsx

    ...

    +1/-1     
    useBlockchainStore.tsx
    ...                                                                                                           

    client/src/hooks/store/useBlockchainStore.tsx

    ...

    +2/-2     
    HexGrid.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/HexGrid.tsx

    ...

    +2/-3     
    GrasslandBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/GrasslandBiome.tsx

    ...

    +2/-3     
    DeepOceanBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/DeepOceanBiome.tsx

    ...

    +2/-3     
    DesertBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/DesertBiome.tsx

    ...

    +2/-3     
    OceanBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/OceanBiome.tsx

    ...

    +2/-3     
    useRealm.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useRealm.tsx

    ...

    +1/-1     
    useQuestStore.tsx
    ...                                                                                                           

    client/src/hooks/store/useQuestStore.tsx

    ...

    +1/-1     
    vitest.config.ts
    ...                                                                                                           

    client/vitest.config.ts

    ...

    +18/-0   
    DeciduousForestBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/DeciduousForestBiome.tsx

    ...

    +2/-3     
    SubtropicalDesertBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/SubtropicalDesertBiome.tsx

    ...

    +2/-3     
    TemperateDesertBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/TemperateDesertBiome.tsx

    ...

    +2/-3     
    TemperateRainforestBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/TemperateRainforestBiome.tsx

    ...

    +2/-3     
    TropicalSeasonalForestBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/TropicalSeasonalForestBiome.tsx

    ...

    +2/-3     
    _mapStore.tsx
    ...                                                                                                           

    client/src/hooks/store/_mapStore.tsx

    ...

    +2/-2     
    ScorchedBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/ScorchedBiome.tsx

    ...

    +2/-3     
    SnowBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/SnowBiome.tsx

    ...

    +2/-3     
    TropicalRainforestBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/TropicalRainforestBiome.tsx

    ...

    +2/-3     
    BeachBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/BeachBiome.tsx

    ...

    +2/-3     
    ShrublandBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/ShrublandBiome.tsx

    ...

    +2/-3     
    TaigaBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/TaigaBiome.tsx

    ...

    +2/-3     
    TundraBiome.tsx
    ...                                                                                                           

    client/src/ui/components/models/biomes/TundraBiome.tsx

    ...

    +2/-3     
    BattleLabel.tsx
    ...                                                                                                           

    client/src/ui/components/worldmap/armies/BattleLabel.tsx

    ...

    +1/-1     
    useCaravans.tsx
    ...                                                                                                           

    client/src/hooks/helpers/useCaravans.tsx

    ...

    +1/-2     
    global.ts
    ...                                                                                                           

    sdk/packages/eternum/src/constants/global.ts

    ...

    +1/-1     
    ArmyPanel.tsx
    ...                                                                                                           

    client/src/ui/components/military/ArmyPanel.tsx

    ...

    +2/-1     
    BattlesArmyTable.tsx
    ...                                                                                                           

    client/src/ui/components/military/BattlesArmyTable.tsx

    ...

    +1/-1     
    DepositResources.tsx
    ...                                                                                                           

    client/src/ui/components/resources/DepositResources.tsx

    ...

    +1/-1     
    pnpm-lock.yaml
    ...                                                                                                           

    pnpm-lock.yaml

    ...

    +569/-39
    tests.cairo
    ...                                                                                                           

    contracts/src/systems/combat/tests.cairo

    ...

    +103/-1 
    package.json
    ...                                                                                                           

    client/package.json

    ...

    +16/-6   
    test-contracts.yml
    ...                                                                                                           

    .github/workflows/test-contracts.yml

    ...

    +63/-1   
    test-client.yml
    ...                                                                                                           

    .github/workflows/test-client.yml

    ...

    +39/-0   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    vercel bot commented Jul 16, 2024

    The latest updates on your projects. Learn more about Vercel for Git ↗︎

    Name Status Preview Comments Updated (UTC)
    eternum ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 17, 2024 8:02am

    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 5 🔵🔵🔵🔵🔵
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Possible Bug
    The function isArmyAlive is using a new parameter Battle which is not defined in the old context. Ensure that the new implementation aligns with the intended logic changes and that all necessary components are correctly passed and utilized.

    Refactoring Needed
    The ArmyChip component has been significantly refactored with new logic for managing army details and interactions. It is crucial to ensure that all new methods and state management hooks are correctly implemented and that there are no regressions in functionality. The use of BattleManager and state hooks like useMemo for updatedArmy and updatedBattle need thorough testing to ensure they handle all edge cases correctly.

    Logic Change
    The BattleActions component now includes changes in how battles are managed, particularly with the introduction of battleManager for updating battle and army states. This change affects how battles are initiated, updated, and concluded within the UI. Special attention should be given to how these changes impact the overall battle management workflow.

    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Error handling
    Add error handling to prevent accessing properties of undefined in the getUpdatedArmy method

    Refactor the getUpdatedArmy method to handle potential exceptions or errors when
    accessing properties of battle that might be undefined or null.

    client/src/dojo/modelManager/BattleManager.ts [79]

    +if (!battle) throw new Error("Battle data is missing.");
     const cloneArmy = structuredClone(army);
     
    Suggestion importance[1-10]: 9

    Why: Adding error handling to check for undefined battle before accessing its properties is crucial for preventing runtime errors, significantly improving the robustness of the method.

    9
    Enhancement
    Add a check for battle_id before fetching a battle

    Consider checking for the existence of battle_id before using it to fetch a battle.
    This prevents unnecessary calls and potential errors if battle_id is not set.

    client/src/hooks/helpers/useArmies.tsx [618]

    -const battle = getBattle(getEntityIdFromKeys([BigInt(army?.battle_id || 0n)]), Battle);
    +const battle = army?.battle_id ? getBattle(getEntityIdFromKeys([BigInt(army.battle_id)]), Battle) : undefined;
     
    Suggestion importance[1-10]: 9

    Why: This suggestion prevents potential errors and unnecessary calls by ensuring battle_id is set before fetching a battle, improving code robustness.

    9
    Simplify handling of undefined or null values using optional chaining and nullish coalescing

    Use optional chaining (?.) and nullish coalescing (??) operators to simplify the
    handling of potentially undefined or null values in the playerStructures mapping
    function. This will make the code more concise and easier to understand.

    client/src/hooks/helpers/useEntities.tsx [52-58]

    -const structureName = getEntityName(structure!.entity_id);
    -const name = realm
    -  ? getRealmNameById(realm.realm_id)
    -  : structureName
    -    ? `${structure?.category} ${structureName}`
    -    : structure?.category;
    +const structureName = getEntityName(structure?.entity_id);
    +const name = getRealmNameById(realm?.realm_id) ?? `${structure?.category} ${structureName}` ?? structure?.category;
     
    Suggestion importance[1-10]: 9

    Why: Using optional chaining and nullish coalescing operators simplifies the code and makes it more readable and concise, which is a significant improvement for handling undefined or null values.

    9
    Best practice
    Enhance type safety by replacing direct type conversion with conditional checking

    Replace the direct use of Number conversion in the getElapsedTime method with a
    safer type-checking approach to avoid runtime errors from invalid conversions.

    client/src/dojo/modelManager/BattleManager.ts [39]

    -const duractionSinceLastUpdate = currentTimestamp - Number(battle.last_updated);
    +const duractionSinceLastUpdate = currentTimestamp - (typeof battle.last_updated === 'number' ? battle.last_updated : parseInt(battle.last_updated, 10));
     
    Suggestion importance[1-10]: 8

    Why: This suggestion improves type safety and prevents potential runtime errors from invalid conversions, making the code more robust and reliable.

    8
    Improve type safety by ensuring the currentTimestamp parameter type matches expected types in operations

    Consider using a more precise type for the currentTimestamp parameter in the
    getElapsedTime method to ensure it aligns with the expected type of
    battle.last_updated. This will prevent potential type mismatches and runtime errors.

    client/src/dojo/modelManager/BattleManager.ts [36]

    -public getElapsedTime(currentTimestamp: number): number {
    +public getElapsedTime(currentTimestamp: bigint): number {
     
    Suggestion importance[1-10]: 7

    Why: Changing the type of currentTimestamp to bigint can improve type safety, but it requires ensuring that all related operations and usages are compatible with bigint. This suggestion is beneficial but not critical.

    7
    Add type checks for id parameters to prevent type errors in getComponentValue calls

    To ensure type safety and avoid potential runtime type errors, explicitly check the
    types of id parameters in getComponentValue calls. This can be done by ensuring id
    is of the expected type before passing it to the function.

    client/src/hooks/helpers/useEntities.tsx [48-50]

    -const realm = getComponentValue(Realm, id);
    -const structure = getComponentValue(Structure, id);
    -const position = getComponentValue(Position, id);
    +const realm = typeof id === 'bigint' ? getComponentValue(Realm, id) : undefined;
    +const structure = typeof id === 'bigint' ? getComponentValue(Structure, id) : undefined;
    +const position = typeof id === 'bigint' ? getComponentValue(Position, id) : undefined;
     
    Suggestion importance[1-10]: 6

    Why: Adding type checks for id parameters can prevent type errors and improve type safety. However, the suggestion could be more concise and might be over-cautious if the type of id is already guaranteed by the context in which it is used.

    6
    Performance
    Use BigInt for calculations involving potentially large numbers to prevent precision loss

    Optimize the calculation of total_knight_health, total_paladin_health, and
    total_crossbowman_health by directly using BigInt for multiplication to avoid
    precision loss in large numbers.

    client/src/dojo/modelManager/BattleManager.ts [132-134]

    -let total_knight_health = health * Number(troops.knight_count);
    +let total_knight_health = BigInt(health) * troops.knight_count;
     
    Suggestion importance[1-10]: 8

    Why: Using BigInt for calculations involving large numbers ensures precision and prevents potential loss, optimizing performance and accuracy in the code.

    8
    Maintainability
    Improve type safety by replacing 'as any' with specific types

    Replace the direct use of as any with proper type casting or interfaces to ensure
    type safety and maintainability.

    client/src/hooks/helpers/battles/test/useBattlesUtils.test.tsx [21]

    -const isInBattle = testedModule.protectorStillInBattle({} as any, {} as any, {} as any);
    +const isInBattle = testedModule.protectorStillInBattle({} as BattleInfo, {} as Component, {} as Entity);
     
    Suggestion importance[1-10]: 8

    Why: This suggestion improves type safety and maintainability by replacing 'as any' with specific types, which is crucial for catching type-related errors early.

    8
    Extract health conversion logic into a utility function

    Refactor the repeated BigInt conversions and calculations into a separate utility
    function to improve code readability and reusability.

    client/src/hooks/helpers/useArmies.tsx [80-85]

    -healthClone.current =
    -  BigInt(healthClone.current) /
    -  (BigInt(EternumGlobalConfig.resources.resourcePrecision) * EternumGlobalConfig.troop.healthPrecision);
    -healthClone.lifetime =
    -  BigInt(healthClone.lifetime) /
    -  (BigInt(EternumGlobalConfig.resources.resourcePrecision) * EternumGlobalConfig.troop.healthPrecision);
    +healthClone.current = convertHealth(healthClone.current);
    +healthClone.lifetime = convertHealth(healthClone.lifetime);
     
    Suggestion importance[1-10]: 8

    Why: Refactoring the repeated BigInt conversions into a utility function enhances code readability and reusability, making the code cleaner and easier to maintain.

    8
    Reduce redundancy by refactoring repeated mocking into a helper function

    Refactor the repeated mocking of runQuery to a helper function to reduce redundancy
    and improve code clarity.

    client/src/hooks/helpers/battles/test/useBattlesUtils.test.tsx [19]

    -vi.mocked(runQuery).mockReturnValue(protectors);
    +mockRunQuery(protectors);
     
    Suggestion importance[1-10]: 7

    Why: Refactoring repeated mocking into a helper function reduces redundancy and improves code clarity, making the tests easier to maintain.

    7
    Encapsulate the calculation of offset.y in a function

    Replace the direct manipulation of offset.y with a function that calculates the new
    offset to ensure consistent behavior and easier maintenance.

    client/src/hooks/helpers/useArmies.tsx [108]

    -offset.y += offsetToAvoidOverlapping;
    +offset.y = calculateNewOffsetY(offset.y, offsetToAvoidOverlapping);
     
    Suggestion importance[1-10]: 7

    Why: Encapsulating the calculation of offset.y in a function improves maintainability and consistency, though it is a minor improvement.

    7
    Refactor repeated logic for mapping realms into a separate function to reduce code duplication

    To improve code readability and maintainability, consider refactoring the repeated
    logic for mapping playerRealms and otherRealms into a separate function. This will
    reduce code duplication and make the codebase cleaner.

    client/src/hooks/helpers/useEntities.tsx [34-42]

    -playerRealms.map((id) => {
    -  const realm = getComponentValue(Realm, id);
    -  return { ...realm, position: getPosition(realm!.realm_id), name: getRealmNameById(realm!.realm_id) };
    +const mapRealms = (realms) => realms.map((id) => {
    +  const realm = getComponentValue(Realm, id) || {};
    +  return { ...realm, position: getPosition(realm.realm_id), name: getRealmNameById(realm.realm_id) };
     });
    -otherRealms.map((id) => {
    -  const realm = getComponentValue(Realm, id);
    -  return { ...realm, position: getPosition(realm!.realm_id), name: getRealmNameById(realm!.realm_id) };
    -});
    +const playerRealmsMapped = mapRealms(playerRealms);
    +const otherRealmsMapped = mapRealms(otherRealms);
     
    Suggestion importance[1-10]: 7

    Why: Refactoring the repeated logic into a separate function improves maintainability and readability by reducing code duplication. However, the suggestion should ensure that the refactored function is properly integrated and tested.

    7
    Possible bug
    Add null checks to prevent runtime errors when component values are missing

    Consider adding null checks for realm and structure in the mapping functions to
    prevent runtime errors if getComponentValue returns undefined. This will ensure that
    the application handles cases where the component values are not found, improving
    the robustness of the code.

    client/src/hooks/helpers/useEntities.tsx [35-48]

    -const realm = getComponentValue(Realm, id);
    -const structure = getComponentValue(Structure, id);
    +const realm = getComponentValue(Realm, id) || {};
    +const structure = getComponentValue(Structure, id) || {};
     
    Suggestion importance[1-10]: 8

    Why: Adding null checks for realm and structure is a good practice to prevent potential runtime errors, enhancing the robustness of the code. However, using empty objects as fallbacks might not be the best approach as it could lead to other issues if the code expects certain properties to be present.

    8
    Readability
    Enhance code readability by using more descriptive variable names

    Use a more descriptive variable name than protectors for the Set variable to enhance
    code readability.

    client/src/hooks/helpers/battles/test/useBattlesUtils.test.tsx [18]

    -const protectors = new Set(["0x0" as Entity]);
    +const protectorEntities = new Set(["0x0" as Entity]);
     
    Suggestion importance[1-10]: 6

    Why: Using more descriptive variable names enhances code readability, which is beneficial for understanding and maintaining the code.

    6
    Improve variable naming for clarity

    Use a more descriptive variable name than armyEntityId for clarity, especially since
    it represents an entity ID and not an army object.

    client/src/hooks/helpers/useArmies.tsx [61]

    -.map((armyEntityId) => {
    +.map((entityId) => {
     
    Suggestion importance[1-10]: 6

    Why: Using a more descriptive variable name improves code readability, but the impact is relatively minor.

    6

    Copy link
    Collaborator

    @bob0005 bob0005 left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    lgtm

    @bob0005 bob0005 merged commit 6773351 into main Jul 18, 2024
    20 checks passed
    @bob0005 bob0005 deleted the ref/combat branch July 18, 2024 14:57
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants