Skip to content
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
45 changes: 44 additions & 1 deletion packages/contracts/out/IWorld.sol/IWorld.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "UD__getItemType",
"inputs": [
{
"name": "itemId",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "uint8",
"internalType": "enum ItemType"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "UD__getItemsContract",
Expand Down Expand Up @@ -626,7 +645,7 @@
],
"outputs": [
{
"name": "isEquipped",
"name": "_isEquipped",
"type": "bool",
"internalType": "bool"
}
Expand Down Expand Up @@ -739,6 +758,30 @@
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "UD__unequipItem",
"inputs": [
{
"name": "characterId",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "itemId",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "success",
"type": "bool",
"internalType": "bool"
}
],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "batchCall",
Expand Down
45 changes: 44 additions & 1 deletion packages/contracts/out/IWorld.sol/IWorld.abi.json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@ declare const abi: [
],
"stateMutability": "view"
},
{
"type": "function",
"name": "UD__getItemType",
"inputs": [
{
"name": "itemId",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "uint8",
"internalType": "enum ItemType"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "UD__getItemsContract",
Expand Down Expand Up @@ -626,7 +645,7 @@ declare const abi: [
],
"outputs": [
{
"name": "isEquipped",
"name": "_isEquipped",
"type": "bool",
"internalType": "bool"
}
Expand Down Expand Up @@ -739,6 +758,30 @@ declare const abi: [
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "UD__unequipItem",
"inputs": [
{
"name": "characterId",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "itemId",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "success",
"type": "bool",
"internalType": "bool"
}
],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "batchCall",
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/out/IWorld.sol/IWorld.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion packages/contracts/src/codegen/world/IItemsSystem.sol

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

61 changes: 59 additions & 2 deletions packages/contracts/src/systems/ItemsSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ contract ItemsSystem is System {
}
}

function isEquipped(uint256 characterId, uint256 itemId) public view returns (bool isEquipped) {
function isEquipped(uint256 characterId, uint256 itemId) public view returns (bool _isEquipped) {
ItemsData memory itemData = Items.get(itemId);
if (uint8(itemData.itemType) == 0) {
uint256[] memory equippedWeap = CharacterEquipment.getEquippedWeapons(characterId);
for (uint256 i; i < equippedWeap.length;) {
if (equippedWeap[i] == itemId) isEquipped = true;
if (equippedWeap[i] == itemId) _isEquipped = true;
break;
{
i++;
Expand Down Expand Up @@ -152,6 +152,58 @@ contract ItemsSystem is System {
}
}

function unequipItem(uint256 characterId, uint256 itemId) public returns (bool success) {
address characterOwner = IWorld(_world()).UD__getOwner(characterId);
require(characterOwner == _msgSender(), "ITEMS: Not Character Owner");
uint8 itemType = uint8(getItemType(itemId));
if (itemType == 0) {
uint256[] memory sortedArray =
_moveIdToEndOfArray(itemId, CharacterEquipment.getEquippedWeapons(characterId));
if (sortedArray[sortedArray.length - 1] == itemId) {
CharacterEquipment.setEquippedWeapons(characterId, sortedArray);
CharacterEquipment.popEquippedWeapons(characterId);
success = true;
}
}
if (itemType == 1) {
uint256[] memory sortedArray = _moveIdToEndOfArray(itemId, CharacterEquipment.getEquippedArmor(characterId));
if (sortedArray[sortedArray.length - 1] == itemId) {
CharacterEquipment.setEquippedArmor(characterId, sortedArray);
CharacterEquipment.popEquippedArmor(characterId);
success = true;
}
}
if (itemType == 2) {
uint256[] memory sortedArray =
_moveIdToEndOfArray(itemId, CharacterEquipment.getEquippedSpells(characterId));
if (sortedArray[sortedArray.length - 1] == itemId) {
CharacterEquipment.setEquippedSpells(characterId, sortedArray);
CharacterEquipment.popEquippedSpells(characterId);
success = true;
}
}
}

function _moveIdToEndOfArray(uint256 itemId, uint256[] memory array)
internal
returns (uint256[] memory sortedArray)
{
uint256[] memory arrayToBeSorted = array;
for (uint256 i = 0; i < arrayToBeSorted.length; i++) {
if (arrayToBeSorted[i] == itemId) {
for (uint256 j = i; j < arrayToBeSorted.length; j++) {
if (j + 1 < arrayToBeSorted.length) {
arrayToBeSorted[j] = arrayToBeSorted[j + 1];
} else if (j + 1 >= arrayToBeSorted.length) {
arrayToBeSorted[j] = itemId;
}
}
break;
}
}
sortedArray = arrayToBeSorted;
}

function getTotalSupply(uint256 tokenId) public view returns (uint256 _supply) {
_supply = TotalSupply.getTotalSupply(_totalSupplyTableId(ITEMS_NAMESPACE), tokenId);
}
Expand All @@ -172,6 +224,11 @@ contract ItemsSystem is System {
ERC1155URIStorage.setUri(_erc1155URIStorageTableId(ITEMS_NAMESPACE), tokenId, tokenUri);
}

function getItemType(uint256 itemId) public view returns (ItemType) {
ItemsData memory itemData = Items.get(itemId);
return itemData.itemType;
}

function _incrementItemsCounter() internal returns (uint256) {
address itemsContract = UltimateDominionConfig.getItems();
uint256 itemsCounter = Counters.getCounter(address(itemsContract)) + 1;
Expand Down
16 changes: 15 additions & 1 deletion packages/contracts/test/ItemsSystem.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ contract Test_ItemsSystem is SetUp, GasReporter {
assertEq(erc1155System.balanceOf(address(alice), 1), 1);
}

function test_equipItem() public {
function test_equipItems() public {
uint256 fees = entropy.getFee(address(1));
vm.startPrank(alice);
world.UD__rollStats{value: fees}(alicesRandomness, alicesCharacterId, Classes.Rogue);
Expand All @@ -87,4 +87,18 @@ contract Test_ItemsSystem is SetUp, GasReporter {

assertTrue(world.UD__isEquipped(alicesCharacterId, 1));
}

function test_unequipItem() public {
uint256 fees = entropy.getFee(address(1));
vm.startPrank(alice);
world.UD__rollStats{value: fees}(alicesRandomness, alicesCharacterId, Classes.Rogue);
world.UD__enterGame(alicesCharacterId);
uint256[] memory itemsToEquip = new uint256[](1);
itemsToEquip[0] = 1;
world.UD__equipItems(alicesCharacterId, itemsToEquip);
assertTrue(world.UD__isEquipped(alicesCharacterId, 1));
world.UD__unequipItem(alicesCharacterId, 1);

assertFalse(world.UD__isEquipped(alicesCharacterId, 1));
}
}
2 changes: 1 addition & 1 deletion packages/contracts/worlds.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"31337": {
"address": "0xab8df557966eed76ebdb840899866e5441cad200"
"address": "0x8e31c48fd7540e10121a136e4153e0bd11309eaa"
},
"84532": {
"address": "0xeafba96222596ac92cd335b0adfc1c2f50357571",
Expand Down