From 07dd6e835c64237fc1d50f78a9c1cf31dac95698 Mon Sep 17 00:00:00 2001 From: 0xNotMe Date: Thu, 3 Jul 2025 18:14:47 +0200 Subject: [PATCH] refactor: add EquipmentInfo struct for cleaner equipment getter returns - Add EquipmentInfo struct to Types.sol for grouped equipment data - Update _getEquippableWeapons to return EquipmentInfo struct - Update _getEquippableArmor to return EquipmentInfo struct - Update pollForFrontendData to use the new struct return values - Improves code readability by grouping related return values --- src/battle-nads/Getters.sol | 57 +++++++++++++++++-------------------- src/battle-nads/Types.sol | 8 ++++++ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/battle-nads/Getters.sol b/src/battle-nads/Getters.sol index 0950bb9..b889095 100644 --- a/src/battle-nads/Getters.sol +++ b/src/battle-nads/Getters.sol @@ -10,7 +10,8 @@ import { Inventory, Weapon, Armor, - DataFeed + DataFeed, + EquipmentInfo } from "./Types.sol"; import { SessionKeyData } from "lib/fastlane-contracts/src/common/relay/types/GasRelayTypes.sol"; @@ -68,8 +69,14 @@ contract Getters is TaskHandler { } combatants = _getCombatantBattleNads(characterID); noncombatants = _getNonCombatantBattleNads(characterID); - (equipableWeaponIDs, equipableWeaponNames,) = _getEquippableWeapons(characterID); - (equipableArmorIDs, equipableArmorNames,) = _getEquippableArmor(characterID); + + EquipmentInfo memory weaponInfo = _getEquippableWeapons(characterID); + equipableWeaponIDs = weaponInfo.itemIDs; + equipableWeaponNames = weaponInfo.itemNames; + + EquipmentInfo memory armorInfo = _getEquippableArmor(characterID); + equipableArmorIDs = armorInfo.itemIDs; + equipableArmorNames = armorInfo.itemNames; if (startBlock >= block.number) { startBlock = block.number - 1; } else if (startBlock < block.number - 20) { @@ -384,18 +391,12 @@ contract Getters is TaskHandler { /** * @notice Get weapons that a character can equip * @param characterID ID of the character - * @return weaponIDs Array of equippable weapon IDs - * @return weaponNames Array of weapon names - * @return currentWeaponID Currently equipped weapon ID + * @return equipmentInfo Struct containing weapon IDs, names, and current weapon */ - function _getEquippableWeapons(bytes32 characterID) - internal - view - returns (uint8[] memory weaponIDs, string[] memory weaponNames, uint8 currentWeaponID) - { + function _getEquippableWeapons(bytes32 characterID) internal view returns (EquipmentInfo memory equipmentInfo) { BattleNad memory character = getBattleNad(characterID); Inventory memory inv = character.inventory; - currentWeaponID = character.stats.weaponID; + equipmentInfo.currentItemID = character.stats.weaponID; // Count available weapons uint256 weaponCount = 0; @@ -406,37 +407,31 @@ contract Getters is TaskHandler { } // Initialize arrays - weaponIDs = new uint8[](weaponCount); - weaponNames = new string[](weaponCount); + equipmentInfo.itemIDs = new uint8[](weaponCount); + equipmentInfo.itemNames = new string[](weaponCount); // Fill arrays uint256 index = 0; for (uint8 i = 0; i < 64; i++) { if (inv.weaponBitmap & (1 << i) != 0) { - weaponIDs[index] = i; - weaponNames[index] = getWeaponName(i); + equipmentInfo.itemIDs[index] = i; + equipmentInfo.itemNames[index] = getWeaponName(i); index++; } } - return (weaponIDs, weaponNames, currentWeaponID); + return equipmentInfo; } /** * @notice Get armor that a character can equip * @param characterID ID of the character - * @return armorIDs Array of equippable armor IDs - * @return armorNames Array of armor names - * @return currentArmorID Currently equipped armor ID + * @return equipmentInfo Struct containing armor IDs, names, and current armor */ - function _getEquippableArmor(bytes32 characterID) - internal - view - returns (uint8[] memory armorIDs, string[] memory armorNames, uint8 currentArmorID) - { + function _getEquippableArmor(bytes32 characterID) internal view returns (EquipmentInfo memory equipmentInfo) { BattleNad memory character = getBattleNad(characterID); Inventory memory inv = character.inventory; - currentArmorID = character.stats.armorID; + equipmentInfo.currentItemID = character.stats.armorID; // Count available armor uint256 armorCount = 0; @@ -447,20 +442,20 @@ contract Getters is TaskHandler { } // Initialize arrays - armorIDs = new uint8[](armorCount); - armorNames = new string[](armorCount); + equipmentInfo.itemIDs = new uint8[](armorCount); + equipmentInfo.itemNames = new string[](armorCount); // Fill arrays uint256 index = 0; for (uint8 i = 0; i < 64; i++) { if (inv.armorBitmap & (1 << i) != 0) { - armorIDs[index] = i; - armorNames[index] = getArmorName(i); + equipmentInfo.itemIDs[index] = i; + equipmentInfo.itemNames[index] = getArmorName(i); index++; } } - return (armorIDs, armorNames, currentArmorID); + return equipmentInfo; } function _shortfallToRecommendedBalanceInMON(BattleNad memory player) diff --git a/src/battle-nads/Types.sol b/src/battle-nads/Types.sol index dc0977b..7edc10d 100644 --- a/src/battle-nads/Types.sol +++ b/src/battle-nads/Types.sol @@ -214,3 +214,11 @@ struct DataFeed { Log[] logs; string[] chatLogs; } + +// Typed return structs for cleaner function signatures + +struct EquipmentInfo { + uint8[] itemIDs; + string[] itemNames; + uint8 currentItemID; +}